forked from Green-Sky/tomato
basic cli args parsing, working plugin paths and semi working configs
This commit is contained in:
parent
df449a475c
commit
04191858de
@ -13,6 +13,9 @@ target_sources(tomato PUBLIC
|
|||||||
./main.cpp
|
./main.cpp
|
||||||
./icon.rc
|
./icon.rc
|
||||||
|
|
||||||
|
./json_to_config.hpp
|
||||||
|
./json_to_config.cpp
|
||||||
|
|
||||||
./screen.hpp
|
./screen.hpp
|
||||||
./start_screen.hpp
|
./start_screen.hpp
|
||||||
./start_screen.cpp
|
./start_screen.cpp
|
||||||
|
67
src/json_to_config.cpp
Normal file
67
src/json_to_config.cpp
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#include "./json_to_config.hpp"
|
||||||
|
|
||||||
|
#include <solanaceae/util/simple_config_model.hpp>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
bool load_json_into_config(const nlohmann::ordered_json& config_json, SimpleConfigModel& conf) {
|
||||||
|
if (!config_json.is_object()) {
|
||||||
|
std::cerr << "TOMATO error: config file is not an json object!!!\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (const auto& [mod, cats] : config_json.items()) {
|
||||||
|
for (const auto& [cat, cat_v] : cats.items()) {
|
||||||
|
if (cat_v.is_object()) {
|
||||||
|
if (cat_v.contains("default")) {
|
||||||
|
const auto& value = cat_v["default"];
|
||||||
|
if (value.is_string()) {
|
||||||
|
conf.set(mod, cat, value.get_ref<const std::string&>());
|
||||||
|
} else if (value.is_boolean()) {
|
||||||
|
conf.set(mod, cat, value.get_ref<const bool&>());
|
||||||
|
} else if (value.is_number_float()) {
|
||||||
|
conf.set(mod, cat, value.get_ref<const double&>());
|
||||||
|
} else if (value.is_number_integer()) {
|
||||||
|
conf.set(mod, cat, value.get_ref<const int64_t&>());
|
||||||
|
} else {
|
||||||
|
std::cerr << "JSON error: wrong value type in " << mod << "::" << cat << " = " << value << "\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cat_v.contains("entries")) {
|
||||||
|
for (const auto& [ent, ent_v] : cat_v["entries"].items()) {
|
||||||
|
if (ent_v.is_string()) {
|
||||||
|
conf.set(mod, cat, ent, ent_v.get_ref<const std::string&>());
|
||||||
|
} else if (ent_v.is_boolean()) {
|
||||||
|
conf.set(mod, cat, ent, ent_v.get_ref<const bool&>());
|
||||||
|
} else if (ent_v.is_number_float()) {
|
||||||
|
conf.set(mod, cat, ent, ent_v.get_ref<const double&>());
|
||||||
|
} else if (ent_v.is_number_integer()) {
|
||||||
|
conf.set(mod, cat, ent, ent_v.get_ref<const int64_t&>());
|
||||||
|
} else {
|
||||||
|
std::cerr << "JSON error: wrong value type in " << mod << "::" << cat << "::" << ent << " = " << ent_v << "\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (cat_v.is_string()) {
|
||||||
|
conf.set(mod, cat, cat_v.get_ref<const std::string&>());
|
||||||
|
} else if (cat_v.is_boolean()) {
|
||||||
|
conf.set(mod, cat, cat_v.get_ref<const bool&>());
|
||||||
|
} else if (cat_v.is_number_float()) {
|
||||||
|
conf.set(mod, cat, cat_v.get_ref<const double&>());
|
||||||
|
} else if (cat_v.is_number_integer()) {
|
||||||
|
conf.set(mod, cat, cat_v.get_ref<const int64_t&>());
|
||||||
|
} else {
|
||||||
|
std::cerr << "JSON error: wrong value type in " << mod << "::" << cat << " = " << cat_v << "\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
9
src/json_to_config.hpp
Normal file
9
src/json_to_config.hpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <nlohmann/json_fwd.hpp>
|
||||||
|
|
||||||
|
// fwd
|
||||||
|
struct SimpleConfigModel;
|
||||||
|
|
||||||
|
bool load_json_into_config(const nlohmann::ordered_json& config_json, SimpleConfigModel& conf);
|
||||||
|
|
@ -12,10 +12,17 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string_view>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
// better args
|
||||||
|
std::vector<std::string_view> args;
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
args.push_back(argv[i]);
|
||||||
|
}
|
||||||
|
|
||||||
// setup hints
|
// setup hints
|
||||||
if (SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1") != SDL_TRUE) {
|
if (SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1") != SDL_TRUE) {
|
||||||
std::cerr << "Failed to set '" << SDL_HINT_VIDEO_ALLOW_SCREENSAVER << "' to 1\n";
|
std::cerr << "Failed to set '" << SDL_HINT_VIDEO_ALLOW_SCREENSAVER << "' to 1\n";
|
||||||
@ -91,7 +98,7 @@ int main(int argc, char** argv) {
|
|||||||
ImGui_ImplSDL3_InitForSDLRenderer(window.get(), renderer.get());
|
ImGui_ImplSDL3_InitForSDLRenderer(window.get(), renderer.get());
|
||||||
ImGui_ImplSDLRenderer3_Init(renderer.get());
|
ImGui_ImplSDLRenderer3_Init(renderer.get());
|
||||||
|
|
||||||
std::unique_ptr<Screen> screen = std::make_unique<StartScreen>(renderer.get(), theme);
|
std::unique_ptr<Screen> screen = std::make_unique<StartScreen>(args, renderer.get(), theme);
|
||||||
|
|
||||||
|
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
|
@ -12,8 +12,9 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
MainScreen::MainScreen(SDL_Renderer* renderer_, Theme& theme_, std::string save_path, std::string save_password, std::string new_username, std::vector<std::string> plugins) :
|
MainScreen::MainScreen(SimpleConfigModel&& conf_, SDL_Renderer* renderer_, Theme& theme_, std::string save_path, std::string save_password, std::string new_username, std::vector<std::string> plugins) :
|
||||||
renderer(renderer_),
|
renderer(renderer_),
|
||||||
|
conf(std::move(conf_)),
|
||||||
rmm(cr),
|
rmm(cr),
|
||||||
msnj{cr, {}, {}},
|
msnj{cr, {}, {}},
|
||||||
mts(rmm),
|
mts(rmm),
|
||||||
|
@ -91,7 +91,7 @@ struct MainScreen final : public Screen {
|
|||||||
uint64_t _window_hidden_ts {0};
|
uint64_t _window_hidden_ts {0};
|
||||||
float _time_since_event {0.f};
|
float _time_since_event {0.f};
|
||||||
|
|
||||||
MainScreen(SDL_Renderer* renderer_, Theme& theme_, std::string save_path, std::string save_password, std::string new_username, std::vector<std::string> plugins);
|
MainScreen(SimpleConfigModel&& conf_, SDL_Renderer* renderer_, Theme& theme_, std::string save_path, std::string save_password, std::string new_username, std::vector<std::string> plugins);
|
||||||
~MainScreen(void);
|
~MainScreen(void);
|
||||||
|
|
||||||
bool handleEvent(SDL_Event& e) override;
|
bool handleEvent(SDL_Event& e) override;
|
||||||
|
@ -2,14 +2,53 @@
|
|||||||
|
|
||||||
#include "./main_screen.hpp"
|
#include "./main_screen.hpp"
|
||||||
|
|
||||||
|
#include "./json_to_config.hpp"
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include <imgui/imgui.h>
|
#include <imgui/imgui.h>
|
||||||
#include <imgui/misc/cpp/imgui_stdlib.h>
|
#include <imgui/misc/cpp/imgui_stdlib.h>
|
||||||
|
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
StartScreen::StartScreen(SDL_Renderer* renderer, Theme& theme) : _renderer(renderer), _theme(theme) {
|
StartScreen::StartScreen(const std::vector<std::string_view>& args, SDL_Renderer* renderer, Theme& theme) : _renderer(renderer), _theme(theme) {
|
||||||
|
for (size_t ai = 0; ai < args.size(); ai++) {
|
||||||
|
if (args.at(ai) == "--config" || args.at(ai) == "-c") {
|
||||||
|
if (args.size() == ai+1) {
|
||||||
|
std::cerr << "TOMATO error: argument '" << args.at(ai) << "' missing parameter!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ai++;
|
||||||
|
|
||||||
|
const auto& config_path = args.at(ai);
|
||||||
|
auto config_file = std::ifstream(static_cast<std::string>(config_path));
|
||||||
|
if (!config_file.is_open()) {
|
||||||
|
std::cerr << "TOMATO error: failed to open config file '" << config_path << "'\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto config_json = nlohmann::ordered_json::parse(config_file);
|
||||||
|
if (!load_json_into_config(config_json, _conf)) {
|
||||||
|
std::cerr << "TOMATO error in config json, exiting...\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (args.at(ai) == "--plugin" || args.at(ai) == "-p") {
|
||||||
|
if (args.size() == ai+1) {
|
||||||
|
std::cerr << "TOMATO error: argument '" << args.at(ai) << "' missing parameter!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ai++;
|
||||||
|
|
||||||
|
const auto& plugin_path = args.at(ai);
|
||||||
|
// TODO: check for dups
|
||||||
|
queued_plugin_paths.push_back(static_cast<std::string>(plugin_path));
|
||||||
|
} else {
|
||||||
|
std::cerr << "TOMATO error: unknown cli arg: '" << args.at(ai) << "'\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Screen* StartScreen::render(float, bool&) {
|
Screen* StartScreen::render(float, bool&) {
|
||||||
@ -143,7 +182,7 @@ Screen* StartScreen::render(float, bool&) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (ImGui::Button("load", {60, 25})) {
|
if (ImGui::Button("load", {60, 25})) {
|
||||||
auto new_screen = std::make_unique<MainScreen>(_renderer, _theme, _tox_profile_path, _password, _user_name, queued_plugin_paths);
|
auto new_screen = std::make_unique<MainScreen>(std::move(_conf), _renderer, _theme, _tox_profile_path, _password, _user_name, queued_plugin_paths);
|
||||||
return new_screen.release();
|
return new_screen.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#include "./chat_gui/theme.hpp"
|
#include "./chat_gui/theme.hpp"
|
||||||
#include "./chat_gui/file_selector.hpp"
|
#include "./chat_gui/file_selector.hpp"
|
||||||
|
|
||||||
|
#include <solanaceae/util/simple_config_model.hpp>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -16,6 +18,7 @@ extern "C" {
|
|||||||
struct StartScreen final : public Screen {
|
struct StartScreen final : public Screen {
|
||||||
SDL_Renderer* _renderer;
|
SDL_Renderer* _renderer;
|
||||||
Theme& _theme;
|
Theme& _theme;
|
||||||
|
SimpleConfigModel _conf;
|
||||||
FileSelector _fss;
|
FileSelector _fss;
|
||||||
|
|
||||||
bool _new_save {false};
|
bool _new_save {false};
|
||||||
@ -28,7 +31,7 @@ struct StartScreen final : public Screen {
|
|||||||
std::vector<std::string> queued_plugin_paths;
|
std::vector<std::string> queued_plugin_paths;
|
||||||
|
|
||||||
StartScreen(void) = delete;
|
StartScreen(void) = delete;
|
||||||
StartScreen(SDL_Renderer* renderer, Theme& theme);
|
StartScreen(const std::vector<std::string_view>& args, SDL_Renderer* renderer, Theme& theme);
|
||||||
~StartScreen(void) = default;
|
~StartScreen(void) = default;
|
||||||
|
|
||||||
// return nullptr if not next
|
// return nullptr if not next
|
||||||
|
Loading…
Reference in New Issue
Block a user