Skip to content

Commit 74781c6

Browse files
authored
Add new functions: pathListDir, pathIsFile, pathIsDirectory (#3189)
1 parent 0fa1fbd commit 74781c6

File tree

8 files changed

+273
-51
lines changed

8 files changed

+273
-51
lines changed

Client/mods/deathmatch/logic/CResource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class CResource
6969
void SetResourceEntity(CClientEntity* pEntity) { m_pResourceEntity = pEntity; }
7070
class CClientEntity* GetResourceDynamicEntity() { return m_pResourceDynamicEntity; }
7171
void SetResourceDynamicEntity(CClientEntity* pEntity) { m_pResourceDynamicEntity = pEntity; }
72+
SString GetResourceDirectoryPath() { return GetResourceDirectoryPath(eAccessType::ACCESS_PUBLIC, ""); }
7273
SString GetResourceDirectoryPath(eAccessType accessType, const SString& strMetaPath);
7374
class CClientEntity* GetResourceGUIEntity() { return m_pResourceGUIEntity; }
7475
void SetResourceGUIEntity(CClientEntity* pEntity) { m_pResourceGUIEntity = pEntity; }

Shared/mods/deathmatch/logic/lua/CLuaShared.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void CLuaShared::LoadFunctions()
7777
CLuaCryptDefs::LoadFunctions();
7878
CLuaFileDefs::LoadFunctions();
7979
CLuaXMLDefs::LoadFunctions();
80+
CLuaPathDefs::LoadFunctions();
8081
CLuaTrainTrackDefs::LoadFunctions();
8182
CLuaUTFDefs::LoadFunctions();
8283
CLuaUtilDefs::LoadFunctions();
@@ -85,6 +86,7 @@ void CLuaShared::LoadFunctions()
8586
void CLuaShared::AddClasses(lua_State* luaVM)
8687
{
8788
CLuaFileDefs::AddClass(luaVM);
89+
CLuaPathDefs::AddClass(luaVM);
8890
CLuaXMLDefs::AddClass(luaVM);
8991
}
9092

Shared/mods/deathmatch/logic/lua/CLuaShared.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
// Lua function definitions (shared)
1313
#include "luadefs/CLuaBitDefs.h"
1414
#include "luadefs/CLuaCryptDefs.h"
15-
#include <luadefs/CLuaFileDefs.h>
15+
#include "luadefs/CLuaFileDefs.h"
1616
#include "luadefs/CLuaMatrixDefs.h"
17+
#include "luadefs/CLuaPathDefs.h"
1718
#include "luadefs/CLuaTrainTrackDefs.h"
1819
#include "luadefs/CLuaUTFDefs.h"
1920
#include "luadefs/CLuaUtilDefs.h"

Shared/mods/deathmatch/logic/luadefs/CLuaFileDefs.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,22 @@ static auto getResourceFilePath(CResource* thisResource, CResource* fileResource
3838
void CLuaFileDefs::LoadFunctions()
3939
{
4040
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
41-
{"fileOpen", fileOpen}, {"fileCreate", fileCreate}, {"fileExists", fileExists}, {"fileCopy", fileCopy},
42-
{"fileRename", fileRename}, {"fileDelete", fileDelete}, {"fileClose", fileClose}, {"fileFlush", fileFlush},
43-
{"fileRead", fileRead}, {"fileWrite", fileWrite}, {"fileGetPos", fileGetPos}, {"fileGetSize", fileGetSize},
44-
{"fileGetPath", fileGetPath}, {"fileIsEOF", fileIsEOF}, {"fileSetPos", fileSetPos}, {"fileGetContents", ArgumentParser<fileGetContents>},
41+
{"fileOpen", fileOpen},
42+
{"fileCreate", fileCreate},
43+
{"fileExists", fileExists},
44+
{"fileCopy", fileCopy},
45+
{"fileRename", fileRename},
46+
{"fileDelete", fileDelete},
47+
{"fileClose", fileClose},
48+
{"fileFlush", fileFlush},
49+
{"fileRead", fileRead},
50+
{"fileWrite", fileWrite},
51+
{"fileGetPos", fileGetPos},
52+
{"fileGetSize", fileGetSize},
53+
{"fileGetPath", fileGetPath},
54+
{"fileIsEOF", fileIsEOF},
55+
{"fileSetPos", fileSetPos},
56+
{"fileGetContents", ArgumentParser<fileGetContents>},
4557
};
4658

4759
// Add functions
@@ -388,8 +400,6 @@ int CLuaFileDefs::fileExists(lua_State* luaVM)
388400
CResource* pResource = pLuaMain->GetResource();
389401
if (CResourceManager::ParseResourcePathInput(strInputPath, pResource, &strAbsPath))
390402
{
391-
SString strFilePath;
392-
393403
// Does file exist?
394404
bool bResult = FileExists(strAbsPath);
395405
lua_pushboolean(luaVM, bResult);
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*****************************************************************************
2+
*
3+
* PROJECT: Multi Theft Auto
4+
* LICENSE: See LICENSE in the top level directory
5+
* FILE: Shared/mods/deathmatch/logic/luadefs/CLuaFileDefs.cpp
6+
*
7+
* Multi Theft Auto is available from http://www.multitheftauto.com/
8+
*
9+
*****************************************************************************/
10+
11+
#include "StdInc.h"
12+
13+
#ifndef MTA_CLIENT
14+
// NOTE: Must be included before ILuaModuleManager.h which defines its own CChecksum type.
15+
#include "CChecksum.h"
16+
#endif
17+
18+
#include "CLuaPathDefs.h"
19+
#include "CScriptFile.h"
20+
#include "CScriptArgReader.h"
21+
#include <lua/CLuaFunctionParser.h>
22+
23+
void CLuaPathDefs::LoadFunctions()
24+
{
25+
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
26+
{"pathListDir", ArgumentParser<pathListDir>},
27+
{"pathIsFile", ArgumentParser<pathIsFile>},
28+
{"pathIsDirectory", ArgumentParser<pathIsDirectory>},
29+
};
30+
31+
// Add functions
32+
for (const auto& [name, func] : functions)
33+
CLuaCFunctions::AddFunction(name, func);
34+
}
35+
36+
void CLuaPathDefs::AddClass(lua_State* luaVM)
37+
{
38+
lua_newclass(luaVM);
39+
40+
lua_classfunction(luaVM, "listDir", "pathListDir");
41+
lua_classfunction(luaVM, "isFile", "pathIsFile");
42+
lua_classfunction(luaVM, "isDirectory", "pathIsDirectory");
43+
44+
lua_registerclass(luaVM, "path");
45+
}
46+
47+
std::optional<std::vector<std::string>> CLuaPathDefs::pathListDir(
48+
lua_State* luaVM,
49+
std::string path
50+
) {
51+
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
52+
if (!pLuaMain)
53+
return std::nullopt;
54+
55+
std::string strAbsPath;
56+
57+
CResource* pResource = pLuaMain->GetResource();
58+
if (!CResourceManager::ParseResourcePathInput(path, pResource, &strAbsPath))
59+
{
60+
m_pScriptDebugging->LogWarning(luaVM, "Cannot parse provided path: \"%s\"",
61+
path.c_str());
62+
return std::nullopt;
63+
}
64+
65+
if (!DirectoryExists(strAbsPath))
66+
{
67+
m_pScriptDebugging->LogWarning(luaVM, "Directory \"%s\" doesn't exist!",
68+
path.c_str());
69+
return std::nullopt;
70+
}
71+
72+
return SharedUtil::ListDir(strAbsPath.c_str());
73+
}
74+
75+
bool CLuaPathDefs::pathIsFile(lua_State* luaVM, std::string path)
76+
{
77+
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
78+
if (!pLuaMain)
79+
return false;
80+
81+
std::string strAbsPath;
82+
83+
CResource* pResource = pLuaMain->GetResource();
84+
if (!CResourceManager::ParseResourcePathInput(path, pResource, &strAbsPath))
85+
{
86+
m_pScriptDebugging->LogWarning(luaVM, "Cannot parse provided path: \"%s\"",
87+
path.c_str());
88+
return false;
89+
}
90+
91+
return SharedUtil::FileExists(strAbsPath);
92+
}
93+
94+
bool CLuaPathDefs::pathIsDirectory(lua_State* luaVM, std::string path)
95+
{
96+
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
97+
if (!pLuaMain)
98+
return false;
99+
100+
std::string strAbsPath;
101+
102+
CResource* pResource = pLuaMain->GetResource();
103+
if (!CResourceManager::ParseResourcePathInput(path, pResource, &strAbsPath))
104+
{
105+
m_pScriptDebugging->LogWarning(luaVM, "Cannot parse provided path: \"%s\"",
106+
path.c_str());
107+
return false;
108+
}
109+
110+
return SharedUtil::DirectoryExists(strAbsPath.c_str());
111+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*****************************************************************************
2+
*
3+
* PROJECT: Multi Theft Auto
4+
* LICENSE: See LICENSE in the top level directory
5+
* FILE: Shared/mods/deathmatch/logic/luadefs/CLuaFileDefs.h
6+
*
7+
* Multi Theft Auto is available from http://www.multitheftauto.com/
8+
*
9+
*****************************************************************************/
10+
11+
#pragma once
12+
#include "luadefs/CLuaDefs.h"
13+
14+
class CLuaPathDefs : public CLuaDefs
15+
{
16+
public:
17+
static void LoadFunctions();
18+
static void AddClass(lua_State* luaVM);
19+
20+
private:
21+
static std::optional<std::vector<std::string>> pathListDir(lua_State* luaVM, std::string path);
22+
23+
static bool pathIsFile(lua_State* luaVM, std::string path);
24+
static bool pathIsDirectory(lua_State* luaVM, std::string path);
25+
};

Shared/sdk/SharedUtil.File.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace SharedUtil
2020
//
2121
// Returns true if the file/directory exists
2222
//
23-
bool FileExists(const SString& strFilename);
24-
bool DirectoryExists(const SString& strPath);
23+
bool FileExists(const std::string& strFilename) noexcept;
24+
bool DirectoryExists(const std::string& strPath) noexcept;
2525

2626
//
2727
// Load from a file
@@ -102,6 +102,8 @@ namespace SharedUtil
102102
WString FromUTF8(const SString& strPath);
103103
SString ToUTF8(const WString& strPath);
104104

105+
std::vector<std::string> ListDir(const char* szPath) noexcept;
106+
105107
namespace File
106108
{
107109
FILE* Fopen(const char* szFilename, const char* szMode);

0 commit comments

Comments
 (0)