dropinmodutil.js 3.5 KB

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