Skip to content

Commit

Permalink
Adds persistent storage
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantJL committed Dec 19, 2023
1 parent b95d4c4 commit 034251a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 17 deletions.
88 changes: 79 additions & 9 deletions lobby-ranks/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "Config.h"

#include <fstream>

#include <json/reader.h>

#include "types.h"
#include "util.h"
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"

#include <json/reader.h>

using namespace jlg;

Json::Reader reader;
Expand All @@ -22,13 +24,20 @@ Config* Config::instance()
return c;
}

void Config::initialize( const std::shared_ptr<CVarManagerWrapper>& cvm )
void Config::initialize( const std::shared_ptr<CVarManagerWrapper>& cvm, const std::filesystem::path& cf )
{
if( c == nullptr )
c = new Config( cvm );
c = new Config( cvm, cf );
}

namespace {
std::unique_ptr<Json::StreamWriter> writer = [](){
Json::StreamWriterBuilder swb;
swb["commentStyle"] = "None";
swb["dropNullPlaceholders"] = true;
return std::unique_ptr<Json::StreamWriter>( swb.newStreamWriter() );
}();

auto genDefaults = []( const std::shared_ptr<CVarManagerWrapper>& cvm ){
using Value = Config::Value;
std::map<std::string, Value> map;
Expand Down Expand Up @@ -72,6 +81,7 @@ auto genDefaults = []( const std::shared_ptr<CVarManagerWrapper>& cvm ){

#pragma region Gui

static bool configChanged = false;
bool gui::reset( const char* name )
{
auto label = (strlen(name) ? std::string("Reset ").append(name) : "Reset" );
Expand All @@ -83,11 +93,15 @@ bool gui::reset( const char* name )

bool gui::drawBool( const char* name, bool& v )
{
return ImGui::Checkbox( name, &v );
bool c = ImGui::Checkbox( name, &v );
configChanged |= c;
return c;
}
bool gui::drawFloat( const char* name, float& v, float min, float max )
{
return ImGui::SliderFloat( name, &v, min, max );
bool c = ImGui::SliderFloat( name, &v, min, max );
configChanged |= c;
return c;
}
bool gui::drawColour( const char* name, LinearColor& v, LinearColor defaultV )
{
Expand All @@ -109,6 +123,7 @@ bool gui::drawColour( const char* name, LinearColor& v, LinearColor defaultV )
if( changed ) v = LinearColor(c.x*255.f, c.y*255.f, c.z*255.f, c.w*255.f);
if( reset ) v = defaultV;

configChanged |= changed || reset;
return changed || reset;
}

Expand Down Expand Up @@ -252,20 +267,24 @@ std::list<Playlist> Config::getPlaylists()
// ----------------------------------------------------------
// CONSTRUCTORS
// ----------------------------------------------------------
Config::Config( const std::shared_ptr<CVarManagerWrapper>& cvm )
Config::Config( const std::shared_ptr<CVarManagerWrapper>& cvm, const std::filesystem::path& configFile )
: configMap( genDefaults(cvm) )
, configFile( configFile )
, showExampleTable( false )
, refreshExampleTable( true )
{}

Config::~Config()
{}
{
save();
}

// ----------------------------------------------------------
// INSTANCE METHODS
// ----------------------------------------------------------
void Config::drawImGuiOptions()
{
configChanged = false;
bool enabled = isEnabled();
if( gui::drawBool("Enabled", enabled) )
configMap.at("enabled").set( enabled );
Expand Down Expand Up @@ -302,6 +321,9 @@ void Config::drawImGuiOptions()
}

drawPlaylistSelection();

if( configChanged )
save();
}

void Config::drawTablePosOptions()
Expand All @@ -310,9 +332,15 @@ void Config::drawTablePosOptions()
auto anchorY = configMap.at( "table_anchor_y" ).get<int32_t>();

if( ImGui::Combo("Table Anchor X", &anchorX, "Left\0Middle\0Right\0\0") )
{
configChanged = true;
configMap.at( "table_anchor_x" ).set( anchorX );
}
if( ImGui::Combo("Table Anchor Y", &anchorY, "Top\0Middle\0Bottom\0\0") )
{
configChanged = true;
configMap.at( "table_anchor_y" ).set( anchorY );
}

auto [posX, posY] = getTablePosition();
if( gui::drawFloat("Table Position X", posX, 0.0, 1.0) )
Expand Down Expand Up @@ -394,6 +422,7 @@ void Config::drawPlaylistSelection()

if( ImGui::Button("Reset Playlists") )
{
configChanged = true;
configValue.set( configValue.getDefault<Value::IntArray>() );
ImGui::PopID();
return;
Expand Down Expand Up @@ -432,7 +461,8 @@ void Config::drawPlaylistSelection()
ImGuiButtonFlags dnbutton = (i==0 || !p.enabled ? ImGuiDir_None : ImGuiDir_Up );
ImGui::Bullet();
ImGui::SameLine();
ImGui::Checkbox( "##Checkbox", &p.enabled );
if( ImGui::Checkbox("##Checkbox", &p.enabled) )
configChanged = true;
if( p.enabled )
{
ImGui::SameLine();
Expand All @@ -447,12 +477,14 @@ void Config::drawPlaylistSelection()
p.index++;
playlistItems.at(i+1).index--;
}
configChanged = true;
}
ImGui::SameLine();
if( ImGui::ArrowButtonEx("##dn", dnbutton, sz, dnflags) )
{
p.index--;
playlistItems.at(i-1).index++;
configChanged = true;
}
ImGui::SameLine();
ImGui::Text( p.name.c_str() );
Expand Down Expand Up @@ -480,6 +512,44 @@ void Config::drawPlaylistSelection()
configValue.set( newPlaylists );
}

bool Config::load()
{
return load( configFile );
}
void Config::save()
{
save( configFile );
}

bool Config::load( const std::filesystem::path& file )
{
using namespace std::filesystem;
if( !exists(file) || !is_regular_file(file) )
return false;

Json::Value json;
std::ifstream ifile( file );
if( !reader.parse(ifile, json, false) )
return false;

for( const auto& name : json.getMemberNames() )
if( configMap.find(name) != configMap.end() )
configMap.at(name).load( json[name] );
return true;
}

void Config::save( const std::filesystem::path& file )
{
Json::Value json;
for( const auto& entry : configMap )
json[entry.first] = entry.second.json();

std::filesystem::create_directories( file.parent_path() );

std::ofstream ifile( file );
writer->write( json, &ifile );
}

void Config::loadFromCfg()
{
for( auto& [k, v] : configMap )
Expand Down
13 changes: 10 additions & 3 deletions lobby-ranks/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <vector>
#include <list>
#include <filesystem>

#include <json/value.h>
#include <json/writer.h>
Expand Down Expand Up @@ -49,8 +50,10 @@ class Config
Json::Value val;

public:
void load( const Json::Value& v );
void loadFromCfg();
void saveToCfg();
Json::Value json() const;
std::string asString();

template<class T>
Expand Down Expand Up @@ -86,16 +89,20 @@ class Config
//---------------------STATIC METHODS-----------------------
public:
static Config* instance();
static void initialize( const std::shared_ptr<CVarManagerWrapper>& );
static void initialize( const std::shared_ptr<CVarManagerWrapper>&, const std::filesystem::path& );

//----------------------CONSTRUCTORS------------------------
public:
Config() = delete;
Config( const std::shared_ptr<CVarManagerWrapper>& cvm );
Config( const std::shared_ptr<CVarManagerWrapper>& cvm, const std::filesystem::path& configFile );
virtual ~Config();

//--------------------INSTANCE METHODS----------------------
public:
bool load();
bool load( const std::filesystem::path& file );
void save();
void save( const std::filesystem::path& file );
void loadFromCfg();
std::string debug();

Expand Down Expand Up @@ -140,8 +147,8 @@ class Config

//-------------------INSTANCE VARIABLES---------------------
private:
std::filesystem::path configFile;
std::map<std::string, Value> configMap;
Json::Value json;
bool showExampleTable;
bool refreshExampleTable;
};
Expand Down
13 changes: 12 additions & 1 deletion lobby-ranks/ConfigValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ Json::Value Config::Value::to<Config::Value::IntArray>( Config::Value::IntArray
return a;
}

void Config::Value::load( const Json::Value& v )
{
val = v;
saveToCfg();
}

void Config::Value::loadFromCfg()
{
static Json::Reader reader;
Expand All @@ -106,7 +112,12 @@ void Config::Value::saveToCfg()
{
if( !cvm ) return;
if( auto cvar = cvm->getCvar(cname) )
cvar.setValue( val.asString() );
cvar.setValue( asString() );
}

Json::Value Config::Value::json() const
{
return val;
}

std::string Config::Value::asString()
Expand Down
9 changes: 6 additions & 3 deletions lobby-ranks/lobby-ranks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <array>
#include <list>
#include <numeric>
#include <fstream>

#define LR_CMD_PREFIX "jlg_lobby_rank_"
#define LR_CVAR_PREFIX LR_CMD_PREFIX "v_"
Expand Down Expand Up @@ -90,7 +91,10 @@ const char* LobbyRanks::Command::debug = "jlg_debug";

void LobbyRanks::onLoad()
{
Config::initialize( cvarManager );
Config::initialize(
cvarManager,
gameWrapper->GetBakkesModPath() / "jlg_lobby_ranks" / "config.json"
);
config = Config::instance();

// Regular Variables
Expand Down Expand Up @@ -182,6 +186,7 @@ void LobbyRanks::refresh()
void LobbyRanks::sync()
{
config->loadFromCfg();
config->load();

// JLG TODO Sync other cvars
}
Expand Down Expand Up @@ -278,8 +283,6 @@ void LobbyRanks::resizeTable( CanvasWrapper c, Table& t )

void LobbyRanks::onUnload()
{
cvarManager->backupCfg( "back_jlg_cfg");

Config* c = Config::instance();
delete c;
c = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion lobby-ranks/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define VERSION_MAJOR 1
#define VERSION_MINOR 2
#define VERSION_PATCH 0
#define VERSION_BUILD 37
#define VERSION_BUILD 61

#define VERSION_FULL stringify(VERSION_MAJOR) "." stringify(VERSION_MINOR) "." stringify(VERSION_PATCH) "." stringify(VERSION_BUILD)

Expand Down

0 comments on commit 034251a

Please sign in to comment.