configmanager.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. },
  39. launcher: {}
  40. },
  41. clientToken: uuidV4().replace(/-/g, ''),
  42. selectedServer: null, // Resolved
  43. selectedAccount: null,
  44. authenticationDatabase: {}
  45. }
  46. let config = null;
  47. // Persistance Utility Functions
  48. /**
  49. * Save the current configuration to a file.
  50. */
  51. exports.save = function(){
  52. const filePath = path.join(dataPath, 'config.json')
  53. fs.writeFileSync(filePath, JSON.stringify(config, null, 4), 'UTF-8')
  54. }
  55. /**
  56. * Load the configuration into memory. If a configuration file exists,
  57. * that will be read and saved. Otherwise, a default configuration will
  58. * be generated. Note that "resolved" values default to null and will
  59. * need to be externally assigned.
  60. */
  61. exports.load = function(){
  62. // Determine the effective configuration.
  63. //const EFFECTIVE_CONFIG = config == null ? DEFAULT_CONFIG : config
  64. const filePath = path.join(dataPath, 'config.json')
  65. if(!fs.existsSync(filePath)){
  66. // Create all parent directories.
  67. mkpath.sync(path.join(filePath, '..'))
  68. config = DEFAULT_CONFIG
  69. exports.save()
  70. } else {
  71. config = JSON.parse(fs.readFileSync(filePath, 'UTF-8'))
  72. }
  73. }
  74. /**
  75. * Retrieve the absolute path of the launcher directory.
  76. *
  77. * @returns {string} The absolute path of the launcher directory.
  78. */
  79. exports.getLauncherDirectory = function(){
  80. return dataPath
  81. }
  82. /**
  83. * Check to see if this is the first time the user has launched the
  84. * application. This is determined by the existance of the data path.
  85. *
  86. * @returns {boolean} True if this is the first launch, otherwise false.
  87. */
  88. exports.isFirstLaunch = function(){
  89. return firstLaunch
  90. }
  91. /**
  92. * Returns the name of the folder in the OS temp directory which we
  93. * will use to extract and store native dependencies for game launch.
  94. *
  95. * @returns {string} The name of the folder.
  96. */
  97. exports.getTempNativeFolder = function(){
  98. return 'WCNatives'
  99. }
  100. // System Settings (Unconfigurable on UI)
  101. /**
  102. * Retrieve the launcher's Client Token.
  103. * There is no default client token.
  104. *
  105. * @returns {string} The launcher's Client Token.
  106. */
  107. exports.getClientToken = function(){
  108. return config.clientToken
  109. }
  110. /**
  111. * Set the launcher's Client Token.
  112. *
  113. * @param {string} clientToken The launcher's new Client Token.
  114. */
  115. exports.setClientToken = function(clientToken){
  116. config.clientToken = clientToken
  117. }
  118. /**
  119. * Retrieve the ID of the selected serverpack.
  120. *
  121. * @param {boolean} def Optional. If true, the default value will be returned.
  122. * @returns {string} The ID of the selected serverpack.
  123. */
  124. exports.getSelectedServer = function(def = false){
  125. return !def ? config.selectedServer : DEFAULT_CONFIG.clientToken
  126. }
  127. /**
  128. * Set the ID of the selected serverpack.
  129. *
  130. * @param {string} serverID The ID of the new selected serverpack.
  131. */
  132. exports.setSelectedServer = function(serverID){
  133. config.selectedServer = serverID
  134. }
  135. /**
  136. * Get an array of each account currently authenticated by the launcher.
  137. *
  138. * @returns {Array.<Object>} An array of each stored authenticated account.
  139. */
  140. exports.getAuthAccounts = function(){
  141. return config.authenticationDatabase
  142. }
  143. /**
  144. * Returns the authenticated account with the given uuid. Value may
  145. * be null.
  146. *
  147. * @param {string} uuid The uuid of the authenticated account.
  148. * @returns {Object} The authenticated account with the given uuid.
  149. */
  150. exports.getAuthAccount = function(uuid){
  151. return config.authenticationDatabase[uuid]
  152. }
  153. /**
  154. * Update the access token of an authenticated account.
  155. *
  156. * @param {string} uuid The uuid of the authenticated account.
  157. * @param {string} accessToken The new Access Token.
  158. *
  159. * @returns {Object} The authenticated account object created by this action.
  160. */
  161. exports.updateAuthAccount = function(uuid, accessToken){
  162. config.authenticationDatabase[uuid].accessToken = accessToken
  163. return config.authenticationDatabase[uuid]
  164. }
  165. /**
  166. * Adds an authenticated account to the database to be stored.
  167. *
  168. * @param {string} uuid The uuid of the authenticated account.
  169. * @param {string} accessToken The accessToken of the authenticated account.
  170. * @param {string} username The username (usually email) of the authenticated account.
  171. * @param {string} displayName The in game name of the authenticated account.
  172. *
  173. * @returns {Object} The authenticated account object created by this action.
  174. */
  175. exports.addAuthAccount = function(uuid, accessToken, username, displayName){
  176. config.selectedAccount = uuid
  177. config.authenticationDatabase[uuid] = {
  178. accessToken,
  179. username,
  180. uuid,
  181. displayName
  182. }
  183. return config.authenticationDatabase[uuid]
  184. }
  185. /**
  186. * Remove an authenticated account from the database. If the account
  187. * was also the selected account, a new one will be selected. If there
  188. * are no accounts, the selected account will be null.
  189. *
  190. * @param {string} uuid The uuid of the authenticated account.
  191. *
  192. * @returns {boolean} True if the account was removed, false if it never existed.
  193. */
  194. exports.removeAuthAccount = function(uuid){
  195. if(config.authenticationDatabase[uuid] != null){
  196. delete config.authenticationDatabase[uuid]
  197. if(config.selectedAccount === uuid){
  198. const keys = Object.keys(config.authenticationDatabase)
  199. if(keys.length > 0){
  200. config.selectedAccount = keys[0]
  201. } else {
  202. config.selectedAccount = null
  203. }
  204. }
  205. return true
  206. }
  207. return false
  208. }
  209. /**
  210. * Get the currently selected authenticated account.
  211. *
  212. * @returns {Object} The selected authenticated account.
  213. */
  214. exports.getSelectedAccount = function(){
  215. return config.authenticationDatabase[config.selectedAccount]
  216. }
  217. /**
  218. * Set the selected authenticated account.
  219. *
  220. * @param {string} uuid The UUID of the account which is to be set
  221. * as the selected account.
  222. *
  223. * @returns {Object} The selected authenticated account.
  224. */
  225. exports.setSelectedAccount = function(uuid){
  226. const authAcc = config.authenticationDatabase[uuid]
  227. if(authAcc != null) {
  228. config.selectedAccount = uuid
  229. }
  230. return authAcc
  231. }
  232. // User Configurable Settings
  233. // Java Settings
  234. /**
  235. * Retrieve the minimum amount of memory for JVM initialization. This value
  236. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  237. * 1024 MegaBytes, etc.
  238. *
  239. * @param {boolean} def Optional. If true, the default value will be returned.
  240. * @returns {string} The minimum amount of memory for JVM initialization.
  241. */
  242. exports.getMinRAM = function(def = false){
  243. return !def ? config.settings.java.minRAM : DEFAULT_CONFIG.settings.java.minRAM
  244. }
  245. /**
  246. * Set the minimum amount of memory for JVM initialization. This value should
  247. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  248. * 1024 MegaBytes, etc.
  249. *
  250. * @param {string} minRAM The new minimum amount of memory for JVM initialization.
  251. */
  252. exports.setMinRAM = function(minRAM){
  253. config.settings.java.minRAM = minRAM
  254. }
  255. /**
  256. * Retrieve the maximum amount of memory for JVM initialization. This value
  257. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  258. * 1024 MegaBytes, etc.
  259. *
  260. * @param {boolean} def Optional. If true, the default value will be returned.
  261. * @returns {string} The maximum amount of memory for JVM initialization.
  262. */
  263. exports.getMaxRAM = function(def = false){
  264. return !def ? config.settings.java.maxRAM : resolveMaxRAM()
  265. }
  266. /**
  267. * Set the maximum amount of memory for JVM initialization. This value should
  268. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  269. * 1024 MegaBytes, etc.
  270. *
  271. * @param {string} maxRAM The new maximum amount of memory for JVM initialization.
  272. */
  273. exports.setMaxRAM = function(maxRAM){
  274. config.settings.java.maxRAM = maxRAM
  275. }
  276. /**
  277. * Retrieve the path of the Java Executable.
  278. *
  279. * This is a resolved configuration value and defaults to null until externally assigned.
  280. *
  281. * @returns {string} The path of the Java Executable.
  282. */
  283. exports.getJavaExecutable = function(){
  284. return config.settings.java.executable
  285. }
  286. /**
  287. * Set the path of the Java Executable.
  288. *
  289. * @param {string} executable The new path of the Java Executable.
  290. */
  291. exports.setJavaExecutable = function(executable){
  292. config.settings.java.executable = executable
  293. }
  294. /**
  295. * Retrieve the additional arguments for JVM initialization. Required arguments,
  296. * such as memory allocation, will be dynamically resolved and will not be included
  297. * in this value.
  298. *
  299. * @param {boolean} def Optional. If true, the default value will be returned.
  300. * @returns {Array.<string>} An array of the additional arguments for JVM initialization.
  301. */
  302. exports.getJVMOptions = function(def = false){
  303. return !def ? config.settings.java.jvmOptions : DEFAULT_CONFIG.settings.java.jvmOptions
  304. }
  305. /**
  306. * Set the additional arguments for JVM initialization. Required arguments,
  307. * such as memory allocation, will be dynamically resolved and should not be
  308. * included in this value.
  309. *
  310. * @param {Array.<string>} jvmOptions An array of the new additional arguments for JVM
  311. * initialization.
  312. */
  313. exports.setJVMOptions = function(jvmOptions){
  314. config.settings.java.jvmOptions = jvmOptions
  315. }
  316. // Game Settings
  317. /**
  318. * Retrieve the absolute path of the game directory.
  319. *
  320. * @param {boolean} def Optional. If true, the default value will be returned.
  321. * @returns {string} The absolute path of the game directory.
  322. */
  323. exports.getGameDirectory = function(def = false){
  324. return !def ? config.settings.game.directory : DEFAULT_CONFIG.settings.game.directory
  325. }
  326. /**
  327. * Set the absolute path of the game directory.
  328. *
  329. * @param {string} directory The absolute path of the new game directory.
  330. */
  331. exports.setGameDirectory = function(directory){
  332. config.settings.game.directory = directory
  333. }
  334. /**
  335. * Retrieve the width of the game window.
  336. *
  337. * @param {boolean} def Optional. If true, the default value will be returned.
  338. * @returns {number} The width of the game window.
  339. */
  340. exports.getGameWidth = function(def = false){
  341. return !def ? config.settings.game.resWidth : DEFAULT_CONFIG.settings.game.resWidth
  342. }
  343. /**
  344. * Set the width of the game window.
  345. *
  346. * @param {number} resWidth The new width of the game window.
  347. */
  348. exports.setGameWidth = function(resWidth){
  349. config.settings.game.resWidth = resWidth
  350. }
  351. /**
  352. * Retrieve the height of the game window.
  353. *
  354. * @param {boolean} def Optional. If true, the default value will be returned.
  355. * @returns {number} The height of the game window.
  356. */
  357. exports.getGameHeight = function(def = false){
  358. return !def ? config.settings.game.resHeight : DEFAULT_CONFIG.settings.game.resHeight
  359. }
  360. /**
  361. * Set the height of the game window.
  362. *
  363. * @param {number} resHeight The new height of the game window.
  364. */
  365. exports.setGameHeight = function(resHeight){
  366. config.settings.game.resHeight = resHeight
  367. }
  368. /**
  369. * Check if the game should be launched in fullscreen mode.
  370. *
  371. * @param {boolean} def Optional. If true, the default value will be returned.
  372. * @returns {boolean} Whether or not the game is set to launch in fullscreen mode.
  373. */
  374. exports.isFullscreen = function(def = false){
  375. return !def ? config.settings.game.fullscreen : DEFAULT_CONFIG.settings.game.fullscreen
  376. }
  377. /**
  378. * Change the status of if the game should be launched in fullscreen mode.
  379. *
  380. * @param {boolean} fullscreen Whether or not the game should launch in fullscreen mode.
  381. */
  382. exports.setFullscreen = function(fullscreen){
  383. config.settings.game.fullscreen = fullscreen
  384. }
  385. /**
  386. * Check if the game should auto connect to servers.
  387. *
  388. * @param {boolean} def Optional. If true, the default value will be returned.
  389. * @returns {boolean} Whether or not the game should auto connect to servers.
  390. */
  391. exports.isAutoConnect = function(def = false){
  392. return !def ? config.settings.game.autoConnect : DEFAULT_CONFIG.settings.game.autoConnect
  393. }
  394. /**
  395. * Change the status of whether or not the game should auto connect to servers.
  396. *
  397. * @param {boolean} autoConnect Whether or not the game should auto connect to servers.
  398. */
  399. exports.setAutoConnect = function(autoConnect){
  400. config.settings.game.autoConnect = autoConnect
  401. }