Skip to content

Commit

Permalink
Provide user configuration via config.toml file (#31)
Browse files Browse the repository at this point in the history
* Add toml11 as submodule

* Add functions to read or create config file

* Add config loading and parsing

* Use config values: stealer.enabled and loader.path

* Use defaults if keys/tables do not exist

* Add debug logging

* Move default config string

* Bump version

* Bump version
  • Loading branch information
lewisclark authored Oct 6, 2022
1 parent caea6f2 commit f66f412
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "spdlog"]
path = spdlog
url = https://github.com/gabime/spdlog
[submodule "toml11"]
path = toml11
url = https://github.com/ToruNiina/toml11
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(gluasteal LANGUAGES CXX VERSION 1.9)
project(gluasteal LANGUAGES CXX VERSION 2.0)

enable_testing()

Expand All @@ -20,5 +20,8 @@ include_directories(glua_headers/include)
# catch2
include_directories(catch2/single_include)

# toml11
include_directories(toml11)

# gluasteal src
add_subdirectory(src)
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ add_subdirectory(library)
# tests
add_subdirectory(tests)

add_library(gluasteal SHARED main.cpp init.cpp logger.cpp)
add_library(gluasteal SHARED main.cpp init.cpp logger.cpp config.cpp)

target_link_libraries(gluasteal PRIVATE ${CMAKE_THREADS_LIBS_INIT} file lua hook gamesdk library)
65 changes: 65 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright (C) 2019 Lewis Clark
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */


#include "config.hpp"

static glt::config::Config cfg;

void glt::config::LoadConfig() {
glt::file::CreateConfig();

const auto& logger = GetLogger();

try {
const auto t = toml::parse(glt::file::GetConfigFilePath());

if (t.contains("stealer")) {
const auto& table_stealer = toml::find(t, "stealer");

if (table_stealer.contains("enabled")) {
const auto stealer_enabled = toml::find(table_stealer, "enabled");

if (stealer_enabled.is_boolean()) {
cfg.stealer_enabled = toml::get<bool>(stealer_enabled);
}
}
}

if (t.contains("loader")) {
const auto& table_loader = toml::find(t, "loader");

if (table_loader.contains("file")) {
const auto& loader_file = toml::find(table_loader, "file");

if (loader_file.is_string()) {
cfg.loader_file = toml::get<std::string>(loader_file);
}
}
}

logger->info("Config loaded");

logger->debug("stealer.enabled = {}, loader.file = {}", cfg.stealer_enabled, cfg.loader_file);
}
catch (const std::exception& ex) {
logger->error("Failed to parse config!");
logger->error(ex.what());
}
}

const glt::config::Config glt::config::GetConfig() {
return cfg;
}
37 changes: 37 additions & 0 deletions src/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright (C) 2019 Lewis Clark
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */


#ifndef CONFIG_H
#define CONFIG_H

#include <toml.hpp>

#include "logger.hpp"
#include "file/file.hpp"

namespace glt::config {
const std::string DEFAULT_CONFIG = "[general]\n\n[stealer]\nenabled = true\n\n[loader]\nfile = \"gluasteal.lua\"\n\n";

struct Config {
bool stealer_enabled = true;
std::string loader_file = "gluasteal.lua";
};

void LoadConfig();
const Config GetConfig();
}

#endif
19 changes: 17 additions & 2 deletions src/file/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ std::filesystem::path glt::file::GetHomeDirectory() {
}

std::filesystem::path glt::file::GetLogFilePath() {
return (GetWorkDirectory() / "log.txt");
return GetWorkDirectory() / "log.txt";
}

std::filesystem::path glt::file::GetConfigFilePath() {
return GetWorkDirectory() / "config.toml";
}

std::filesystem::path glt::file::GetServerStorePath() {
return (GetWorkDirectory() / "servers");
return GetWorkDirectory() / "servers";
}

std::string glt::file::ReadFile(const std::string& path) {
Expand All @@ -65,3 +69,14 @@ std::string glt::file::ReadFile(const std::string& path) {

return ss.str();
}

void glt::file::CreateConfig() {
if (!std::filesystem::exists(GetConfigFilePath())) {
auto ofconfig = std::ofstream(GetConfigFilePath());

ofconfig << glt::config::DEFAULT_CONFIG;
ofconfig.close();

GetLogger()->debug("Created config file and filled with defaults");
}
}
4 changes: 4 additions & 0 deletions src/file/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <sstream>

#include "os.hpp"
#include "logger.hpp"
#include "config.hpp"

#if (defined(OS_LINUX) || defined(OS_MAC))
#include <unistd.h>
Expand All @@ -32,6 +34,8 @@ namespace glt::file {
std::filesystem::path GetWorkDirectory(); // Returns a path where everything is stored
std::filesystem::path GetHomeDirectory();
std::filesystem::path GetLogFilePath();
std::filesystem::path GetConfigFilePath();
std::filesystem::path GetServerStorePath();
std::string ReadFile(const std::string& path); // Relative to work directory
void CreateConfig();
}
3 changes: 2 additions & 1 deletion src/hook/luainterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ static bool __FASTCALL__ RunStringExHk(glt::ssdk::ILuaInterface* thisptr, std::u
ret = RunStringExOrig(thisptr, filename, path, buf, b1, b2, b3, b4);
}

glt::lua::DumpLua(std::move(strfilename), std::move(strcode));
if (glt::config::GetConfig().stealer_enabled)
glt::lua::DumpLua(std::move(strfilename), std::move(strcode));

return ret;
}
Expand Down
1 change: 1 addition & 0 deletions src/hook/luainterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <filesystem>

#include "logger.hpp"
#include "config.hpp"
#include "file/file.hpp"
#include "file/sanitization.hpp"

Expand Down
2 changes: 2 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void glt::Init() {
const auto& logger = GetLogger();
logger->info("Initializing gluasteal v{:.1f}", GLUASTEAL_VERSION);

glt::config::LoadConfig();

while (true) {
try {
lib::Library("engine").GetInterface<ssdk::IVEngineClient>("VEngineClient015");
Expand Down
2 changes: 2 additions & 0 deletions src/init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <thread>

#include "logger.hpp"
#include "config.hpp"
#include "file/file.hpp"
#include "library/library.hpp"

Expand All @@ -35,6 +36,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */

namespace glt {
void Init();
void InitConfig();
}

#endif
6 changes: 4 additions & 2 deletions src/lua/lualoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ void glt::lua::RunLua(ssdk::ILuaInterface* lua, const std::string& identifier, c
}

bool glt::lua::LoadLua(ssdk::ILuaInterface* lua, const std::string& filename, const std::string& code) {
const auto& loader_file = glt::config::GetConfig().loader_file;

try {
RunLua(lua, "gluasteal.lua", GetLuaFileContents(), filename, code);
RunLua(lua, loader_file, GetLuaFileContents(loader_file), filename, code);
}
catch (const std::filesystem::filesystem_error&) { // gluasteal.lua doesn't exist, supress.
catch (const std::filesystem::filesystem_error&) { // loader file does not exist, supress exception
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/lua/lualoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */

#include "luaexports.hpp"
#include "logger.hpp"
#include "config.hpp"
#include "file/file.hpp"

namespace glt::lua {
Expand All @@ -35,7 +36,7 @@ namespace glt::lua {
bool LoadLua(ssdk::ILuaInterface* lua, const std::string& filename, const std::string& code);

// Returns the lua code to run, relative to the gluasteal work directory
std::string GetLuaFileContents(const std::string& path = "gluasteal.lua");
std::string GetLuaFileContents(const std::string& path);

// filename and code are the variables to be filled by RunStringEx to indicate the garrys mod lua file
void CreateEnvironment(ssdk::ILuaInterface* lua, const std::string& filename, const std::string& code);
Expand Down
1 change: 1 addition & 0 deletions toml11
Submodule toml11 added at 41908b

0 comments on commit f66f412

Please sign in to comment.