Add config class

This commit is contained in:
wheremyfoodat 2023-07-08 16:16:00 +03:00
parent 90a88eee39
commit bc3377ac78
6 changed files with 75 additions and 2 deletions

3
.gitmodules vendored
View file

@ -16,3 +16,6 @@
[submodule "third_party/xbyak"] [submodule "third_party/xbyak"]
path = third_party/xbyak path = third_party/xbyak
url = https://github.com/herumi/xbyak url = https://github.com/herumi/xbyak
[submodule "third_party/toml11"]
path = third_party/toml11
url = https://github.com/ToruNiina/toml11

View file

@ -41,7 +41,9 @@ set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_SHARED OFF CACHE BOOL "" FORCE) set(SDL_SHARED OFF CACHE BOOL "" FORCE)
add_subdirectory(third_party/SDL2) add_subdirectory(third_party/SDL2)
add_subdirectory(third_party/glad) add_subdirectory(third_party/glad)
add_subdirectory(third_party/toml11)
include_directories(${SDL2_INCLUDE_DIR}) include_directories(${SDL2_INCLUDE_DIR})
include_directories(third_party/toml11)
set(BOOST_ROOT "${CMAKE_SOURCE_DIR}/third_party/boost") set(BOOST_ROOT "${CMAKE_SOURCE_DIR}/third_party/boost")
set(Boost_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/third_party/boost") set(Boost_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/third_party/boost")
@ -83,8 +85,8 @@ else()
message(FATAL_ERROR "Currently unsupported CPU architecture") message(FATAL_ERROR "Currently unsupported CPU architecture")
endif() endif()
set(SOURCE_FILES src/main.cpp src/emulator.cpp src/io_file.cpp src/gl_state.cpp src/core/CPU/cpu_dynarmic.cpp set(SOURCE_FILES src/main.cpp src/emulator.cpp src/io_file.cpp src/gl_state.cpp src/config.cpp
src/core/CPU/dynarmic_cycles.cpp src/core/memory.cpp src/core/CPU/cpu_dynarmic.cpp src/core/CPU/dynarmic_cycles.cpp src/core/memory.cpp
) )
set(CRYPTO_SOURCE_FILES src/core/crypto/aes_engine.cpp) set(CRYPTO_SOURCE_FILES src/core/crypto/aes_engine.cpp)
set(KERNEL_SOURCE_FILES src/core/kernel/kernel.cpp src/core/kernel/resource_limits.cpp set(KERNEL_SOURCE_FILES src/core/kernel/kernel.cpp src/core/kernel/resource_limits.cpp
@ -139,6 +141,7 @@ set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/opengl.hpp inc
include/result/result_common.hpp include/result/result_fs.hpp include/result/result_fnd.hpp include/result/result_common.hpp include/result/result_fs.hpp include/result/result_fnd.hpp
include/result/result_gsp.hpp include/result/result_kernel.hpp include/result/result_os.hpp include/result/result_gsp.hpp include/result/result_kernel.hpp include/result/result_os.hpp
include/crypto/aes_engine.hpp include/metaprogramming.hpp include/PICA/pica_vertex.hpp include/gl_state.hpp include/crypto/aes_engine.hpp include/metaprogramming.hpp include/PICA/pica_vertex.hpp include/gl_state.hpp
include/config.hpp
) )
set(THIRD_PARTY_SOURCE_FILES third_party/imgui/imgui.cpp set(THIRD_PARTY_SOURCE_FILES third_party/imgui/imgui.cpp

10
include/config.hpp Normal file
View file

@ -0,0 +1,10 @@
#pragma once
#include <filesystem>
// Remember to initialize everything field here to its default value otherwise bad things will happen
struct EmulatorConfig {
bool shaderJitEnabled = false;
void load(const std::filesystem::path& path);
void save(const std::filesystem::path& path);
};

View file

@ -9,6 +9,7 @@
#include "PICA/gpu.hpp" #include "PICA/gpu.hpp"
#include "cpu.hpp" #include "cpu.hpp"
#include "config.hpp"
#include "crypto/aes_engine.hpp" #include "crypto/aes_engine.hpp"
#include "io_file.hpp" #include "io_file.hpp"
#include "memory.hpp" #include "memory.hpp"
@ -24,6 +25,7 @@ class Emulator {
Crypto::AESEngine aesEngine; Crypto::AESEngine aesEngine;
GLStateManager gl; GLStateManager gl;
EmulatorConfig config;
SDL_Window* window; SDL_Window* window;
SDL_GLContext glContext; SDL_GLContext glContext;
SDL_GameController* gameController = nullptr; SDL_GameController* gameController = nullptr;
@ -49,6 +51,7 @@ class Emulator {
public: public:
Emulator(); Emulator();
~Emulator();
void step(); void step();
void render(); void render();

53
src/config.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "config.hpp"
#include <fstream>
#include "helpers.hpp"
#include "toml.hpp"
// Largely based on https://github.com/nba-emu/NanoBoyAdvance/blob/master/src/platform/core/src/config.cpp
void EmulatorConfig::load(const std::filesystem::path& path) {
// If the configuration file does not exist, create it and return
if (!std::filesystem::exists(path)) {
save(path);
return;
}
toml::value data;
try {
data = toml::parse(path);
} catch (std::exception& ex) {
Helpers::warn("Got exception trying to load config file. Exception: %s\n", ex.what());
return;
}
if (data.contains("GPU")) {
auto gpuResult = toml::expect<toml::value>(data.at("GPU"));
if (gpuResult.is_ok()) {
auto gpu = gpuResult.unwrap();
shaderJitEnabled = toml::find_or<toml::boolean>(gpu, "EnableShaderJIT", false);
}
}
}
void EmulatorConfig::save(const std::filesystem::path& path) {
toml::basic_value<toml::preserve_comments> data;
if (std::filesystem::exists(path)) {
try {
data = toml::parse<toml::preserve_comments>(path);
} catch (std::exception& ex) {
Helpers::warn("Got exception trying to save config file. Exception: %s\n", ex.what());
return;
}
}
data["GPU"]["EnableShaderJIT"] = shaderJitEnabled;
std::ofstream file(path, std::ios::out);
file << data;
file.close();
}

1
third_party/toml11 vendored Submodule

@ -0,0 +1 @@
Subproject commit 1340692442b530ada93a63f4b76a1421b3c139fe