configmanager.js 11 KB

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