More progress:
Abstracted many fs funcs with helper class. Removed much duplicated code. Fixed ReadFile assuming FS errors for ValveFS. Added ValveFS support for ReadFileString, WriteFile, WriteFileString, DeleteFile. Added missing param in doc for OpenFile.
This commit is contained in:
parent
5e7e7ce551
commit
573aea20fb
@ -91,10 +91,13 @@ public:
|
||||
virtual bool FileExists(const char *pFileName, const char *pPathID = 0) = 0;
|
||||
virtual unsigned int Size(const char *pFileName, const char *pPathID = 0) = 0;
|
||||
virtual int Read(void* pOutput, int size, FileHandle_t file) = 0;
|
||||
virtual int Write(void const* pInput, int size, FileHandle_t file) = 0;
|
||||
virtual void Seek(FileHandle_t file, int post, int seekType) = 0;
|
||||
virtual unsigned int Tell(FileHandle_t file) = 0;
|
||||
virtual int FPrint(FileHandle_t file, const char *pData) = 0;
|
||||
virtual void Flush(FileHandle_t file) = 0;
|
||||
virtual bool IsOk(FileHandle_t file) = 0;
|
||||
virtual void RemoveFile(const char *pRelativePath, const char *pathID = 0) = 0;
|
||||
};
|
||||
|
||||
namespace SourceMod
|
||||
|
@ -44,6 +44,111 @@ HandleType_t g_ValveFileType;
|
||||
HandleType_t g_DirType;
|
||||
IChangeableForward *g_pLogHook = NULL;
|
||||
|
||||
enum class FSType
|
||||
{
|
||||
STDIO,
|
||||
VALVE,
|
||||
};
|
||||
|
||||
class FSHelper
|
||||
{
|
||||
public:
|
||||
void SetFSType(FSType fstype)
|
||||
{
|
||||
_fstype = fstype;
|
||||
}
|
||||
public:
|
||||
inline void *Open(const char *filename, const char *mode)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return smcore.filesystem->Open(filename, mode);
|
||||
else
|
||||
return fopen(filename, mode);
|
||||
}
|
||||
|
||||
inline int Read(void *pOut, int size, void *pFile)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return smcore.filesystem->Read(pOut, size, (FileHandle_t)pFile);
|
||||
else
|
||||
return fread(pOut, 1, size, (FILE *)pFile) * size;
|
||||
}
|
||||
|
||||
inline char *ReadLine(char *pOut, int size, void *pFile)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return smcore.filesystem->ReadLine(pOut, size, (FileHandle_t)pFile);
|
||||
else
|
||||
return fgets(pOut, size, (FILE *)pFile);
|
||||
}
|
||||
|
||||
inline size_t Write(const void *pData, int size, void *pFile)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return (size_t)smcore.filesystem->Write(pData, size, (FileHandle_t)pFile);
|
||||
else
|
||||
return fwrite(pData, 1, size, (FILE *)pFile);
|
||||
}
|
||||
|
||||
inline void Seek(void *pFile, int pos, int seekType)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
smcore.filesystem->Seek((FileHandle_t)pFile, pos, seekType);
|
||||
else
|
||||
fseek((FILE *)pFile, pos, seekType);
|
||||
}
|
||||
|
||||
inline int Tell(void *pFile)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return smcore.filesystem->Tell((FileHandle_t)pFile);
|
||||
else
|
||||
return ftell((FILE *)pFile);
|
||||
}
|
||||
|
||||
inline bool HasError(void *pFile)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return !smcore.filesystem->IsOk((FileHandle_t)pFile);
|
||||
else
|
||||
return ferror((FILE *)pFile) != 0;
|
||||
}
|
||||
|
||||
inline void Close(void *pFile)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
smcore.filesystem->Close((FileHandle_t)pFile);
|
||||
else
|
||||
fclose((FILE *)pFile);
|
||||
}
|
||||
|
||||
inline bool Remove(const char *pFilePath)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return smcore.filesystem->RemoveFile(pFilePath), true;
|
||||
else
|
||||
return unlink(pFilePath) == 0;
|
||||
}
|
||||
|
||||
inline bool EndOfFile(void *pFile)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return smcore.filesystem->EndOfFile((FileHandle_t)pFile);
|
||||
else
|
||||
return feof((FILE *)pFile) != 0;
|
||||
}
|
||||
|
||||
inline bool Flush(void *pFile)
|
||||
{
|
||||
if (_fstype == FSType::VALVE)
|
||||
return smcore.filesystem->Flush((FileHandle_t)pFile), true;
|
||||
else
|
||||
return fflush((FILE *)pFile) == 0;
|
||||
}
|
||||
private:
|
||||
FSType _fstype;
|
||||
};
|
||||
|
||||
class FileNatives :
|
||||
public SMGlobalClass,
|
||||
public IHandleTypeDispatch,
|
||||
@ -201,25 +306,29 @@ static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params)
|
||||
}
|
||||
|
||||
Handle_t handle = 0;
|
||||
|
||||
HandleType_t handleType;
|
||||
FSHelper fshelper;
|
||||
const char *openpath;
|
||||
if (params[0] <= 2 || !params[3])
|
||||
{
|
||||
handleType = g_FileType;
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
|
||||
char realpath[PLATFORM_MAX_PATH];
|
||||
g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
|
||||
|
||||
FILE *pFile = fopen(realpath, mode);
|
||||
if (pFile)
|
||||
{
|
||||
handle = handlesys->CreateHandle(g_FileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL);
|
||||
}
|
||||
openpath = realpath;
|
||||
}
|
||||
else
|
||||
{
|
||||
FileHandle_t pFile = smcore.filesystem->Open(name, mode); // Should we allow pathID?
|
||||
if (pFile)
|
||||
{
|
||||
handle = handlesys->CreateHandle(g_ValveFileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL);
|
||||
}
|
||||
handleType = g_ValveFileType;
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
openpath = name;
|
||||
}
|
||||
|
||||
void *pFile = fshelper.Open(openpath, mode);
|
||||
if (pFile)
|
||||
{
|
||||
handle = handlesys->CreateHandle(handleType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL);
|
||||
}
|
||||
|
||||
return handle;
|
||||
@ -235,10 +344,22 @@ static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params)
|
||||
return 0;
|
||||
}
|
||||
|
||||
char realpath[PLATFORM_MAX_PATH];
|
||||
g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
|
||||
FSHelper fshelper;
|
||||
const char *filepath;
|
||||
if (params[0] < 2 || !params[2])
|
||||
{
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
char realpath[PLATFORM_MAX_PATH];
|
||||
g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
|
||||
filepath = realpath;
|
||||
}
|
||||
else
|
||||
{
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
filepath = name;
|
||||
}
|
||||
|
||||
return (unlink(realpath)) ? 0 : 1;
|
||||
return fshelper.Remove(filepath) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params)
|
||||
@ -248,7 +369,7 @@ static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params)
|
||||
HandleSecurity sec;
|
||||
int err;
|
||||
|
||||
void *pTempFile;
|
||||
void *pFile;
|
||||
sec.pOwner = NULL;
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
|
||||
@ -259,28 +380,22 @@ static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
FSHelper fshelper;
|
||||
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FILE *pFile = (FILE *) pTempFile;
|
||||
if (fgets(buf, params[3], pFile) == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FileHandle_t pFile = (FileHandle_t) pTempFile;
|
||||
if (smcore.filesystem->ReadLine(buf, params[3], pFile) == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
return 1;
|
||||
return fshelper.ReadLine(buf, params[3], pFile) == NULL ? 0 : 1;
|
||||
}
|
||||
|
||||
static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params)
|
||||
@ -288,28 +403,26 @@ static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
void *pTempFile;
|
||||
cell_t ret;
|
||||
void *pFile;
|
||||
|
||||
sec.pOwner = NULL;
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
FSHelper fshelper;
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FILE *pFile = (FILE *) pTempFile;
|
||||
ret = feof(pFile) ? 1 : 0;
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FileHandle_t pFile = (FileHandle_t) pTempFile;
|
||||
ret = smcore.filesystem->EndOfFile(pFile) ? 1 : 0;
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
return ret;
|
||||
return fshelper.EndOfFile(pFile) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params)
|
||||
@ -317,25 +430,26 @@ static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
void *pTempFile;
|
||||
void *pFile;
|
||||
|
||||
sec.pOwner = NULL;
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
FSHelper fshelper;
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FILE *pFile = (FILE *) pTempFile;
|
||||
fseek(pFile, params[2], params[3]);
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FileHandle_t pFile = (FileHandle_t) pTempFile;
|
||||
smcore.filesystem->Seek(pFile, params[2], params[3]);
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
fshelper.Seek(pFile, params[2], params[3]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -345,28 +459,27 @@ static cell_t sm_FilePosition(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
void *pTempFile;
|
||||
cell_t ret;
|
||||
void *pFile;
|
||||
|
||||
sec.pOwner = NULL;
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
FSHelper fshelper;
|
||||
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FILE *pFile = (FILE *) pTempFile;
|
||||
ret = ftell(pFile);
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FileHandle_t pFile = (FileHandle_t) pTempFile;
|
||||
ret = smcore.filesystem->Tell(pFile);
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
return ret;
|
||||
return (cell_t)fshelper.Tell(pFile);
|
||||
}
|
||||
|
||||
static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params)
|
||||
@ -576,13 +689,13 @@ static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params)
|
||||
|
||||
char buffer[2048];
|
||||
int arg = 3;
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, &pTempFile)) == HandleError_None)
|
||||
{
|
||||
FILE *pFile = (FILE *) pTempFile;
|
||||
smcore.atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg);
|
||||
fprintf(pFile, "%s\n", buffer);
|
||||
}
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pTempFile)) == HandleError_None)
|
||||
{
|
||||
FileHandle_t pFile = (FileHandle_t) pTempFile;
|
||||
smcore.atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg);
|
||||
@ -602,28 +715,26 @@ static cell_t sm_FlushFile(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
void *pTempFile;
|
||||
cell_t ret = 1;
|
||||
void *pFile;
|
||||
|
||||
sec.pOwner = NULL;
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
FSHelper fshelper;
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FILE *pFile = (FILE *) pTempFile;
|
||||
ret = (fflush(pFile) == 0) ? 1 : 0;
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FileHandle_t pFile = (FileHandle_t) pTempFile;
|
||||
smcore.filesystem->Flush(pFile);// This doesnt have a return assume 1?
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
return ret;
|
||||
return fshelper.Flush(pFile) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params)
|
||||
@ -790,111 +901,65 @@ static cell_t sm_ReadFile(IPluginContext *pContext, const cell_t *params)
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
size_t read = 0;
|
||||
|
||||
void *pTempFile = NULL;
|
||||
void *pFile = NULL;
|
||||
FSHelper fshelper;
|
||||
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
if ((herr = handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FILE *pFile = (FILE *) pTempFile;
|
||||
|
||||
if (params[4] != 1 && params[4] != 2 && params[4] != 4)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid size specifier (%d is not 1, 2, or 4)", params[4]);
|
||||
}
|
||||
|
||||
cell_t *data;
|
||||
pContext->LocalToPhysAddr(params[2], &data);
|
||||
|
||||
if (params[4] == 4)
|
||||
{
|
||||
read = fread(data, sizeof(cell_t), params[3], pFile);
|
||||
}
|
||||
else if (params[4] == 2)
|
||||
{
|
||||
uint16_t val;
|
||||
for (cell_t i = 0; i < params[3]; i++)
|
||||
{
|
||||
if (fread(&val, sizeof(uint16_t), 1, pFile) != 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
data[read++] = val;
|
||||
}
|
||||
}
|
||||
else if (params[4] == 1)
|
||||
{
|
||||
uint8_t val;
|
||||
for (cell_t i = 0; i < params[3]; i++)
|
||||
{
|
||||
if (fread(&val, sizeof(uint8_t), 1, pFile) != 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
data[read++] = val;
|
||||
}
|
||||
}
|
||||
|
||||
if (read != (size_t) params[3] && ferror(pFile) != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, (void **) &pTempFile)) == HandleError_None)
|
||||
else if ((herr = handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
FileHandle_t pFile = (FileHandle_t) pTempFile;
|
||||
|
||||
if (params[4] != 1 && params[4] != 2 && params[4] != 4)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid size specifier (%d is not 1, 2, or 4)", params[4]);
|
||||
}
|
||||
|
||||
cell_t *data;
|
||||
pContext->LocalToPhysAddr(params[2], &data);
|
||||
|
||||
if (params[4] == 4)
|
||||
{
|
||||
read = 1;
|
||||
if (smcore.filesystem->Read(data, sizeof(cell_t), pFile) != sizeof(cell_t))
|
||||
{
|
||||
read = 0;
|
||||
}
|
||||
}
|
||||
else if (params[4] == 2)
|
||||
{
|
||||
uint16_t val;
|
||||
for (cell_t i = 0; i < params[3]; i++)
|
||||
{
|
||||
if (smcore.filesystem->Read(&val, sizeof(uint16_t), pFile) != sizeof(uint16_t))
|
||||
{
|
||||
break;
|
||||
}
|
||||
data[read++] = val;
|
||||
}
|
||||
}
|
||||
else if (params[4] == 1)
|
||||
{
|
||||
uint8_t val;
|
||||
for (cell_t i = 0; i < params[3]; i++)
|
||||
{
|
||||
if (smcore.filesystem->Read(&val, sizeof(uint8_t), pFile) != sizeof(uint8_t))
|
||||
{
|
||||
break;
|
||||
}
|
||||
data[read++] = val;
|
||||
}
|
||||
}
|
||||
|
||||
if (read != (size_t) params[3])
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
return read;
|
||||
if (params[4] != 1 && params[4] != 2 && params[4] != 4)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid size specifier (%d is not 1, 2, or 4)", params[4]);
|
||||
}
|
||||
|
||||
cell_t *data;
|
||||
pContext->LocalToPhysAddr(params[2], &data);
|
||||
|
||||
if (params[4] == 4)
|
||||
{
|
||||
read = fshelper.Read(data, sizeof(cell_t) * params[3], pFile);
|
||||
}
|
||||
else if (params[4] == 2)
|
||||
{
|
||||
uint16_t val;
|
||||
for (cell_t i = 0; i < params[3]; i++)
|
||||
{
|
||||
if (fshelper.Read(&val, sizeof(uint16_t), pFile) != 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
data[read += sizeof(uint16_t)] = val;
|
||||
}
|
||||
}
|
||||
else if (params[4] == 1)
|
||||
{
|
||||
uint8_t val;
|
||||
for (cell_t i = 0; i < params[3]; i++)
|
||||
{
|
||||
if (fshelper.Read(&val, sizeof(uint8_t), pFile) != 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
data[read += sizeof(uint8_t)] = val;
|
||||
}
|
||||
}
|
||||
|
||||
if (read != ((size_t)params[3] * params[4]) && fshelper.HasError(pFile))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return read / params[4];
|
||||
}
|
||||
|
||||
static cell_t sm_ReadFileString(IPluginContext *pContext, const cell_t *params)
|
||||
@ -902,18 +967,27 @@ static cell_t sm_ReadFileString(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
FILE *pFile;
|
||||
void *pFile;
|
||||
cell_t num_read = 0;
|
||||
|
||||
if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
char *buffer;
|
||||
pContext->LocalToString(params[2], &buffer);
|
||||
|
||||
FSHelper fshelper;
|
||||
|
||||
if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr=handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
if (params[4] != -1)
|
||||
{
|
||||
if (size_t(params[4]) > size_t(params[3]))
|
||||
@ -923,9 +997,9 @@ static cell_t sm_ReadFileString(IPluginContext *pContext, const cell_t *params)
|
||||
params[3]);
|
||||
}
|
||||
|
||||
num_read = (cell_t)fread(buffer, 1, params[4], pFile);
|
||||
num_read = (cell_t)fshelper.Read(buffer, params[4], pFile);
|
||||
|
||||
if (num_read != params[4] && ferror(pFile))
|
||||
if (num_read != params[4] && fshelper.HasError(pFile))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@ -940,9 +1014,9 @@ static cell_t sm_ReadFileString(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (fread(&val, sizeof(val), 1, pFile) != 1)
|
||||
if (fshelper.Read(&val, sizeof(val), pFile) != 1)
|
||||
{
|
||||
if (ferror(pFile))
|
||||
if (fshelper.HasError(pFile))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@ -971,10 +1045,18 @@ static cell_t sm_WriteFile(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
FILE *pFile;
|
||||
void *pFile;
|
||||
FSHelper fshelper;
|
||||
|
||||
if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
|
||||
!= HandleError_None)
|
||||
if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr=handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
@ -991,7 +1073,7 @@ static cell_t sm_WriteFile(IPluginContext *pContext, const cell_t *params)
|
||||
|
||||
if (params[4] == 4)
|
||||
{
|
||||
if (fwrite(data, sizeof(cell_t), params[3], pFile) != (size_t)params[3])
|
||||
if (fshelper.Write(data, sizeof(cell_t) * params[3], pFile) != (sizeof(cell_t) * (size_t)params[3]))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -1000,7 +1082,7 @@ static cell_t sm_WriteFile(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
for (cell_t i = 0; i < params[3]; i++)
|
||||
{
|
||||
if (fwrite(&data[i], sizeof(int16_t), 1, pFile) != 1)
|
||||
if (fshelper.Write(&data[i], sizeof(int16_t), pFile) != sizeof(int16_t))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -1010,7 +1092,7 @@ static cell_t sm_WriteFile(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
for (cell_t i = 0; i < params[3]; i++)
|
||||
{
|
||||
if (fwrite(&data[i], sizeof(int8_t), 1, pFile) != 1)
|
||||
if (fshelper.Write(&data[i], sizeof(int8_t), pFile) != sizeof(int8_t))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -1025,10 +1107,18 @@ static cell_t sm_WriteFileString(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
FILE *pFile;
|
||||
void *pFile;
|
||||
FSHelper fshelper;
|
||||
|
||||
if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
|
||||
!= HandleError_None)
|
||||
if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
fshelper.SetFSType(FSType::STDIO);
|
||||
}
|
||||
else if ((herr=handlesys->ReadHandle(hndl, g_ValveFileType, &sec, &pFile)) == HandleError_None)
|
||||
{
|
||||
fshelper.SetFSType(FSType::VALVE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
@ -1043,7 +1133,7 @@ static cell_t sm_WriteFileString(IPluginContext *pContext, const cell_t *params)
|
||||
len++;
|
||||
}
|
||||
|
||||
return (fwrite(buffer, sizeof(char), len, pFile) == len) ? 1 : 0;
|
||||
return (fshelper.Write(buffer, len, pFile) == len) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t sm_AddGameLogHook(IPluginContext *pContext, const cell_t *params)
|
||||
|
@ -178,6 +178,10 @@ public:
|
||||
{
|
||||
return filesystem->Read(pOutput, size, file);
|
||||
}
|
||||
int Write(void const* pInput, int size, FileHandle_t file)
|
||||
{
|
||||
return filesystem->Write(pInput, size, file);
|
||||
}
|
||||
void Seek(FileHandle_t file, int pos, int seekType)
|
||||
{
|
||||
filesystem->Seek(file, pos, (FileSystemSeek_t) seekType);
|
||||
@ -194,6 +198,14 @@ public:
|
||||
{
|
||||
filesystem->Flush(file);
|
||||
}
|
||||
bool IsOk(FileHandle_t file)
|
||||
{
|
||||
return filesystem->IsOk(file);
|
||||
}
|
||||
void RemoveFile(const char *pRelativePath, const char *pathID)
|
||||
{
|
||||
filesystem->RemoveFile(pRelativePath, pathID);
|
||||
}
|
||||
};
|
||||
|
||||
static VFileSystem_Logic logic_filesystem;
|
||||
|
@ -136,17 +136,25 @@ native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=
|
||||
*
|
||||
* @param file File to open.
|
||||
* @param mode Open mode.
|
||||
* @param use_valve_fs If true, the Valve file system will be used instead.
|
||||
* This can be used to find files existing in any of
|
||||
* the GAME search paths, rather than solely files
|
||||
* existing directly in the gamedir.
|
||||
* @return A Handle to the file, INVALID_HANDLE on open error.
|
||||
*/
|
||||
native Handle:OpenFile(const String:file[], const String:mode[]);
|
||||
native Handle:OpenFile(const String:file[], const String:mode[], bool:use_valve_fs=false);
|
||||
|
||||
/**
|
||||
* Deletes a file.
|
||||
*
|
||||
* @param path Path of the file to delete.
|
||||
* @param use_valve_fs If true, the Valve file system will be used instead.
|
||||
* This can be used to find files existing in any of
|
||||
* the GAME search paths, rather than solely files
|
||||
* existing directly in the gamedir.
|
||||
* @return True on success, false otherwise.
|
||||
*/
|
||||
native bool:DeleteFile(const String:path[]);
|
||||
native bool:DeleteFile(const String:path[], bool:use_valve_fs=false);
|
||||
|
||||
/**
|
||||
* Reads a line from a text file.
|
||||
|
Loading…
Reference in New Issue
Block a user