2020-07-22 23:36:52 -04:00
|
|
|
#include "STDInclude.hpp"
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
void Client::AddFunctions()
|
|
|
|
{
|
|
|
|
//File functions
|
|
|
|
|
|
|
|
Script::AddFunction("fileWrite", [](Game::scr_entref_t) // gsc: fileWrite(<filepath>, <string>, <mode>)
|
|
|
|
{
|
|
|
|
std::string path = Game::Scr_GetString(0);
|
|
|
|
auto text = Game::Scr_GetString(1);
|
|
|
|
auto mode = Game::Scr_GetString(2);
|
|
|
|
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileWrite: filepath not defined!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const char*> queryStrings = { R"(..)", R"(../)", R"(..\)" };
|
|
|
|
for (auto i = 0u; i < queryStrings.size(); i++)
|
|
|
|
{
|
|
|
|
if (path.find(queryStrings[i]) != std::string::npos)
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileWrite: directory traversal is not allowed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != "append"s && mode != "write"s)
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Script::AddFunction("fileRead", [](Game::scr_entref_t) // gsc: fileRead(<filepath>)
|
|
|
|
{
|
|
|
|
std::string path = Game::Scr_GetString(0);
|
|
|
|
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileRead: filepath not defined!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const char*> queryStrings = { R"(..)", R"(../)", R"(..\)" };
|
|
|
|
for (auto i = 0u; i < queryStrings.size(); i++)
|
|
|
|
{
|
|
|
|
if (path.find(queryStrings[i]) != std::string::npos)
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileRead: directory traversal is not allowed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:14:38 -04:00
|
|
|
if (!FileSystem::FileReader(path).exists())
|
2020-07-22 23:36:52 -04:00
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileRead: file not found!\n");
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
Script::AddFunction("fileExists", [](Game::scr_entref_t) // gsc: fileExists(<filepath>)
|
|
|
|
{
|
|
|
|
std::string path = Game::Scr_GetString(0);
|
|
|
|
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileExists: filepath not defined!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const char*> queryStrings = { R"(..)", R"(../)", R"(..\)" };
|
|
|
|
for (auto i = 0u; i < queryStrings.size(); i++)
|
|
|
|
{
|
|
|
|
if (path.find(queryStrings[i]) != std::string::npos)
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileExists: directory traversal is not allowed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:14:38 -04:00
|
|
|
Game::Scr_AddInt(FileSystem::FileReader(path).exists());
|
2020-07-22 23:36:52 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Script::AddFunction("fileRemove", [](Game::scr_entref_t) // gsc: fileRemove(<filepath>)
|
|
|
|
{
|
|
|
|
std::string path = Game::Scr_GetString(0);
|
|
|
|
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileRemove: filepath not defined!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const char*> queryStrings = { R"(..)", R"(../)", R"(..\)" };
|
|
|
|
for (auto i = 0u; i < queryStrings.size(); i++)
|
|
|
|
{
|
|
|
|
if (path.find(queryStrings[i]) != std::string::npos)
|
|
|
|
{
|
|
|
|
Game::Com_Printf(0, "^1fileRemove: directory traversal is not allowed!\n");
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::AddMethods()
|
|
|
|
{
|
|
|
|
// Client 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-01-16 08:45:18 -05:00
|
|
|
const auto* gentity = Script::GetEntFromEntRef(entref);
|
2022-01-07 16:00:44 -05:00
|
|
|
const auto* client = Script::GetClientFromEnt(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-07 16:00:44 -05:00
|
|
|
if (ip.find_first_of(":") != std::string::npos)
|
|
|
|
ip.erase(ip.begin() + ip.find_first_of(":"), 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-01-16 08:45:18 -05:00
|
|
|
const auto* gentity = Script::GetEntFromEntRef(entref);
|
2022-01-07 16:00:44 -05:00
|
|
|
const auto* client = Script::GetClientFromEnt(gentity);
|
|
|
|
|
|
|
|
Game::Scr_AddInt(client->ping);
|
2020-07-22 23:36:52 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Client::Client()
|
|
|
|
{
|
|
|
|
Client::AddFunctions();
|
|
|
|
Client::AddMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
Client::~Client()
|
|
|
|
{
|
|
|
|
}
|
2022-01-07 16:00:44 -05:00
|
|
|
}
|