unitState.js 6.3 KB

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