core/smn_filesystem.cpp
author Borja Ferrer <faluco@alliedmods.net>
Mon Jan 01 23:14:20 2007 +0000 (2007-01-01)
changeset 258 2637fcf24c2f
parent 255 236589ed0c92
child 262 de0e374ffa47
permissions -rw-r--r--
more comments
     1 #include "sm_globals.h"
     2 #include "HandleSys.h"
     3 #include "LibrarySys.h"
     4 
     5 HandleType_t g_FileType;
     6 HandleType_t g_DirType;
     7 
     8 class FileNatives : 
     9 	public SMGlobalClass,
    10 	public IHandleTypeDispatch
    11 {
    12 public:
    13 	virtual void OnSourceModAllInitialized()
    14 	{
    15 		HandleSecurity sec;
    16 		sec.owner = g_pCoreIdent;
    17 		sec.access[HandleAccess_Inherit] = false;
    18 		sec.access[HandleAccess_Create] = false;
    19 
    20 		g_FileType = g_HandleSys.CreateTypeEx("File", this, 0, &sec, NULL);
    21 		g_DirType = g_HandleSys.CreateTypeEx("Directory", this, 0, &sec, NULL);
    22 	}
    23 	virtual void OnSourceModShutdown()
    24 	{
    25 		g_HandleSys.RemoveType(g_DirType, g_pCoreIdent);
    26 		g_HandleSys.RemoveType(g_FileType, g_pCoreIdent);
    27 		g_DirType = 0;
    28 		g_FileType = 0;
    29 	}
    30 	virtual void OnHandleDestroy(HandleType_t type, void *object)
    31 	{
    32 		if (type == g_FileType)
    33 		{
    34 			FILE *fp = (FILE *)object;
    35 			fclose(fp);
    36 		} else if (type == g_DirType) {
    37 			IDirectory *pDir = (IDirectory *)object;
    38 			g_LibSys.CloseDirectory(pDir);
    39 		}
    40 	}
    41 };
    42 
    43 
    44 cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
    45 {
    46 	char *path;
    47 	int err;
    48 	if ((err=pContext->LocalToString(params[1], &path)) != SP_ERROR_NONE)
    49 	{
    50 		pContext->ThrowNativeErrorEx(err, NULL);
    51 		return 0;
    52 	}
    53 
    54 	char realpath[PLATFORM_MAX_PATH+1];
    55 	g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), path);
    56 
    57 	IDirectory *pDir = g_LibSys.OpenDirectory(realpath);
    58 	if (!pDir)
    59 	{
    60 		return 0;
    61 	}
    62 
    63 	return g_HandleSys.CreateScriptHandle(g_DirType, pDir, pContext, g_pCoreIdent);
    64 }
    65 
    66 cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params)
    67 {
    68 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    69 
    70 	IDirectory *pDir;
    71 	HandleError herr;
    72 	int err;
    73 	if ((herr=g_HandleSys.ReadHandle(hndl, g_DirType, g_pCoreIdent, (void **)&pDir))
    74 		!= HandleError_None)
    75 	{
    76 		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
    77 	}
    78 
    79 	if (!pDir->MoreFiles())
    80 	{
    81 		return false;
    82 	}
    83 
    84 	cell_t *filetype;
    85 	if ((err=pContext->LocalToPhysAddr(params[4], &filetype)) != SP_ERROR_NONE)
    86 	{
    87 		pContext->ThrowNativeErrorEx(err, NULL);
    88 		return 0;
    89 	}
    90 
    91 	if (pDir->IsEntryDirectory())
    92 	{
    93 		*filetype = 1;
    94 	} else if (pDir->IsEntryFile()) {
    95 		*filetype = 2;
    96 	} else {
    97 		*filetype = 0;
    98 	}
    99 
   100 	const char *path = pDir->GetEntryName();
   101 	if ((err=pContext->StringToLocalUTF8(params[2], params[3], path, NULL))
   102 		!= SP_ERROR_NONE)
   103 	{
   104 		pContext->ThrowNativeErrorEx(err, NULL);
   105 		return 0;
   106 	}
   107 
   108 	pDir->NextEntry();
   109 
   110 	return true;
   111 }
   112 
   113 cell_t PrintStuff(IPluginContext *pContext, const cell_t *params)
   114 {
   115 	char *stuff;
   116 	pContext->LocalToString(params[1], &stuff);
   117 
   118 	FILE *fp = fopen("c:\\debug.txt", "at");
   119 	fprintf(fp, "%s\n", stuff);
   120 	fclose(fp);
   121 
   122 	return 0;
   123 }
   124 
   125 static FileNatives s_FileNatives;
   126 
   127 REGISTER_NATIVES(filesystem)
   128 {
   129 	{"OpenDirectory",			sm_OpenDirectory},
   130 	{"ReadDirEntry",			sm_ReadDirEntry},
   131 	{"PrintStuff",				PrintStuff},
   132 	{NULL,						NULL},
   133 };