Advertisement
szymski

Untitled

Jan 30th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /*--------------------------------
  2. SLib Configuration system
  3. ----------------------------------*/
  4.  
  5. /*----------------------------
  6. Folders
  7. ------------------------------*/
  8.  
  9. file.CreateDir("slib_configs");
  10.  
  11. /*----------------------------
  12. Stuff
  13. ------------------------------*/
  14.  
  15. SLib.Config.All = { }; // All configurations are stored here
  16.  
  17. var values = { };
  18.  
  19. /*----------------------------
  20. Configuration class
  21. ------------------------------*/
  22.  
  23. class SLib_Config {
  24.  
  25. var AddonId, AddonNiceName;
  26.  
  27. // Default values table: { Name, Type, Value, Description }
  28. var DefaultValues = { };
  29. var Values = { };
  30.  
  31. /*
  32. Registers default value
  33. */
  34. function RegisterValue(name, type, value, description) {
  35. this.DefaultValues[#this.DefaultValues + 1] = {
  36. Name = name,
  37. Type = type,
  38. Value = value,
  39. Description = description
  40. };
  41.  
  42. this:SetValue(name, value); // Add the value into Values table
  43. }
  44.  
  45. /*
  46. Sets config value
  47. */
  48. function SetValue(name, value) {
  49. this.Values[name] = value;
  50. }
  51.  
  52. /*
  53. Gets config value
  54. */
  55. function GetValue(name, value) {
  56. return this.Values[name];
  57. }
  58.  
  59. /*
  60. Builds the config and calls the hook
  61. */
  62. function Save() {
  63. var built = this:Build();
  64.  
  65. hook.Call("SLib_ConfigReloaded", null, this.AddonId, built);
  66.  
  67. file.Write("slib_configs/" .. this.AddonId .. ".txt", util.TableToJSON(this.Values));
  68. }
  69.  
  70. /*
  71. Loads the config from a file
  72. */
  73. function LoadFromFile() {
  74. if(!file.Exists("slib_configs/" .. this.AddonId .. ".txt" ,"DATA"))
  75. return;
  76.  
  77. print(this.AddonNiceName .. " configuration has been loaded from file");
  78.  
  79. var data = util.JSONToTable(file.Read("slib_configs/" .. this.AddonId .. ".txt"));
  80.  
  81. this.Values = data;
  82. }
  83.  
  84. /*
  85. Finishes
  86. */
  87. function Finish() {
  88. this:LoadFromFile();
  89.  
  90. // TODO: Sending to clients
  91. }
  92.  
  93. /*
  94. Builds a table with all config values
  95. */
  96. function Build() {
  97. var tbl = { };
  98.  
  99. foreach(var k, v in this.Values) {
  100. tbl[k] = v;
  101. }
  102.  
  103. return tbl;
  104. }
  105. }
  106.  
  107. /*----------------------------
  108. Functions
  109. ------------------------------*/
  110.  
  111. /*
  112. Creates a new configuration object.
  113. Called only in configuration file, to initialize settings.
  114. */
  115. function SLib.Config.Create(addonId, addonNiceName) {
  116. var config = SLib_Config();
  117. config.AddonId = addonId;
  118. config.AddonNiceName = addonNiceName;
  119.  
  120. SLib.Config.All[addonId] = config;
  121.  
  122. return config;
  123. }
  124.  
  125. /*----------------------------
  126. Tests
  127. ------------------------------*/
  128.  
  129. /*
  130. var config = SLib.Config.Create("test_config", "Test Configuration");
  131. config:RegisterValue("SuperVar2", "", "Default Value", "Description");
  132. config:Finish();
  133. */
  134.  
  135. #if DEBUG
  136.  
  137. file.Delete("slib_configs/test_config.txt");
  138.  
  139. // Create a simple config
  140.  
  141. var config = SLib.Config.Create("test_config", "Test Configuration");
  142.  
  143. config:RegisterValue("SuperVar", "", "Hello", "Description");
  144. config:RegisterValue("SuperVar2", "", "Kek", "xD");
  145.  
  146. config:Finish();
  147. config:Save(); // Save it
  148.  
  149. // Load and change a value
  150.  
  151. var built = config:Build();
  152.  
  153. if(built.SuperVar != "Hello") {
  154. error(_FILENAME_ .. _LINE_ ..": Value isn't valid");
  155. }
  156.  
  157.  
  158. config = SLib.Config.Create("test_config", "Test Configuration");
  159.  
  160. config:RegisterValue("SuperVar", "", "Hello", "Description");
  161. config:RegisterValue("SuperVar2", "", "Kek", "xD");
  162.  
  163. config:Finish();
  164.  
  165. config:SetValue("SuperVar2", 123);
  166.  
  167. config:Save();
  168.  
  169. // Load once again and test
  170.  
  171. config = SLib.Config.Create("test_config", "Test Configuration");
  172.  
  173. config:RegisterValue("SuperVar", "", "Hello", "Description");
  174. config:RegisterValue("SuperVar2", "", "Kek", "xD");
  175.  
  176. config:Finish();
  177.  
  178. var built = config:Build();
  179.  
  180. if(built.SuperVar != "Hello") {
  181. error(_FILENAME_ .. _LINE_ ..": Value isn't valid");
  182. }
  183.  
  184. if(built.SuperVar2 != 123) {
  185. error(_FILENAME_ .. _LINE_ ..": Value isn't valid");
  186. }
  187.  
  188. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement