-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: moved json parser to platform/windows package
- Loading branch information
1 parent
939889d
commit 8cdaa01
Showing
6 changed files
with
196 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
#include "config_parser.h" | ||
#include <nlohmann/json.hpp> | ||
#include <fmt/core.h> | ||
#include "config.h" | ||
#include "device/controller/controller_type.h" | ||
#include "device/gpu/rendering_mode.h" | ||
#include "utils/file.h" | ||
#include "utils/json_enum.h" | ||
|
||
// For now .json format is used for config serialization. | ||
// Might switch to .toml some day | ||
|
||
const char* CONFIG_NAME = "config.json"; | ||
void saveConfigFile(const char* configName); | ||
void loadConfigFile(const char* configName); | ||
|
||
JSON_ENUM(ControllerType); | ||
JSON_ENUM(RenderingMode); | ||
|
||
void saveConfigFile(const char* configName) { | ||
nlohmann::json json; | ||
json["bios"] = config.bios; | ||
json["extension"] = config.extension; | ||
json["iso"] = config.iso; | ||
|
||
for (int i = 0; i < 2; i++) { | ||
json["controller"][std::to_string(i + 1)] = { | ||
{"type", config.controller[i].type}, | ||
{"keys", config.controller[i].keys}, | ||
}; | ||
} | ||
|
||
auto g = config.options.graphics; | ||
json["options"]["graphics"] = { | ||
{"renderingMode", g.renderingMode}, | ||
{"widescreen", g.widescreen}, | ||
{"forceWidescreen", g.forceWidescreen}, | ||
{ | ||
"resolution", | ||
{ | ||
{"width", g.resolution.width}, | ||
{"height", g.resolution.height}, | ||
}, | ||
}, | ||
{"vsync", g.vsync}, | ||
{"forceNtsc", g.forceNtsc}, | ||
}; | ||
|
||
json["options"]["sound"] = { | ||
{"enabled", config.options.sound.enabled}, | ||
}; | ||
|
||
json["options"]["emulator"] = { | ||
{"preserveState", config.options.emulator.preserveState}, | ||
{"timeTravel", config.options.emulator.timeTravel}, | ||
}; | ||
|
||
auto l = config.debug.log; | ||
json["debug"]["log"] = { | ||
{"bios", l.bios}, // | ||
{"cdrom", l.cdrom}, // | ||
{"controller", l.controller}, // | ||
{"dma", l.dma}, // | ||
{"gpu", l.gpu}, // | ||
{"gte", l.gte}, // | ||
{"mdec", l.mdec}, // | ||
{"memoryCard", l.memoryCard}, // | ||
{"spu", l.spu}, // | ||
{"system", l.system}, // | ||
}; | ||
|
||
for (int i = 0; i < 2; i++) { | ||
json["memoryCard"][std::to_string(i + 1)] = config.memoryCard[i].path; | ||
} | ||
|
||
json["gui"] = { | ||
{"lastPath", config.gui.lastPath}, | ||
}; | ||
|
||
putFileContents(configName, json.dump(4)); | ||
} | ||
|
||
void loadConfigFile(const char* configName) { | ||
auto file = getFileContents(configName); | ||
if (file.empty()) { | ||
saveConfigFile(configName); | ||
return; | ||
} | ||
nlohmann::json json = nlohmann::json::parse(file.begin(), file.end()); | ||
|
||
try { | ||
config.bios = json["bios"]; | ||
config.extension = json["extension"]; | ||
config.iso = json["iso"]; | ||
|
||
for (int i = 0; i < 2; i++) { | ||
auto ctrl = json["controller"][std::to_string(i + 1)]; | ||
if (ctrl.is_null()) continue; | ||
|
||
config.controller[i].type = ctrl["type"]; | ||
config.controller[i].keys = ctrl["keys"].get<KeyBindings>(); | ||
} | ||
|
||
if (auto g = json["options"]["graphics"]; !g.is_null()) { | ||
config.options.graphics.renderingMode = g["renderingMode"]; | ||
config.options.graphics.widescreen = g["widescreen"]; | ||
config.options.graphics.forceWidescreen = g["forceWidescreen"]; | ||
config.options.graphics.resolution.width = g["resolution"]["width"]; | ||
config.options.graphics.resolution.height = g["resolution"]["height"]; | ||
config.options.graphics.vsync = g["vsync"]; | ||
config.options.graphics.forceNtsc = g["forceNtsc"]; | ||
} | ||
|
||
if (auto s = json["options"]["sound"]; !s.is_null()) { | ||
config.options.sound.enabled = s["enabled"]; | ||
} | ||
|
||
if (auto e = json["options"]["emulator"]; !e.is_null()) { | ||
config.options.emulator.preserveState = e["preserveState"]; | ||
config.options.emulator.timeTravel = e["timeTravel"]; | ||
} | ||
|
||
if (auto l = json["debug"]["log"]; !l.is_null()) { | ||
config.debug.log.bios = l["bios"]; | ||
config.debug.log.cdrom = l["cdrom"]; | ||
config.debug.log.controller = l["controller"]; | ||
config.debug.log.dma = l["dma"]; | ||
config.debug.log.gpu = l["gpu"]; | ||
config.debug.log.gte = l["gte"]; | ||
config.debug.log.mdec = l["mdec"]; | ||
config.debug.log.memoryCard = l["memoryCard"]; | ||
config.debug.log.spu = l["spu"]; | ||
config.debug.log.system = l["system"]; | ||
} | ||
|
||
for (int i = 0; i < 2; i++) { | ||
auto card = json["memoryCard"][std::to_string(i + 1)]; | ||
if (card.is_null()) continue; | ||
|
||
config.memoryCard[i].path = card; | ||
} | ||
|
||
if (auto g = json["gui"]; !g.is_null()) { | ||
config.gui.lastPath = g["lastPath"]; | ||
} | ||
|
||
} catch (nlohmann::json::exception& e) { | ||
fmt::print("[ERROR] Cannot load {} - {}\n", configName, e.what()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
extern const char* CONFIG_NAME; | ||
void saveConfigFile(const char* configName); | ||
void loadConfigFile(const char* configName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters