[Config] New function 'loadConfigFromFile' automatically loads 'config.lua' if present.

This commit is contained in:
Quentin Bazin 2020-02-15 23:06:01 +09:00
parent 93cd68ffe9
commit c4ea0d84e1
4 changed files with 42 additions and 0 deletions

1
.gitignore vendored
View File

@ -57,4 +57,5 @@ server/openminer_server
# Misc
*.zip
test_atlas.png
config.lua

View File

@ -44,6 +44,8 @@ namespace Config {
// Input
extern u8 mouseSensitivity;
void loadConfigFromFile(const char *file);
}
#endif // CONFIG_HPP_

View File

@ -50,6 +50,8 @@ void ClientApplication::init() {
if (m_argumentParser.getArgument("port").isFound)
m_port = std::stoi(m_argumentParser.getArgument("port").parameter);
Config::loadConfigFromFile("config.lua");
m_keyboardHandler.loadKeysFromFile("resources/config/keys.xml");
gk::GamePad::init(m_keyboardHandler);

View File

@ -41,3 +41,40 @@ u8 Config::guiScale = 3;
// Input
u8 Config::mouseSensitivity = 8;
#include <iostream>
#include <gk/core/Filesystem.hpp>
#include <sol.hpp>
void Config::loadConfigFromFile(const char *file) {
if (gk::Filesystem::fileExists(file)) {
sol::state lua;
try {
lua.safe_script_file(file);
isFlyModeEnabled = lua["isFlyModeEnabled"].get_or(isFlyModeEnabled);
isNoClipEnabled = lua["isNoClipEnabled"].get_or(isNoClipEnabled);
renderDistance = lua["renderDistance"].get_or(renderDistance);
isTorchSmoothLightingEnabled = lua["isTorchSmoothLightingEnabled"].get_or(isTorchSmoothLightingEnabled);
isSunSmoothLightingEnabled = lua["isSunSmoothLightingEnabled"].get_or(isSunSmoothLightingEnabled);
isAmbientOcclusionEnabled = lua["isAmbientOcclusionEnabled"].get_or(isAmbientOcclusionEnabled);
isWireframeModeEnabled = lua["isWireframeModeEnabled"].get_or(isWireframeModeEnabled);
isFullscreenModeEnabled = lua["isFullscreenModeEnabled"].get_or(isFullscreenModeEnabled);
cameraFOV = lua["cameraFOV"].get_or(cameraFOV);
screenWidth = lua["screenWidth"].get_or(screenWidth);
screenHeight = lua["screenHeight"].get_or(screenHeight);
guiScale = lua["guiScale"].get_or(guiScale);
mouseSensitivity = lua["mouseSensitivity"].get_or(mouseSensitivity);
std::cout << "Config file loaded successfully" << std::endl;
}
catch (sol::error &e) {
std::cerr << e.what() << std::endl;
}
}
}