mirror of
				https://github.com/JezuzLizard/T4SP-Server-Plugin.git
				synced 2025-10-30 17:27:01 +00:00 
			
		
		
		
	Compare commits
	
		
			5 Commits
		
	
	
		
			v1.0.1
			...
			a3a7b8847c
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | a3a7b8847c | ||
|  | 7fccea636f | ||
|  | 38a860e4ce | ||
|  | 3995bed200 | ||
|  | 3502a70933 | 
| @@ -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. | * `<array of strings> FS_ListFiles(<folder string>)` Returns a list of files inside of the folder given. | ||||||
|   ```gsc |   ```gsc | ||||||
|   files = FS_ListFiles("testfolder/"); |   folder = "testfolder/"; | ||||||
|  |   files = FS_ListFiles(folder); | ||||||
|  |  | ||||||
|   for (i = 0; i < files.size; i++) |   for (i = 0; i < files.size; i++) | ||||||
|   { |   { | ||||||
|     file = files[i]; // will be "testfolder/<filename>" |     filename = files[i]; | ||||||
|  |  | ||||||
|     // do something with the filename |     // do something with the filename | ||||||
|  |     filepath = folder + filename; | ||||||
|   } |   } | ||||||
|   ``` |   ``` | ||||||
|  |  | ||||||
|   | |||||||
| @@ -34,15 +34,16 @@ namespace fileio | |||||||
|  |  | ||||||
| 		bool validate_scr_path(const std::string& fpath) | 		bool validate_scr_path(const std::string& fpath) | ||||||
| 		{ | 		{ | ||||||
| 			if (fpath.empty()) | 			auto toks = utils::string::split(fpath, '/'); | ||||||
|  |  | ||||||
|  | 			for (const auto& tok : toks) | ||||||
| 			{ | 			{ | ||||||
| 				return true; | 				if (tok == "." || tok == "..") | ||||||
|  | 				{ | ||||||
|  | 					return false; | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 			constexpr static std::array bad_strings { R"(..)", R"(../)", R"(..\)" }; | 				if (tok.find(":") != std::string::npos) | ||||||
| 			for (auto i = 0u; i < bad_strings.size(); i++) |  | ||||||
| 			{ |  | ||||||
| 				if (fpath.find(bad_strings[i]) != std::string::npos) |  | ||||||
| 				{ | 				{ | ||||||
| 					return false; | 					return false; | ||||||
| 				} | 				} | ||||||
| @@ -51,13 +52,17 @@ namespace fileio | |||||||
| 			return true; | 			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)) | 			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; | 			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; | 			auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1; | ||||||
|  |  | ||||||
| 			if (fh < 0 || fh >= max_fhs) | 			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) | 			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); | 			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) | 						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) | 					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 | 					// check mode | ||||||
| @@ -240,7 +252,7 @@ namespace fileio | |||||||
| 					} | 					} | ||||||
| 					else | 					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 | #ifdef DEBUG | ||||||
| @@ -261,16 +273,11 @@ namespace fileio | |||||||
|  |  | ||||||
| 			gsc::function::add("fs_readline", []() | 			gsc::function::add("fs_readline", []() | ||||||
| 				{ | 				{ | ||||||
| 					auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1; | 					auto fh = scr_get_fh(); | ||||||
|  |  | ||||||
| 					if (fh < 0 || fh >= max_fhs) |  | ||||||
| 					{ |  | ||||||
| 						game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false); |  | ||||||
| 					} |  | ||||||
|  |  | ||||||
| 					if (scr_fhs[fh].type != scr_fh_type_e::READ) | 					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 | 					// file is completed being read | ||||||
| @@ -319,16 +326,11 @@ namespace fileio | |||||||
|  |  | ||||||
| 			gsc::function::add("fs_read", []() | 			gsc::function::add("fs_read", []() | ||||||
| 				{ | 				{ | ||||||
| 					auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1; | 					auto fh = scr_get_fh(); | ||||||
|  |  | ||||||
| 					if (fh < 0 || fh >= max_fhs) |  | ||||||
| 					{ |  | ||||||
| 						game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false); |  | ||||||
| 					} |  | ||||||
|  |  | ||||||
| 					if (scr_fhs[fh].type != scr_fh_type_e::READ) | 					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 | 					// file is completed being read | ||||||
| @@ -345,7 +347,7 @@ namespace fileio | |||||||
|  |  | ||||||
| 						if (bytes_to_read <= 0) | 						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", []() | 			gsc::function::add("fs_fclose", []() | ||||||
| 				{ | 				{ | ||||||
| 					auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1; | 					auto fh = scr_get_fh(); | ||||||
|  |  | ||||||
| 					if (fh < 0 || fh >= max_fhs) |  | ||||||
| 					{ |  | ||||||
| 						game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false); |  | ||||||
| 					} |  | ||||||
|  |  | ||||||
| 					if (scr_fhs[fh].type == scr_fh_type_e::UNUSED) | 					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]); | 					free_scr_fh(scr_fhs[fh]); | ||||||
| @@ -404,14 +401,7 @@ namespace fileio | |||||||
|  |  | ||||||
| 			gsc::function::add("fs_listfiles", []() | 			gsc::function::add("fs_listfiles", []() | ||||||
| 				{ | 				{ | ||||||
| 					std::string dir = game::Scr_GetString(0, game::SCRIPTINSTANCE_SERVER); | 					auto fpath = build_base_path(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); |  | ||||||
|  |  | ||||||
| 					int numfiles; | 					int numfiles; | ||||||
| 					auto* files = game::FS_ListFiles(fpath.c_str(), "", game::FS_LIST_ALL, &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); | 					game::Scr_MakeArray(game::SCRIPTINSTANCE_SERVER); | ||||||
| 					for (int i = 0; i < numfiles; i++) | 					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); | 						game::Scr_AddArray(game::SCRIPTINSTANCE_SERVER); | ||||||
| 					} | 					} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user