Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functions: pathListDir, pathIsFile, pathIsDirectory #3189

Merged
merged 31 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c9529c2
Upgrade lunasvg from 2.3.5 to 2.3.8 (#3060)
Lpsd Jun 11, 2023
0d4a2e6
Force rebuild
patrikjuvonen Jun 17, 2023
c041dd9
Force rebuild
patrikjuvonen Jun 20, 2023
7287dc6
Visual Studio Update
MTABot Jul 12, 2023
f866e17
Visual Studio Update
MTABot Jul 26, 2023
25a24c4
Bump build
botder Aug 5, 2023
ec9afae
Bump build
botder Aug 18, 2023
4deba70
Bump build
botder Aug 28, 2023
9c9653a
Visual Studio Update
MTABot Sep 13, 2023
009f411
Visual Studio Update
MTABot Sep 15, 2023
1f11d5d
Add new function: `fileListDir`
TracerDS Sep 18, 2023
ec9c522
Fixed Client/Server code differences
TracerDS Sep 18, 2023
2e5560f
Bump build
botder Oct 2, 2023
1e335e8
Added `isFile`, `isDirectory`
TracerDS Oct 5, 2023
3ea6e50
Updated due to review
TracerDS Oct 19, 2023
890dc31
Force rebuild #3
Dutchman101 Nov 21, 2023
5a38e71
Improved functionality
TracerDS Dec 8, 2023
b55ebc0
Added separate class for path-like functions
TracerDS Feb 2, 2024
6832b22
Bump build [ci skip]
botder Dec 10, 2023
f1a448c
Bump build [ci skip]
botder Dec 11, 2023
c7473fb
Force rebuild
Dutchman101 Dec 17, 2023
c5d2126
Force rebuild #2
Dutchman101 Dec 17, 2023
f88c681
Force rebuild #3
Dutchman101 Dec 17, 2023
db8018a
Force rebuild #4
Dutchman101 Dec 17, 2023
b2571d6
Rebased branch
TracerDS Feb 5, 2024
919d8e5
Merge branch 'master' of https://github.com/multitheftauto/mtasa-blue…
TracerDS Mar 25, 2024
6d52a20
Updated code
TracerDS Mar 25, 2024
b4feb0e
Fixed Search & Replace issues
TracerDS Mar 27, 2024
b5b988a
Merge branch 'master' into 180923_Add-fileListDir
TracerDS Apr 7, 2024
aa057db
Merge branch 'master' into 180923_Add-fileListDir
TracerDS May 23, 2024
61ac532
Merge branch 'master' into 180923_Add-fileListDir
Dutchman101 May 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
TracerDS marked this conversation as resolved.
Show resolved Hide resolved
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;
TracerDS marked this conversation as resolved.
Show resolved Hide resolved
}

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;
TracerDS marked this conversation as resolved.
Show resolved Hide resolved
}

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;
TracerDS marked this conversation as resolved.
Show resolved Hide resolved

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