configmanager.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. const fs = require('fs')
  2. const mkpath = require('mkdirp')
  3. const os = require('os')
  4. const path = require('path')
  5. const uuidV4 = require('uuid/v4')
  6. function resolveMaxRAM(){
  7. const mem = os.totalmem()
  8. return mem >= 8000000000 ? '4G' : (mem >= 6000000000 ? '3G' : '2G')
  9. }
  10. /**
  11. * Three types of values:
  12. * Static = Explicitly declared.
  13. * Dynamic = Calculated by a private function.
  14. * Resolved = Resolved externally, defaults to null.
  15. */
  16. const DEFAULT_CONFIG = {
  17. settings: {
  18. java: {
  19. minRAM: '2G',
  20. maxRAM: resolveMaxRAM(), // Dynamic
  21. executable: 'C:\\Program Files\\Java\\jdk1.8.0_152\\bin\\javaw.exe', // TODO Resolve
  22. jvmOptions: [
  23. '-XX:+UseConcMarkSweepGC',
  24. '-XX:+CMSIncrementalMode',
  25. '-XX:-UseAdaptiveSizePolicy',
  26. '-Xmn128M'
  27. ],
  28. },
  29. game: {
  30. directory: path.join(__dirname, '..', '..', '..', 'target', 'test', 'mcfiles'),
  31. resWidth: 1280,
  32. resHeight: 720,
  33. fullscreen: false,
  34. autoConnect: true
  35. },
  36. launcher: {
  37. }
  38. },
  39. clientToken: uuidV4(),
  40. selectedServer: null, // Resolved
  41. selectedAccount: null,
  42. authenticationDatabase: [],
  43. discord: {
  44. clientID: 385581240906022916
  45. }
  46. }
  47. let config = null;
  48. // Persistance Utility Functions
  49. /**
  50. * Save the current configuration to a file.
  51. */
  52. exports.save = function(){
  53. const filePath = path.join(config.settings.game.directory, 'config.json')
  54. fs.writeFileSync(filePath, JSON.stringify(config, null, 4), 'UTF-8')
  55. }
  56. /**
  57. * Load the configuration into memory. If a configuration file exists,
  58. * that will be read and saved. Otherwise, a default configuration will
  59. * be generated. Note that "resolved" values default to null and will
  60. * need to be externally assigned.
  61. */
  62. exports.load = function(){
  63. // Determine the effective configuration.
  64. const EFFECTIVE_CONFIG = config == null ? DEFAULT_CONFIG : config
  65. const filePath = path.join(EFFECTIVE_CONFIG.settings.game.directory, 'config.json')
  66. if(!fs.existsSync(filePath)){
  67. // Create all parent directories.
  68. mkpath.sync(path.join(filePath, '..'))
  69. config = DEFAULT_CONFIG
  70. exports.save()
  71. } else {
  72. config = JSON.parse(fs.readFileSync(filePath, 'UTF-8'))
  73. }
  74. }
  75. // System Settings (Unconfigurable on UI)
  76. /**
  77. * Retrieve the launcher's Client Token.
  78. *
  79. * @param {Boolean} def - optional. If true, the default value will be returned.
  80. * @returns {String} - the launcher's Client Token.
  81. */
  82. exports.getClientToken = function(def = false){
  83. return !def ? config.clientToken : DEFAULT_CONFIG.clientToken
  84. }
  85. /**
  86. * Set the launcher's Client Token.
  87. *
  88. * @param {String} clientToken - the launcher's new Client Token.
  89. */
  90. exports.setClientToken = function(clientToken){
  91. config.clientToken = clientToken
  92. }
  93. /**
  94. * Retrieve the ID of the selected serverpack.
  95. *
  96. * @param {Boolean} def - optional. If true, the default value will be returned.
  97. * @returns {String} - the ID of the selected serverpack.
  98. */
  99. exports.getSelectedServer = function(def = false){
  100. return !def ? config.selectedServer : DEFAULT_CONFIG.clientToken
  101. }
  102. /**
  103. * Set the ID of the selected serverpack.
  104. *
  105. * @param {String} serverID - the ID of the new selected serverpack.
  106. */
  107. exports.setSelectedServer = function(serverID){
  108. config.selectedServer = serverID
  109. }
  110. //TODO Write Authentication Database/Selected Account accessors here
  111. /**
  112. * Retrieve the launcher's Discord Client ID.
  113. *
  114. * @param {Boolean} def - optional. If true, the default value will be returned.
  115. * @returns {String} - the launcher's Discord Client ID.
  116. */
  117. exports.getDiscordClientID = function(def = false){
  118. return !def ? config.discord.clientID : DEFAULT_CONFIG.discord.clientID
  119. }
  120. /**
  121. * Set the launcher's Discord Client ID.
  122. *
  123. * @param {String} clientID - the launcher's new Discord Client ID.
  124. */
  125. exports.setDiscordClientID = function(clientID){
  126. config.discord.clientID = clientID
  127. }
  128. // User Configurable Settings
  129. // Java Settings
  130. /**
  131. * Retrieve the minimum amount of memory for JVM initialization. This value
  132. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  133. * 1024 MegaBytes, etc.
  134. *
  135. * @param {Boolean} def - optional. If true, the default value will be returned.
  136. * @returns {String} - the minimum amount of memory for JVM initialization.
  137. */
  138. exports.getMinRAM = function(def = false){
  139. return !def ? config.settings.java.minRAM : DEFAULT_CONFIG.settings.java.minRAM
  140. }
  141. /**
  142. * Set the minimum amount of memory for JVM initialization. This value should
  143. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  144. * 1024 MegaBytes, etc.
  145. *
  146. * @param {String} minRAM - the new minimum amount of memory for JVM initialization.
  147. */
  148. exports.setMinRAM = function(minRAM){
  149. config.settings.java.minRAM = minRAM
  150. }
  151. /**
  152. * Retrieve the maximum amount of memory for JVM initialization. This value
  153. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  154. * 1024 MegaBytes, etc.
  155. *
  156. * @param {Boolean} def - optional. If true, the default value will be returned.
  157. * @returns {String} - the maximum amount of memory for JVM initialization.
  158. */
  159. exports.getMaxRAM = function(def = false){
  160. return !def ? config.settings.java.maxRAM : resolveMaxRAM()
  161. }
  162. /**
  163. * Set the maximum amount of memory for JVM initialization. This value should
  164. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  165. * 1024 MegaBytes, etc.
  166. *
  167. * @param {String} maxRAM - the new maximum amount of memory for JVM initialization.
  168. */
  169. exports.setMaxRAM = function(maxRAM){
  170. config.settings.java.maxRAM = maxRAM
  171. }
  172. /**
  173. * Retrieve the path of the Java Executable.
  174. *
  175. * This is a resolved configuration value and defaults to null until externally assigned.
  176. *
  177. * @returns {String} - the path of the Java Executable.
  178. */
  179. exports.getJavaExecutable = function(){
  180. return config.settings.java.executable
  181. }
  182. /**
  183. * Set the path of the Java Executable.
  184. *
  185. * @param {String} executable - the new path of the Java Executable.
  186. */
  187. exports.setJavaExecutable = function(executable){
  188. config.settings.java.executable = executable
  189. }
  190. /**
  191. * Retrieve the additional arguments for JVM initialization. Required arguments,
  192. * such as memory allocation, will be dynamically resolved and will not be included
  193. * in this value.
  194. *
  195. * @param {Boolean} def - optional. If true, the default value will be returned.
  196. * @returns {Array.<String>} - an array of the additional arguments for JVM initialization.
  197. */
  198. exports.getJVMOptions = function(def = false){
  199. return !def ? config.settings.java.jvmOptions : DEFAULT_CONFIG.settings.java.jvmOptions
  200. }
  201. /**
  202. * Set the additional arguments for JVM initialization. Required arguments,
  203. * such as memory allocation, will be dynamically resolved and should not be
  204. * included in this value.
  205. *
  206. * @param {Array.<String>} jvmOptions - an array of the new additional arguments for JVM
  207. * initialization.
  208. */
  209. exports.setJVMOptions = function(jvmOptions){
  210. config.settings.java.jvmOptions = jvmOptions
  211. }
  212. // Game Settings
  213. /**
  214. * Retrieve the absolute path of the game directory.
  215. *
  216. * @param {Boolean} def - optional. If true, the default value will be returned.
  217. * @returns {String} - the absolute path of the game directory.
  218. */
  219. exports.getGameDirectory = function(def = false){
  220. return !def ? config.settings.game.directory : DEFAULT_CONFIG.settings.game.directory
  221. }
  222. /**
  223. * Set the absolute path of the game directory.
  224. *
  225. * @param {String} directory - the absolute path of the new game directory.
  226. */
  227. exports.setGameDirectory = function(directory){
  228. config.settings.game.directory = directory
  229. }
  230. /**
  231. * Retrieve the width of the game window.
  232. *
  233. * @param {Boolean} def - optional. If true, the default value will be returned.
  234. * @returns {Number} - the width of the game window.
  235. */
  236. exports.getGameWidth = function(def = false){
  237. return !def ? config.settings.game.resWidth : DEFAULT_CONFIG.settings.game.resWidth
  238. }
  239. /**
  240. * Set the width of the game window.
  241. *
  242. * @param {Number} resWidth - the new width of the game window.
  243. */
  244. exports.setGameWidth = function(resWidth){
  245. config.settings.game.resWidth = resWidth
  246. }
  247. /**
  248. * Retrieve the height of the game window.
  249. *
  250. * @param {Boolean} def - optional. If true, the default value will be returned.
  251. * @returns {Number} - the height of the game window.
  252. */
  253. exports.getGameHeight = function(def = false){
  254. return !def ? config.settings.game.resHeight : DEFAULT_CONFIG.settings.game.resHeight
  255. }
  256. /**
  257. * Set the height of the game window.
  258. *
  259. * @param {Number} resHeight - the new height of the game window.
  260. */
  261. exports.setGameHeight = function(resHeight){
  262. config.settings.game.resHeight = resHeight
  263. }
  264. /**
  265. * Check if the game should be launched in fullscreen mode.
  266. *
  267. * @param {Boolean} def - optional. If true, the default value will be returned.
  268. * @returns {Boolean} - whether or not the game is set to launch in fullscreen mode.
  269. */
  270. exports.isFullscreen = function(def = false){
  271. return !def ? config.settings.game.fullscreen : DEFAULT_CONFIG.settings.game.fullscreen
  272. }
  273. /**
  274. * Change the status of if the game should be launched in fullscreen mode.
  275. *
  276. * @param {Boolean} fullscreen - whether or not the game should launch in fullscreen mode.
  277. */
  278. exports.setFullscreen = function(fullscreen){
  279. config.settings.game.fullscreen = fullscreen
  280. }
  281. /**
  282. * Check if the game should auto connect to servers.
  283. *
  284. * @param {Boolean} def - optional. If true, the default value will be returned.
  285. * @returns {Boolean} - whether or not the game should auto connect to servers.
  286. */
  287. exports.isAutoConnect = function(def = false){
  288. return !def ? config.settings.game.autoConnect : DEFAULT_CONFIG.settings.game.autoConnect
  289. }
  290. /**
  291. * Change the status of whether or not the game should auto connect to servers.
  292. *
  293. * @param {Boolean} autoConnect - whether or not the game should auto connect to servers.
  294. */
  295. exports.setAutoConnect = function(autoConnect){
  296. config.settings.game.autoConnect = autoConnect
  297. }