configmanager.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. //TODO Write Authentication Database/Selected Account accessors here
  108. // User Configurable Settings
  109. // Java Settings
  110. /**
  111. * Retrieve the minimum amount of memory for JVM initialization. This value
  112. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  113. * 1024 MegaBytes, etc.
  114. *
  115. * @param {Boolean} def - optional. If true, the default value will be returned.
  116. * @returns {String} - the minimum amount of memory for JVM initialization.
  117. */
  118. exports.getMinRAM = function(def = false){
  119. return !def ? config.settings.java.minRAM : DEFAULT_CONFIG.settings.java.minRAM
  120. }
  121. /**
  122. * Set the minimum amount of memory for JVM initialization. This value should
  123. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  124. * 1024 MegaBytes, etc.
  125. *
  126. * @param {String} minRAM - the new minimum amount of memory for JVM initialization.
  127. */
  128. exports.setMinRAM = function(minRAM){
  129. config.settings.java.minRAM = minRAM
  130. }
  131. /**
  132. * Retrieve the maximum amount of memory for JVM initialization. This value
  133. * contains the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  134. * 1024 MegaBytes, etc.
  135. *
  136. * @param {Boolean} def - optional. If true, the default value will be returned.
  137. * @returns {String} - the maximum amount of memory for JVM initialization.
  138. */
  139. exports.getMaxRAM = function(def = false){
  140. return !def ? config.settings.java.maxRAM : resolveMaxRAM()
  141. }
  142. /**
  143. * Set the maximum amount of memory for JVM initialization. This value should
  144. * contain the units of memory. For example, '5G' = 5 GigaBytes, '1024M' =
  145. * 1024 MegaBytes, etc.
  146. *
  147. * @param {String} maxRAM - the new maximum amount of memory for JVM initialization.
  148. */
  149. exports.setMaxRAM = function(maxRAM){
  150. config.settings.java.maxRAM = maxRAM
  151. }
  152. /**
  153. * Retrieve the path of the Java Executable.
  154. *
  155. * This is a resolved configuration value and defaults to null until externally assigned.
  156. *
  157. * @returns {String} - the path of the Java Executable.
  158. */
  159. exports.getJavaExecutable = function(){
  160. return config.settings.java.executable
  161. }
  162. /**
  163. * Set the path of the Java Executable.
  164. *
  165. * @param {String} executable - the new path of the Java Executable.
  166. */
  167. exports.setJavaExecutable = function(executable){
  168. config.settings.java.executable = executable
  169. }
  170. /**
  171. * Retrieve the additional arguments for JVM initialization. Required arguments,
  172. * such as memory allocation, will be dynamically resolved and will not be included
  173. * in this value.
  174. *
  175. * @param {Boolean} def - optional. If true, the default value will be returned.
  176. * @returns {Array.<String>} - an array of the additional arguments for JVM initialization.
  177. */
  178. exports.getJVMOptions = function(def = false){
  179. return !def ? config.settings.java.jvmOptions : DEFAULT_CONFIG.settings.java.jvmOptions
  180. }
  181. /**
  182. * Set the additional arguments for JVM initialization. Required arguments,
  183. * such as memory allocation, will be dynamically resolved and should not be
  184. * included in this value.
  185. *
  186. * @param {Array.<String>} jvmOptions - an array of the new additional arguments for JVM
  187. * initialization.
  188. */
  189. exports.setJVMOptions = function(jvmOptions){
  190. config.settings.java.jvmOptions = jvmOptions
  191. }
  192. // Game Settings
  193. /**
  194. * Retrieve the absolute path of the game directory.
  195. *
  196. * @param {Boolean} def - optional. If true, the default value will be returned.
  197. * @returns {String} - the absolute path of the game directory.
  198. */
  199. exports.getGameDirectory = function(def = false){
  200. return !def ? config.settings.game.directory : DEFAULT_CONFIG.settings.game.directory
  201. }
  202. /**
  203. * Set the absolute path of the game directory.
  204. *
  205. * @param {String} directory - the absolute path of the new game directory.
  206. */
  207. exports.setGameDirectory = function(directory){
  208. config.settings.game.directory = directory
  209. }
  210. /**
  211. * Retrieve the width of the game window.
  212. *
  213. * @param {Boolean} def - optional. If true, the default value will be returned.
  214. * @returns {Number} - the width of the game window.
  215. */
  216. exports.getGameWidth = function(def = false){
  217. return !def ? config.settings.game.resWidth : DEFAULT_CONFIG.settings.game.resWidth
  218. }
  219. /**
  220. * Set the width of the game window.
  221. *
  222. * @param {Number} resWidth - the new width of the game window.
  223. */
  224. exports.setGameWidth = function(resWidth){
  225. config.settings.game.resWidth = resWidth
  226. }
  227. /**
  228. * Retrieve the height of the game window.
  229. *
  230. * @param {Boolean} def - optional. If true, the default value will be returned.
  231. * @returns {Number} - the height of the game window.
  232. */
  233. exports.getGameHeight = function(def = false){
  234. return !def ? config.settings.game.resHeight : DEFAULT_CONFIG.settings.game.resHeight
  235. }
  236. /**
  237. * Set the height of the game window.
  238. *
  239. * @param {Number} resHeight - the new height of the game window.
  240. */
  241. exports.setGameHeight = function(resHeight){
  242. config.settings.game.resHeight = resHeight
  243. }
  244. /**
  245. * Check if the game should be launched in fullscreen mode.
  246. *
  247. * @param {Boolean} def - optional. If true, the default value will be returned.
  248. * @returns {Boolean} - whether or not the game is set to launch in fullscreen mode.
  249. */
  250. exports.isFullscreen = function(def = false){
  251. return !def ? config.settings.game.fullscreen : DEFAULT_CONFIG.settings.game.fullscreen
  252. }
  253. /**
  254. * Change the status of if the game should be launched in fullscreen mode.
  255. *
  256. * @param {Boolean} fullscreen - whether or not the game should launch in fullscreen mode.
  257. */
  258. exports.setFullscreen = function(fullscreen){
  259. config.settings.game.fullscreen = fullscreen
  260. }
  261. /**
  262. * Check if the game should auto connect to servers.
  263. *
  264. * @param {Boolean} def - optional. If true, the default value will be returned.
  265. * @returns {Boolean} - whether or not the game should auto connect to servers.
  266. */
  267. exports.isAutoConnect = function(def = false){
  268. return !def ? config.settings.game.autoConnect : DEFAULT_CONFIG.settings.game.autoConnect
  269. }
  270. /**
  271. * Change the status of whether or not the game should auto connect to servers.
  272. *
  273. * @param {Boolean} autoConnect - whether or not the game should auto connect to servers.
  274. */
  275. exports.setAutoConnect = function(autoConnect){
  276. config.settings.game.autoConnect = autoConnect
  277. }