Preferences.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. private _cachedProjectPreferences: Object = null;
  30. constructor() {
  31. this.fileSystem = Atomic.getFileSystem();
  32. Preferences.instance = this;
  33. }
  34. registerRecentProject(path: string): void {
  35. var index = this._prefs.recentProjects.indexOf(path);
  36. if (index >= 0) {
  37. this._prefs.recentProjects.splice(index, 1);
  38. }
  39. this._prefs.recentProjects.unshift(path);
  40. this.updateRecentProjects(true);
  41. }
  42. updateRecentProjects(write: boolean = false): void {
  43. for (var i = 0; i < this._prefs.recentProjects.length; i++) {
  44. var path = this._prefs.recentProjects[i];
  45. if (!this.fileSystem.exists(path)) {
  46. this._prefs.recentProjects.splice(i, 1);
  47. write = true;
  48. }
  49. }
  50. if (write)
  51. this.write();
  52. }
  53. deleteRecentProjects(): void {
  54. this._prefs.recentProjects.length = 0;
  55. this.write();
  56. }
  57. getPreferencesFullPath(): string {
  58. var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
  59. filePath += "prefs.json";
  60. return filePath;
  61. }
  62. read(): void {
  63. var filePath = this.getPreferencesFullPath();
  64. var jsonFile;
  65. //check if file doesn't exist, create default json
  66. if (!this.fileSystem.fileExists(filePath)) {
  67. this.useDefaultConfig();
  68. this.write();
  69. return;
  70. }
  71. //Read file
  72. jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
  73. var prefs = null;
  74. try {
  75. if (jsonFile.isOpen())
  76. prefs = <PreferencesFormat>JSON.parse(jsonFile.readText());
  77. } catch (e) {
  78. prefs = null;
  79. }
  80. if (prefs) {
  81. const defaultPrefs = new PreferencesFormat();
  82. const shouldWrite = defaultPrefs.applyMissingDefaults(prefs);
  83. this._prefs = prefs;
  84. if (shouldWrite) {
  85. this.write();
  86. }
  87. } else {
  88. console.log("Editor preference file missing or invalid, regenerating default configuration");
  89. this.useDefaultConfig();
  90. this.write();
  91. }
  92. }
  93. write(): boolean {
  94. var filePath = this.getPreferencesFullPath();
  95. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  96. if (!jsonFile.isOpen()) return false;
  97. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  98. }
  99. saveEditorWindowData(windowData: WindowData) {
  100. this._prefs.editorWindow = windowData;
  101. this.write();
  102. }
  103. savePlayerWindowData(windowData: WindowData) {
  104. this._prefs.playerWindow = windowData;
  105. this.write();
  106. }
  107. useDefaultConfig(): void {
  108. this._prefs = new PreferencesFormat();
  109. }
  110. get cachedProjectPreferences():any {
  111. return this._cachedProjectPreferences;
  112. }
  113. get cachedApplicationPreferences():PreferencesFormat {
  114. return this._prefs;
  115. }
  116. get editorWindow(): WindowData {
  117. return this._prefs.editorWindow;
  118. }
  119. get playerWindow(): WindowData {
  120. return this._prefs.playerWindow;
  121. }
  122. get recentProjects(): string[] {
  123. return this._prefs.recentProjects;
  124. }
  125. get uiData(): UserInterfaceData {
  126. return this._prefs.uiData;
  127. }
  128. static getInstance(): Preferences {
  129. return Preferences.instance;
  130. }
  131. /**
  132. * Return a preference value or the provided default from the user settings file located in the project
  133. * @param {string} settingsGroup name of the group these settings should fall under
  134. * @param {string} preferenceName name of the preference to retrieve
  135. * @param {number | boolean | string} defaultValue value to return if pref doesn't exist
  136. * @return {number|boolean|string}
  137. */
  138. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
  139. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
  140. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
  141. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: any): any {
  142. // Cache the settings so we don't keep going out to the file
  143. if (this._cachedProjectPreferences == null) {
  144. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  145. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  146. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  147. try {
  148. let prefs = JSON.parse(prefsFile.readText());
  149. this._cachedProjectPreferences = prefs;
  150. } finally {
  151. prefsFile.close();
  152. }
  153. }
  154. }
  155. if (this._cachedProjectPreferences && this._cachedProjectPreferences[settingsGroup]) {
  156. return this._cachedProjectPreferences[settingsGroup][preferenceName] || defaultValue;
  157. }
  158. // if all else fails
  159. return defaultValue;
  160. }
  161. /**
  162. * Sets a user preference value in the user settings file located in the project
  163. * @param {string} settingsGroup name of the group the preference lives under
  164. * @param {string} preferenceName name of the preference to set
  165. * @param {number | boolean | string} value value to set
  166. */
  167. setUserPreference(settingsGroup: string, preferenceName: string, value: number | boolean | string) {
  168. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  169. let prefs = {};
  170. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  171. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  172. try {
  173. prefs = JSON.parse(prefsFile.readText());
  174. } finally {
  175. prefsFile.close();
  176. }
  177. }
  178. prefs[settingsGroup] = prefs[settingsGroup] || {};
  179. prefs[settingsGroup][preferenceName] = value;
  180. let saveFile = new Atomic.File(prefsFileLoc, Atomic.FILE_WRITE);
  181. try {
  182. saveFile.writeString(JSON.stringify(prefs, null, " "));
  183. } finally {
  184. saveFile.flush();
  185. saveFile.close();
  186. }
  187. // Cache the update
  188. this._cachedProjectPreferences = prefs;
  189. }
  190. /**
  191. * Sets a group of user preference values in the user settings file located in the project. Elements in the
  192. * group will merge in with existing group preferences. Use this method if setting a bunch of settings
  193. * at once.
  194. * @param {string} settingsGroup name of the group the preference lives under
  195. * @param {string} groupPreferenceValues an object literal containing all of the preferences for the group.
  196. */
  197. setUserPreferenceGroup(settingsGroup: string, groupPreferenceValues: Object) {
  198. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  199. let prefs = {};
  200. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  201. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  202. try {
  203. prefs = JSON.parse(prefsFile.readText());
  204. } finally {
  205. prefsFile.close();
  206. }
  207. }
  208. prefs[settingsGroup] = prefs[settingsGroup] || {};
  209. for (let preferenceName in groupPreferenceValues) {
  210. prefs[settingsGroup][preferenceName] = groupPreferenceValues[preferenceName];
  211. }
  212. let saveFile = new Atomic.File(prefsFileLoc, Atomic.FILE_WRITE);
  213. try {
  214. saveFile.writeString(JSON.stringify(prefs, null, " "));
  215. } finally {
  216. saveFile.flush();
  217. saveFile.close();
  218. }
  219. // Cache the update
  220. this._cachedProjectPreferences = prefs;
  221. }
  222. }
  223. interface WindowData {
  224. x: number;
  225. y: number;
  226. width: number;
  227. height: number;
  228. monitor: number;
  229. maximized: boolean;
  230. }
  231. interface AceEditorSettings {
  232. theme: string;
  233. keyboardHandler: string;
  234. fontSize: number;
  235. showInvisibles: boolean;
  236. useSoftTabs: boolean;
  237. tabSize: number;
  238. }
  239. interface UserInterfaceData {
  240. skinPath : string;
  241. defaultSkinPath : string;
  242. fontFile : string;
  243. fontName : string;
  244. fontSize : number;
  245. }
  246. class PreferencesFormat {
  247. constructor() {
  248. this.setDefault();
  249. }
  250. setDefault() {
  251. this.recentProjects = [];
  252. this.editorWindow = {
  253. x: 0,
  254. y: 0,
  255. width: 0,
  256. height: 0,
  257. monitor: 0,
  258. maximized: true
  259. };
  260. this.playerWindow = {
  261. x: 0,
  262. y: 0,
  263. width: 0,
  264. height: 0,
  265. monitor: 0,
  266. maximized: false
  267. };
  268. this.codeEditorSettings = {
  269. theme: "ace/theme/monokai",
  270. keyboardHandler: "ace/keyboard/textinput",
  271. fontSize: 12,
  272. showInvisibles: false,
  273. useSoftTabs: true,
  274. tabSize: 4
  275. };
  276. this.uiData = {
  277. skinPath : "AtomicEditor/editor/skin/",
  278. defaultSkinPath : "AtomicEditor/resources/default_skin/",
  279. fontFile : "AtomicEditor/resources/vera.ttf",
  280. fontName : "Vera",
  281. fontSize : 12
  282. };
  283. }
  284. /**
  285. * Run through a provided prefs block and verify that all the sections are present. If any
  286. * are missing, add the defaults in
  287. * @param {PreferencesFormat} prefs
  288. * @return boolean returns true if any missing defaults were updated
  289. */
  290. applyMissingDefaults(prefs: PreferencesFormat) {
  291. let updatedMissingDefaults = false;
  292. if (!prefs.recentProjects) {
  293. prefs.recentProjects = this.recentProjects;
  294. updatedMissingDefaults = true;
  295. }
  296. if (!prefs.editorWindow) {
  297. prefs.editorWindow = this.editorWindow;
  298. updatedMissingDefaults = true;
  299. }
  300. if (!prefs.playerWindow) {
  301. prefs.playerWindow = this.playerWindow;
  302. updatedMissingDefaults = true;
  303. }
  304. if (!prefs.codeEditorSettings) {
  305. prefs.codeEditorSettings = this.codeEditorSettings;
  306. updatedMissingDefaults = true;
  307. }
  308. if (!prefs.uiData) {
  309. prefs.uiData = this.uiData;
  310. updatedMissingDefaults = true;
  311. }
  312. return updatedMissingDefaults;
  313. }
  314. recentProjects: string[];
  315. editorWindow: WindowData;
  316. playerWindow: WindowData;
  317. codeEditorSettings: AceEditorSettings;
  318. uiData : UserInterfaceData;
  319. }
  320. export = Preferences;