configmanager.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. const sysRoot = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME)
  7. const dataPath = path.join(sysRoot, '.westeroscraft')
  8. const firstLaunch = !fs.existsSync(dataPath)
  9. function resolveMaxRAM(){
  10. const mem = os.totalmem()
  11. return mem >= 8000000000 ? '4G' : (mem >= 6000000000 ? '3G' : '2G')
  12. }
  13. /**
  14. * Three types of values:
  15. * Static = Explicitly declared.
  16. * Dynamic = Calculated by a private function.
  17. * Resolved = Resolved externally, defaults to null.
  18. */
  19. const DEFAULT_CONFIG = {
  20. settings: {
  21. java: {
  22. minRAM: '2G',
  23. maxRAM: resolveMaxRAM(), // Dynamic
  24. executable: null,
  25. jvmOptions: [
  26. '-XX:+UseConcMarkSweepGC',
  27. '-XX:+CMSIncrementalMode',
  28. '-XX:-UseAdaptiveSizePolicy',
  29. '-Xmn128M'
  30. ],
  31. },
  32. game: {
  33. directory: path.join(dataPath, 'game'),
  34. resWidth: 1280,
  35. resHeight: 720,
  36. fullscreen: false,
  37. autoConnect: true,
  38. launchDetached: true
  39. },
  40. launcher: {}
  41. },
  42. clientToken: uuidV4().replace(/-/g, ''),
  43. selectedServer: null, // Resolved
  44. selectedAccount: null,
  45. authenticationDatabase: {}
  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(dataPath, '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(dataPath, '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. config = validateKeySet(DEFAULT_CONFIG, config)
  74. exports.save()
  75. }
  76. }
  77. /**
  78. * Validate that the destination object has at least every field
  79. * present in the source object. Assign a default value otherwise.
  80. *
  81. * @param {Object} srcObj The source object to reference against.
  82. * @param {Object} destObj The destination object.
  83. * @returns {Object} A validated destination object.
  84. */
  85. function validateKeySet(srcObj, destObj){
  86. if(srcObj == null){
  87. srcObj = {}
  88. }
  89. const validationBlacklist = ['authenticationDatabase']
  90. const keys = Object.keys(srcObj)
  91. for(let i=0; i<keys.length; i++){
  92. if(typeof destObj[keys[i]] === 'undefined'){
  93. destObj[keys[i]] = srcObj[keys[i]]
  94. } else if(typeof srcObj[keys[i]] === 'object' && srcObj[keys[i]] != null && !(srcObj[keys[i]] instanceof Array) && validationBlacklist.indexOf(keys[i]) === -1){
  95. destObj[keys[i]] = validateKeySet(srcObj[keys[i]], destObj[keys[i]])
  96. }
  97. }
  98. return destObj
  99. }
  100. /**
  101. * Retrieve the absolute path of the launcher directory.
  102. *
  103. * @returns {string} The absolute path of the launcher directory.
  104. */
  105. exports.getLauncherDirectory = function(){
  106. return dataPath
  107. }
  108. /**
  109. * Check to see if this is the first time the user has launched the
  110. * application. This is determined by the existance of the data path.
  111. *
  112. * @returns {boolean} True if this is the first launch, otherwise false.
  113. */
  114. exports.isFirstLaunch = function(){
  115. return firstLaunch
  116. }
  117. /**
  118. * Returns the name of the folder in the OS temp directory which we
  119. * will use to extract and store native dependencies for game launch.
  120. *
  121. * @returns {string} The name of the folder.
  122. */
  123. exports.getTempNativeFolder = function(){
  124. return 'WCNatives'
  125. }
  126. // System Settings (Unconfigurable on UI)
  127. /**
  128. * Retrieve the launcher's Client Token.
  129. * There is no default client token.
  130. *
  131. * @returns {string} The launcher's Client Token.
  132. */
  133. exports.getClientToken = function(){
  134. return config.clientToken
  135. }
  136. /**
  137. * Set the launcher's Client Token.
  138. *
  139. * @param {string} clientToken The launcher's new Client Token.
  140. */
  141. exports.setClientToken = function(clientToken){
  142. config.clientToken = clientToken
  143. }
  144. /**
  145. * Retrieve the ID of the selected serverpack.
  146. *
  147. * @param {boolean} def Optional. If true, the default value will be returned.
  148. * @returns {string} The ID of the selected serverpack.
  149. */
  150. exports.getSelectedServer = function(def = false){
  151. return !def ? config.selectedServer : DEFAULT_CONFIG.clientToken
  152. }
  153. /**
  154. * Set the ID of the selected serverpack.
  155. *
  156. * @param {string} serverID The ID of the new selected serverpack.
  157. */
  158. exports.setSelectedServer = function(serverID){
  159. config.selectedServer = serverID
  160. }
  161. /**
  162. * Get an array of each account currently authenticated by the launcher.
  163. *
  164. * @returns {Array.<Object>} An array of each stored authenticated account.
  165. */
  166. exports.getAuthAccounts = function(){
  167. return config.authenticationDatabase
  168. }
  169. /**
  170. * Returns the authenticated account with the given uuid. Value may
  171. * be null.
  172. *
  173. * @param {string} uuid The uuid of the authenticated account.
  174. * @returns {Object} The authenticated account with the given uuid.
  175. */
  176. exports.getAuthAccount = function(uuid){
  177. return config.authenticationDatabase[uuid]
  178. }
  179. /**
  180. * Update the access token of an authenticated account.
  181. *
  182. * @param {string} uuid The uuid of the authenticated account.
  183. * @param {string} accessToken The new Access Token.
  184. *
  185. * @returns {Object} The authenticated account object created by this action.
  186. */
  187. exports.updateAuthAccount = function(uuid, accessToken){
  188. config.authenticationDatabase[uuid].accessToken = accessToken
  189. return config.authenticationDatabase[uuid]
  190. }
  191. /**
  192. * Adds an authenticated account to the database to be stored.
  193. *
  194. * @param {string} uuid The uuid of the authenticated account.
  195. * @param {string} accessToken The accessToken of the authenticated account.
  196. * @param {string} username The username (usually email) of the authenticated account.
  197. * @param {string} displayName The in game name of the authenticated account.
  198. *
  199. * @returns {Object} The authenticated account object created by this action.
  200. */
  201. exports.addAuthAccount = function(uuid, accessToken, username, displayName){
  202. config.selectedAccount = uuid
  203. config.authenticationDatabase[uuid] = {
  204. accessToken,
  205. username,
  206. uuid,
  207. displayName
  208. }
  209. return config.authenticationDatabase[uuid]
  210. }
  211. /**
  212. * Remove an authenticated account from the database. If the account
  213. * was also the selected account, a new one will be selected. If there
  214. * are no accounts, the selected account will be null.
  215. *
  216. * @param {string} uuid The uuid of the authenticated account.
  217. *
  218. * @returns {boolean} True if the account was removed, false if it never existed.
  219. */
  220. exports.removeAuthAccount = function(uuid){
  221. if(config.authenticationDatabase[uuid] != null){
  222. delete config.authenticationDatabase[uuid]
  223. if(config.selectedAccount === uuid){
  224. const keys = Object.keys(config.authenticationDatabase)
  225. if(keys.length > 0){
  226. config.selectedAccount = keys[0]
  227. } else {
  228. config.selectedAccount = null
  229. }
  230. }
  231. return true
  232. }
  233. return false
  234. }
  235. /**
  236. * Get the currently selected authenticated account.
  237. *
  238. * @returns {Object} The selected authenticated account.
  239. */
  240. exports.getSelectedAccount = function(){
  241. return config.authenticationDatabase[config.selectedAccount]
  242. }
  243. /**
  244. * Set the selected authenticated account.
  245. *
  246. * @param {string} uuid The UUID of the account which is to be set
  247. * as the selected account.
  248. *
  249. * @returns {Object} The selected authenticated account.
  250. */
  251. exports.setSelectedAccount = function(uuid){
  252. const authAcc = config.authenticationDatabase[uuid]
  253. if(authAcc != null) {
  254. config.selectedAccount = uuid
  255. }
  256. return authAcc
  257. }
  258. // User Configurable Settings
  259. // Java Settings
  260. /**
  261. * Retrieve the minimum amount of memory for JVM initialization. This value
  262. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  263. * 1024 MegaBytes, etc.
  264. *
  265. * @param {boolean} def Optional. If true, the default value will be returned.
  266. * @returns {string} The minimum amount of memory for JVM initialization.
  267. */
  268. exports.getMinRAM = function(def = false){
  269. return !def ? config.settings.java.minRAM : DEFAULT_CONFIG.settings.java.minRAM
  270. }
  271. /**
  272. * Set the minimum amount of memory for JVM initialization. This value should
  273. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  274. * 1024 MegaBytes, etc.
  275. *
  276. * @param {string} minRAM The new minimum amount of memory for JVM initialization.
  277. */
  278. exports.setMinRAM = function(minRAM){
  279. config.settings.java.minRAM = minRAM
  280. }
  281. /**
  282. * Retrieve the maximum amount of memory for JVM initialization. This value
  283. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  284. * 1024 MegaBytes, etc.
  285. *
  286. * @param {boolean} def Optional. If true, the default value will be returned.
  287. * @returns {string} The maximum amount of memory for JVM initialization.
  288. */
  289. exports.getMaxRAM = function(def = false){
  290. return !def ? config.settings.java.maxRAM : resolveMaxRAM()
  291. }
  292. /**
  293. * Set the maximum amount of memory for JVM initialization. This value should
  294. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  295. * 1024 MegaBytes, etc.
  296. *
  297. * @param {string} maxRAM The new maximum amount of memory for JVM initialization.
  298. */
  299. exports.setMaxRAM = function(maxRAM){
  300. config.settings.java.maxRAM = maxRAM
  301. }
  302. /**
  303. * Retrieve the path of the Java Executable.
  304. *
  305. * This is a resolved configuration value and defaults to null until externally assigned.
  306. *
  307. * @returns {string} The path of the Java Executable.
  308. */
  309. exports.getJavaExecutable = function(){
  310. return config.settings.java.executable
  311. }
  312. /**
  313. * Set the path of the Java Executable.
  314. *
  315. * @param {string} executable The new path of the Java Executable.
  316. */
  317. exports.setJavaExecutable = function(executable){
  318. config.settings.java.executable = executable
  319. }
  320. /**
  321. * Retrieve the additional arguments for JVM initialization. Required arguments,
  322. * such as memory allocation, will be dynamically resolved and will not be included
  323. * in this value.
  324. *
  325. * @param {boolean} def Optional. If true, the default value will be returned.
  326. * @returns {Array.<string>} An array of the additional arguments for JVM initialization.
  327. */
  328. exports.getJVMOptions = function(def = false){
  329. return !def ? config.settings.java.jvmOptions : DEFAULT_CONFIG.settings.java.jvmOptions
  330. }
  331. /**
  332. * Set the additional arguments for JVM initialization. Required arguments,
  333. * such as memory allocation, will be dynamically resolved and should not be
  334. * included in this value.
  335. *
  336. * @param {Array.<string>} jvmOptions An array of the new additional arguments for JVM
  337. * initialization.
  338. */
  339. exports.setJVMOptions = function(jvmOptions){
  340. config.settings.java.jvmOptions = jvmOptions
  341. }
  342. // Game Settings
  343. /**
  344. * Retrieve the absolute path of the game directory.
  345. *
  346. * @param {boolean} def Optional. If true, the default value will be returned.
  347. * @returns {string} The absolute path of the game directory.
  348. */
  349. exports.getGameDirectory = function(def = false){
  350. return !def ? config.settings.game.directory : DEFAULT_CONFIG.settings.game.directory
  351. }
  352. /**
  353. * Set the absolute path of the game directory.
  354. *
  355. * @param {string} directory The absolute path of the new game directory.
  356. */
  357. exports.setGameDirectory = function(directory){
  358. config.settings.game.directory = directory
  359. }
  360. /**
  361. * Retrieve the width of the game window.
  362. *
  363. * @param {boolean} def Optional. If true, the default value will be returned.
  364. * @returns {number} The width of the game window.
  365. */
  366. exports.getGameWidth = function(def = false){
  367. return !def ? config.settings.game.resWidth : DEFAULT_CONFIG.settings.game.resWidth
  368. }
  369. /**
  370. * Set the width of the game window.
  371. *
  372. * @param {number} resWidth The new width of the game window.
  373. */
  374. exports.setGameWidth = function(resWidth){
  375. config.settings.game.resWidth = resWidth
  376. }
  377. /**
  378. * Retrieve the height of the game window.
  379. *
  380. * @param {boolean} def Optional. If true, the default value will be returned.
  381. * @returns {number} The height of the game window.
  382. */
  383. exports.getGameHeight = function(def = false){
  384. return !def ? config.settings.game.resHeight : DEFAULT_CONFIG.settings.game.resHeight
  385. }
  386. /**
  387. * Set the height of the game window.
  388. *
  389. * @param {number} resHeight The new height of the game window.
  390. */
  391. exports.setGameHeight = function(resHeight){
  392. config.settings.game.resHeight = resHeight
  393. }
  394. /**
  395. * Check if the game should be launched in fullscreen mode.
  396. *
  397. * @param {boolean} def Optional. If true, the default value will be returned.
  398. * @returns {boolean} Whether or not the game is set to launch in fullscreen mode.
  399. */
  400. exports.isFullscreen = function(def = false){
  401. return !def ? config.settings.game.fullscreen : DEFAULT_CONFIG.settings.game.fullscreen
  402. }
  403. /**
  404. * Change the status of if the game should be launched in fullscreen mode.
  405. *
  406. * @param {boolean} fullscreen Whether or not the game should launch in fullscreen mode.
  407. */
  408. exports.setFullscreen = function(fullscreen){
  409. config.settings.game.fullscreen = fullscreen
  410. }
  411. /**
  412. * Check if the game should auto connect to servers.
  413. *
  414. * @param {boolean} def Optional. If true, the default value will be returned.
  415. * @returns {boolean} Whether or not the game should auto connect to servers.
  416. */
  417. exports.isAutoConnect = function(def = false){
  418. return !def ? config.settings.game.autoConnect : DEFAULT_CONFIG.settings.game.autoConnect
  419. }
  420. /**
  421. * Change the status of whether or not the game should auto connect to servers.
  422. *
  423. * @param {boolean} autoConnect Whether or not the game should auto connect to servers.
  424. */
  425. exports.setAutoConnect = function(autoConnect){
  426. config.settings.game.autoConnect = autoConnect
  427. }
  428. /**
  429. * Check if the game should launch as a detached process.
  430. *
  431. * @param {boolean} def Optional. If true, the default value will be returned.
  432. * @returns {boolean} Whether or not the game will launch as a detached process.
  433. */
  434. exports.isLaunchDetached = function(def = false){
  435. return !def ? config.settings.game.launchDetached : DEFAULT_CONFIG.settings.game.launchDetached
  436. }
  437. /**
  438. * Change the status of whether or not the game should launch as a detached process.
  439. *
  440. * @param {boolean} launchDetached Whether or not the game should launch as a detached process.
  441. */
  442. exports.setLaunchDetached = function(launchDetached){
  443. config.settings.game.launchDetached = launchDetached
  444. }