configmanager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. }
  44. let config = null;
  45. // Persistance Utility Functions
  46. /**
  47. * Save the current configuration to a file.
  48. */
  49. exports.save = function(){
  50. const filePath = path.join(config.settings.game.directory, 'config.json')
  51. fs.writeFileSync(filePath, JSON.stringify(config, null, 4), 'UTF-8')
  52. }
  53. /**
  54. * Load the configuration into memory. If a configuration file exists,
  55. * that will be read and saved. Otherwise, a default configuration will
  56. * be generated. Note that "resolved" values default to null and will
  57. * need to be externally assigned.
  58. */
  59. exports.load = function(){
  60. // Determine the effective configuration.
  61. const EFFECTIVE_CONFIG = config == null ? DEFAULT_CONFIG : config
  62. const filePath = path.join(EFFECTIVE_CONFIG.settings.game.directory, 'config.json')
  63. if(!fs.existsSync(filePath)){
  64. // Create all parent directories.
  65. mkpath.sync(path.join(filePath, '..'))
  66. config = DEFAULT_CONFIG
  67. exports.save()
  68. } else {
  69. config = JSON.parse(fs.readFileSync(filePath, 'UTF-8'))
  70. }
  71. }
  72. // System Settings (Unconfigurable on UI)
  73. /**
  74. * Retrieve the launcher's Client Token.
  75. *
  76. * @param {Boolean} def - optional. If true, the default value will be returned.
  77. * @returns {String} - the launcher's Client Token.
  78. */
  79. exports.getClientToken = function(def = false){
  80. return !def ? config.clientToken : DEFAULT_CONFIG.clientToken
  81. }
  82. /**
  83. * Set the launcher's Client Token.
  84. *
  85. * @param {String} clientToken - the launcher's new Client Token.
  86. */
  87. exports.setClientToken = function(clientToken){
  88. config.clientToken = clientToken
  89. }
  90. /**
  91. * Retrieve the ID of the selected serverpack.
  92. *
  93. * @param {Boolean} def - optional. If true, the default value will be returned.
  94. * @returns {String} - the ID of the selected serverpack.
  95. */
  96. exports.getSelectedServer = function(def = false){
  97. return !def ? config.selectedServer : DEFAULT_CONFIG.clientToken
  98. }
  99. /**
  100. * Set the ID of the selected serverpack.
  101. *
  102. * @param {String} serverID - the ID of the new selected serverpack.
  103. */
  104. exports.setSelectedServer = function(serverID){
  105. config.selectedServer = serverID
  106. }
  107. /**
  108. * Get an array of each account currently authenticated by the launcher.
  109. *
  110. * @returns {Array.<Object>} - an array of each stored authenticated account.
  111. */
  112. exports.getAuthAccounts = function(){
  113. return config.authenticationDatabase
  114. }
  115. /**
  116. * Returns the authenticated account with the given uuid. Value may
  117. * be null.
  118. *
  119. * @param {String} uuid - the uuid of the authenticated account.
  120. * @returns {Object} - the authenticated account with the given uuid.
  121. */
  122. exports.getAuthAccount = function(uuid){
  123. return config.authenticationDatabase[uuid]
  124. }
  125. /**
  126. * Update the access token of an authenticated account.
  127. *
  128. * @param {String} uuid - uuid of the authenticated account.
  129. * @param {String} accessToken - the new Access Token.
  130. *
  131. * @returns {Object} - the authenticated account object created by this action.
  132. */
  133. exports.updateAuthAccount = function(uuid, accessToken){
  134. config.authenticationDatabase[uuid].accessToken = accessToken
  135. return config.authenticationDatabase[uuid]
  136. }
  137. /**
  138. * Adds an authenticated account to the database to be stored.
  139. *
  140. * @param {String} uuid - uuid of the authenticated account.
  141. * @param {String} accessToken - accessToken of the authenticated account.
  142. * @param {String} username - username (usually email) of the authenticated account.
  143. * @param {String} displayName - in game name of the authenticated account.
  144. *
  145. * @returns {Object} - the authenticated account object created by this action.
  146. */
  147. exports.addAuthAccount = function(uuid, accessToken, username, displayName){
  148. config.selectedAccount = uuid
  149. config.authenticationDatabase[uuid] = {
  150. accessToken,
  151. username,
  152. uuid,
  153. displayName
  154. }
  155. return config.authenticationDatabase[uuid]
  156. }
  157. /**
  158. * Get the currently selected authenticated account.
  159. *
  160. * @returns {Object} - the selected authenticated account.
  161. */
  162. exports.getSelectedAccount = function(){
  163. return config.authenticationDatabase[config.selectedAccount]
  164. }
  165. // User Configurable Settings
  166. // Java Settings
  167. /**
  168. * Retrieve the minimum amount of memory for JVM initialization. This value
  169. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  170. * 1024 MegaBytes, etc.
  171. *
  172. * @param {Boolean} def - optional. If true, the default value will be returned.
  173. * @returns {String} - the minimum amount of memory for JVM initialization.
  174. */
  175. exports.getMinRAM = function(def = false){
  176. return !def ? config.settings.java.minRAM : DEFAULT_CONFIG.settings.java.minRAM
  177. }
  178. /**
  179. * Set the minimum amount of memory for JVM initialization. This value should
  180. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  181. * 1024 MegaBytes, etc.
  182. *
  183. * @param {String} minRAM - the new minimum amount of memory for JVM initialization.
  184. */
  185. exports.setMinRAM = function(minRAM){
  186. config.settings.java.minRAM = minRAM
  187. }
  188. /**
  189. * Retrieve the maximum amount of memory for JVM initialization. This value
  190. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  191. * 1024 MegaBytes, etc.
  192. *
  193. * @param {Boolean} def - optional. If true, the default value will be returned.
  194. * @returns {String} - the maximum amount of memory for JVM initialization.
  195. */
  196. exports.getMaxRAM = function(def = false){
  197. return !def ? config.settings.java.maxRAM : resolveMaxRAM()
  198. }
  199. /**
  200. * Set the maximum amount of memory for JVM initialization. This value should
  201. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  202. * 1024 MegaBytes, etc.
  203. *
  204. * @param {String} maxRAM - the new maximum amount of memory for JVM initialization.
  205. */
  206. exports.setMaxRAM = function(maxRAM){
  207. config.settings.java.maxRAM = maxRAM
  208. }
  209. /**
  210. * Retrieve the path of the Java Executable.
  211. *
  212. * This is a resolved configuration value and defaults to null until externally assigned.
  213. *
  214. * @returns {String} - the path of the Java Executable.
  215. */
  216. exports.getJavaExecutable = function(){
  217. return config.settings.java.executable
  218. }
  219. /**
  220. * Set the path of the Java Executable.
  221. *
  222. * @param {String} executable - the new path of the Java Executable.
  223. */
  224. exports.setJavaExecutable = function(executable){
  225. config.settings.java.executable = executable
  226. }
  227. /**
  228. * Retrieve the additional arguments for JVM initialization. Required arguments,
  229. * such as memory allocation, will be dynamically resolved and will not be included
  230. * in this value.
  231. *
  232. * @param {Boolean} def - optional. If true, the default value will be returned.
  233. * @returns {Array.<String>} - an array of the additional arguments for JVM initialization.
  234. */
  235. exports.getJVMOptions = function(def = false){
  236. return !def ? config.settings.java.jvmOptions : DEFAULT_CONFIG.settings.java.jvmOptions
  237. }
  238. /**
  239. * Set the additional arguments for JVM initialization. Required arguments,
  240. * such as memory allocation, will be dynamically resolved and should not be
  241. * included in this value.
  242. *
  243. * @param {Array.<String>} jvmOptions - an array of the new additional arguments for JVM
  244. * initialization.
  245. */
  246. exports.setJVMOptions = function(jvmOptions){
  247. config.settings.java.jvmOptions = jvmOptions
  248. }
  249. // Game Settings
  250. /**
  251. * Retrieve the absolute path of the game directory.
  252. *
  253. * @param {Boolean} def - optional. If true, the default value will be returned.
  254. * @returns {String} - the absolute path of the game directory.
  255. */
  256. exports.getGameDirectory = function(def = false){
  257. return !def ? config.settings.game.directory : DEFAULT_CONFIG.settings.game.directory
  258. }
  259. /**
  260. * Set the absolute path of the game directory.
  261. *
  262. * @param {String} directory - the absolute path of the new game directory.
  263. */
  264. exports.setGameDirectory = function(directory){
  265. config.settings.game.directory = directory
  266. }
  267. /**
  268. * Retrieve the width of the game window.
  269. *
  270. * @param {Boolean} def - optional. If true, the default value will be returned.
  271. * @returns {Number} - the width of the game window.
  272. */
  273. exports.getGameWidth = function(def = false){
  274. return !def ? config.settings.game.resWidth : DEFAULT_CONFIG.settings.game.resWidth
  275. }
  276. /**
  277. * Set the width of the game window.
  278. *
  279. * @param {Number} resWidth - the new width of the game window.
  280. */
  281. exports.setGameWidth = function(resWidth){
  282. config.settings.game.resWidth = resWidth
  283. }
  284. /**
  285. * Retrieve the height of the game window.
  286. *
  287. * @param {Boolean} def - optional. If true, the default value will be returned.
  288. * @returns {Number} - the height of the game window.
  289. */
  290. exports.getGameHeight = function(def = false){
  291. return !def ? config.settings.game.resHeight : DEFAULT_CONFIG.settings.game.resHeight
  292. }
  293. /**
  294. * Set the height of the game window.
  295. *
  296. * @param {Number} resHeight - the new height of the game window.
  297. */
  298. exports.setGameHeight = function(resHeight){
  299. config.settings.game.resHeight = resHeight
  300. }
  301. /**
  302. * Check if the game should be launched in fullscreen mode.
  303. *
  304. * @param {Boolean} def - optional. If true, the default value will be returned.
  305. * @returns {Boolean} - whether or not the game is set to launch in fullscreen mode.
  306. */
  307. exports.isFullscreen = function(def = false){
  308. return !def ? config.settings.game.fullscreen : DEFAULT_CONFIG.settings.game.fullscreen
  309. }
  310. /**
  311. * Change the status of if the game should be launched in fullscreen mode.
  312. *
  313. * @param {Boolean} fullscreen - whether or not the game should launch in fullscreen mode.
  314. */
  315. exports.setFullscreen = function(fullscreen){
  316. config.settings.game.fullscreen = fullscreen
  317. }
  318. /**
  319. * Check if the game should auto connect to servers.
  320. *
  321. * @param {Boolean} def - optional. If true, the default value will be returned.
  322. * @returns {Boolean} - whether or not the game should auto connect to servers.
  323. */
  324. exports.isAutoConnect = function(def = false){
  325. return !def ? config.settings.game.autoConnect : DEFAULT_CONFIG.settings.game.autoConnect
  326. }
  327. /**
  328. * Change the status of whether or not the game should auto connect to servers.
  329. *
  330. * @param {Boolean} autoConnect - whether or not the game should auto connect to servers.
  331. */
  332. exports.setAutoConnect = function(autoConnect){
  333. config.settings.game.autoConnect = autoConnect
  334. }