1 #include "sm_globals.h"
3 #include "LibrarySys.h"
5 HandleType_t g_FileType;
6 HandleType_t g_DirType;
10 public IHandleTypeDispatch
13 virtual void OnSourceModAllInitialized()
16 sec.owner = g_pCoreIdent;
17 sec.access[HandleAccess_Inherit] = false;
18 sec.access[HandleAccess_Create] = false;
20 g_FileType = g_HandleSys.CreateTypeEx("File", this, 0, &sec, NULL);
21 g_DirType = g_HandleSys.CreateTypeEx("Directory", this, 0, &sec, NULL);
23 virtual void OnSourceModShutdown()
25 g_HandleSys.RemoveType(g_DirType, g_pCoreIdent);
26 g_HandleSys.RemoveType(g_FileType, g_pCoreIdent);
30 virtual void OnHandleDestroy(HandleType_t type, void *object)
32 if (type == g_FileType)
34 FILE *fp = (FILE *)object;
36 } else if (type == g_DirType) {
37 IDirectory *pDir = (IDirectory *)object;
38 g_LibSys.CloseDirectory(pDir);
44 cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
48 if ((err=pContext->LocalToString(params[1], &path)) != SP_ERROR_NONE)
50 pContext->ThrowNativeErrorEx(err, NULL);
54 char realpath[PLATFORM_MAX_PATH+1];
55 g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), path);
57 IDirectory *pDir = g_LibSys.OpenDirectory(realpath);
63 return g_HandleSys.CreateScriptHandle(g_DirType, pDir, pContext, g_pCoreIdent);
66 cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params)
68 Handle_t hndl = static_cast<Handle_t>(params[1]);
73 if ((herr=g_HandleSys.ReadHandle(hndl, g_DirType, g_pCoreIdent, (void **)&pDir))
76 return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
79 if (!pDir->MoreFiles())
85 if ((err=pContext->LocalToPhysAddr(params[4], &filetype)) != SP_ERROR_NONE)
87 pContext->ThrowNativeErrorEx(err, NULL);
91 if (pDir->IsEntryDirectory())
94 } else if (pDir->IsEntryFile()) {
100 const char *path = pDir->GetEntryName();
101 if ((err=pContext->StringToLocalUTF8(params[2], params[3], path, NULL))
104 pContext->ThrowNativeErrorEx(err, NULL);
113 cell_t PrintStuff(IPluginContext *pContext, const cell_t *params)
116 pContext->LocalToString(params[1], &stuff);
118 FILE *fp = fopen("c:\\debug.txt", "at");
119 fprintf(fp, "%s\n", stuff);
125 static FileNatives s_FileNatives;
127 REGISTER_NATIVES(filesystem)
129 {"OpenDirectory", sm_OpenDirectory},
130 {"ReadDirEntry", sm_ReadDirEntry},
131 {"PrintStuff", PrintStuff},