From 71ab8d560eaf76cef8d3056c113a997c9e7a83aa Mon Sep 17 00:00:00 2001 From: Arkshine Date: Mon, 9 Mar 2015 00:38:05 +0100 Subject: [PATCH] VFS: Add more sane FileRead/Write* natives to read/write a single value --- amxmodx/file.cpp | 55 +++++++++++++++++++++++--- plugins/include/file.inc | 85 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 5 deletions(-) diff --git a/amxmodx/file.cpp b/amxmodx/file.cpp index f64c7a72..8596b09b 100755 --- a/amxmodx/file.cpp +++ b/amxmodx/file.cpp @@ -1132,11 +1132,49 @@ static cell SetFilePermissions(AMX *amx, cell *params) } return _chmod(realpath, mask) == 0; -#elif +#else return chmod(realpath, params[2]) == 0; #endif } +template +static cell File_ReadTyped(AMX *amx, cell *params) +{ + FileObject* fp = reinterpret_cast(params[1]); + + if (!fp) + { + return 0; + } + + cell* data = get_amxaddr(amx, params[2]); + + T value; + + if (fp->Read(&value, sizeof(value)) != sizeof(value)) + { + return 0; + } + + *data = value; + return 1; +} + +template +static cell File_WriteTyped(AMX *amx, cell *params) +{ + FileObject* fp = reinterpret_cast(params[1]); + + if (!fp) + { + return 0; + } + + T value = static_cast(params[2]); + + return !!(fp->Write(&value, sizeof(value)) == sizeof(value)); +} + AMX_NATIVE_INFO file_Natives[] = { {"read_dir", read_dir}, @@ -1181,10 +1219,17 @@ AMX_NATIVE_INFO file_Natives[] = {"rmdir", amx_rmdir}, {"mkdir", amx_mkdir}, - {"LoadFileForMe", LoadFileForMe}, - {"GetFileTime", GetFileTime}, - {"SetFilePermissions", SetFilePermissions}, + {"LoadFileForMe", LoadFileForMe}, + {"GetFileTime", GetFileTime}, + {"SetFilePermissions", SetFilePermissions}, + {"FileReadInt8", File_ReadTyped}, + {"FileReadUint8", File_ReadTyped}, + {"FileReadInt16", File_ReadTyped}, + {"FileReadUint16", File_ReadTyped}, + {"FileReadInt32", File_ReadTyped}, + {"FileWriteInt8", File_WriteTyped}, + {"FileWriteInt16", File_WriteTyped}, + {"FileWriteInt32", File_WriteTyped}, {NULL, NULL} }; - diff --git a/plugins/include/file.inc b/plugins/include/file.inc index 9b73b874..82016380 100755 --- a/plugins/include/file.inc +++ b/plugins/include/file.inc @@ -566,3 +566,88 @@ native GetFileTime(const file[], FileTimeType:tmode); * @return True on success, false otherwise */ native bool:SetFilePermissions(const path[], mode); + +/** + * Reads a single int8 (byte) from a file. The returned value is sign- + * extended to an int32. + * + * @param file Handle to the file + * @param data Variable to store the data read + * + * @return True on success, false on failure + */ +native bool:FileReadInt8(file, &any:data); + +/** + * Reads a single uint8 (unsigned byte) from a file. The returned value is + * zero-extended to an int32. + * + * @param file Handle to the file + * @param data Variable to store the data read + * + * @return True on success, false on failure + */ +native bool:FileReadUint8(file, &any:data); + +/** + * Reads a single int16 (short) from a file. The value is sign-extended to + * an int32. + * + * @param file Handle to the file + * @param data Variable to store the data read + * + * @return True on success, false on failure + */ +native bool:FileReadInt16(file, &any:data); + +/** + * Reads a single unt16 (unsigned short) from a file. The value is zero- + * extended to an int32. + * + * @param file Handle to the file + * @param data Variable to store the data read + * + * @return True on success, false on failure + */ +native bool:FileReadUint16(file, &any:data); + +/** + * Reads a single int32 (int/cell) from a file. + * + * @param file Handle to the file + * @param data Variable to store the data read + * + * @return True on success, false on failure + */ +native bool:FileReadInt32(file, &any:data); + +/** + * Writes a single int8 (byte) to a file. + * + * @param file Handle to the file + * @param data Data to write (truncated to an int8) + * + * @return True on success, false on failure + */ +native bool:FileWriteInt8(file, any:data); + +/** + * Writes a single int16 (short) to a file. + * + * @param file Handle to the file + * @param data Data to write (truncated to an int16) + * + * @return True on success, false on failure + */ +native bool:FileWriteInt16(file, any:data); + +/** + * Writes a single int32 (int/cell) to a file. + * + * @param file Handle to the file + * @param data Data to write + * + * @return True on success, false on failure + */ +native bool:FileWriteInt32(file, any:data); +