Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--------------------------------
- SLib Configuration system
- ----------------------------------*/
- /*----------------------------
- Folders
- ------------------------------*/
- file.CreateDir("slib_configs");
- /*----------------------------
- Stuff
- ------------------------------*/
- SLib.Config.All = { }; // All configurations are stored here
- var values = { };
- /*----------------------------
- Configuration class
- ------------------------------*/
- class SLib_Config {
- var AddonId, AddonNiceName;
- // Default values table: { Name, Type, Value, Description }
- var DefaultValues = { };
- var Values = { };
- /*
- Registers default value
- */
- function RegisterValue(name, type, value, description) {
- this.DefaultValues[#this.DefaultValues + 1] = {
- Name = name,
- Type = type,
- Value = value,
- Description = description
- };
- this:SetValue(name, value); // Add the value into Values table
- }
- /*
- Sets config value
- */
- function SetValue(name, value) {
- this.Values[name] = value;
- }
- /*
- Gets config value
- */
- function GetValue(name, value) {
- return this.Values[name];
- }
- /*
- Builds the config and calls the hook
- */
- function Save() {
- var built = this:Build();
- hook.Call("SLib_ConfigReloaded", null, this.AddonId, built);
- file.Write("slib_configs/" .. this.AddonId .. ".txt", util.TableToJSON(this.Values));
- }
- /*
- Loads the config from a file
- */
- function LoadFromFile() {
- if(!file.Exists("slib_configs/" .. this.AddonId .. ".txt" ,"DATA"))
- return;
- print(this.AddonNiceName .. " configuration has been loaded from file");
- var data = util.JSONToTable(file.Read("slib_configs/" .. this.AddonId .. ".txt"));
- this.Values = data;
- }
- /*
- Finishes
- */
- function Finish() {
- this:LoadFromFile();
- // TODO: Sending to clients
- }
- /*
- Builds a table with all config values
- */
- function Build() {
- var tbl = { };
- foreach(var k, v in this.Values) {
- tbl[k] = v;
- }
- return tbl;
- }
- }
- /*----------------------------
- Functions
- ------------------------------*/
- /*
- Creates a new configuration object.
- Called only in configuration file, to initialize settings.
- */
- function SLib.Config.Create(addonId, addonNiceName) {
- var config = SLib_Config();
- config.AddonId = addonId;
- config.AddonNiceName = addonNiceName;
- SLib.Config.All[addonId] = config;
- return config;
- }
- /*----------------------------
- Tests
- ------------------------------*/
- /*
- var config = SLib.Config.Create("test_config", "Test Configuration");
- config:RegisterValue("SuperVar2", "", "Default Value", "Description");
- config:Finish();
- */
- #if DEBUG
- file.Delete("slib_configs/test_config.txt");
- // Create a simple config
- var config = SLib.Config.Create("test_config", "Test Configuration");
- config:RegisterValue("SuperVar", "", "Hello", "Description");
- config:RegisterValue("SuperVar2", "", "Kek", "xD");
- config:Finish();
- config:Save(); // Save it
- // Load and change a value
- var built = config:Build();
- if(built.SuperVar != "Hello") {
- error(_FILENAME_ .. _LINE_ ..": Value isn't valid");
- }
- config = SLib.Config.Create("test_config", "Test Configuration");
- config:RegisterValue("SuperVar", "", "Hello", "Description");
- config:RegisterValue("SuperVar2", "", "Kek", "xD");
- config:Finish();
- config:SetValue("SuperVar2", 123);
- config:Save();
- // Load once again and test
- config = SLib.Config.Create("test_config", "Test Configuration");
- config:RegisterValue("SuperVar", "", "Hello", "Description");
- config:RegisterValue("SuperVar2", "", "Kek", "xD");
- config:Finish();
- var built = config:Build();
- if(built.SuperVar != "Hello") {
- error(_FILENAME_ .. _LINE_ ..": Value isn't valid");
- }
- if(built.SuperVar2 != 123) {
- error(_FILENAME_ .. _LINE_ ..": Value isn't valid");
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement