2022-07-06 11:48:40 -04:00
|
|
|
#include <STDInclude.hpp>
|
2023-01-03 07:16:44 -05:00
|
|
|
|
|
|
|
#include <json.hpp>
|
|
|
|
|
2022-07-06 11:48:40 -04:00
|
|
|
#include "ScriptStorage.hpp"
|
|
|
|
#include "Script.hpp"
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
std::unordered_map<std::string, std::string> ScriptStorage::Data;
|
|
|
|
|
|
|
|
void ScriptStorage::AddScriptFunctions()
|
|
|
|
{
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("StorageSet", [] // gsc: StorageSet(<str key>, <str data>);
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
const auto* key = Game::Scr_GetString(0);
|
|
|
|
const auto* value = Game::Scr_GetString(1);
|
|
|
|
|
|
|
|
if (key == nullptr || value == nullptr)
|
|
|
|
{
|
|
|
|
Game::Scr_Error("^1StorageSet: Illegal parameters!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data.insert_or_assign(key, value);
|
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("StorageRemove", [] // gsc: StorageRemove(<str key>);
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
const auto* key = Game::Scr_GetString(0);
|
|
|
|
|
|
|
|
if (key == nullptr)
|
|
|
|
{
|
|
|
|
Game::Scr_ParamError(0, "^1StorageRemove: Illegal parameter!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Data.contains(key))
|
|
|
|
{
|
|
|
|
Game::Scr_Error(Utils::String::VA("^1StorageRemove: Store does not have key '%s'!\n", key));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data.erase(key);
|
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("StorageGet", [] // gsc: StorageGet(<str key>);
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
const auto* key = Game::Scr_GetString(0);
|
|
|
|
|
|
|
|
if (key == nullptr)
|
|
|
|
{
|
|
|
|
Game::Scr_ParamError(0, "^1StorageGet: Illegal parameter!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Data.contains(key))
|
|
|
|
{
|
|
|
|
Game::Scr_Error(Utils::String::VA("^1StorageGet: Store does not have key '%s'!\n", key));
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& data = Data.at(key);
|
|
|
|
Game::Scr_AddString(data.data());
|
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("StorageHas", [] // gsc: StorageHas(<str key>);
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
const auto* key = Game::Scr_GetString(0);
|
|
|
|
|
|
|
|
if (key == nullptr)
|
|
|
|
{
|
|
|
|
Game::Scr_ParamError(0, "^1StorageHas: Illegal parameter!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::Scr_AddBool(Data.contains(key));
|
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("StorageDump", [] // gsc: StorageDump();
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
if (Data.empty())
|
|
|
|
{
|
|
|
|
Game::Scr_Error("^1StorageDump: ScriptStorage is empty!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-29 15:54:18 -04:00
|
|
|
const nlohmann::json json = Data;
|
2022-07-06 11:48:40 -04:00
|
|
|
|
|
|
|
FileSystem::FileWriter("scriptdata/scriptstorage.json").write(json.dump());
|
|
|
|
});
|
|
|
|
|
2022-11-25 13:33:53 -05:00
|
|
|
Script::AddFunction("StorageLoad", [] // gsc: StorageLoad();
|
|
|
|
{
|
|
|
|
FileSystem::File storageFile("scriptdata/scriptstorage.json");
|
|
|
|
if (!storageFile.exists())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& buffer = storageFile.getBuffer();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const nlohmann::json storageDef = nlohmann::json::parse(buffer);
|
|
|
|
const auto& newData = storageDef.get<std::unordered_map<std::string, std::string>>();
|
|
|
|
Data.insert(newData.begin(), newData.end());
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Logger::PrintError(Game::CON_CHANNEL_ERROR, "Json Parse Error: {}. File {} is invalid\n", ex.what(), storageFile.getName());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("StorageClear", [] // gsc: StorageClear();
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
Data.clear();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptStorage::ScriptStorage()
|
|
|
|
{
|
|
|
|
AddScriptFunctions();
|
|
|
|
}
|
|
|
|
}
|