2020-07-22 23:36:52 -04:00
|
|
|
#include "STDInclude.hpp"
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
static const char* queryStrings[] = { R"(..)", R"(../)", R"(..\)" };
|
|
|
|
|
2022-01-23 14:32:20 -05:00
|
|
|
void ScriptExtension::AddFunctions()
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
|
|
|
//File functions
|
|
|
|
|
2022-01-23 14:32:20 -05:00
|
|
|
Script::AddFunction("FileWrite", [](Game::scr_entref_t) // gsc: FileWrite(<filepath>, <string>, <mode>)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
const auto* path = Game::Scr_GetString(0);
|
2022-01-23 14:32:20 -05:00
|
|
|
auto* text = Game::Scr_GetString(1);
|
|
|
|
auto* mode = Game::Scr_GetString(2);
|
2020-07-22 23:36:52 -04:00
|
|
|
|
2022-01-26 17:02:48 -05:00
|
|
|
if (path == nullptr)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
Game::Scr_ParamError(0, "^1FileWrite: filepath is not defined!\n");
|
2022-01-23 14:32:20 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text == nullptr || mode == nullptr)
|
|
|
|
{
|
|
|
|
Game::Scr_Error("^1FileWrite: Illegal parameters!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:02:48 -05:00
|
|
|
for (auto i = 0u; i < ARRAYSIZE(queryStrings); ++i)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
if (std::strstr(path, queryStrings[i]) != nullptr)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-23 14:32:20 -05:00
|
|
|
Logger::Print("^1FileWrite: directory traversal is not allowed!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != "append"s && mode != "write"s)
|
|
|
|
{
|
2022-01-23 14:32:20 -05:00
|
|
|
Logger::Print("^3FileWrite: mode not defined or was wrong, defaulting to 'write'\n");
|
2022-01-07 16:00:44 -05:00
|
|
|
mode = "write";
|
2020-07-22 23:36:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == "write"s)
|
|
|
|
{
|
2020-09-02 06:14:38 -04:00
|
|
|
FileSystem::FileWriter(path).write(text);
|
2020-07-22 23:36:52 -04:00
|
|
|
}
|
|
|
|
else if (mode == "append"s)
|
|
|
|
{
|
2020-09-02 06:14:38 -04:00
|
|
|
FileSystem::FileWriter(path, true).write(text);
|
2020-07-22 23:36:52 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-01-23 14:32:20 -05:00
|
|
|
Script::AddFunction("FileRead", [](Game::scr_entref_t) // gsc: FileRead(<filepath>)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
const auto* path = Game::Scr_GetString(0);
|
2020-07-22 23:36:52 -04:00
|
|
|
|
2022-01-26 17:02:48 -05:00
|
|
|
if (path == nullptr)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
Game::Scr_ParamError(0, "^1FileRead: filepath is not defined!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:02:48 -05:00
|
|
|
for (auto i = 0u; i < ARRAYSIZE(queryStrings); ++i)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
if (std::strstr(path, queryStrings[i]) != nullptr)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-23 14:32:20 -05:00
|
|
|
Logger::Print("^1FileRead: directory traversal is not allowed!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:14:38 -04:00
|
|
|
if (!FileSystem::FileReader(path).exists())
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-23 14:32:20 -05:00
|
|
|
Logger::Print("^1FileRead: file not found!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:14:38 -04:00
|
|
|
Game::Scr_AddString(FileSystem::FileReader(path).getBuffer().data());
|
2020-07-22 23:36:52 -04:00
|
|
|
});
|
|
|
|
|
2022-01-23 14:32:20 -05:00
|
|
|
Script::AddFunction("FileExists", [](Game::scr_entref_t) // gsc: FileExists(<filepath>)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
const auto* path = Game::Scr_GetString(0);
|
2020-07-22 23:36:52 -04:00
|
|
|
|
2022-01-26 17:02:48 -05:00
|
|
|
if (path == nullptr)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
Game::Scr_ParamError(0, "^1FileExists: filepath is not defined!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:02:48 -05:00
|
|
|
for (auto i = 0u; i < ARRAYSIZE(queryStrings); ++i)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
if (std::strstr(path, queryStrings[i]) != nullptr)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-23 14:32:20 -05:00
|
|
|
Logger::Print("^1FileExists: directory traversal is not allowed!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:14:38 -04:00
|
|
|
Game::Scr_AddInt(FileSystem::FileReader(path).exists());
|
2020-07-22 23:36:52 -04:00
|
|
|
});
|
|
|
|
|
2022-01-23 14:32:20 -05:00
|
|
|
Script::AddFunction("FileRemove", [](Game::scr_entref_t) // gsc: FileRemove(<filepath>)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
const auto* path = Game::Scr_GetString(0);
|
2020-07-22 23:36:52 -04:00
|
|
|
|
2022-01-26 17:02:48 -05:00
|
|
|
if (path == nullptr)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
Game::Scr_ParamError(0, "^1FileRemove: filepath is not defined!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:02:48 -05:00
|
|
|
for (auto i = 0u; i < ARRAYSIZE(queryStrings); ++i)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-26 17:02:48 -05:00
|
|
|
if (std::strstr(path, queryStrings[i]) != nullptr)
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-23 14:32:20 -05:00
|
|
|
Logger::Print("^1fileRemove: directory traversal is not allowed!\n");
|
2020-07-22 23:36:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:14:38 -04:00
|
|
|
auto p = std::filesystem::path(path);
|
|
|
|
std::string folder = p.parent_path().string();
|
|
|
|
std::string file = p.filename().string();
|
|
|
|
Game::Scr_AddInt(FileSystem::DeleteFile(folder, file));
|
2020-07-22 23:36:52 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-23 14:32:20 -05:00
|
|
|
void ScriptExtension::AddMethods()
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-23 14:32:20 -05:00
|
|
|
// ScriptExtension methods
|
2022-01-16 08:45:18 -05:00
|
|
|
Script::AddFunction("GetIp", [](Game::scr_entref_t entref) // gsc: self GetIp()
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-02-01 07:52:53 -05:00
|
|
|
const auto* gentity = Script::GetEntity(entref);
|
|
|
|
const auto* client = Script::GetClient(gentity);
|
2020-07-22 23:36:52 -04:00
|
|
|
|
2022-01-07 16:00:44 -05:00
|
|
|
std::string ip = Game::NET_AdrToString(client->netchan.remoteAddress);
|
2020-07-22 23:36:52 -04:00
|
|
|
|
2022-01-23 14:32:20 -05:00
|
|
|
const auto pos = ip.find_first_of(":");
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
ip.erase(ip.begin() + pos, ip.end()); // Erase port
|
2020-07-22 23:36:52 -04:00
|
|
|
|
2022-01-07 16:00:44 -05:00
|
|
|
Game::Scr_AddString(ip.data());
|
2020-07-22 23:36:52 -04:00
|
|
|
});
|
|
|
|
|
2022-01-16 08:45:18 -05:00
|
|
|
Script::AddFunction("GetPing", [](Game::scr_entref_t entref) // gsc: self GetPing()
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-02-01 07:52:53 -05:00
|
|
|
const auto* gentity = Script::GetEntity(entref);
|
|
|
|
const auto* client = Script::GetClient(gentity);
|
2022-01-07 16:00:44 -05:00
|
|
|
|
|
|
|
Game::Scr_AddInt(client->ping);
|
2020-07-22 23:36:52 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-23 14:32:20 -05:00
|
|
|
ScriptExtension::ScriptExtension()
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
2022-01-23 14:32:20 -05:00
|
|
|
ScriptExtension::AddFunctions();
|
|
|
|
ScriptExtension::AddMethods();
|
2020-07-22 23:36:52 -04:00
|
|
|
}
|
2022-01-07 16:00:44 -05:00
|
|
|
}
|