unitState.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. "use strict";
  2. /*
  3. * Unit for Fps Manager. Managing module state and events
  4. * Author: SaltyMonkey
  5. * Contributors:
  6. */
  7. /**
  8. * @typedef {import("../fpsManager.js").deps} deps
  9. */
  10. //using es5 compatible mode because json not supports es6 types and es6 version is slow ->(require("fast-deep-equal/es6");)
  11. const EventEmitter = require("events").EventEmitter;
  12. const rfdc = require("rfdc")({ "proto": true });
  13. const utils = require("./helpers/utils");
  14. //const watchdog = require("./helpers/watchdog");
  15. //import enums
  16. const { RAW_HOOK_MODE, HOOK_SETTINGS, ABN, EVENTS } = require("./helpers/enums");
  17. //types for abnormals to find in client data
  18. const shapeChangers = [ABN.BigHead, ABN.ShapeChanger];
  19. const onScreenEffects = [ABN.FX, ABN.PostProcessingEffect];
  20. const ccbEffects = [ABN.CCB];
  21. class unitState extends EventEmitter {
  22. /**
  23. *Creates an instance of unitState.
  24. * @param {deps} deps dependencies
  25. * @memberof unitState
  26. */
  27. constructor(deps) {
  28. super();
  29. this.__mod = deps.mod;
  30. this.__proto = deps.proto;
  31. this.__log = deps.log;
  32. Object.assign(this, {
  33. //main properties for state management
  34. "presets": {}, // all loaded per class presets
  35. "preset": {}, //current chosen preset
  36. "activeMode": undefined,
  37. "prevMode": undefined,
  38. "lockedMode": false,
  39. "availableClasses": new Set(),
  40. "userTemplateToClassMap": {},
  41. "lockedPacketsData": [],
  42. "throttlePacketsData": [],
  43. //lookup structures
  44. "shapeChangersBuffs": new Set(),
  45. "onScreenEffectsBuffs": new Set(),
  46. "ccbBuffs": new Set(),
  47. "smtData": new Set(),
  48. "specialAs": new Set(),
  49. "whitelistAbnormals": new Set(),
  50. "whitelistProjectiles": new Set()
  51. });
  52. deps.mod.game.data.abnormalities.forEach(abnormality => {
  53. let types = abnormality.effects.map(x => x.type);
  54. if (utils.arraysHasIntersect(types, shapeChangers))
  55. this.shapeChangersBuffs.add(abnormality.id);
  56. if (utils.arraysHasIntersect(types, onScreenEffects))
  57. this.onScreenEffectsBuffs.add(abnormality.id);
  58. if (utils.arraysHasIntersect(types, ccbEffects))
  59. this.ccbBuffs.add(abnormality.id);
  60. });
  61. this.__mod.queryData("/S1ActionScripts@type=?/Script/", ["Teleport"], true, false, ["id"]).then(result => {
  62. let res = result.map(rs => rs.attributes.id);
  63. res.forEach(x => { this.specialAs.add(x); });
  64. });
  65. this.availableClasses.add("common");
  66. deps.mod.game.data.users.forEach(template => {
  67. this.availableClasses.add(template.class);
  68. this.userTemplateToClassMap[template.id] = template.class;
  69. });
  70. this.Init();
  71. }
  72. Init() {
  73. ([...this.availableClasses]).forEach(x => {
  74. this.presets[x] = utils.loadJson(utils.getFullPath(__dirname, `../settings/presets/${x}.json`));
  75. });
  76. let staticData = utils.loadJson(utils.getFullPath(__dirname, "../data/staticData.json"));
  77. this.lockedPacketsData = staticData.blockedProto;
  78. this.throttlePacketsData = staticData.throttledProto;
  79. this.smtData = new Set(staticData.blockedSmt);
  80. this.whitelistAbnormals = new Set(staticData.excludedAbnormals);
  81. this.whitelistProjectiles = new Set(staticData.excludedProjectiles);
  82. this.__mod.hook("S_LOGIN", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  83. this.lockedMode = false;
  84. this.preset = this.__mod.settings.classBasedPresets
  85. ? (this.presets[this.__mod.game.me.class] || this.presets["common"])
  86. : this.presets["common"];
  87. if (!this.preset) { throw new Error("Preset can't be loaded! Critical error. "); }
  88. this.activeMode = this.preset.modes[this.preset.directives["default"]];
  89. if (this.__mod.settings.interactive) this.__log.send(`Default mode: "${this.preset.directives["default"]}".`);
  90. this._eventTriggerPartial();
  91. });
  92. this.__mod.hook(...this.__proto.getData("S_LOAD_TOPO"), (event) => {
  93. if (!this.__mod.settings.enableEvents || this.lockedMode) return;
  94. this.prevMode = rfdc(this.activeMode);
  95. if (this.__mod.game.me.inBattleground && this.preset.directives.triggerBG) {
  96. this.activeMode = this.preset.modes[this.preset.directives.triggerBG];
  97. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerBG}".`);
  98. }
  99. else if (this.__mod.game.me.inCivilUnrest && this.preset.directives.triggerCU) {
  100. this.activeMode = this.preset.modes[this.preset.directives.triggerCU];
  101. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerCU}".`);
  102. }
  103. else if (this.__mod.game.me.inDungeon && this.preset.directives.triggerDungeons) {
  104. let mode = undefined;
  105. let name = undefined;
  106. if (this.preset.modes[this.preset.directives.triggerDungeons[event.zone]]) {
  107. name = this.preset.directives.triggerDungeons[event.zone];
  108. mode = this.preset.modes[this.preset.directives.triggerDungeons[event.zone]];
  109. }
  110. else if (this.preset.modes[this.preset.directives.triggerDungeons["all"]]) {
  111. name = this.preset.directives.triggerDungeons["all"];
  112. mode = this.preset.modes[this.preset.directives.triggerDungeons["all"]];
  113. }
  114. if (mode) {
  115. this.activeMode = mode;
  116. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to ${name}`);
  117. }
  118. }
  119. else if (this.__mod.game.me.inOpenWorld && this.preset.directives.triggerOW) {
  120. this.activeMode = this.preset.modes[this.preset.directives.triggerOW];
  121. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerOW}".`);
  122. }
  123. this._eventTriggerPartial();
  124. });
  125. this.__mod.hook("S_FIELD_EVENT_ON_ENTER", RAW_HOOK_MODE.EVENT, () => {
  126. if (!this.__mod.settings.enableEvents) return;
  127. this.prevMode = rfdc(this.activeMode);
  128. this.lockedMode = true;
  129. this.activeMode = this.preset.modes[this.preset.directives.triggerGuardians];
  130. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerGuardians}".`);
  131. this._eventTrigger();
  132. });
  133. this.__mod.hook("S_FIELD_EVENT_ON_LEAVE", RAW_HOOK_MODE.EVENT, () => {
  134. if (!this.__mod.settings.enableEvents) return;
  135. this.activeMode = rfdc(this.prevMode);
  136. this.lockedMode = false;
  137. if (this.__mod.settings.interactive) this.__log.send("Mode was changed back");
  138. this._eventTrigger();
  139. });
  140. }
  141. _eventTrigger() {
  142. this.emit(EVENTS.RECONFIGURE);
  143. }
  144. _eventTriggerPartial() {
  145. this.emit(EVENTS.RECONFIGUREPARTIAL);
  146. }
  147. }
  148. module.exports = unitState;