* update the method of reading json to be less wasteful + get path as const ref whe loading json

* fix vs warnings in gamestats interface
* refactor logger slightly
This commit is contained in:
otavepto 2024-07-03 01:22:00 +03:00
parent 3fdb45e43b
commit 7f940079d1
7 changed files with 25 additions and 32 deletions

View File

@ -57,6 +57,7 @@
#include <stdio.h> #include <stdio.h>
#include <filesystem> #include <filesystem>
#include <optional> #include <optional>
#include <numeric>
// common includes // common includes
#include "common_helpers/common_helpers.hpp" #include "common_helpers/common_helpers.hpp"

View File

@ -91,7 +91,7 @@ public:
bool update_save_filenames(std::string folder); bool update_save_filenames(std::string folder);
bool load_json(std::string full_path, nlohmann::json& json); bool load_json(const std::string &full_path, nlohmann::json& json);
bool load_json_file(std::string folder, std::string const& file, nlohmann::json& json); bool load_json_file(std::string folder, std::string const& file, nlohmann::json& json);
bool write_json_file(std::string folder, std::string const& file, nlohmann::json const& json); bool write_json_file(std::string folder, std::string const& file, nlohmann::json const& json);

View File

@ -238,7 +238,7 @@ public:
// game stats // game stats
ISteamGameStats *GetISteamGameStats( HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char *pchVersion ); ISteamGameStats *GetISteamGameStats( HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char *pchVersion );
// game timeline // steam timeline
ISteamTimeline *GetISteamTimeline( HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char *pchVersion ); ISteamTimeline *GetISteamTimeline( HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char *pchVersion );

View File

