Skip to content

Commit

Permalink
Add new functions: pathListDir, pathIsFile, pathIsDirectory (#3189
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TracerDS committed May 25, 2024
1 parent 0fa1fbd commit 74781c6
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 51 deletions.
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/CResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class CResource
void SetResourceEntity(CClientEntity* pEntity) { m_pResourceEntity = pEntity; }
class CClientEntity* GetResourceDynamicEntity() { return m_pResourceDynamicEntity; }
void SetResourceDynamicEntity(CClientEntity* pEntity) { m_pResourceDynamicEntity = pEntity; }
SString GetResourceDirectoryPath() { return GetResourceDirectoryPath(eAccessType::ACCESS_PUBLIC, ""); }
SString GetResourceDirectoryPath(eAccessType accessType, const SString& strMetaPath);
class CClientEntity* GetResourceGUIEntity() { return m_pResourceGUIEntity; }
void SetResourceGUIEntity(CClientEntity* pEntity) { m_pResourceGUIEntity = pEntity; }
Expand Down
2 changes: 2 additions & 0 deletions Shared/mods/deathmatch/logic/lua/CLuaShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void CLuaShared::LoadFunctions()
CLuaCryptDefs::LoadFunctions();
CLuaFileDefs::LoadFunctions();
CLuaXMLDefs::LoadFunctions();
CLuaPathDefs::LoadFunctions();
CLuaTrainTrackDefs::LoadFunctions();
CLuaUTFDefs::LoadFunctions();
CLuaUtilDefs::LoadFunctions();
Expand All @@ -85,6 +86,7 @@ void CLuaShared::LoadFunctions()
void CLuaShared::AddClasses(lua_State* luaVM)
{
CLuaFileDefs::AddClass(luaVM);
CLuaPathDefs::AddClass(luaVM);
CLuaXMLDefs::AddClass(luaVM);
}

Expand Down
3 changes: 2 additions & 1 deletion Shared/mods/deathmatch/logic/lua/CLuaShared.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// Lua function definitions (shared)
#include "luadefs/CLuaBitDefs.h"
#include "luadefs/CLuaCryptDefs.h"
#include <luadefs/CLuaFileDefs.h>
#include "luadefs/CLuaFileDefs.h"
#include "luadefs/CLuaMatrixDefs.h"
#include "luadefs/CLuaPathDefs.h"
#include "luadefs/CLuaTrainTrackDefs.h"
#include "luadefs/CLuaUTFDefs.h"
#include "luadefs/CLuaUtilDefs.h"
Expand Down
22 changes: 16 additions & 6 deletions Shared/mods/deathmatch/logic/luadefs/CLuaFileDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,22 @@ static auto getResourceFilePath(CResource* thisResource, CResource* fileResource
void CLuaFileDefs::LoadFunctions()
{
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
{"fileOpen", fileOpen}, {"fileCreate", fileCreate}, {"fileExists", fileExists}, {"fileCopy", fileCopy},
{"fileRename", fileRename}, {"fileDelete", fileDelete}, {"fileClose", fileClose}, {"fileFlush", fileFlush},
{"fileRead", fileRead}, {"fileWrite", fileWrite}, {"fileGetPos", fileGetPos}, {"fileGetSize", fileGetSize},
{"fileGetPath", fileGetPath}, {"fileIsEOF", fileIsEOF}, {"fileSetPos", fileSetPos}, {"fileGetContents", ArgumentParser<fileGetContents>},
{"fileOpen", fileOpen},
{"fileCreate", fileCreate},
{"fileExists", fileExists},
{"fileCopy", fileCopy},
{"fileRename", fileRename},
{"fileDelete", fileDelete},
{"fileClose", fileClose},
{"fileFlush", fileFlush},
{"fileRead", fileRead},
{"fileWrite", fileWrite},
{"fileGetPos", fileGetPos},
{"fileGetSize", fileGetSize},
{"fileGetPath", fileGetPath},
{"fileIsEOF", fileIsEOF},
{"fileSetPos", fileSetPos},
{"fileGetContents", ArgumentParser<fileGetContents>},
};

// Add functions
Expand Down Expand Up @@ -388,8 +400,6 @@ int CLuaFileDefs::fileExists(lua_State* luaVM)
CResource* pResource = pLuaMain->GetResource();
if (CResourceManager::ParseResourcePathInput(strInputPath, pResource, &strAbsPath))
{
SString strFilePath;

// Does file exist?
bool bResult = FileExists(strAbsPath);
lua_pushboolean(luaVM, bResult);
Expand Down
111 changes: 111 additions & 0 deletions Shared/mods/deathmatch/logic/luadefs/CLuaPathDefs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: Shared/mods/deathmatch/logic/luadefs/CLuaFileDefs.cpp
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#include "StdInc.h"

#ifndef MTA_CLIENT
// NOTE: Must be included before ILuaModuleManager.h which defines its own CChecksum type.
#include "CChecksum.h"
#endif

#include "CLuaPathDefs.h"
#include "CScriptFile.h"
#include "CScriptArgReader.h"
#include <lua/CLuaFunctionParser.h>

void CLuaPathDefs::LoadFunctions()
{
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
{"pathListDir", ArgumentParser<pathListDir>},
{"pathIsFile", ArgumentParser<pathIsFile>},
{"pathIsDirectory", ArgumentParser<pathIsDirectory>},
};

// Add functions
for (const auto& [name, func] : functions)
CLuaCFunctions::AddFunction(name, func);
}

void CLuaPathDefs::AddClass(lua_State* luaVM)
{
lua_newclass(luaVM);

lua_classfunction(luaVM, "listDir", "pathListDir");
lua_classfunction(luaVM, "isFile", "pathIsFile");
lua_classfunction(luaVM, "isDirectory", "pathIsDirectory");

lua_registerclass(luaVM, "path");
}

std::optional<std::vector<std::string>> CLuaPathDefs::pathListDir(
lua_State* luaVM,
std::string path
) {
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (!pLuaMain)
return std::nullopt;

std::string strAbsPath;

CResource* pResource = pLuaMain->GetResource();
if (!CResourceManager::ParseResourcePathInput(path, pResource, &strAbsPath))
{
m_pScriptDebugging->LogWarning(luaVM, "Cannot parse provided path: \"%s\"",
path.c_str());
return std::nullopt;
}

if (!DirectoryExists(strAbsPath))
{
m_pScriptDebugging->LogWarning(luaVM, "Directory \"%s\" doesn't exist!",
path.c_str());
return std::nullopt;
}

return SharedUtil::ListDir(strAbsPath.c_str());
}

bool CLuaPathDefs::pathIsFile(lua_State* luaVM, std::string path)
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (!pLuaMain)
return false;

std::string strAbsPath;

CResource* pResource = pLuaMain->GetResource();
if (!CResourceManager::ParseResourcePathInput(path, pResource, &strAbsPath))
{
m_pScriptDebugging->LogWarning(luaVM, "Cannot parse provided path: \"%s\"",
path.c_str());
return false;
}

return SharedUtil::FileExists(strAbsPath);
}

bool CLuaPathDefs::pathIsDirectory(lua_State* luaVM, std::string path)
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (!pLuaMain)
return false;

std::string strAbsPath;

CResource* pResource = pLuaMain->GetResource();
if (!CResourceManager::ParseResourcePathInput(path, pResource, &strAbsPath))
{
m_pScriptDebugging->LogWarning(luaVM, "Cannot parse provided path: \"%s\"",
path.c_str());
return false;
}

return SharedUtil::DirectoryExists(strAbsPath.c_str());
}
25 changes: 25 additions & 0 deletions Shared/mods/deathmatch/logic/luadefs/CLuaPathDefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: Shared/mods/deathmatch/logic/luadefs/CLuaFileDefs.h
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once
#include "luadefs/CLuaDefs.h"

class CLuaPathDefs : public CLuaDefs
{
public:
static void LoadFunctions();
static void AddClass(lua_State* luaVM);

private:
static std::optional<std::vector<std::string>> pathListDir(lua_State* luaVM, std::string path);

static bool pathIsFile(lua_State* luaVM, std::string path);
static bool pathIsDirectory(lua_State* luaVM, std::string path);
};
6 changes: 4 additions & 2 deletions Shared/sdk/SharedUtil.File.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace SharedUtil
//
// Returns true if the file/directory exists
//
bool FileExists(const SString& strFilename);
bool DirectoryExists(const SString& strPath);
bool FileExists(const std::string& strFilename) noexcept;
bool DirectoryExists(const std::string& strPath) noexcept;

//
// Load from a file
Expand Down Expand Up @@ -102,6 +102,8 @@ namespace SharedUtil
WString FromUTF8(const SString& strPath);
SString ToUTF8(const WString& strPath);

std::vector<std::string> ListDir(const char* szPath) noexcept;

namespace File
{
FILE* Fopen(const char* szFilename, const char* szMode);
Expand Down
Loading

0 comments on commit 74781c6

Please sign in to comment.