From 8b3433887dfcec119a81a9a7b07d8b007d3ca5b6 Mon Sep 17 00:00:00 2001 From: anotheruselesaccount <160650467+anotheruselesaccount@users.noreply.github.com> Date: Mon, 22 Apr 2024 12:00:50 +0300 Subject: [PATCH] Update fileio.cpp Add file io functions * directoryexists * createdirectory * fileexists * readfile * writefile --- src/component/fileio.cpp | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/component/fileio.cpp b/src/component/fileio.cpp index c421f98..004af45 100644 --- a/src/component/fileio.cpp +++ b/src/component/fileio.cpp @@ -32,6 +32,7 @@ namespace fileio std::array scr_fhs = {}; + bool validate_scr_path(const std::string& fpath) { if (fpath.empty()) @@ -196,6 +197,9 @@ namespace fileio auto fd = 0; auto file_length = game::FS_FOpenFileRead(fpath.c_str(), &fd); + printf("FD %d", fd); + printf("FILE LENGHT %d", file_length); + if (!fd || file_length < 0) { game::Com_PrintWarning(game::CON_CHANNEL_SCRIPT, "Failed to open file for reading: %s\n", fpath.c_str()); @@ -418,6 +422,63 @@ namespace fileio game::FS_FreeFileList(files); }); + + gsc::function::add("writefile", []() + { + const auto path = game::Scr_GetString(0, game::SCRIPTINSTANCE_SERVER); + const auto data = game::Scr_GetString(1, game::SCRIPTINSTANCE_SERVER); + + auto append = false; + if (game::Scr_GetNumParam(game::SCRIPTINSTANCE_SERVER) > 2) + { + append = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 2); + } + + game::Scr_AddInt(game::SCRIPTINSTANCE_SERVER, utils::io::write_file(path, data, append)); + }); + + gsc::function::add("readfile", []() + { + const auto path = game::Scr_GetString(0, game::SCRIPTINSTANCE_SERVER); + game::Scr_AddString(game::SCRIPTINSTANCE_SERVER, utils::io::read_file(path).c_str()); + }); + + gsc::function::add("fileexists", []() + { + const auto path = game::Scr_GetString(0, game::SCRIPTINSTANCE_SERVER); + + if (!utils::io::file_exists(path)) + { + game::Scr_AddInt(game::SCRIPTINSTANCE_SERVER, 0); + return; + } + + game::Scr_AddInt(game::SCRIPTINSTANCE_SERVER, 1); + }); + + gsc::function::add("createdirectory", []() + { + const auto path = game::Scr_GetString(0, game::SCRIPTINSTANCE_SERVER); + if (!utils::io::create_directory(path)) + { + game::Scr_AddInt(game::SCRIPTINSTANCE_SERVER, 0); + return; + } + + game::Scr_AddInt(game::SCRIPTINSTANCE_SERVER, 1); + }); + + gsc::function::add("directoryexists", []() + { + const auto path = game::Scr_GetString(0, game::SCRIPTINSTANCE_SERVER); + if(!utils::io::directory_exists(path)) + { + game::Scr_AddInt(game::SCRIPTINSTANCE_SERVER, 0); + return; + } + + game::Scr_AddInt(game::SCRIPTINSTANCE_SERVER, 1); + }); } }