unitState.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. });
  48. deps.mod.game.data.abnormalities.forEach(abnormality => {
  49. let types = abnormality.effects.map(x => x.type);
  50. if (utils.arraysHasIntersect(types, shapeChangers))
  51. this.shapeChangersBuffs.add(abnormality.id);
  52. if (utils.arraysHasIntersect(types, onScreenEffects))
  53. this.onScreenEffectsBuffs.add(abnormality.id);
  54. });
  55. this.__mod.queryData("/S1ActionScripts@type=?/Script/", ["Teleport"], true, false, ["id"]).then(result => {
  56. let res = result.map(rs => rs.attributes.id);
  57. res.forEach(x => { this.specialAs.add(x); });
  58. });
  59. this.availableClasses.add("common");
  60. deps.mod.game.data.users.forEach(template => {
  61. this.availableClasses.add(template.class);
  62. this.userTemplateToClassMap[template.id] = template.class;
  63. });
  64. this.Init();
  65. }
  66. Init() {
  67. ([...this.availableClasses]).forEach(x => {
  68. this.presets[x] = utils.loadJson(utils.getFullPath(__dirname, `../settings/presets/${x}.json`));
  69. });
  70. let staticData = utils.loadJson(utils.getFullPath(__dirname, "../data/staticData.json"));
  71. this.lockedPacketsData = staticData.blockedProto;
  72. this.throttlePacketsData = staticData.throttledProto;
  73. this.smtData = new Set(...staticData.blockedSmt);
  74. this.__mod.hook("S_LOGIN", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  75. this.lockedMode = false;
  76. this.preset = this.__mod.settings.classBasedPresets
  77. ? (this.presets[this.__mod.game.me.class] || this.presets["common"])
  78. : this.presets["common"];
  79. if (!this.preset) { throw new Error("Preset can't be loaded! Critical error. "); }
  80. this.activeMode = this.preset.modes[this.preset.directives["default"]];
  81. if (this.__mod.settings.interactive) this.__log.send(`Default mode: "${this.preset.directives["default"]}".`);
  82. this._eventTriggerPartial();
  83. });
  84. this.__mod.hook(...this.__proto.getData("S_LOAD_TOPO"), (event) => {
  85. if (!this.__mod.settings.enableEvents || this.lockedMode) return;
  86. this.prevMode = rfdc(this.activeMode);
  87. if (this.__mod.game.me.inBattleground && this.preset.directives.triggerBG) {
  88. this.activeMode = this.preset.modes[this.preset.directives.triggerBG];
  89. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerBG}".`);
  90. }
  91. else if (this.__mod.game.me.inCivilUnrest && this.preset.directives.triggerCU) {
  92. this.activeMode = this.preset.modes[this.preset.directives.triggerCU];
  93. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerCU}".`);
  94. }
  95. else if (this.__mod.game.me.inDungeon && this.preset.directives.triggerDungeons) {
  96. let mode = undefined;
  97. let name = undefined;
  98. if (this.preset.modes[this.preset.directives.triggerDungeons[event.zone]]) {
  99. name = this.preset.directives.triggerDungeons[event.zone];
  100. mode = this.preset.modes[this.preset.directives.triggerDungeons[event.zone]];
  101. }
  102. else if (this.preset.modes[this.preset.directives.triggerDungeons["all"]]) {
  103. name = this.preset.directives.triggerDungeons["all"];
  104. mode = this.preset.modes[this.preset.directives.triggerDungeons["all"]];
  105. }
  106. if (mode) {
  107. this.activeMode = mode;
  108. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to ${name}`);
  109. }
  110. }
  111. else if (this.__mod.game.me.inOpenWorld && this.preset.directives.triggerOW) {
  112. this.activeMode = this.preset.modes[this.preset.directives.triggerOW];
  113. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerOW}".`);
  114. }
  115. this._eventTriggerPartial();
  116. });
  117. this.__mod.hook("S_FIELD_EVENT_ON_ENTER", RAW_HOOK_MODE.EVENT, () => {
  118. if (!this.__mod.settings.enableEvents) return;
  119. this.prevMode = rfdc(this.activeMode);
  120. this.lockedMode = true;
  121. this.activeMode = this.preset.modes[this.preset.directives.triggerGuardians];
  122. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerGuardians}".`);
  123. this._eventTrigger();
  124. });
  125. this.__mod.hook("S_FIELD_EVENT_ON_LEAVE", RAW_HOOK_MODE.EVENT, () => {
  126. if (!this.__mod.settings.enableEvents) return;
  127. this.activeMode = rfdc(this.prevMode);
  128. this.lockedMode = false;
  129. if (this.__mod.settings.interactive) this.__log.send("Mode was changed back");
  130. this._eventTrigger();
  131. });
  132. }
  133. _eventTrigger() {
  134. this.emit(EVENTS.RECONFIGURE);
  135. }
  136. _eventTriggerPartial() {
  137. this.emit(EVENTS.RECONFIGUREPARTIAL);
  138. }
  139. }
  140. module.exports = unitState;