debug.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict'
  2. const {SkillID} = require('tera-data-parser').types
  3. class SPDebug {
  4. constructor(mod) {
  5. this.mod = mod
  6. this.hooks = []
  7. this.reload()
  8. }
  9. reload() {
  10. this.unload()
  11. // Skills
  12. if(this.mod.settings.debug.skills) {
  13. for(let options of [
  14. ['S_CREST_APPLY', 2, _ => [_.id, _.enable ? 'on' : 'off']],
  15. ['S_CREST_MESSAGE', 2, _ => [_.unk, _.type, skillId(_.skill)]],
  16. ['S_CANNOT_START_SKILL', 4, _ => skillId(_.skill.id)]
  17. ])
  18. this.hookFirst(...options)
  19. for(let options of [
  20. ['C_START_SKILL', 7, _ => [Number(_.unk), Number(_.moving), Number(_.continue), gameId(_.target)]],
  21. ['C_PRESS_SKILL', 4, _ => _.press],
  22. ['C_START_TARGETED_SKILL', 7, _ => `[${_.targets.map(t => `${gameId(t.gameId)} ${t.unk}`).join(',')}]`],
  23. ['C_START_COMBO_INSTANT_SKILL', 6, _ => `[${_.targets.map(t => `${t.unk1} ${gameId(t.target)} ${t.unk2}`).join(',')}]`],
  24. ['C_START_INSTANCE_SKILL', 7, _ => [_.unk, `[${_.targets.map(t => `${t.unk1} ${gameId(t.target)} ${t.unk2}`).join(',')}]`]],
  25. ['C_START_INSTANCE_SKILL_EX', 5, _ => [_.unk, gameId(_.projectile)]],
  26. ['C_NOTIMELINE_SKILL', 3],
  27. ['S_GRANT_SKILL', 3],
  28. ['C_CANCEL_SKILL', 3, _ => _.type]
  29. ])
  30. this.hookMod(options[0], options[1], (_, name) => [skillId(_.skill), ...(() => {
  31. const strs = (options[2] || (() => []))(_, name)
  32. return Array.isArray(strs) ? strs : [strs]
  33. })(), ...(() => {
  34. const strs = []
  35. if(this.mod.settings.debug.loc) {
  36. if(['C_START_SKILL', 'C_PRESS_SKILL', 'C_START_TARGETED_SKILL', 'C_START_COMBO_INSTANT_SKILL',
  37. 'C_START_INSTANCE_SKILL', 'C_START_INSTANCE_SKILL_EX'].includes(name))
  38. strs.push(pos(_.loc))
  39. if(['C_START_SKILL', 'C_START_TARGETED_SKILL', 'C_START_INSTANCE_SKILL_EX'].includes(name))
  40. strs.push(`> ${pos(_.dest)}`)
  41. if(['C_START_COMBO_INSTANT_SKILL', 'C_START_INSTANCE_SKILL'].includes(name))
  42. strs.push(`> [${_.endpoints.map(p => pos(p.loc)).join(', ')}]`)
  43. }
  44. return strs
  45. })()])
  46. }
  47. // Location
  48. if(this.mod.settings.debug.loc)
  49. for(let pkt of ['C_NOTIFY_LOCATION_IN_ACTION', 'C_NOTIFY_LOCATION_IN_DASH'])
  50. this.hookMod(pkt, 4, _ => [skillId(_.skill), _.stage, pos(_.loc), dir(_.w)])
  51. }
  52. unload() {
  53. if(this.hooks.length) {
  54. for(let h of this.hooks) this.mod.unhook(h)
  55. this.hooks = []
  56. }
  57. }
  58. hookFirst(name, ver, cb) {
  59. this.hook(name, ver, {order: -999}, event => {
  60. const out = cb(event, name)
  61. debug(`${event.$incoming ? '<-' : '->'} ${name} ${Array.isArray(out) ? out.join(' ') : out}`)
  62. })
  63. }
  64. hookMod(name, ver, cb) {
  65. this.hook(name, ver, {order: 999, filter: {fake: null, silenced: null}}, event => {
  66. if(event.$fake && event.$silenced) return
  67. const out = cb(event, name),
  68. typeChar = event.$silenced ? 'X' : (event.$fake ? '*' : (event.$modified ? '~' : '-'))
  69. debug(`${event.$incoming ? '<' + typeChar : typeChar + '>'} ${name} ${Array.isArray(out) ? out.join(' ') : out}`)
  70. })
  71. }
  72. hook(...args) { return this.hooks.push(this.mod.hook(...args)) }
  73. toggle() {
  74. //if(this.mod.settings.debug.skills) debug(`SP debug enabled. Race=${player.race} Class=${player.job}`)
  75. }
  76. }
  77. // Utilities
  78. function debug() {
  79. console.log(`[${(Date.now() % 10000).toString().padStart(4, '0')}]`, ...arguments)
  80. }
  81. function pos(p) { return `(${p.roundN().toString()})` }
  82. function dir(w) { return (Math.round(w / Math.PI * 100) / 100) + '/t' }
  83. function decimal(n, p) {
  84. p = 10 ** p
  85. return Math.round(n * p) / p
  86. }
  87. function gameId(id) {
  88. if(id === 0n) return '@'
  89. return '@' + id.toString(16).toUpperCase().padStart(16, '0')
  90. }
  91. function skillId(skill) {
  92. if(!(skill instanceof SkillID)) skill = new SkillID(skill)
  93. let str = skill.reserved ? `[X${skill.reserved.toString(16)}]` : ''
  94. switch(skill.type) {
  95. case 1: str += 'A'; break
  96. case 2: str += 'R'; break
  97. default: str += `[T${skill.type}]`; break
  98. }
  99. if(skill.npc) {
  100. if(skill.type === 1) return `${str}${skill.huntingZoneId}:${skill.id}`
  101. return str + skill.id
  102. }
  103. const id = skill.id.toString()
  104. switch(skill.type) {
  105. case 1: return str + [id.slice(0, -4), id.slice(-4, -2), id.slice(-2)].join('-')
  106. case 2: return str + [id.slice(0, -2), id.slice(-2)].join('-')
  107. default: return str + id
  108. }
  109. }
  110. // Export
  111. module.exports = SPDebug