mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-09-02 12:32:40 +02:00
Add fs.isLink
This commit is contained in:
@@ -17,6 +17,7 @@ namespace Platform
|
|||||||
bool Stat(ByteString filename);
|
bool Stat(ByteString filename);
|
||||||
bool FileExists(ByteString filename);
|
bool FileExists(ByteString filename);
|
||||||
bool DirectoryExists(ByteString directory);
|
bool DirectoryExists(ByteString directory);
|
||||||
|
bool IsLink(ByteString path);
|
||||||
/**
|
/**
|
||||||
* @return true on success
|
* @return true on success
|
||||||
*/
|
*/
|
||||||
|
@@ -82,6 +82,26 @@ bool DirectoryExists(ByteString directory)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IsLink(ByteString path)
|
||||||
|
{
|
||||||
|
struct stat s;
|
||||||
|
if (stat(path.c_str(), &s) == 0)
|
||||||
|
{
|
||||||
|
if (s.st_mode & S_IFLNK)
|
||||||
|
{
|
||||||
|
return true; // Is path
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false; // Is file or something else
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false; // Doesn't exist
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool RemoveFile(ByteString filename)
|
bool RemoveFile(ByteString filename)
|
||||||
{
|
{
|
||||||
return remove(filename.c_str()) == 0;
|
return remove(filename.c_str()) == 0;
|
||||||
|
@@ -11,18 +11,18 @@
|
|||||||
#include <shlwapi.h>
|
#include <shlwapi.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <crtdbg.h>
|
#include <crtdbg.h>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace Platform
|
namespace Platform
|
||||||
{
|
{
|
||||||
ByteString GetCwd()
|
ByteString GetCwd()
|
||||||
{
|
{
|
||||||
ByteString cwd;
|
ByteString cwd;
|
||||||
wchar_t *cwdPtr = _wgetcwd(NULL, 0);
|
auto cwdPtr = std::unique_ptr<wchar_t, decltype(&free)>(_wgetcwd(NULL, 0), free);
|
||||||
if (cwdPtr)
|
if (cwdPtr)
|
||||||
{
|
{
|
||||||
cwd = WinNarrow(cwdPtr);
|
cwd = WinNarrow(cwdPtr.get());
|
||||||
}
|
}
|
||||||
free(cwdPtr);
|
|
||||||
return cwd;
|
return cwd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ long unsigned int GetTime()
|
|||||||
bool Stat(ByteString filename)
|
bool Stat(ByteString filename)
|
||||||
{
|
{
|
||||||
struct _stat s;
|
struct _stat s;
|
||||||
if (_stat(filename.c_str(), &s) == 0)
|
if (_wstat(WinWiden(filename).c_str(), &s) == 0)
|
||||||
{
|
{
|
||||||
return true; // Something exists, be it a file, directory, link, etc.
|
return true; // Something exists, be it a file, directory, link, etc.
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,7 @@ bool Stat(ByteString filename)
|
|||||||
bool FileExists(ByteString filename)
|
bool FileExists(ByteString filename)
|
||||||
{
|
{
|
||||||
struct _stat s;
|
struct _stat s;
|
||||||
if (_stat(filename.c_str(), &s) == 0)
|
if (_wstat(WinWiden(filename).c_str(), &s) == 0)
|
||||||
{
|
{
|
||||||
if(s.st_mode & S_IFREG)
|
if(s.st_mode & S_IFREG)
|
||||||
{
|
{
|
||||||
@@ -80,7 +80,7 @@ bool FileExists(ByteString filename)
|
|||||||
bool DirectoryExists(ByteString directory)
|
bool DirectoryExists(ByteString directory)
|
||||||
{
|
{
|
||||||
struct _stat s;
|
struct _stat s;
|
||||||
if (_stat(directory.c_str(), &s) == 0)
|
if (_wstat(WinWiden(directory).c_str(), &s) == 0)
|
||||||
{
|
{
|
||||||
if(s.st_mode & S_IFDIR)
|
if(s.st_mode & S_IFDIR)
|
||||||
{
|
{
|
||||||
@@ -97,6 +97,26 @@ bool DirectoryExists(ByteString directory)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IsLink(ByteString path)
|
||||||
|
{
|
||||||
|
struct _stat s;
|
||||||
|
if (_wstat(WinWiden(path).c_str(), &s) == 0)
|
||||||
|
{
|
||||||
|
if (GetFileAttributesW(WinWiden(path).c_str()) & FILE_ATTRIBUTE_REPARSE_POINT)
|
||||||
|
{
|
||||||
|
return true; // Is directory
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false; // Is file or something else
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false; // Doesn't exist
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool RemoveFile(ByteString filename)
|
bool RemoveFile(ByteString filename)
|
||||||
{
|
{
|
||||||
return _wremove(WinWiden(filename).c_str()) == 0;
|
return _wremove(WinWiden(filename).c_str()) == 0;
|
||||||
|
@@ -4002,6 +4002,14 @@ int LuaScriptInterface::graphics_setClipRect(lua_State * l)
|
|||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int fsIsLink(lua_State * l)
|
||||||
|
{
|
||||||
|
auto dirname = tpt_lua_checkByteString(l, 1);
|
||||||
|
bool ret = Platform::IsLink(dirname);
|
||||||
|
lua_pushboolean(l, ret);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
void LuaScriptInterface::initFileSystemAPI()
|
void LuaScriptInterface::initFileSystemAPI()
|
||||||
{
|
{
|
||||||
//Methods
|
//Methods
|
||||||
@@ -4010,6 +4018,7 @@ void LuaScriptInterface::initFileSystemAPI()
|
|||||||
{"exists", fileSystem_exists},
|
{"exists", fileSystem_exists},
|
||||||
{"isFile", fileSystem_isFile},
|
{"isFile", fileSystem_isFile},
|
||||||
{"isDirectory", fileSystem_isDirectory},
|
{"isDirectory", fileSystem_isDirectory},
|
||||||
|
{"isLink", fsIsLink},
|
||||||
{"makeDirectory", fileSystem_makeDirectory},
|
{"makeDirectory", fileSystem_makeDirectory},
|
||||||
{"removeDirectory", fileSystem_removeDirectory},
|
{"removeDirectory", fileSystem_removeDirectory},
|
||||||
{"removeFile", fileSystem_removeFile},
|
{"removeFile", fileSystem_removeFile},
|
||||||
|
Reference in New Issue
Block a user