[ScriptStorage]: Allow users to load the storage file

This commit is contained in:
FutureRave
2022-11-25 18:33:53 +00:00
parent 10333e0076
commit 4a677c5d08
7 changed files with 69 additions and 22 deletions

View File

@ -103,14 +103,14 @@ namespace Assets
return;
}
nlohmann::json fontDef = nlohmann::json::parse(fontDefFile.getBuffer());
nlohmann::json fontDef;
try
{
fontDef = nlohmann::json::parse(fontDefFile.getBuffer());
}
catch (const nlohmann::json::parse_error& ex)
{
Components::Logger::Error(Game::ERR_FATAL, "Json Parse Error: {}. Font {} is invalid", ex.what(), name);
Components::Logger::Error(Game::ERR_FATAL, "Json Parse Error: {}. Font {} is invalid\n", ex.what(), name);
return;
}

View File

@ -37,9 +37,9 @@ namespace Components
{
const auto ent = &Game::g_entities[clientNum];
if (ent->client == nullptr)
if (!ent->client)
{
Logger::Debug("ClientCommand: client {} is not fully in game yet", clientNum);
Logger::Debug("ClientCommand: client {} is not fully connected", clientNum);
return;
}
@ -129,7 +129,7 @@ namespace Components
Add("setviewpos", [](Game::gentity_s* ent, [[maybe_unused]] const Command::ServerParams* params)
{
assert(ent != nullptr);
assert(ent);
if (!CheatsOk(ent))
return;
@ -245,7 +245,7 @@ namespace Components
Add("kill", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] const Command::ServerParams* params)
{
assert(ent->client != nullptr);
assert(ent->client);
assert(ent->client->sess.connected != Game::CON_DISCONNECTED);
if (ent->client->sess.sessionState != Game::SESS_STATE_PLAYING || !CheatsOk(ent))
@ -299,7 +299,7 @@ namespace Components
duration = static_cast<int>(std::floorf(input * 1000.0f + 0.5f));
}
assert(ent->client != nullptr);
assert(ent->client);
constexpr auto visMode = Game::visionSetMode_t::VISIONSET_NORMAL;
const auto* name = params->get(1);
@ -327,7 +327,7 @@ namespace Components
duration = static_cast<int>(std::floorf(input * 1000.0f + 0.5f));
}
assert(ent->client != nullptr);
assert(ent->client);
constexpr auto visMode = Game::visionSetMode_t::VISIONSET_NIGHT;
const auto* name = params->get(1);
@ -342,7 +342,7 @@ namespace Components
Add("g_testCmd", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] const Command::ServerParams* params)
{
assert(ent != nullptr);
assert(ent);
ent->client->ps.stunTime = 1000 + Game::level->time; // 1000 is the default test stun time
Logger::Debug("playerState_s.stunTime is {}", ent->client->ps.stunTime);
@ -464,9 +464,11 @@ namespace Components
const auto* line = EntInfoLine(i);
const auto lineLen = std::strlen(line);
assert(line);
assert(lineLen);
Game::FS_Write(line, lineLen, h);
Game::FS_Write(line, static_cast<int>(lineLen), h);
}
Game::FS_FCloseFile(h);

View File

@ -86,6 +86,27 @@ namespace Components
FileSystem::FileWriter("scriptdata/scriptstorage.json").write(json.dump());
});
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());
}
});
Script::AddFunction("StorageClear", [] // gsc: StorageClear();
{
Data.clear();

View File

@ -33,12 +33,14 @@ namespace Components
template <typename... Args>
static void Print(std::string_view fmt, Args&&... args)
{
(Utils::String::SanitizeFormatArgs(args), ...);
PrintInternal(Game::CON_CHANNEL_DONT_FILTER, fmt, std::make_format_args(args...));
}
template <typename... Args>
static void Print(int channel, std::string_view fmt, Args&&... args)
{
(Utils::String::SanitizeFormatArgs(args), ...);
PrintInternal(channel, fmt, std::make_format_args(args...));
}
@ -50,6 +52,7 @@ namespace Components
template <typename... Args>
static void Error(Game::errorParm_t error, std::string_view fmt, Args&&... args)
{
(Utils::String::SanitizeFormatArgs(args), ...);
ErrorInternal(error, fmt, std::make_format_args(args...));
}
@ -61,6 +64,7 @@ namespace Components
template <typename... Args>
static void Warning(int channel, std::string_view fmt, Args&&... args)
{
(Utils::String::SanitizeFormatArgs(args), ...);
WarningInternal(channel, fmt, std::make_format_args(args...));
}
@ -72,6 +76,7 @@ namespace Components
template <typename... Args>
static void PrintError(int channel, std::string_view fmt, Args&&... args)
{
(Utils::String::SanitizeFormatArgs(args), ...);
PrintErrorInternal(channel, fmt, std::make_format_args(args...));
}
@ -82,6 +87,7 @@ namespace Components
Debug([[maybe_unused]] std::string_view fmt, [[maybe_unused]] const Args&... args, [[maybe_unused]] const std::source_location& loc = std::source_location::current())
{
#ifdef _DEBUG
(Utils::String::SanitizeFormatArgs(args), ...);
DebugInternal(fmt, std::make_format_args(args...), loc);
#endif
}

View File

@ -339,7 +339,7 @@ namespace Components
}
catch (const std::exception& ex)
{
Logger::PrintError(Game::CON_CHANNEL_ERROR, "{}: parsing of 'normal' failed", ex.what());
Logger::PrintError(Game::CON_CHANNEL_ERROR, "{}: parsing of 'normal' failed\n", ex.what());
return false;
}