@ -175,7 +175,7 @@ bool Local_Storage::update_save_filenames(std::string folder)
return true; return true;
} }
bool Local_Storage::load_json(std::string full_path, nlohmann::json& json) bool Local_Storage::load_json(const std::string &full_path, nlohmann::json& json)
{ {
return false; return false;
} }
@ -801,25 +801,16 @@ bool Local_Storage::update_save_filenames(std::string folder)
return true; return true;
} }
bool Local_Storage::load_json(std::string full_path, nlohmann::json& json) bool Local_Storage::load_json(const std::string &full_path, nlohmann::json& json)
{ {
std::ifstream inventory_file(std::filesystem::u8path(full_path), std::ios::binary | std::ios::in); std::ifstream inventory_file(std::filesystem::u8path(full_path), std::ios::in | std::ios::binary);
// If there is a file and we opened it // If there is a file and we opened it
if (inventory_file) { if (inventory_file) {
inventory_file.seekg(0, std::ios::end);
size_t size = static_cast<size_t>(inventory_file.tellg());
std::string buffer(size, '\0');
inventory_file.seekg(0, std::ios::beg);
// Read it entirely, if the .json file gets too big,
// I should look into this and split reads into smaller parts.
inventory_file.read(&buffer[0], size);
inventory_file.close();
try { try {
json = std::move(nlohmann::json::parse(buffer)); json = nlohmann::json::parse(inventory_file);
PRINT_DEBUG("Loaded json '%s' (%zu items)", full_path.c_str(), json.size()); PRINT_DEBUG("Loaded json '%s' (%zu items)", full_path.c_str(), json.size());
return true; return true;
} catch (std::exception& e) { } catch (const std::exception& e) {
PRINT_DEBUG("Error while parsing '%s' json error: %s", full_path.c_str(), e.what()); PRINT_DEBUG("Error while parsing '%s' json error: %s", full_path.c_str(), e.what());
} }
} else { } else {

View File

@ -143,7 +143,7 @@ Steam_GameStats::Attribute_t *Steam_GameStats::get_or_create_row_att(uint64 ulRo
{ {
Attribute_t *att{}; Attribute_t *att{};
{ {
auto &row = table.rows[ulRowID]; auto &row = table.rows[static_cast<unsigned>(ulRowID)];
auto att_itr = row.attributes.find(att_name); auto att_itr = row.attributes.find(att_name);
if (att_itr != row.attributes.end()) { if (att_itr != row.attributes.end()) {
att = &att_itr->second; att = &att_itr->second;
@ -211,7 +211,7 @@ SteamAPICall_t Steam_GameStats::EndSession( uint64 ulSessionID, RTime32 rtTimeEn
return ret; return ret;
} }
auto &session = sessions[ulSessionID - 1]; auto& session = sessions[static_cast<unsigned>(ulSessionID - 1)];
if (session.ended) { if (session.ended) {
GameStatsSessionClosed_t data_invalid{}; GameStatsSessionClosed_t data_invalid{};
data_invalid.m_eResult = EResult::k_EResultExpired; // TODO is this correct? data_invalid.m_eResult = EResult::k_EResultExpired; // TODO is this correct?
@ -244,7 +244,7 @@ EResult Steam_GameStats::AddSessionAttributeInt( uint64 ulSessionID, const char*
if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrName) return EResult::k_EResultInvalidParam; // TODO is this correct? if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrName) return EResult::k_EResultInvalidParam; // TODO is this correct?
auto &session = sessions[ulSessionID - 1]; auto& session = sessions[static_cast<unsigned>(ulSessionID - 1)];
if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? if (session.ended) return EResult::k_EResultExpired; // TODO is this correct?
auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Int); auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Int);
@ -261,7 +261,7 @@ EResult Steam_GameStats::AddSessionAttributeString( uint64 ulSessionID, const ch
if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrName || !pstrData) return EResult::k_EResultInvalidParam; // TODO is this correct? if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrName || !pstrData) return EResult::k_EResultInvalidParam; // TODO is this correct?
auto &session = sessions[ulSessionID - 1]; auto& session = sessions[static_cast<unsigned>(ulSessionID - 1)];
if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? if (session.ended) return EResult::k_EResultExpired; // TODO is this correct?
auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Str); auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Str);
@ -278,7 +278,7 @@ EResult Steam_GameStats::AddSessionAttributeFloat( uint64 ulSessionID, const cha
if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrName) return EResult::k_EResultInvalidParam; // TODO is this correct? if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrName) return EResult::k_EResultInvalidParam; // TODO is this correct?
auto &session = sessions[ulSessionID - 1]; auto& session = sessions[static_cast<unsigned>(ulSessionID - 1)];
if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? if (session.ended) return EResult::k_EResultExpired; // TODO is this correct?
auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Float); auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Float);
@ -296,7 +296,7 @@ EResult Steam_GameStats::AddNewRow( uint64 *pulRowID, uint64 ulSessionID, const
if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrTableName) return EResult::k_EResultInvalidParam; // TODO is this correct? if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrTableName) return EResult::k_EResultInvalidParam; // TODO is this correct?
auto &session = sessions[ulSessionID - 1]; auto& session = sessions[static_cast<unsigned>(ulSessionID - 1)];
if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? if (session.ended) return EResult::k_EResultExpired; // TODO is this correct?
auto table = get_or_create_session_table(session, pstrTableName); auto table = get_or_create_session_table(session, pstrTableName);
@ -318,7 +318,7 @@ EResult Steam_GameStats::CommitRow( uint64 ulRowID )
if (ulRowID >= table.rows.size()) return EResult::k_EResultInvalidParam; // TODO is this correct? if (ulRowID >= table.rows.size()) return EResult::k_EResultInvalidParam; // TODO is this correct?
// TODO what if it was already committed ? // TODO what if it was already committed ?
auto &row = table.rows[ulRowID]; auto& row = table.rows[static_cast<unsigned>(ulRowID)];
row.committed = true; row.committed = true;
return EResult::k_EResultOK; return EResult::k_EResultOK;
@ -331,7 +331,7 @@ EResult Steam_GameStats::CommitOutstandingRows( uint64 ulSessionID )
if (ulSessionID == 0 || ulSessionID > sessions.size()) return EResult::k_EResultInvalidParam; // TODO is this correct? if (ulSessionID == 0 || ulSessionID > sessions.size()) return EResult::k_EResultInvalidParam; // TODO is this correct?
auto &session = sessions[ulSessionID - 1]; auto& session = sessions[static_cast<unsigned>(ulSessionID - 1)];
if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? if (session.ended) return EResult::k_EResultExpired; // TODO is this correct?
if (session.tables.size()) { if (session.tables.size()) {
@ -411,7 +411,7 @@ EResult Steam_GameStats::AddSessionAttributeInt64( uint64 ulSessionID, const cha
if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrName) return EResult::k_EResultInvalidParam; // TODO is this correct? if (ulSessionID == 0 || ulSessionID > sessions.size() || !pstrName) return EResult::k_EResultInvalidParam; // TODO is this correct?
auto &session = sessions[ulSessionID - 1]; auto& session = sessions[static_cast<unsigned>(ulSessionID - 1)];
if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? if (session.ended) return EResult::k_EResultExpired; // TODO is this correct?
auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Int64); auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Int64);

View File

@ -21,7 +21,7 @@ void dbg_log::open()
// https://en.cppreference.com/w/cpp/filesystem/path/u8path // https://en.cppreference.com/w/cpp/filesystem/path/u8path
const auto fsp = std::filesystem::u8path(filepath); const auto fsp = std::filesystem::u8path(filepath);
#if defined(__WINDOWS__) #if defined(__WINDOWS__)
out_file = _wfopen(fsp.c_str(), L"a"); out_file = _wfopen(fsp.c_str(), L"at");
#else #else
out_file = std::fopen(fsp.c_str(), "a"); out_file = std::fopen(fsp.c_str(), "a");
#endif #endif
@ -72,20 +72,20 @@ dbg_log::~dbg_log()
} }
void dbg_log::write(std::string_view str) void dbg_log::write(const std::string &str)
{ {
#ifndef EMU_RELEASE_BUILD #ifndef EMU_RELEASE_BUILD
write(str.data()); write("%s", str.c_str());
#endif #endif
} }
void dbg_log::write(std::wstring_view str) void dbg_log::write(const std::wstring &str)
{ {
#ifndef EMU_RELEASE_BUILD #ifndef EMU_RELEASE_BUILD
write(str.data()); write("%s", str.c_str());
#endif #endif
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <string>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <mutex> #include <mutex>
@ -22,8 +23,8 @@ public:
dbg_log(std::wstring_view path); dbg_log(std::wstring_view path);
~dbg_log(); ~dbg_log();
void write(std::string_view str); void write(const std::string &str);
void write(std::wstring_view str); void write(const std::wstring &str);
void write(const char* fmt, ...); void write(const char* fmt, ...);
void write(const wchar_t* fmt, ...); void write(const wchar_t* fmt, ...);