From 7f940079d1c0eca12faa3affb3d37abcf327b1b2 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Wed, 3 Jul 2024 01:22:00 +0300 Subject: [PATCH] * 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 --- dll/dll/common_includes.h | 1 + dll/dll/local_storage.h | 2 +- dll/dll/steam_client.h | 2 +- dll/local_storage.cpp | 19 +++++-------------- dll/steam_gamestats.cpp | 18 +++++++++--------- helpers/dbg_log.cpp | 10 +++++----- helpers/dbg_log/dbg_log.hpp | 5 +++-- 7 files changed, 25 insertions(+), 32 deletions(-) diff --git a/dll/dll/common_includes.h b/dll/dll/common_includes.h index 621f101d..89939fa5 100644 --- a/dll/dll/common_includes.h +++ b/dll/dll/common_includes.h @@ -57,6 +57,7 @@ #include #include #include +#include // common includes #include "common_helpers/common_helpers.hpp" diff --git a/dll/dll/local_storage.h b/dll/dll/local_storage.h index a61aa83e..c692ead9 100644 --- a/dll/dll/local_storage.h +++ b/dll/dll/local_storage.h @@ -91,7 +91,7 @@ public: 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 write_json_file(std::string folder, std::string const& file, nlohmann::json const& json); diff --git a/dll/dll/steam_client.h b/dll/dll/steam_client.h index 36d84a7b..af958afe 100644 --- a/dll/dll/steam_client.h +++ b/dll/dll/steam_client.h @@ -238,7 +238,7 @@ public: // game stats ISteamGameStats *GetISteamGameStats( HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char *pchVersion ); - // game timeline + // steam timeline ISteamTimeline *GetISteamTimeline( HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char *pchVersion ); diff --git a/dll/local_storage.cpp b/dll/local_storage.cpp index 42754300..09ed210b 100644 --- a/dll/local_storage.cpp +++ b/dll/local_storage.cpp @@ -175,7 +175,7 @@ bool Local_Storage::update_save_filenames(std::string folder) 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; } @@ -801,25 +801,16 @@ bool Local_Storage::update_save_filenames(std::string folder) 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 (inventory_file) { - inventory_file.seekg(0, std::ios::end); - size_t size = static_cast(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 { - 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()); 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()); } } else { diff --git a/dll/steam_gamestats.cpp b/dll/steam_gamestats.cpp index e7b576a5..4ee3b674 100644 --- a/dll/steam_gamestats.cpp +++ b/dll/steam_gamestats.cpp @@ -143,7 +143,7 @@ Steam_GameStats::Attribute_t *Steam_GameStats::get_or_create_row_att(uint64 ulRo { Attribute_t *att{}; { - auto &row = table.rows[ulRowID]; + auto &row = table.rows[static_cast(ulRowID)]; auto att_itr = row.attributes.find(att_name); if (att_itr != row.attributes.end()) { att = &att_itr->second; @@ -211,7 +211,7 @@ SteamAPICall_t Steam_GameStats::EndSession( uint64 ulSessionID, RTime32 rtTimeEn return ret; } - auto &session = sessions[ulSessionID - 1]; + auto& session = sessions[static_cast(ulSessionID - 1)]; if (session.ended) { GameStatsSessionClosed_t data_invalid{}; 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? - auto &session = sessions[ulSessionID - 1]; + auto& session = sessions[static_cast(ulSessionID - 1)]; if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? 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? - auto &session = sessions[ulSessionID - 1]; + auto& session = sessions[static_cast(ulSessionID - 1)]; if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? 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? - auto &session = sessions[ulSessionID - 1]; + auto& session = sessions[static_cast(ulSessionID - 1)]; if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? 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? - auto &session = sessions[ulSessionID - 1]; + auto& session = sessions[static_cast(ulSessionID - 1)]; if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? 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? // TODO what if it was already committed ? - auto &row = table.rows[ulRowID]; + auto& row = table.rows[static_cast(ulRowID)]; row.committed = true; 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? - auto &session = sessions[ulSessionID - 1]; + auto& session = sessions[static_cast(ulSessionID - 1)]; if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? 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? - auto &session = sessions[ulSessionID - 1]; + auto& session = sessions[static_cast(ulSessionID - 1)]; if (session.ended) return EResult::k_EResultExpired; // TODO is this correct? auto att = get_or_create_session_att(pstrName, session, AttributeType_t::Int64); diff --git a/helpers/dbg_log.cpp b/helpers/dbg_log.cpp index 7ae1c140..732eaf32 100644 --- a/helpers/dbg_log.cpp +++ b/helpers/dbg_log.cpp @@ -21,7 +21,7 @@ void dbg_log::open() // https://en.cppreference.com/w/cpp/filesystem/path/u8path const auto fsp = std::filesystem::u8path(filepath); #if defined(__WINDOWS__) - out_file = _wfopen(fsp.c_str(), L"a"); + out_file = _wfopen(fsp.c_str(), L"at"); #else out_file = std::fopen(fsp.c_str(), "a"); #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 - write(str.data()); + write("%s", str.c_str()); #endif } -void dbg_log::write(std::wstring_view str) +void dbg_log::write(const std::wstring &str) { #ifndef EMU_RELEASE_BUILD - write(str.data()); + write("%s", str.c_str()); #endif } diff --git a/helpers/dbg_log/dbg_log.hpp b/helpers/dbg_log/dbg_log.hpp index c0a4ea46..8d984f7e 100644 --- a/helpers/dbg_log/dbg_log.hpp +++ b/helpers/dbg_log/dbg_log.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -22,8 +23,8 @@ public: dbg_log(std::wstring_view path); ~dbg_log(); - void write(std::string_view str); - void write(std::wstring_view str); + void write(const std::string &str); + void write(const std::wstring &str); void write(const char* fmt, ...); void write(const wchar_t* fmt, ...);