Preferences.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. class Preferences {
  8. private static Ctor = (() => {
  9. new Preferences();
  10. })();
  11. private fileSystem: Atomic.FileSystem;
  12. private static instance: Preferences;
  13. private _prefs: PreferencesFormat;
  14. constructor() {
  15. this.fileSystem = Atomic.getFileSystem();
  16. Preferences.instance = this;
  17. }
  18. registerRecentProject(path: string): void {
  19. var index = this._prefs.recentProjects.indexOf(path);
  20. if (index >= 0) {
  21. this._prefs.recentProjects.splice(index, 1);
  22. }
  23. this._prefs.recentProjects.unshift(path);
  24. this.updateRecentProjects(true);
  25. }
  26. updateRecentProjects(write: boolean = false): void {
  27. for (var i in this._prefs.recentProjects) {
  28. var path = this._prefs.recentProjects[i];
  29. if (!this.fileSystem.exists(path)) {
  30. this._prefs.recentProjects.splice(i, 1);
  31. write = true;
  32. }
  33. }
  34. if (write)
  35. this.write();
  36. }
  37. deleteRecentProjects(): void {
  38. this._prefs.recentProjects.length = 0;
  39. this.write();
  40. }
  41. getPreferencesFullPath(): string {
  42. var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
  43. filePath += "prefs.json";
  44. return filePath;
  45. }
  46. read(): void {
  47. var filePath = this.getPreferencesFullPath();
  48. var jsonFile;
  49. //check if file doesn't exist, create default json
  50. if (!this.fileSystem.fileExists(filePath)) {
  51. this.useDefaultConfig();
  52. this.write();
  53. return;
  54. }
  55. //Read file
  56. jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
  57. var prefs = null;
  58. try {
  59. if (jsonFile.isOpen())
  60. prefs = <PreferencesFormat>JSON.parse(jsonFile.readText());
  61. } catch (e) {
  62. prefs = null;
  63. }
  64. if (prefs && (!prefs.editorWindow || !prefs.playerWindow || !prefs.recentProjects)) {
  65. prefs = null;
  66. }
  67. if (prefs) {
  68. this._prefs = prefs;
  69. } else {
  70. console.log("Editor preference file invalid, regenerating default configuration");
  71. this.useDefaultConfig();
  72. this.write();
  73. }
  74. }
  75. write(): boolean {
  76. var filePath = this.getPreferencesFullPath();
  77. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  78. if (!jsonFile.isOpen()) return false;
  79. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  80. }
  81. saveEditorWindowData(windowData: WindowData) {
  82. this._prefs.editorWindow = windowData;
  83. this.write();
  84. }
  85. savePlayerWindowData(windowData: WindowData) {
  86. this._prefs.playerWindow = windowData;
  87. this.write();
  88. }
  89. useDefaultConfig(): void {
  90. this._prefs = new PreferencesFormat();
  91. }
  92. get editorWindow(): WindowData {
  93. return this._prefs.editorWindow;
  94. }
  95. get playerWindow(): WindowData {
  96. return this._prefs.playerWindow;
  97. }
  98. get recentProjects(): string[] {
  99. return this._prefs.recentProjects;
  100. }
  101. static getInstance(): Preferences {
  102. return Preferences.instance;
  103. }
  104. }
  105. interface WindowData {
  106. x: number;
  107. y: number;
  108. width: number;
  109. height: number;
  110. monitor: number;
  111. maximized: boolean;
  112. }
  113. class PreferencesFormat {
  114. constructor() {
  115. this.setDefault();
  116. }
  117. setDefault() {
  118. this.recentProjects = [];
  119. this.editorWindow = {
  120. x: 0,
  121. y: 0,
  122. width: 0,
  123. height: 0,
  124. monitor: 0,
  125. maximized: true
  126. };
  127. this.playerWindow = {
  128. x: 0,
  129. y: 0,
  130. width: 0,
  131. height: 0,
  132. monitor: 0,
  133. maximized: false
  134. };
  135. }
  136. recentProjects: string[];
  137. editorWindow: WindowData;
  138. playerWindow: WindowData;
  139. }
  140. export = Preferences;