skyrim-console-server/EmpathicQubit.ConsoleServer/Configuration.cs

86 lines
2.4 KiB
C#
Raw Permalink Normal View History

2021-05-16 10:43:37 -04:00
using IniParser;
using IniParser.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmpathicQbt.ConsoleServer {
public class Configuration {
private readonly string CONFIG_FILE_NAME = "DragonbornSpeaksNaturally.ini";
// NOTE: Relative to SkyrimVR.exe
private static readonly string[] SEARCH_DIRECTORIES = {
"Data\\Plugins\\Sumwunn\\",
"..\\",
""
};
private string iniFilePath = null;
private IniData global = null;
private IniData local = null;
private IniData merged = null;
public Configuration() {
iniFilePath = ResolveFilePath(CONFIG_FILE_NAME);
loadLocal();
loadGlobal();
merged = new IniData();
merged.Merge(global);
merged.Merge(local);
}
public string GetIniFilePath() {
return iniFilePath;
}
public string Get(string section, string key, string def) {
string val = merged[section][key];
if (val == null)
return def;
return val;
}
private void loadGlobal() {
global = new IniData();
}
private void loadLocal() {
local = loadIniFromFilePath(iniFilePath);
if (local == null)
local = new IniData();
}
public static string ResolveFilePath(string filename) {
foreach (string directory in SEARCH_DIRECTORIES) {
string filepath = directory + filename;
if (File.Exists(filepath)) {
return Path.GetFullPath(filepath); ;
}
}
return null;
}
private IniData loadIniFromFilePath(string filepath) {
if (filepath != null) {
Trace.TraceInformation("Loading ini from path " + filepath);
try {
var parser = new FileIniDataParser();
return parser.ReadFile(filepath);
} catch (Exception ex) {
Trace.TraceError("Failed to load ini file at " + filepath);
Trace.TraceError(ex.ToString());
}
}
return null;
}
}
}