2022-07-06 11:48:40 -04:00
|
|
|
#include <STDInclude.hpp>
|
|
|
|
#include "IO.hpp"
|
|
|
|
#include "Script.hpp"
|
|
|
|
|
2023-03-05 08:14:47 -05:00
|
|
|
namespace Components::GSC
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
const char* IO::QueryStrings[] = { R"(..)", R"(../)", R"(..\)" };
|
|
|
|
|
|
|
|
void IO::AddScriptFunctions()
|
|
|
|
{
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("FileWrite", [] // gsc: FileWrite(<filepath>, <string>, <mode>)
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
const auto* path = Game::Scr_GetString(0);
|
|
|
|
auto* text = Game::Scr_GetString(1);
|
|
|
|
auto* mode = Game::Scr_GetString(2);
|
|
|
|
|
2023-02-09 13:53:52 -05:00
|
|
|
if (!path)
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
2023-03-12 07:40:01 -04:00
|
|
|
Game::Scr_ParamError(0, "FileWrite: filepath is not defined!");
|
2022-07-06 11:48:40 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:53:52 -05:00
|
|
|
if (!text || !mode)
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
2023-03-12 07:40:01 -04:00
|
|
|
Game::Scr_Error("FileWrite: Illegal parameters!");
|
2022-07-06 11:48:40 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < ARRAYSIZE(QueryStrings); ++i)
|
|
|
|
{
|
|
|
|
if (std::strstr(path, QueryStrings[i]) != nullptr)
|
|
|
|
{
|
|
|
|
Logger::PrintError(Game::CON_CHANNEL_ERROR, "FileWrite: directory traversal is not allowed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != "append"s && mode != "write"s)
|
|
|
|
{
|
|
|
|
Logger::Warning(Game::CON_CHANNEL_SCRIPT, "FileWrite: mode not defined or was wrong, defaulting to 'write'\n");
|
|
|
|
mode = "write";
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* scriptData = Utils::String::VA("%s/%s", "scriptdata", path);
|
|
|
|
|
|
|
|
if (mode == "write"s)
|
|
|
|
{
|
|
|
|
FileSystem::FileWriter(scriptData).write(text);
|
|
|
|
}
|
|
|
|
else if (mode == "append"s)
|
|
|
|
{
|
|
|
|
FileSystem::FileWriter(scriptData, true).write(text);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("FileRead", [] // gsc: FileRead(<filepath>)
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
const auto* path = Game::Scr_GetString(0);
|
|
|
|
|
2023-02-09 13:53:52 -05:00
|
|
|
if (!path)
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
2023-03-12 07:40:01 -04:00
|
|
|
Game::Scr_ParamError(0, "FileRead: filepath is not defined!");
|
2022-07-06 11:48:40 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < ARRAYSIZE(QueryStrings); ++i)
|
|
|
|
{
|
|
|
|
if (std::strstr(path, QueryStrings[i]) != nullptr)
|
|
|
|
{
|
|
|
|
Logger::PrintError(Game::CON_CHANNEL_ERROR, "FileRead: directory traversal is not allowed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* scriptData = Utils::String::VA("%s/%s", "scriptdata", path);
|
|
|
|
|
2023-02-09 13:53:52 -05:00
|
|
|
const auto file = FileSystem::FileReader(scriptData);
|
|
|
|
if (!file.exists())
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
Logger::PrintError(Game::CON_CHANNEL_ERROR, "FileRead: file '{}' not found!\n", scriptData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:53:52 -05:00
|
|
|
auto buffer = file.getBuffer();
|
|
|
|
buffer = buffer.substr(0, 1024 - 1); // 1024 is the max string size for the SL system
|
|
|
|
Game::Scr_AddString(buffer.data());
|
2022-07-06 11:48:40 -04:00
|
|
|
});
|
|
|
|
|
2022-07-23 17:22:58 -04:00
|
|
|
Script::AddFunction("FileExists", [] // gsc: FileExists(<filepath>)
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
|
|
|
const auto* path = Game::Scr_GetString(0);
|
|
|
|
|
2023-02-09 13:53:52 -05:00
|
|
|
if (!path)
|
2022-07-06 11:48:40 -04:00
|
|
|
{
|
2023-03-12 07:40:01 -04:00
|
|
|
Game::Scr_ParamError(0, "FileExists: filepath is not defined!");
|
2022-07-06 11:48:40 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < ARRAYSIZE(QueryStrings); ++i)
|
|
|
|
{
|
|
|
|
if (std::strstr(path, QueryStrings[i]) != nullptr)
|
|
|
|
{
|
|
|
|
Logger::PrintError(Game::CON_CHANNEL_ERROR, "FileExists: directory traversal is not allowed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* scriptData = Utils::String::VA("%s/%s", "scriptdata", path);
|
|
|
|
Game::Scr_AddBool(FileSystem::FileReader(scriptData).exists());
|
|
|
|
});
|
2022-10-04 17:56:42 -04:00
|
|
|
|
|
|
|
Script::AddFunction("FileRemove", [] // gsc: FileRemove(<filepath>)
|
|
|
|
{
|
|
|
|
const auto* path = Game::Scr_GetString(0);
|
|
|
|
|
2023-02-09 13:53:52 -05:00
|
|
|
if (!path)
|
2022-10-04 17:56:42 -04:00
|
|
|
{
|
2023-03-12 07:40:01 -04:00
|
|
|
Game::Scr_ParamError(0, "FileRemove: filepath is not defined!");
|
2022-10-04 17:56:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < ARRAYSIZE(QueryStrings); ++i)
|
|
|
|
{
|
|
|
|
if (std::strstr(path, QueryStrings[i]) != nullptr)
|
|
|
|
{
|
2023-03-12 07:40:01 -04:00
|
|
|
Logger::Print("FileRemove: directory traversal is not allowed!\n");
|
2022-10-04 17:56:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 14:20:07 -05:00
|
|
|
const auto p = "scriptdata"s / std::filesystem::path(path);
|
2022-10-04 17:56:42 -04:00
|
|
|
const auto folder = p.parent_path().string();
|
|
|
|
const auto file = p.filename().string();
|
|
|
|
Game::Scr_AddInt(FileSystem::_DeleteFile(folder, file));
|
|
|
|
});
|
2022-07-06 11:48:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
IO::IO()
|
|
|
|
{
|
|
|
|
AddScriptFunctions();
|
|
|
|
}
|
|
|
|
}
|