Preferences.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. get editorBuildData(): EditorBuildData {
  129. return this._prefs.editorBuildData;
  130. }
  131. static getInstance(): Preferences {
  132. return Preferences.instance;
  133. }
  134. /**
  135. * Return a preference value or the provided default from the user settings file located in the project
  136. * @param {string} settingsGroup name of the group these settings should fall under
  137. * @param {string} preferenceName name of the preference to retrieve
  138. * @param {number | boolean | string} defaultValue value to return if pref doesn't exist
  139. * @return {number|boolean|string}
  140. */
  141. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
  142. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
  143. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
  144. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: any): any {
  145. // Cache the settings so we don't keep going out to the file
  146. if (this._cachedProjectPreferences == null) {
  147. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  148. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  149. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  150. try {
  151. let prefs = JSON.parse(prefsFile.readText());
  152. this._cachedProjectPreferences = prefs;
  153. } finally {
  154. prefsFile.close();
  155. }
  156. }
  157. }
  158. if (this._cachedProjectPreferences && this._cachedProjectPreferences[settingsGroup]) {
  159. return this._cachedProjectPreferences[settingsGroup][preferenceName] || defaultValue;
  160. }
  161. // if all else fails
  162. return defaultValue;
  163. }
  164. /**
  165. * Sets a user preference value in the user settings file located in the project
  166. * @param {string} settingsGroup name of the group the preference lives under
  167. * @param {string} preferenceName name of the preference to set
  168. * @param {number | boolean | string} value value to set
  169. */
  170. setUserPreference(settingsGroup: string, preferenceName: string, value: number | boolean | string) {
  171. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  172. let prefs = {};
  173. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  174. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  175. try {
  176. prefs = JSON.parse(prefsFile.readText());
  177. } finally {
  178. prefsFile.close();
  179. }
  180. }
  181. prefs[settingsGroup] = prefs[settingsGroup] || {};
  182. prefs[settingsGroup][preferenceName] = value;
  183. let saveFile = new Atomic.File(prefsFileLoc, Atomic.FILE_WRITE);
  184. try {
  185. saveFile.writeString(JSON.stringify(prefs, null, " "));
  186. } finally {
  187. saveFile.flush();
  188. saveFile.close();
  189. }
  190. // Cache the update
  191. this._cachedProjectPreferences = prefs;
  192. }
  193. /**
  194. * Sets a group of user preference values in the user settings file located in the project. Elements in the
  195. * group will merge in with existing group preferences. Use this method if setting a bunch of settings
  196. * at once.
  197. * @param {string} settingsGroup name of the group the preference lives under
  198. * @param {string} groupPreferenceValues an object literal containing all of the preferences for the group.
  199. */
  200. setUserPreferenceGroup(settingsGroup: string, groupPreferenceValues: Object) {
  201. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  202. let prefs = {};
  203. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  204. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  205. try {
  206. prefs = JSON.parse(prefsFile.readText());
  207. } finally {
  208. prefsFile.close();
  209. }
  210. }
  211. prefs[settingsGroup] = prefs[settingsGroup] || {};
  212. for (let preferenceName in groupPreferenceValues) {
  213. prefs[settingsGroup][preferenceName] = groupPreferenceValues[preferenceName];
  214. }
  215. let saveFile = new Atomic.File(prefsFileLoc, Atomic.FILE_WRITE);
  216. try {
  217. saveFile.writeString(JSON.stringify(prefs, null, " "));
  218. } finally {
  219. saveFile.flush();
  220. saveFile.close();
  221. }
  222. // Cache the update
  223. this._cachedProjectPreferences = prefs;
  224. }
  225. }
  226. interface WindowData {
  227. x: number;
  228. y: number;
  229. width: number;
  230. height: number;
  231. monitor: number;
  232. maximized: boolean;
  233. }
  234. interface AceEditorSettings {
  235. theme: string;
  236. keyboardHandler: string;
  237. fontSize: number;
  238. showInvisibles: boolean;
  239. useSoftTabs: boolean;
  240. tabSize: number;
  241. }
  242. interface UserInterfaceData {
  243. skinPath : string;
  244. defaultSkinPath : string;
  245. fontFile : string;
  246. fontName : string;
  247. fontSize : number;
  248. }
  249. interface EditorBuildData {
  250. lastEditorBuildSHA : string;
  251. }
  252. class PreferencesFormat {
  253. constructor() {
  254. this.setDefault();
  255. }
  256. setDefault() {
  257. this.recentProjects = [];
  258. this.editorWindow = {
  259. x: 0,
  260. y: 0,
  261. width: 0,
  262. height: 0,
  263. monitor: 0,
  264. maximized: true
  265. };
  266. this.playerWindow = {
  267. x: 0,
  268. y: 0,
  269. width: 0,
  270. height: 0,
  271. monitor: 0,
  272. maximized: false
  273. };
  274. this.codeEditorSettings = {
  275. theme: "ace/theme/monokai",
  276. keyboardHandler: "ace/keyboard/textinput",
  277. fontSize: 12,
  278. showInvisibles: false,
  279. useSoftTabs: true,
  280. tabSize: 4
  281. };
  282. this.uiData = {
  283. skinPath : "AtomicEditor/editor/skin/",
  284. defaultSkinPath : "AtomicEditor/resources/default_skin/",
  285. fontFile : "AtomicEditor/resources/vera.ttf",
  286. fontName : "Vera",
  287. fontSize : 12
  288. };
  289. this.editorBuildData = {
  290. lastEditorBuildSHA : "Unversioned Build"
  291. }
  292. }
  293. /**
  294. * Run through a provided prefs block and verify that all the sections are present. If any
  295. * are missing, add the defaults in
  296. * @param {PreferencesFormat} prefs
  297. * @return boolean returns true if any missing defaults were updated
  298. */
  299. applyMissingDefaults(prefs: PreferencesFormat) {
  300. let updatedMissingDefaults = false;
  301. if (!prefs.recentProjects) {
  302. prefs.recentProjects = this.recentProjects;
  303. updatedMissingDefaults = true;
  304. }
  305. if (!prefs.editorWindow) {
  306. prefs.editorWindow = this.editorWindow;
  307. updatedMissingDefaults = true;
  308. }
  309. if (!prefs.playerWindow) {
  310. prefs.playerWindow = this.playerWindow;
  311. updatedMissingDefaults = true;
  312. }
  313. if (!prefs.codeEditorSettings) {
  314. prefs.codeEditorSettings = this.codeEditorSettings;
  315. updatedMissingDefaults = true;
  316. }
  317. if (!prefs.uiData) {
  318. prefs.uiData = this.uiData;
  319. updatedMissingDefaults = true;
  320. }
  321. if (!prefs.editorBuildData) {
  322. prefs.editorBuildData = this.editorBuildData;
  323. updatedMissingDefaults = true;
  324. }
  325. return updatedMissingDefaults;
  326. }
  327. recentProjects: string[];
  328. editorWindow: WindowData;
  329. playerWindow: WindowData;
  330. codeEditorSettings: AceEditorSettings;
  331. uiData : UserInterfaceData;
  332. editorBuildData : EditorBuildData;
  333. }
  334. export = Preferences;