configmanager.js 15 KB

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