5 Commits

Author SHA1 Message Date
a3a7b8847c note 2024-05-12 11:43:16 -06:00
7fccea636f update to io 2024-05-12 11:41:57 -06:00
38a860e4ce Merge branch 'main' of https://github.com/JezuzLizard/T4SP-Server-Plugin 2023-12-15 15:05:41 -06:00
3995bed200 update 2023-12-15 15:05:36 -06:00
3502a70933 Update README.md 2023-12-15 15:03:41 -06:00
2 changed files with 40 additions and 48 deletions

View File

@ -91,13 +91,15 @@ All files will be closed upon GSC restart (map_restart or fast_restart or missio
* `<array of strings> FS_ListFiles(<folder string>)` Returns a list of files inside of the folder given.
```gsc
files = FS_ListFiles("testfolder/");
folder = "testfolder/";
files = FS_ListFiles(folder);
for (i = 0; i < files.size; i++)
{
file = files[i]; // will be "testfolder/<filename>"
filename = files[i];
// do something with the filename
filepath = folder + filename;
}
```

View File

@ -34,15 +34,16 @@ namespace fileio
bool validate_scr_path(const std::string& fpath)
{
if (fpath.empty())
{
return true;
}
auto toks = utils::string::split(fpath, '/');
constexpr static std::array bad_strings { R"(..)", R"(../)", R"(..\)" };
for (auto i = 0u; i < bad_strings.size(); i++)
for (const auto& tok : toks)
{
if (fpath.find(bad_strings[i]) != std::string::npos)
if (tok == "." || tok == "..")
{
return false;
}
if (tok.find(":") != std::string::npos)
{
return false;
}
@ -51,13 +52,17 @@ namespace fileio
return true;
}
std::string build_base_path(const std::string& path)
std::string build_base_path(const std::string& path_)
{
auto path = path_;
std::replace(path.begin(), path.end(), '\\', '/');
if (!validate_scr_path(path))
{
game::Scr_Error(utils::string::va("Invalid path: %s", path.c_str()), game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, utils::string::va("Invalid path: %s", path_.c_str()));
}
// its sandboxed, but what about symlinks?
return path.starts_with("scriptdata/") ? path : "scriptdata/" + path;
}
@ -102,18 +107,25 @@ namespace fileio
}
}
void fwrite_to_file(bool append_newline)
int scr_get_fh()
{
auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1;
if (fh < 0 || fh >= max_fhs)
{
game::Scr_Error("fs_fwrite: invalid filehandle", game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "fs_fwrite: invalid filehandle");
}
return fh;
}
void fwrite_to_file(bool append_newline)
{
auto fh = scr_get_fh();
if (scr_fhs[fh].type != scr_fh_type_e::WRITE && scr_fhs[fh].type != scr_fh_type_e::APPEND)
{
game::Scr_Error("File not opened for writing", game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File not opened for writing");
}
std::string to_write = game::Scr_GetString(1, game::SCRIPTINSTANCE_SERVER);
@ -170,7 +182,7 @@ namespace fileio
{
if (scr_fd.type != scr_fh_type_e::UNUSED && scr_fd.base_path == fpath)
{
game::Scr_Error("File already opened", game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File already opened");
}
}
@ -186,7 +198,7 @@ namespace fileio
if (i >= max_fhs)
{
game::Scr_Error("Too many files opened", game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "Too many files opened");
}
// check mode
@ -240,7 +252,7 @@ namespace fileio
}
else
{
game::Scr_Error(utils::string::va("Invalid mode: %s", mode), game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(1, game::SCRIPTINSTANCE_SERVER, utils::string::va("Invalid mode: %s", mode));
}
#ifdef DEBUG
@ -261,16 +273,11 @@ namespace fileio
gsc::function::add("fs_readline", []()
{
auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1;
if (fh < 0 || fh >= max_fhs)
{
game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false);
}
auto fh = scr_get_fh();
if (scr_fhs[fh].type != scr_fh_type_e::READ)
{
game::Scr_Error("File not opened for reading", game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File not opened for reading");
}
// file is completed being read
@ -319,16 +326,11 @@ namespace fileio
gsc::function::add("fs_read", []()
{
auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1;
if (fh < 0 || fh >= max_fhs)
{
game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false);
}
auto fh = scr_get_fh();
if (scr_fhs[fh].type != scr_fh_type_e::READ)
{
game::Scr_Error("File not opened for reading", game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File not opened for reading");
}
// file is completed being read
@ -345,7 +347,7 @@ namespace fileio
if (bytes_to_read <= 0)
{
game::Scr_Error("Trying to read <1 bytes", game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(1, game::SCRIPTINSTANCE_SERVER, "Trying to read <1 bytes");
}
}
@ -370,16 +372,11 @@ namespace fileio
gsc::function::add("fs_fclose", []()
{
auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1;
if (fh < 0 || fh >= max_fhs)
{
game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false);
}
auto fh = scr_get_fh();
if (scr_fhs[fh].type == scr_fh_type_e::UNUSED)
{
game::Scr_Error("File not opened", game::SCRIPTINSTANCE_SERVER, false);
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File not opened");
}
free_scr_fh(scr_fhs[fh]);
@ -404,14 +401,7 @@ namespace fileio
gsc::function::add("fs_listfiles", []()
{
std::string dir = game::Scr_GetString(0, game::SCRIPTINSTANCE_SERVER);
if (dir.ends_with("\\") || dir.ends_with("/"))
{
dir = dir.substr(0, dir.length() - 1);
}
auto fpath = build_base_path(dir);
auto fpath = build_base_path(game::Scr_GetString(0, game::SCRIPTINSTANCE_SERVER));
int numfiles;
auto* files = game::FS_ListFiles(fpath.c_str(), "", game::FS_LIST_ALL, &numfiles);
@ -419,7 +409,7 @@ namespace fileio
game::Scr_MakeArray(game::SCRIPTINSTANCE_SERVER);
for (int i = 0; i < numfiles; i++)
{
game::Scr_AddString(game::SCRIPTINSTANCE_SERVER, (dir + "/" + files[i]).c_str());
game::Scr_AddString(game::SCRIPTINSTANCE_SERVER, files[i]);
game::Scr_AddArray(game::SCRIPTINSTANCE_SERVER);
}