Fixed import
This commit is contained in:
@ -1,41 +1,41 @@
|
|||||||
module thirdworld.common.config;
|
module thirdworld.common.config;
|
||||||
|
|
||||||
import common.log;
|
import thirdworld.common.log;
|
||||||
|
|
||||||
import std.file : readText, exists, isFile, write;
|
import std.file : readText, exists, isFile, write;
|
||||||
import std.json;
|
import std.json;
|
||||||
import std.path : absolutePath;
|
import std.path : absolutePath;
|
||||||
|
|
||||||
JSONValue structToJSON(T)(T s) {
|
JSONValue structToJSON(T)(T s) {
|
||||||
JSONValue json;
|
JSONValue json;
|
||||||
foreach (member; __traits(allMembers, T)) {
|
foreach (member; __traits(allMembers, T)) {
|
||||||
json[member] = JSONValue(__traits(getMember, s, member));
|
json[member] = JSONValue(__traits(getMember, s, member));
|
||||||
}
|
}
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
void createConfigFile(T)(string path) {
|
void createConfigFile(T)(string path) {
|
||||||
path = absolutePath(path);
|
path = absolutePath(path);
|
||||||
if (exists(path) && isFile(path)) {
|
if (exists(path) && isFile(path)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
logInfo("No config file found, creating default config file at '%s'", path);
|
logInfo("No config file found, creating default config file at '%s'", path);
|
||||||
|
|
||||||
T config;
|
T config;
|
||||||
auto json = structToJSON(config);
|
auto json = structToJSON(config);
|
||||||
write(path, json.toPrettyString());
|
write(path, json.toPrettyString());
|
||||||
}
|
}
|
||||||
|
|
||||||
T configFromFile(T)(string path) {
|
T configFromFile(T)(string path) {
|
||||||
string text = readText(path);
|
string text = readText(path);
|
||||||
JSONValue json = parseJSON(text);
|
JSONValue json = parseJSON(text);
|
||||||
|
|
||||||
T config;
|
T config;
|
||||||
|
|
||||||
foreach (member; __traits(allMembers, T)) {
|
foreach (member; __traits(allMembers, T)) {
|
||||||
config[member] = json[member];
|
config[member] = json[member];
|
||||||
}
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user