Skip to content

Commit

Permalink
core - config opts
Browse files Browse the repository at this point in the history
use ini config for simplicity instead of json
  • Loading branch information
shdwmtr committed Jul 14, 2024
1 parent 24b95ae commit 2e74ce8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 81 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
[submodule "vendor/crow"]
path = vendor/crow
url = https://github.com/CrowCpp/Crow.git
[submodule "vendor/ini"]
path = vendor/ini
url = https://github.com/metayeti/mINI.git
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/vendor/nlohmann/include
${CMAKE_SOURCE_DIR}/vendor/websocketpp
${CMAKE_SOURCE_DIR}/vendor/crow/include
${CMAKE_SOURCE_DIR}/vendor/ini/src
)

add_compile_definitions(
Expand Down
49 changes: 25 additions & 24 deletions src/api/executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,42 @@

PyObject* GetUserSettings(PyObject* self, PyObject* args)
{
std::unique_ptr<SettingsStore> settingsStorePtr = std::make_unique<SettingsStore>();
const nlohmann::json settingsInfo = settingsStorePtr->GetSettings();
// std::unique_ptr<SettingsStore> settingsStorePtr = std::make_unique<SettingsStore>();
// const nlohmann::json settingsInfo = settingsStorePtr->GetSetting();

PyObject* resultBuffer = PyDict_New();
// PyObject* resultBuffer = PyDict_New();

for (auto it = settingsInfo.begin(); it != settingsInfo.end(); ++it)
{
PyObject* key = PyUnicode_FromString(it.key().c_str());
PyObject* value = PyUnicode_FromString(it.value().get<std::string>().c_str());
// for (auto it = settingsInfo.begin(); it != settingsInfo.end(); ++it)
// {
// PyObject* key = PyUnicode_FromString(it.key().c_str());
// PyObject* value = PyUnicode_FromString(it.value().get<std::string>().c_str());

PyDict_SetItem(resultBuffer, key, value);
Py_DECREF(key);
Py_DECREF(value);
}
// PyDict_SetItem(resultBuffer, key, value);
// Py_DECREF(key);
// Py_DECREF(value);
// }

return resultBuffer;
//return resultBuffer;
Py_RETURN_NONE;
}

PyObject* SetUserSettings(PyObject* self, PyObject* args)
{
std::unique_ptr<SettingsStore> settingsStorePtr = std::make_unique<SettingsStore>();
// std::unique_ptr<SettingsStore> settingsStorePtr = std::make_unique<SettingsStore>();

const char* key;
const char* value;
// const char* key;
// const char* value;

if (!PyArg_ParseTuple(args, "ss", &key, &value))
{
return NULL;
}
// if (!PyArg_ParseTuple(args, "ss", &key, &value))
// {
// return NULL;
// }

nlohmann::json settingsInfo = settingsStorePtr->GetSettings();
{
settingsInfo[key] = value;
}
settingsStorePtr->SetSettings(settingsInfo.dump(4));
// nlohmann::json settingsInfo = settingsStorePtr->GetSetting();
// {
// settingsInfo[key] = value;
// }
// settingsStorePtr->SetSetting(settingsInfo.dump(4));

Py_RETURN_NONE;
}
Expand Down
12 changes: 10 additions & 2 deletions src/sys/locals.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#pragma once
#define MINI_CASE_SENSITIVE
#include <string>
#include <filesystem>
#include <vector>
#include <nlohmann/json.hpp>
#include <mini/ini.h>

class SettingsStore
{
public:
mINI::INIFile file;
mINI::INIStructure ini;

static constexpr const char* pluginConfigFile = "plugin.json";

struct PluginTypeSchema
Expand All @@ -25,10 +30,13 @@ class SettingsStore
bool IsEnabledPlugin(std::string pluginName);
bool TogglePluginStatus(std::string pluginName, bool enabled);

nlohmann::json GetSettings();
std::string GetSetting(std::string key, std::string defaultValue);
void SetSetting(std::string key, std::string settingsData);

void SetSettings(std::string settingsData);
int InitializeSettingsStore();
std::vector<std::string> ParsePluginList();

SettingsStore();

private:
void LintPluginData(nlohmann::json json, std::string pluginName);
Expand Down
116 changes: 61 additions & 55 deletions src/sys/settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,109 +11,115 @@

namespace FileSystem = std::filesystem;

void SettingsStore::SetSettings(std::string file_data)
SettingsStore::SettingsStore() : file(mINI::INIFile(std::string())), ini(mINI::INIStructure())
{
const auto path = SystemIO::GetSteamPath() / "ext" / "plugins.json";
const auto path = SystemIO::GetSteamPath() / "ext" / "millennium.ini";

if (!FileSystem::exists(path))
{
FileSystem::create_directories(path.parent_path());
std::ofstream outputFile(path.string());
outputFile << "{}";
}
SystemIO::WriteFileSync(path, file_data);
}

nlohmann::json SettingsStore::GetSettings()
{
const auto path = SystemIO::GetSteamPath() / "ext" / "plugins.json";
try
{
this->file = mINI::INIFile(path.string());

if (!FileSystem::exists(path))
this->file.read(ini);
this->file.write(ini);
}
catch (const std::exception& ex)
{
FileSystem::create_directories(path.parent_path());
std::ofstream outputFile(path.string());
outputFile << "{}";
Logger.Warn("An error occurred reading settings file -> {}", ex.what());
}
}

bool success;
const auto json = SystemIO::ReadJsonSync(path.string(), &success);
void SettingsStore::SetSetting(std::string key, std::string settingsData)
{
this->ini["Settings"][key] = settingsData;
this->file.write(ini);
}

if (!success)
std::string SettingsStore::GetSetting(std::string key, std::string defaultValue)
{
if (!this->ini["Settings"].has(key))
{
std::ofstream outputFile(path.string());
outputFile << "{}";
this->ini["Settings"][key] = defaultValue;
}

return success ? json : nlohmann::json({});
return this->ini["Settings"][key];
}

int SettingsStore::InitializeSettingsStore()
std::vector<std::string> SettingsStore::ParsePluginList()
{
auto SettingsStore = this->GetSettings();
std::vector<std::string> enabledPlugins;
std::string token;
std::istringstream tokenStream(this->GetSetting("enabled_plugins", "core"));

if (SettingsStore.contains("enabled") && SettingsStore["enabled"].is_array())
while (std::getline(tokenStream, token, '|'))
{
bool found = false;
enabledPlugins.push_back(token);
}

for (const auto& enabledPlugin : SettingsStore["enabled"])
{
if (enabledPlugin == "core")
{
found = true;
break;
}
}
if (!found)
{
SettingsStore["enabled"].push_back("core");
}
return enabledPlugins;
}

std::string ConvertVectorToString(std::vector<std::string> enabledPlugins)
{
std::string strEnabledPlugins;
for (const auto& plugin : enabledPlugins)
{
strEnabledPlugins += plugin + "|";
}
else

return strEnabledPlugins.substr(0, strEnabledPlugins.size() - 1);
}

int SettingsStore::InitializeSettingsStore()
{
auto enabledPlugins = this->ParsePluginList();

// check if core is in the list
if (std::find(enabledPlugins.begin(), enabledPlugins.end(), "core") == enabledPlugins.end())
{
SettingsStore["enabled"] = nlohmann::json::array({ "core" });
enabledPlugins.push_back("core");
}

SetSettings(SettingsStore.dump(4));
return true;
SetSetting("enabled_plugins", ConvertVectorToString(enabledPlugins));
SetSetting("enabled_plugins", ConvertVectorToString(enabledPlugins));

GetSetting("check_updates", "true"); // default to true
return 0;
}

bool SettingsStore::TogglePluginStatus(std::string pluginName, bool enabled)
{
Logger.Log("opting to {} {}", enabled ? "enable" : "disable", pluginName);
auto SettingsStore = this->GetSettings();
auto SettingsStore = this->ParsePluginList();

if (enabled)
{
if (SettingsStore.contains("enabled") && SettingsStore["enabled"].is_array())
if (std::find(SettingsStore.begin(), SettingsStore.end(), pluginName) == SettingsStore.end())
{
SettingsStore["enabled"].push_back(pluginName);
}
else {
SettingsStore["enabled"] = nlohmann::json::array({ pluginName });
SettingsStore.push_back(pluginName);
}
}
// disable the plugin
else if (!enabled && SettingsStore.contains("enabled") && SettingsStore["enabled"].is_array())
else if (!enabled)
{
auto EnabledPlugins = SettingsStore["enabled"].get<std::vector<std::string>>();
auto Iterator = std::find(EnabledPlugins.begin(), EnabledPlugins.end(), pluginName);

if (Iterator != EnabledPlugins.end())
if (std::find(SettingsStore.begin(), SettingsStore.end(), pluginName) != SettingsStore.end())
{
EnabledPlugins.erase(Iterator);
SettingsStore.erase(std::remove(SettingsStore.begin(), SettingsStore.end(), pluginName), SettingsStore.end());
}

SettingsStore["enabled"] = EnabledPlugins;
}

SetSettings(SettingsStore.dump(4));
SetSetting("enabled_plugins", ConvertVectorToString(SettingsStore));
return true;
}

std::vector<std::string> SettingsStore::GetEnabledPlugins()
{
auto json = this->GetSettings();
return json["enabled"].get<std::vector<std::string>>();
return this->ParsePluginList();
}

bool SettingsStore::IsEnabledPlugin(std::string plugin_name)
Expand Down

0 comments on commit 2e74ce8

Please sign in to comment.