configmanager.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. class ConfigManager {
  7. constructor(path){
  8. this.path = path
  9. this.config = null
  10. this.load()
  11. }
  12. /* Private functions to resolve default settings based on system specs. */
  13. static _resolveMaxRAM(){
  14. const mem = os.totalmem()
  15. return mem >= 8000000000 ? '4G' : (mem >= 6000000000 ? '3G' : '2G')
  16. }
  17. /**
  18. * Generates a default configuration object and saves it.
  19. *
  20. * @param {Boolean} save - optional. If true, the default config will be saved after being generated.
  21. */
  22. _generateDefault(save = true){
  23. this.config = {
  24. settings: {
  25. java: {
  26. minRAM: '2G',
  27. maxRAM: ConfigManager._resolveMaxRAM(),
  28. executable: 'C:\\Program Files\\Java\\jdk1.8.0_152\\bin\\javaw.exe', //TODO Resolve
  29. jvmOptions: [
  30. '-XX:+UseConcMarkSweepGC',
  31. '-XX:+CMSIncrementalMode',
  32. '-XX:-UseAdaptiveSizePolicy',
  33. '-Xmn128M'
  34. ],
  35. },
  36. game: {
  37. resWidth: 1280,
  38. resHeight: 720,
  39. fullscreen: false,
  40. autoConnect: true
  41. },
  42. launcher: {
  43. }
  44. },
  45. clientToken: uuidV4(),
  46. selectedServer: null,
  47. selectedAccount: null,
  48. authenticationDatabase: [],
  49. discord: {
  50. clientID: 385581240906022916
  51. }
  52. }
  53. if(save){
  54. this.save()
  55. }
  56. }
  57. /**
  58. * Load the launcher configuration into memory. If the specified file does
  59. * not exist, a default configuration will be generated and saved.
  60. */
  61. load(){
  62. if(!fs.existsSync(this.path)){
  63. mkpath.sync(path.join(this.path, '..'))
  64. this._generateDefault()
  65. } else {
  66. this.config = JSON.parse(fs.readFileSync(this.path, 'UTF-8'))
  67. }
  68. }
  69. /**
  70. * Save the launcher configuration to the specified file.
  71. */
  72. save(){
  73. fs.writeFileSync(this.path, JSON.stringify(this.config, null, 4), 'UTF-8')
  74. }
  75. /**
  76. * Retrieve the launcher's Client Token.
  77. */
  78. getClientToken(){
  79. return this.config.clientToken
  80. }
  81. /**
  82. * Retrieve the selected server configuration value.
  83. */
  84. getSelectedServer(){
  85. return this.config.selectedServer
  86. }
  87. /**
  88. * Set the selected server configuration value.
  89. *
  90. * @param {String} serverID - the id of the new selected server.
  91. */
  92. setSelectedServer(serverID){
  93. this.config.selectedServer = serverID
  94. this.save()
  95. }
  96. /**
  97. * Retrieve the launcher's Discord Client ID.
  98. */
  99. getDiscordClientID(){
  100. return this.config.discord.clientID
  101. }
  102. /**
  103. * Retrieve the minimum amount of memory for JVM initialization.
  104. */
  105. getMinRAM(){
  106. return this.config.settings.java.minRAM
  107. }
  108. /**
  109. * Retrieve the maximum amount of memory for JVM initialization.
  110. */
  111. getMaxRAM(){
  112. return this.config.settings.java.maxRAM
  113. }
  114. /**
  115. * Retrieve the path of the java executable.
  116. */
  117. getJavaExecutable(){
  118. return this.config.settings.java.executable
  119. }
  120. /**
  121. * Retrieve the additional arguments for JVM initialization. Required arguments,
  122. * such as memory allocation, will be dynamically resolved.
  123. */
  124. getJVMOptions(){
  125. return this.config.settings.java.jvmOptions
  126. }
  127. /**
  128. * Retrieve the width of the game window.
  129. */
  130. getGameWidth(){
  131. return this.config.settings.game.resWidth
  132. }
  133. /**
  134. * Retrieve the height of the game window.
  135. */
  136. getGameHeight(){
  137. return this.config.settings.game.resHeight
  138. }
  139. /**
  140. * Check if the game should be launched in fullscreen mode.
  141. */
  142. isFullscreen(){
  143. return this.config.settings.game.fullscreen
  144. }
  145. /**
  146. * Check if auto connect is enabled.
  147. */
  148. isAutoConnect(){
  149. return this.config.settings.game.autoConnect
  150. }
  151. }
  152. module.exports = ConfigManager