You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
1.8 KiB

  1. const fileLister = require('../misc/fileLister');
  2. const events = require('../misc/events');
  3. module.exports = {
  4. mods: [],
  5. init: async function () {
  6. const modList = fileLister.getFolderList('mods');
  7. //Load all mods
  8. let loadList = modList.map(m => {
  9. const path = `../mods/${m}/index`;
  10. const mod = require(path);
  11. const id = mod.id ? mod.id : m.replace('iwd-', '');
  12. return {
  13. id,
  14. folderName: m,
  15. path,
  16. mod
  17. };
  18. });
  19. //Reorder mods so that mods that depend on others load later
  20. loadList = loadList.sort((a, b) => {
  21. const { id: idA, mod: { dependsOn: depA = [] } } = a;
  22. const { id: idB, mod: { dependsOn: depB = [] } } = b;
  23. if (depB.includes(idA) || !depA.length)
  24. return -1;
  25. else if (depA.includes(idB) || !depB.length)
  26. return 1;
  27. return 0;
  28. });
  29. //Initialize mods
  30. for (const m of loadList) {
  31. const { folderName, mod } = m;
  32. await this.onGetMod(folderName, mod);
  33. this.mods.push(mod);
  34. }
  35. },
  36. onGetMod: async function (name, mod) {
  37. if (mod.disabled)
  38. return;
  39. const isMapThread = global.instancer;
  40. mod.isMapThread = isMapThread;
  41. mod.events = events;
  42. mod.folderName = 'server/mods/' + name;
  43. mod.relativeFolderName = 'mods/' + name;
  44. let list = (mod.extraScripts || []);
  45. let lLen = list.length;
  46. for (let i = 0; i < lLen; i++) {
  47. let extra = require('../mods/' + name + '/' + list[i]);
  48. this.onGetExtra(name, mod, extra);
  49. }
  50. if (typeof mod.init === 'function')
  51. await mod.init();
  52. if (isMapThread && typeof mod.initMapThread === 'function')
  53. await mod.initMapThread();
  54. else if (!isMapThread && typeof mod.initMainThread === 'function')
  55. await mod.initMainThread();
  56. },
  57. onGetExtra: function (name, mod, extra) {
  58. extra.folderName = 'server/mods/' + name;
  59. },
  60. tick: function () {
  61. this.mods.forEach(m => {
  62. if (m.tick)
  63. m.tick();
  64. });
  65. }
  66. };