core/smn_filesystem.cpp
changeset 252 ce166c465bad
child 255 236589ed0c92
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_filesystem.cpp	Mon Jan 01 19:50:16 2007 +0000
     1.3 @@ -0,0 +1,114 @@
     1.4 +#include "sm_globals.h"
     1.5 +#include "HandleSys.h"
     1.6 +#include "LibrarySys.h"
     1.7 +
     1.8 +HandleType_t g_FileType;
     1.9 +HandleType_t g_DirType;
    1.10 +
    1.11 +class FileNatives : 
    1.12 +	public SMGlobalClass,
    1.13 +	public IHandleTypeDispatch
    1.14 +{
    1.15 +public:
    1.16 +	virtual void OnSourceModAllInitialized()
    1.17 +	{
    1.18 +		HandleSecurity sec;
    1.19 +		sec.owner = g_pCoreIdent;
    1.20 +		sec.access[HandleAccess_Inherit] = false;
    1.21 +		sec.access[HandleAccess_Create] = false;
    1.22 +
    1.23 +		g_FileType = g_HandleSys.CreateTypeEx("File", this, 0, &sec, NULL);
    1.24 +		g_DirType = g_HandleSys.CreateTypeEx("Directory", this, 0, &sec, NULL);
    1.25 +	}
    1.26 +	virtual void OnSourceModShutdown()
    1.27 +	{
    1.28 +		g_HandleSys.RemoveType(g_DirType, g_pCoreIdent);
    1.29 +		g_HandleSys.RemoveType(g_FileType, g_pCoreIdent);
    1.30 +		g_DirType = 0;
    1.31 +		g_FileType = 0;
    1.32 +	}
    1.33 +	virtual void OnHandleDestroy(HandleType_t type, void *object)
    1.34 +	{
    1.35 +		if (type == g_FileType)
    1.36 +		{
    1.37 +			FILE *fp = (FILE *)object;
    1.38 +			fclose(fp);
    1.39 +		} else if (type == g_DirType) {
    1.40 +			IDirectory *pDir = (IDirectory *)object;
    1.41 +			g_LibSys.CloseDirectory(pDir);
    1.42 +		}
    1.43 +	}
    1.44 +};
    1.45 +
    1.46 +
    1.47 +cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
    1.48 +{
    1.49 +	char *path;
    1.50 +	int err;
    1.51 +	if ((err=pContext->LocalToString(params[1], &path)) != SP_ERROR_NONE)
    1.52 +	{
    1.53 +		pContext->ThrowNativeErrorEx(err, NULL);
    1.54 +		return 0;
    1.55 +	}
    1.56 +
    1.57 +	IDirectory *pDir = g_LibSys.OpenDirectory(path);
    1.58 +	if (!pDir)
    1.59 +	{
    1.60 +		return 0;
    1.61 +	}
    1.62 +
    1.63 +	return g_HandleSys.CreateScriptHandle(g_DirType, pDir, pContext, g_pCoreIdent);
    1.64 +}
    1.65 +
    1.66 +cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params)
    1.67 +{
    1.68 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.69 +
    1.70 +	IDirectory *pDir;
    1.71 +	HandleError herr;
    1.72 +	int err;
    1.73 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_DirType, g_pCoreIdent, (void **)&pDir))
    1.74 +		!= HandleError_None)
    1.75 +	{
    1.76 +		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
    1.77 +	}
    1.78 +
    1.79 +	if (!pDir->MoreFiles())
    1.80 +	{
    1.81 +		return false;
    1.82 +	}
    1.83 +
    1.84 +	cell_t *filetype;
    1.85 +	if ((err=pContext->LocalToPhysAddr(params[4], &filetype)) != SP_ERROR_NONE)
    1.86 +	{
    1.87 +		pContext->ThrowNativeErrorEx(err, NULL);
    1.88 +		return 0;
    1.89 +	}
    1.90 +
    1.91 +	if (pDir->IsEntryDirectory())
    1.92 +	{
    1.93 +		*filetype = 1;
    1.94 +	} else if (pDir->IsEntryFile()) {
    1.95 +		*filetype = 2;
    1.96 +	} else {
    1.97 +		*filetype = 0;
    1.98 +	}
    1.99 +
   1.100 +	const char *path = pDir->GetEntryName();
   1.101 +	if ((err=pContext->StringToLocalUTF8(params[2], params[3],path, NULL))
   1.102 +		!= SP_ERROR_NONE)
   1.103 +	{
   1.104 +		pContext->ThrowNativeErrorEx(err, NULL);
   1.105 +		return 0;
   1.106 +	}
   1.107 +
   1.108 +	return true;
   1.109 +}
   1.110 +
   1.111 +
   1.112 +REGISTER_NATIVES(filesystem)
   1.113 +{
   1.114 +	{"OpenDirectory",			sm_OpenDirectory},
   1.115 +	{"ReadDirEntry",			sm_ReadDirEntry},
   1.116 +	{NULL,						NULL},
   1.117 +};