Add support for specifying gameinfo search path when using valveFS in file natives

This commit is contained in:
Nicholas Hastings 2014-08-22 06:00:43 -07:00
parent b95e7ff145
commit 73115f7afa
4 changed files with 107 additions and 41 deletions

View File

@ -82,7 +82,6 @@ class IFileSystem_Logic
{ {
public: public:
virtual const char *FindFirstEx(const char *pWildCard, const char *pPathID, FileFindHandle_t *pHandle) = 0; virtual const char *FindFirstEx(const char *pWildCard, const char *pPathID, FileFindHandle_t *pHandle) = 0;
virtual const char *FindFirst(const char *pWildCard, FileFindHandle_t *pHandle) = 0;
virtual const char *FindNext(FileFindHandle_t handle) = 0; virtual const char *FindNext(FileFindHandle_t handle) = 0;
virtual bool FindIsDirectory(FileFindHandle_t handle) = 0; virtual bool FindIsDirectory(FileFindHandle_t handle) = 0;
virtual void FindClose(FileFindHandle_t handle) = 0; virtual void FindClose(FileFindHandle_t handle) = 0;

View File

@ -59,10 +59,10 @@ public:
_fstype = fstype; _fstype = fstype;
} }
public: public:
inline void *Open(const char *filename, const char *mode) inline void *Open(const char *filename, const char *mode, const char *pathID)
{ {
if (_fstype == FSType::VALVE) if (_fstype == FSType::VALVE)
return smcore.filesystem->Open(filename, mode); return smcore.filesystem->Open(filename, mode, pathID);
else else
return fopen(filename, mode); return fopen(filename, mode);
} }
@ -123,16 +123,16 @@ public:
fclose((FILE *)pFile); fclose((FILE *)pFile);
} }
inline bool Remove(const char *pFilePath) inline bool Remove(const char *pFilePath, const char *pathID)
{ {
if (_fstype == FSType::VALVE) if (_fstype == FSType::VALVE)
{ {
if (!smcore.filesystem->FileExists(pFilePath)) if (!smcore.filesystem->FileExists(pFilePath, pathID))
return false; return false;
smcore.filesystem->RemoveFile(pFilePath); smcore.filesystem->RemoveFile(pFilePath, pathID);
if (smcore.filesystem->FileExists(pFilePath)) if (smcore.filesystem->FileExists(pFilePath, pathID))
return false; return false;
return true; return true;
@ -263,7 +263,15 @@ static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
char wildcardedPath[PLATFORM_MAX_PATH]; char wildcardedPath[PLATFORM_MAX_PATH];
snprintf(wildcardedPath, sizeof(wildcardedPath), "%s*", path); snprintf(wildcardedPath, sizeof(wildcardedPath), "%s*", path);
ValveDirectory *valveDir = new ValveDirectory; ValveDirectory *valveDir = new ValveDirectory;
const char *pFirst = smcore.filesystem->FindFirst(wildcardedPath, &valveDir->hndl);
char *pathID;
if ((err=pContext->LocalToStringNULL(params[3], &pathID)) != SP_ERROR_NONE)
{
pContext->ThrowNativeErrorEx(err, NULL);
return 0;
}
const char *pFirst = smcore.filesystem->FindFirstEx(wildcardedPath, pathID, &valveDir->hndl);
if (pFirst) if (pFirst)
{ {
valveDir->bHandledFirstPath = false; valveDir->bHandledFirstPath = false;
@ -409,6 +417,7 @@ static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params)
HandleType_t handleType; HandleType_t handleType;
FSHelper fshelper; FSHelper fshelper;
const char *openpath; const char *openpath;
char *pathID;
if (params[0] <= 2 || !params[3]) if (params[0] <= 2 || !params[3])
{ {
handleType = g_FileType; handleType = g_FileType;
@ -420,12 +429,18 @@ static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params)
} }
else else
{ {
if ((err=pContext->LocalToStringNULL(params[4], &pathID)) != SP_ERROR_NONE)
{
pContext->ThrowNativeErrorEx(err, NULL);
return 0;
}
handleType = g_ValveFileType; handleType = g_ValveFileType;
fshelper.SetFSType(FSType::VALVE); fshelper.SetFSType(FSType::VALVE);
openpath = name; openpath = name;
} }
void *pFile = fshelper.Open(openpath, mode); void *pFile = fshelper.Open(openpath, mode, pathID);
if (pFile) if (pFile)
{ {
handle = handlesys->CreateHandle(handleType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL); handle = handlesys->CreateHandle(handleType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL);
@ -446,6 +461,7 @@ static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params)
FSHelper fshelper; FSHelper fshelper;
const char *filepath; const char *filepath;
char *pathID;
if (params[0] < 2 || !params[2]) if (params[0] < 2 || !params[2])
{ {
fshelper.SetFSType(FSType::STDIO); fshelper.SetFSType(FSType::STDIO);
@ -455,11 +471,17 @@ static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params)
} }
else else
{ {
if ((err=pContext->LocalToStringNULL(params[3], &pathID)) != SP_ERROR_NONE)
{
pContext->ThrowNativeErrorEx(err, NULL);
return 0;
}
fshelper.SetFSType(FSType::VALVE); fshelper.SetFSType(FSType::VALVE);
filepath = name; filepath = name;
} }
return fshelper.Remove(filepath) ? 1 : 0; return fshelper.Remove(filepath, pathID) ? 1 : 0;
} }
static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params) static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params)
@ -594,7 +616,17 @@ static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params)
if (params[0] >= 2 && params[2] == 1) if (params[0] >= 2 && params[2] == 1)
{ {
return smcore.filesystem->FileExists(name) ? 1 : 0; char *pathID = NULL;
if (params[0] >= 3)
{
if ((err=pContext->LocalToStringNULL(params[3], &pathID)) != SP_ERROR_NONE)
{
pContext->ThrowNativeErrorEx(err, NULL);
return 0;
}
}
return smcore.filesystem->FileExists(name, pathID) ? 1 : 0;
} }
char realpath[PLATFORM_MAX_PATH]; char realpath[PLATFORM_MAX_PATH];
@ -641,7 +673,14 @@ static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params)
if (params[0] >= 3 && params[3] == 1) if (params[0] >= 3 && params[3] == 1)
{ {
smcore.filesystem->RenameFile(oldpath, newpath); char *pathID;
if ((err=pContext->LocalToStringNULL(params[4], &pathID)) != SP_ERROR_NONE)
{
pContext->ThrowNativeErrorEx(err, NULL);
return 0;
}
smcore.filesystem->RenameFile(oldpath, newpath, pathID);
return 1; return 1;
} }
@ -669,7 +708,14 @@ static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params)
if (params[0] >= 2 && params[2] == 1) if (params[0] >= 2 && params[2] == 1)
{ {
return smcore.filesystem->IsDirectory(name) ? 1 : 0; char *pathID;
if ((err=pContext->LocalToStringNULL(params[3], &pathID)) != SP_ERROR_NONE)
{
pContext->ThrowNativeErrorEx(err, NULL);
return 0;
}
return smcore.filesystem->IsDirectory(name, pathID) ? 1 : 0;
} }
char realpath[PLATFORM_MAX_PATH]; char realpath[PLATFORM_MAX_PATH];
@ -711,9 +757,19 @@ static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params)
if (params[0] >= 2 && params[2] == 1) if (params[0] >= 2 && params[2] == 1)
{ {
if (smcore.filesystem->FileExists(name)) char *pathID = NULL;
if (params[0] >= 3)
{ {
return smcore.filesystem->Size(name); if ((err=pContext->LocalToStringNULL(params[3], &pathID)) != SP_ERROR_NONE)
{
pContext->ThrowNativeErrorEx(err, NULL);
return -1;
}
}
if (smcore.filesystem->FileExists(name, pathID))
{
return smcore.filesystem->Size(name, pathID);
} }
else else
{ {
@ -755,12 +811,19 @@ static cell_t sm_CreateDirectory(IPluginContext *pContext, const cell_t *params)
if (params[0] >= 3 && params[3] == 1) if (params[0] >= 3 && params[3] == 1)
{ {
if (smcore.filesystem->IsDirectory(name)) int err;
char *pathID;
if ((err=pContext->LocalToStringNULL(params[4], &pathID)) != SP_ERROR_NONE)
{
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (smcore.filesystem->IsDirectory(name, pathID))
return 0; return 0;
smcore.filesystem->CreateDirHierarchy(name); smcore.filesystem->CreateDirHierarchy(name, pathID);
if (smcore.filesystem->IsDirectory(name)) if (smcore.filesystem->IsDirectory(name, pathID))
return 1; return 1;
return 0; return 0;

View File

@ -142,10 +142,6 @@ public:
{ {
return filesystem->FindFirstEx(pWildCard, pPathID, pHandle); return filesystem->FindFirstEx(pWildCard, pPathID, pHandle);
} }
const char *FindFirst(const char *pWildCard, FileFindHandle_t *pHandle)
{
return filesystem->FindFirst(pWildCard, pHandle);
}
const char *FindNext(FileFindHandle_t handle) const char *FindNext(FileFindHandle_t handle)
{ {
return filesystem->FindNext(handle); return filesystem->FindNext(handle);

View File

@ -110,11 +110,12 @@ native BuildPath(PathType:type, String:buffer[], maxlength, const String:fmt[],
* @param path Path to open. * @param path Path to open.
* @param use_valve_fs If true, the Valve file system will be used instead. * @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 * This can be used to find files existing in any of
* the GAME search paths, rather than solely files * the Valve search paths, rather than solely files
* existing directly in the gamedir. * existing directly in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return A Handle to the directory, INVALID_HANDLE on open error. * @return A Handle to the directory, INVALID_HANDLE on open error.
*/ */
native Handle:OpenDirectory(const String:path[], bool:use_valve_fs=false); native Handle:OpenDirectory(const String:path[], bool:use_valve_fs=false, const String:valve_path_id[]="GAME");
/** /**
* Reads the current directory entry as a local filename, then moves to the next file. * Reads the current directory entry as a local filename, then moves to the next file.
@ -141,24 +142,26 @@ native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=
* @param file File to open. * @param file File to open.
* @param mode Open mode. * @param mode Open mode.
* @param use_valve_fs If true, the Valve file system will be used instead. * @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 * This can be used to find files existing in valve
* the GAME search paths, rather than solely files * search paths, rather than solely files existing directly
* existing directly in the gamedir. * in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return A Handle to the file, INVALID_HANDLE on open error. * @return A Handle to the file, INVALID_HANDLE on open error.
*/ */
native Handle:OpenFile(const String:file[], const String:mode[], bool:use_valve_fs=false); native Handle:OpenFile(const String:file[], const String:mode[], bool:use_valve_fs=false, const String:valve_path_id[]="GAME");
/** /**
* Deletes a file. * Deletes a file.
* *
* @param path Path of the file to delete. * @param path Path of the file to delete.
* @param use_valve_fs If true, the Valve file system will be used instead. * @param use_valve_fs If true, the Valve file system will be used instead.
* This can be used to delete files existing in the game's * This can be used to delete files existing in the Valve
* DEFAULT_WRITE_PATH search path, rather than solely files * search path, rather than solely files existing directly
* existing directly in the gamedir. * in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return True on success, false otherwise. * @return True on success, false otherwise.
*/ */
native bool:DeleteFile(const String:path[], bool:use_valve_fs=false); native bool:DeleteFile(const String:path[], bool:use_valve_fs=false, const String:valve_path_id="DEFAULT_WRITE_PATH");
/** /**
* Reads a line from a text file. * Reads a line from a text file.
@ -316,11 +319,12 @@ native FilePosition(Handle:file);
* @param path Path to the file. * @param path Path to the file.
* @param use_valve_fs If true, the Valve file system will be used instead. * @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 * This can be used to find files existing in any of
* the GAME search paths, rather than solely files * the Valve search paths, rather than solely files
* existing directly in the gamedir. * existing directly in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return True if the file exists, false otherwise. * @return True if the file exists, false otherwise.
*/ */
native bool:FileExists(const String:path[], bool:use_valve_fs=false); native bool:FileExists(const String:path[], bool:use_valve_fs=false, const String:valve_path_id="GAME");
/** /**
* Renames a file. * Renames a file.
@ -329,10 +333,11 @@ native bool:FileExists(const String:path[], bool:use_valve_fs=false);
* @param oldpath Path to the existing file. * @param oldpath Path to the existing file.
* @param use_valve_fs If true, the Valve file system will be used instead. * @param use_valve_fs If true, the Valve file system will be used instead.
* This can be used to rename files in the game's * This can be used to rename files in the game's
* DEFAULT_WRITE_PATH search path, rather than directly in the gamedir. * Valve search paths, rather than directly in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return True on success or use_valve_fs specified, false otherwise. * @return True on success or use_valve_fs specified, false otherwise.
*/ */
native bool:RenameFile(const String:newpath[], const String:oldpath[], bool:use_valve_fs=false); native bool:RenameFile(const String:newpath[], const String:oldpath[], bool:use_valve_fs=false, const String:valve_path_id="DEFAULT_WRITE_PATH");
/** /**
* Checks if a directory exists. * Checks if a directory exists.
@ -340,11 +345,12 @@ native bool:RenameFile(const String:newpath[], const String:oldpath[], bool:use_
* @param path Path to the directory. * @param path Path to the directory.
* @param use_valve_fs If true, the Valve file system will be used instead. * @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 * This can be used to find files existing in any of
* the GAME search paths, rather than solely files * the Valve search paths, rather than solely files
* existing directly in the gamedir. * existing directly in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return True if the directory exists, false otherwise. * @return True if the directory exists, false otherwise.
*/ */
native bool:DirExists(const String:path[], bool:use_valve_fs=false); native bool:DirExists(const String:path[], bool:use_valve_fs=false, const String:valve_path_id="GAME");
/** /**
* Get the file size in bytes. * Get the file size in bytes.
@ -352,11 +358,12 @@ native bool:DirExists(const String:path[], bool:use_valve_fs=false);
* @param path Path to the file. * @param path Path to the file.
* @param use_valve_fs If true, the Valve file system will be used instead. * @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 * This can be used to find files existing in any of
* the GAME search paths, rather than solely files * the Valve search paths, rather than solely files
* existing directly in the gamedir. * existing directly in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return File size in bytes, -1 if file not found. * @return File size in bytes, -1 if file not found.
*/ */
native FileSize(const String:path[], bool:use_valve_fs=false); native FileSize(const String:path[], bool:use_valve_fs=false, const String:valve_path_id="GAME");
/** /**
* Flushes a file's buffered output; any buffered output * Flushes a file's buffered output; any buffered output
@ -395,7 +402,8 @@ native bool:RemoveDir(const String:path[]);
* the execute bit set on Linux. On Windows, the mode is ignored. * the execute bit set on Linux. On Windows, the mode is ignored.
* @param use_valve_fs If true, the Valve file system will be used instead. * @param use_valve_fs If true, the Valve file system will be used instead.
* This can be used to create folders in the game's * This can be used to create folders in the game's
* DEFAULT_WRITE_PATH search path, rather than directly in the gamedir. * Valve search paths, rather than directly in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for default.
* In this case, mode is ignored. * In this case, mode is ignored.
*/ */
native bool:CreateDirectory(const String:path[], mode, bool:use_valve_fs=false); native bool:CreateDirectory(const String:path[], mode, bool:use_valve_fs=false);