Preferences.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. class Preferences {
  23. private static Ctor = (() => {
  24. new Preferences();
  25. })();
  26. private fileSystem: Atomic.FileSystem;
  27. private static instance: Preferences;
  28. private _prefs: PreferencesFormat;
  29. constructor() {
  30. this.fileSystem = Atomic.getFileSystem();
  31. Preferences.instance = this;
  32. }
  33. registerRecentProject(path: string): void {
  34. var index = this._prefs.recentProjects.indexOf(path);
  35. if (index >= 0) {
  36. this._prefs.recentProjects.splice(index, 1);
  37. }
  38. this._prefs.recentProjects.unshift(path);
  39. this.updateRecentProjects(true);
  40. }
  41. updateRecentProjects(write: boolean = false): void {
  42. for (var i = 0; i < this._prefs.recentProjects.length; i++) {
  43. var path = this._prefs.recentProjects[i];
  44. if (!this.fileSystem.exists(path)) {
  45. this._prefs.recentProjects.splice(i, 1);
  46. write = true;
  47. }
  48. }
  49. if (write)
  50. this.write();
  51. }
  52. deleteRecentProjects(): void {
  53. this._prefs.recentProjects.length = 0;
  54. this.write();
  55. }
  56. getPreferencesFullPath(): string {
  57. var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
  58. filePath += "prefs.json";
  59. return filePath;
  60. }
  61. read(): void {
  62. var filePath = this.getPreferencesFullPath();
  63. var jsonFile;
  64. //check if file doesn't exist, create default json
  65. if (!this.fileSystem.fileExists(filePath)) {
  66. this.useDefaultConfig();
  67. this.write();
  68. return;
  69. }
  70. //Read file
  71. jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
  72. var prefs = null;
  73. try {
  74. if (jsonFile.isOpen())
  75. prefs = <PreferencesFormat>JSON.parse(jsonFile.readText());
  76. } catch (e) {
  77. prefs = null;
  78. }
  79. if (prefs && (!prefs.editorWindow || !prefs.playerWindow || !prefs.recentProjects)) {
  80. prefs = null;
  81. }
  82. if (prefs) {
  83. this._prefs = prefs;
  84. } else {
  85. console.log("Editor preference file invalid, regenerating default configuration");
  86. this.useDefaultConfig();
  87. this.write();
  88. }
  89. }
  90. write(): boolean {
  91. var filePath = this.getPreferencesFullPath();
  92. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  93. if (!jsonFile.isOpen()) return false;
  94. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  95. }
  96. saveEditorWindowData(windowData: WindowData) {
  97. this._prefs.editorWindow = windowData;
  98. this.write();
  99. }
  100. savePlayerWindowData(windowData: WindowData) {
  101. this._prefs.playerWindow = windowData;
  102. this.write();
  103. }
  104. useDefaultConfig(): void {
  105. this._prefs = new PreferencesFormat();
  106. }
  107. get editorWindow(): WindowData {
  108. return this._prefs.editorWindow;
  109. }
  110. get playerWindow(): WindowData {
  111. return this._prefs.playerWindow;
  112. }
  113. get recentProjects(): string[] {
  114. return this._prefs.recentProjects;
  115. }
  116. static getInstance(): Preferences {
  117. return Preferences.instance;
  118. }
  119. }
  120. interface WindowData {
  121. x: number;
  122. y: number;
  123. width: number;
  124. height: number;
  125. monitor: number;
  126. maximized: boolean;
  127. }
  128. class PreferencesFormat {
  129. constructor() {
  130. this.setDefault();
  131. }
  132. setDefault() {
  133. this.recentProjects = [];
  134. this.editorWindow = {
  135. x: 0,
  136. y: 0,
  137. width: 0,
  138. height: 0,
  139. monitor: 0,
  140. maximized: true
  141. };
  142. this.playerWindow = {
  143. x: 0,
  144. y: 0,
  145. width: 0,
  146. height: 0,
  147. monitor: 0,
  148. maximized: false
  149. };
  150. }
  151. recentProjects: string[];
  152. editorWindow: WindowData;
  153. playerWindow: WindowData;
  154. }
  155. export = Preferences;