Merge branch 'master' into my_master

This commit is contained in:
Nemirtingas 2019-11-08 15:58:54 +01:00
parent c8288d6bf3
commit a318e04959
7 changed files with 636 additions and 42 deletions

View file

@ -158,17 +158,17 @@ std::string get_lib_path() {
int i = 0;
struct dirent *ep;
dp = opendir (dir.c_str());
unsigned long long int p = (unsigned long long int)&get_lib_path;
uintptr_t p = (uintptr_t)&get_lib_path;
if (dp != NULL)
{
while ((ep = readdir (dp))) {
if (memcmp(ep->d_name, ".", 2) != 0 && memcmp(ep->d_name, "..", 3) != 0) {
char *upper = NULL;
unsigned long long int lower_bound = strtoull(ep->d_name, &upper, 16);
uintptr_t lower_bound = strtoull(ep->d_name, &upper, 16);
if (lower_bound) {
++upper;
unsigned long long int upper_bound = strtoull(upper, &upper, 16);
uintptr_t upper_bound = strtoull(upper, &upper, 16);
if (upper_bound && (lower_bound < p && p < upper_bound)) {
std::string path = dir + PATH_SEPARATOR + ep->d_name;
char link[PATH_MAX] = {};

View file

@ -127,6 +127,21 @@ bool Local_Storage::update_save_filenames(std::string folder)
return true;
}
bool Local_Storage::load_json(std::string full_path, nlohmann::json& json)
{
return false;
}
bool Local_Storage::load_json_file(std::string folder, std::string const&file, nlohmann::json& json)
{
return false;
}
bool Local_Storage::write_json_file(std::string folder, std::string const&file, nlohmann::json const& json)
{
return false;
}
std::vector<std::string> Local_Storage::get_filenames_path(std::string path)
{
return std::vector<std::string>();
@ -680,4 +695,68 @@ bool Local_Storage::update_save_filenames(std::string folder)
return true;
}
bool Local_Storage::load_json(std::string full_path, nlohmann::json& json)
{
std::ifstream inventory_file(full_path);
// If there is a file and we opened it
if (inventory_file)
{
inventory_file.seekg(0, std::ios::end);
size_t size = inventory_file.tellg();
std::string buffer(size, '\0');
inventory_file.seekg(0);
// 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));
PRINT_DEBUG("Loaded json \"%s\". Loaded %u items.\n", full_path.c_str(), json.size());
return true;
} catch (std::exception& e) {
PRINT_DEBUG("Error while parsing \"%s\" json: %s\n", full_path.c_str(), e.what());
}
}
else
{
PRINT_DEBUG("Couldn't open file \"%s\" to read json\n", full_path.c_str());
}
return false;
}
bool Local_Storage::load_json_file(std::string folder, std::string const&file, nlohmann::json& json)
{
if (!folder.empty() && folder.back() != *PATH_SEPARATOR) {
folder.append(PATH_SEPARATOR);
}
std::string inv_path = std::move(save_directory + appid + folder);
std::string full_path = inv_path + file;
return load_json(full_path, json);
}
bool Local_Storage::write_json_file(std::string folder, std::string const&file, nlohmann::json const& json)
{
if (!folder.empty() && folder.back() != *PATH_SEPARATOR) {
folder.append(PATH_SEPARATOR);
}
std::string inv_path = std::move(save_directory + appid + folder);
std::string full_path = inv_path + file;
create_directory(inv_path);
std::ofstream inventory_file(full_path, std::ios::trunc | std::ios::out);
if (inventory_file)
{
inventory_file << std::setw(2) << json;
return true;
}
PRINT_DEBUG("Couldn't open file \"%s\" to write json\n", full_path.c_str());
return false;
}
#endif

View file

@ -59,6 +59,10 @@ public:
std::string get_path(std::string folder);
bool update_save_filenames(std::string folder);
bool load_json(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);
};
#endif

View file

@ -58,8 +58,7 @@ class Steam_Inventory :
std::string items_db_file;
std::once_flag load_items_flag;
bool call_definition_update;
bool definition_update_called;
bool full_update_called;
bool item_definitions_loaded;
struct Steam_Inventory_Requests* new_inventory_result(bool full_query=true, const SteamItemInstanceID_t* pInstanceIDs = NULL, uint32 unCountInstanceIDs = 0)
{
@ -89,6 +88,25 @@ struct Steam_Inventory_Requests *get_inventory_result(SteamInventoryResult_t res
return &(*request);
}
void read_items_db()
{
std::string items_db_path = Local_Storage::get_game_settings_path() + items_user_file;
PRINT_DEBUG("Items file path: %s\n", items_db_path.c_str());
local_storage->load_json(items_db_path, defined_items);
}
void read_inventory_db()
{
// If we havn't got any inventory
if (!local_storage->load_json_file("", items_user_file, user_items))
{
// Try to load a default one
std::string items_db_path = Local_Storage::get_game_settings_path() + items_default_file;
PRINT_DEBUG("Default items file path: %s\n", items_db_path.c_str());
local_storage->load_json(items_db_path, user_items);
}
}
public:
static void run_every_runcb_cb(void *object)
@ -99,7 +117,17 @@ static void run_every_runcb_cb(void *object)
obj->RunCallbacks();
}
Steam_Inventory(class Settings *settings, class SteamCallResults *callback_results, class SteamCallBacks *callbacks, class RunEveryRunCB *run_every_runcb, std::string items_db_file_path)
Steam_Inventory(class Settings *settings, class SteamCallResults *callback_results, class SteamCallBacks *callbacks, class RunEveryRunCB *run_every_runcb, class Local_Storage *local_storage):
settings(settings),
callback_results(callback_results),
callbacks(callbacks),
run_every_runcb(run_every_runcb),
local_storage(local_storage),
defined_items(nlohmann::json::object()),
user_items(nlohmann::json::object()),
inventory_loaded(false),
call_definition_update(false),
item_definitions_loaded(false)
{
items_db_file = items_db_file_path;
PRINT_DEBUG("Items file path: %s\n", items_db_file.c_str());
@ -163,6 +191,7 @@ bool GetResultItems( SteamInventoryResult_t resultHandle,
if (pOutItemsArray != nullptr)
{
SteamItemDetails_t *items_array_base = pOutItemsArray;
uint32 max_items = *punOutItemsArraySize;
if (request->full_query) {
@ -175,22 +204,38 @@ bool GetResultItems( SteamInventoryResult_t resultHandle,
pOutItemsArray->m_unFlags = k_ESteamItemNoTrade;
++pOutItemsArray;
}
*punOutItemsArraySize = std::min(*punOutItemsArraySize, static_cast<uint32>(items.size()));
} else {
for (auto &itemid : request->instance_ids) {
if (!max_items) break;
pOutItemsArray->m_iDefinition = itemid;
pOutItemsArray->m_itemId = itemid;
pOutItemsArray->m_unQuantity = 1;
pOutItemsArray->m_unFlags = k_ESteamItemNoTrade;
++pOutItemsArray;
--max_items;
auto it = user_items.find(std::to_string(itemid));
if (it != user_items.end()) {
pOutItemsArray->m_iDefinition = itemid;
pOutItemsArray->m_itemId = itemid;
try
{
pOutItemsArray->m_unQuantity = it->get<int>();
}
catch (...)
{
pOutItemsArray->m_unQuantity = 0;
}
pOutItemsArray->m_unFlags = k_ESteamItemNoTrade;
++pOutItemsArray;
--max_items;
}
}
}
*punOutItemsArraySize = pOutItemsArray - items_array_base;
}
else if (punOutItemsArraySize != nullptr)
{
*punOutItemsArraySize = items.size();
if (request->full_query) {
*punOutItemsArraySize = user_items.size();
} else {
*punOutItemsArraySize = std::count_if(request->instance_ids.begin(), request->instance_ids.end(), [this](SteamItemInstanceID_t item_id){ return user_items.find(std::to_string(item_id)) != user_items.end();});
}
}
PRINT_DEBUG("GetResultItems good\n");
@ -278,8 +323,6 @@ bool GetAllItems( SteamInventoryResult_t *pResultHandle )
std::lock_guard<std::recursive_mutex> lock(global_mutex);
struct Steam_Inventory_Requests* request = new_inventory_result();
if (!definition_update_called) call_definition_update = true;
if (pResultHandle != nullptr)
*pResultHandle = request->inventory_result;
@ -531,7 +574,7 @@ bool LoadItemDefinitions()
PRINT_DEBUG("LoadItemDefinitions\n");
std::lock_guard<std::recursive_mutex> lock(global_mutex);
if (!definition_update_called) {
if (!item_definitions_loaded) {
call_definition_update = true;
}
@ -619,6 +662,7 @@ bool GetItemDefinitionProperty( SteamItemDef_t iDefinition, const char *pchPrope
{
*punValueBufferSizeOut = 0;
PRINT_DEBUG("Attr %s not found for item %d\n", pchPropertyName, iDefinition);
return false;
}
}
else // Pass a NULL pointer for pchPropertyName to get a comma - separated list of available property names.
@ -654,8 +698,11 @@ bool GetItemDefinitionProperty( SteamItemDef_t iDefinition, const char *pchPrope
}
}
}
return true;
}
return true;
return false;
}
@ -782,35 +829,37 @@ bool SubmitUpdateProperties( SteamInventoryUpdateHandle_t handle, SteamInventory
void RunCallbacks()
{
if (call_definition_update || inventory_requests.size()) {
std::call_once(load_items_flag, [&]() {
std::thread items_load_thread(read_items_db, items_db_file, &items, &items_loaded);
items_load_thread.detach();
});
}
if (!item_definitions_loaded) {
read_items_db();
item_definitions_loaded = true;
if (items_loaded) {
if (call_definition_update) {
//only gets called once
//also gets called when getting items
SteamInventoryDefinitionUpdate_t data = {};
callbacks->addCBResult(data.k_iCallback, &data, sizeof(data));
call_definition_update = false;
definition_update_called = true;
}
call_definition_update = false;
}
if (inventory_requests.size() && !inventory_loaded) {
read_inventory_db();
inventory_loaded = true;
}
if (inventory_loaded)
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
for (auto & r : inventory_requests) {
if (!r.done && std::chrono::duration_cast<std::chrono::duration<double>>(now - r.time_created).count() > r.timeout) {
if (r.full_query) {
if (!full_update_called) {
// SteamInventoryFullUpdate_t callbacks are triggered when GetAllItems
// successfully returns a result which is newer / fresher than the last
// known result.
//TODO: should this always be returned for each get all item calls?
struct SteamInventoryFullUpdate_t data;
data.m_handle = r.inventory_result;
callbacks->addCBResult(data.k_iCallback, &data, sizeof(data));
full_update_called = true;
}
// SteamInventoryFullUpdate_t callbacks are triggered when GetAllItems
// successfully returns a result which is newer / fresher than the last
// known result.
struct SteamInventoryFullUpdate_t data;
data.m_handle = r.inventory_result;
callbacks->addCBResult(data.k_iCallback, &data, sizeof(data));
}
{

View file

@ -31,7 +31,6 @@ unsigned int Steam_User_Stats::find_leaderboard(std::string name)
void Steam_User_Stats::load_achievements_db()
{
std::string file_path = Local_Storage::get_game_settings_path() + achievements_user_file;
local_storage->load_json(file_path, defined_achievements);
}
@ -40,12 +39,12 @@ void Steam_User_Stats::load_achievements()
local_storage->load_json_file("", achievements_user_file, user_achievements);
}
void Steam_User_Stats::save_achievements()
void save_achievements()
{
local_storage->write_json_file("", achievements_user_file, user_achievements);
}
Steam_User_Stats::Steam_User_Stats(Settings* settings, Local_Storage* local_storage, class SteamCallResults* callback_results, class SteamCallBacks* callbacks) :
Steam_User_Stats::Steam_User_Stats(Settings *settings, Local_Storage *local_storage, class SteamCallResults *callback_results, class SteamCallBacks *callbacks):
settings(settings),
local_storage(local_storage),
callback_results(callback_results),
@ -317,7 +316,7 @@ const char* Steam_User_Stats::GetAchievementDisplayAttribute(const char* pchName
std::lock_guard<std::recursive_mutex> lock(global_mutex);
if (strcmp(pchKey, "name") == 0) {
if (strcmp (pchKey, "name") == 0) {
try {
auto it = std::find_if(defined_achievements.begin(), defined_achievements.end(), [pchName](nlohmann::json& item) {
return static_cast<std::string const&>(item["name"]) == pchName;
@ -368,7 +367,7 @@ bool Steam_User_Stats::IndicateAchievementProgress(const char* pchName, uint32 n
try {
auto it = std::find_if(defined_achievements.begin(), defined_achievements.end(), [pchName](nlohmann::json& item) {
return static_cast<std::string const&>(item["name"]) == pchName;
});
});
auto ach = user_achievements.find(pchName);
if (it != defined_achievements.end()) {
bool achieved = false;