1 /* 2 Copyright © 2020, Inochi2D Project 3 Distributed under the 2-Clause BSD License, see LICENSE file. 4 5 Authors: Luna Nielsen 6 */ 7 module creator.core.settings; 8 import std.json; 9 import std.file; 10 import std.path : buildPath; 11 import creator.core.path; 12 13 private { 14 JSONValue settings = JSONValue(string[string].init); 15 } 16 17 string incSettingsPath() { 18 return buildPath(incGetAppConfigPath(), "settings.json"); 19 } 20 21 /** 22 Load settings from settings file 23 */ 24 void incSettingsLoad() { 25 if (exists(incSettingsPath())) { 26 settings = parseJSON(readText(incSettingsPath())); 27 } 28 } 29 30 /** 31 Saves settings from settings store 32 */ 33 void incSettingsSave() { 34 write(incSettingsPath(), settings.toString()); 35 } 36 37 /** 38 Sets a setting 39 */ 40 void incSettingsSet(T)(string name, T value) { 41 settings[name] = value; 42 } 43 44 /** 45 Gets a value from the settings store 46 */ 47 T incSettingsGet(T)(string name) { 48 if (name in settings) { 49 return settings[name].get!T; 50 } 51 return T.init; 52 } 53 54 /** 55 Gets a value from the settings store, with custom default value 56 */ 57 T incSettingsGet(T)(string name, T default_) { 58 if (name in settings) { 59 return settings[name].get!T; 60 } 61 return default_; 62 } 63 64 /** 65 Gets whether a setting is obtainable 66 */ 67 bool incSettingsCanGet(string name) { 68 return (name in settings) !is null; 69 }