index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const SettingsUI = require('tera-mod-ui').Settings;
  2. exports.NetworkMod = function(mod) {
  3. const PURPOSES = ['enchant', 'upgrade', 'soulbind', 'merge', 'dismantle'];
  4. let hooks = {};
  5. function hook(purpose, ...args) {
  6. if (!hooks[purpose])
  7. hooks[purpose] = [];
  8. hooks[purpose].push(mod.hook(...args));
  9. }
  10. let enchanting = null;
  11. let upgrading = null;
  12. function enable(purpose) {
  13. switch (purpose) {
  14. case 'enchant': {
  15. if(mod.majorPatchVersion >= 61) {
  16. hook('enchant', 'C_REGISTER_ENCHANT_ITEM', 1, event => { enchanting = event });
  17. hook('enchant', 'C_START_ENCHANT', 1, event => {
  18. if (enchanting && event.contract === enchanting.contract) {
  19. mod.send('C_REQUEST_ENCHANT', 1, enchanting);
  20. return false;
  21. }
  22. });
  23. hook('enchant', 'C_REQUEST_ENCHANT', 'event', () => false);
  24. } else {
  25. // TODO Classic
  26. }
  27. break;
  28. }
  29. case 'upgrade': {
  30. if(mod.majorPatchVersion >= 79) {
  31. hook('upgrade', 'C_REGISTER_EVOLUTION_ITEM', 1, event => { upgrading = event });
  32. hook('upgrade', 'C_START_EVOLUTION', 1, event => {
  33. if (upgrading && event.contract === upgrading.contract) {
  34. mod.send('C_REQUEST_EVOLUTION', 1, upgrading);
  35. return false;
  36. }
  37. });
  38. hook('upgrade', 'C_REQUEST_EVOLUTION', 'event', () => false);
  39. }
  40. break;
  41. }
  42. case 'soulbind': {
  43. hook('soulbind', 'C_BIND_ITEM_BEGIN_PROGRESS', 1, event => {
  44. mod.send('C_BIND_ITEM_EXECUTE', 1, {
  45. contractId: event.contractId,
  46. });
  47. process.nextTick(() => {
  48. mod.send('S_CANCEL_CONTRACT', 1, {
  49. type: 32,
  50. id: event.contractId,
  51. });
  52. });
  53. });
  54. hook('soulbind', 'C_BIND_ITEM_EXECUTE', 'event', () => false);
  55. break;
  56. }
  57. case 'merge': {
  58. hook('merge', 'S_REQUEST_CONTRACT', mod.majorPatchVersion ? 2 : 1, event => {
  59. if (!mod.game.me.is(event.senderId) || event.type != 33)
  60. return;
  61. mod.send('C_MERGE_ITEM_EXECUTE', 1, {
  62. contractId: event.id,
  63. });
  64. process.nextTick(() => {
  65. mod.send('S_CANCEL_CONTRACT', 1, {
  66. type: 33,
  67. id: event.id,
  68. });
  69. });
  70. });
  71. hook('merge', 'C_MERGE_ITEM_EXECUTE', 'event', () => false);
  72. break;
  73. }
  74. case 'dismantle': {
  75. if(mod.majorPatchVersion >= 77) {
  76. hook('dismantle', 'C_RQ_START_SOCIAL_ON_PROGRESS_DECOMPOSITION', 1, event => {
  77. mod.send('C_RQ_COMMIT_DECOMPOSITION_CONTRACT', 1, {
  78. contract: event.contract,
  79. });
  80. return false;
  81. });
  82. hook('dismantle', 'C_RQ_COMMIT_DECOMPOSITION_CONTRACT', 'event', () => false);
  83. }
  84. break;
  85. }
  86. }
  87. }
  88. function disable(purpose) {
  89. if (hooks[purpose]) {
  90. hooks[purpose].forEach(h => mod.unhook(h));
  91. hooks[purpose] = [];
  92. }
  93. }
  94. // Main
  95. PURPOSES.forEach(purpose => {
  96. if (mod.settings[purpose])
  97. enable(purpose);
  98. });
  99. mod.command.add('instant', {
  100. $default(purpose) {
  101. if (PURPOSES.indexOf(purpose) < 0) {
  102. if (ui) {
  103. ui.show();
  104. } else {
  105. mod.command.message(purpose ? `Invalid mode: ${purpose}!` : 'Must specify mode!');
  106. mod.command.message(`Valid modes: ${PURPOSES.join(', ')}`);
  107. }
  108. return;
  109. }
  110. if (mod.settings[purpose]) {
  111. disable(purpose);
  112. mod.command.message(`Instant ${purpose} disabled!`);
  113. } else {
  114. enable(purpose);
  115. mod.command.message(`Instant ${purpose} enabled!`);
  116. }
  117. mod.settings[purpose] = !mod.settings[purpose];
  118. }
  119. });
  120. // Settings UI
  121. let ui = null;
  122. if (global.TeraProxy.GUIMode) {
  123. ui = new SettingsUI(mod, require('./settings_structure'), mod.settings, { height: 232 });
  124. ui.on('update', settings => { mod.settings = settings; });
  125. this.destructor = () => {
  126. if (ui) {
  127. ui.close();
  128. ui = null;
  129. }
  130. };
  131. }
  132. };