dropinmodutil.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const fs = require('fs')
  2. const mkpath = require('mkdirp')
  3. const path = require('path')
  4. const { shell } = require('electron')
  5. // Group #1: File Name (without .disabled, if any)
  6. // Group #2: File Extension (jar, zip, or litemod)
  7. // Group #3: If it is disabled (if string 'disabled' is present)
  8. const MOD_REGEX = /^(.+(jar|zip|litemod))(?:\.(disabled))?$/
  9. const DISABLED_EXT = '.disabled'
  10. /**
  11. * Validate that the given mods directory exists. If not,
  12. * it is created.
  13. *
  14. * @param {string} modsDir The path to the mods directory.
  15. */
  16. exports.validateModsDir = function(modsDir) {
  17. if(!fs.existsSync(modsDir)) {
  18. mkpath.sync(modsDir)
  19. }
  20. }
  21. /**
  22. * Scan for drop-in mods in both the mods folder and version
  23. * safe mods folder.
  24. *
  25. * @param {string} modsDir The path to the mods directory.
  26. * @param {string} version The minecraft version of the server configuration.
  27. *
  28. * @returns {{fullName: string, name: string, ext: string, disabled: boolean}[]}
  29. * An array of objects storing metadata about each discovered mod.
  30. */
  31. exports.scanForDropinMods = function(modsDir, version) {
  32. const modsDiscovered = []
  33. if(fs.existsSync(modsDir)){
  34. let modCandidates = fs.readdirSync(modsDir)
  35. let verCandidates = []
  36. const versionDir = path.join(modsDir, version)
  37. if(fs.existsSync(versionDir)){
  38. verCandidates = fs.readdirSync(versionDir)
  39. }
  40. for(let file of modCandidates){
  41. const match = MOD_REGEX.exec(file)
  42. if(match != null){
  43. modsDiscovered.push({
  44. fullName: match[0],
  45. name: match[1],
  46. ext: match[2],
  47. disabled: match[3] != null
  48. })
  49. }
  50. }
  51. for(let file of verCandidates){
  52. const match = MOD_REGEX.exec(file)
  53. if(match != null){
  54. modsDiscovered.push({
  55. fullName: path.join(version, match[0]),
  56. name: match[1],
  57. ext: match[2],
  58. disabled: match[3] != null
  59. })
  60. }
  61. }
  62. }
  63. return modsDiscovered
  64. }
  65. /**
  66. * Delete a drop-in mod from the file system.
  67. *
  68. * @param {string} modsDir The path to the mods directory.
  69. * @param {string} fullName The fullName of the discovered mod to delete.
  70. *
  71. * @returns {boolean} True if the mod was deleted, otherwise false.
  72. */
  73. exports.deleteDropinMod = function(modsDir, fullName){
  74. const res = shell.moveItemToTrash(path.join(modsDir, fullName))
  75. if(!res){
  76. shell.beep()
  77. }
  78. return res
  79. }
  80. /**
  81. * Toggle a discovered mod on or off. This is achieved by either
  82. * adding or disabling the .disabled extension to the local file.
  83. *
  84. * @param {string} modsDir The path to the mods directory.
  85. * @param {string} fullName The fullName of the discovered mod to toggle.
  86. * @param {boolean} enable Whether to toggle on or off the mod.
  87. *
  88. * @returns {Promise.<void>} A promise which resolves when the mod has
  89. * been toggled. If an IO error occurs the promise will be rejected.
  90. */
  91. exports.toggleDropinMod = function(modsDir, fullName, enable){
  92. return new Promise((resolve, reject) => {
  93. const oldPath = path.join(modsDir, fullName)
  94. const newPath = path.join(modsDir, enable ? fullName.substring(0, fullName.indexOf(DISABLED_EXT)) : fullName + DISABLED_EXT)
  95. fs.rename(oldPath, newPath, (err) => {
  96. if(err){
  97. reject(err)
  98. } else {
  99. resolve()
  100. }
  101. })
  102. })
  103. }
  104. /**
  105. * Check if a drop-in mod is enabled.
  106. *
  107. * @param {string} fullName The fullName of the discovered mod to toggle.
  108. * @returns {boolean} True if the mod is enabled, otherwise false.
  109. */
  110. exports.isDropinModEnabled = function(fullName){
  111. return !fullName.endsWith(DISABLED_EXT)
  112. }