Browse Source

Can now create new configs on the fly using: local config = g_configs.create(file)

BenDol 7 years ago
parent
commit
2d84fae2c8

+ 1
- 0
init.lua View File

@@ -27,7 +27,7 @@ g_resources.setupUserWriteDir(g_app.getCompactName())
27 27
 -- search all packages
28 28
 g_resources.searchAndAddPackages('/', '.otpkg', true)
29 29
 
30
+-- load settings
30 31
 g_configs.loadSettings("/config.otml")
31 32
 
32 33
 g_modules.discoverModules()

+ 27
- 6
src/framework/core/configmanager.cpp View File

@@ -59,7 +59,6 @@ ConfigPtr ConfigManager::get(const std::string& file)
59 59
             return config;
60 60
         }
61 61
     }
62
-    g_logger.error(stdext::format("Unable to find configuration for '%s' ", file));
63 62
     return nullptr;
64 63
 }
65 64
 
@@ -76,19 +75,41 @@ ConfigPtr ConfigManager::loadSettings(const std::string file)
76 75
     return nullptr;
77 76
 }
78 77
 
78
+ConfigPtr ConfigManager::create(const std::string& file)
79
+{
80
+    ConfigPtr config = load(file);
81
+    if(!config) {
82
+        config = ConfigPtr(new Config());
83
+
84
+        config->load(file);
85
+        config->save();
86
+
87
+        m_configs.push_back(config);
88
+    }
89
+    return config;
90
+}
91
+
79 92
 ConfigPtr ConfigManager::load(const std::string& file)
80 93
 {
81 94
     if(file.empty()) {
82 95
         g_logger.error("Must provide a configuration file to load.");
96
+        return nullptr;
83 97
     }
84 98
     else {
85
-        ConfigPtr config = ConfigPtr(new Config());
86
-        if(config->load(file)) {
87
-            m_configs.push_back(config);
88
-            return config;
99
+        ConfigPtr config = get(file);
100
+        if(!config) {
101
+            config = ConfigPtr(new Config());
102
+
103
+            if(config->load(file)) {
104
+                m_configs.push_back(config);
105
+            }
106
+            else {
107
+                // cannot load config
108
+                config = nullptr;
109
+            }
89 110
         }
111
+        return config;
90 112
     }
91
-    return nullptr;
92 113
 }
93 114
 
94 115
 bool ConfigManager::unload(const std::string& file)

+ 1
- 0
src/framework/core/configmanager.h View File

@@ -35,6 +35,7 @@ public:
35 35
     ConfigPtr getSettings();
36 36
     ConfigPtr get(const std::string& file);
37 37
 
38
+    ConfigPtr create(const std::string& file);
38 39
     ConfigPtr loadSettings(const std::string file);
39 40
     ConfigPtr load(const std::string& file);
40 41
     bool unload(const std::string& file);

+ 1
- 0
src/framework/luafunctions.cpp View File

@@ -141,6 +141,7 @@ void Application::registerLuaFunctions()
141 141
     g_lua.bindSingletonFunction("g_configs", "loadSettings", &ConfigManager::loadSettings, &g_configs);
142 142
     g_lua.bindSingletonFunction("g_configs", "load", &ConfigManager::load, &g_configs);
143 143
     g_lua.bindSingletonFunction("g_configs", "unload", &ConfigManager::unload, &g_configs);
144
+    g_lua.bindSingletonFunction("g_configs", "create", &ConfigManager::create, &g_configs);
144 145
 
145 146
     // Logger
146 147
     g_lua.registerSingletonClass("g_logger");

Loading…
Cancel
Save