Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 7f2f078

Browse files
committed
Add LuaMan::FileExists, since FileOpen now spits out an error if the file doesn't exist so can't be cleanly used as a check (and shouldn't anyway)
1 parent bc4555b commit 7f2f078

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ This can be accessed via the new Lua (R/W) `SettingsMan` property `AIUpdateInter
637637

638638
- Added `Activity` Lua function `GetPlayerController`, which gets you the `Controller` used for GUI stuff and when there's no `Actor` selected in an `Activity`. Be aware, it's very likely possible to cause problems by doing dumb things with this.
639639

640+
- Added `LuaMan` Lua function `FileExists`, which lets you check whether a specified file exists. Like with `FileOpen`, the file must be inside a folder ending in `.rte`.
641+
640642
</details>
641643

642644
<details><summary><b>Changed</b></summary>

Managers/LuaMan.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace RTE {
5959
.def_readonly("TempEntities", &LuaMan::m_TempEntityVector, luabind::return_stl_iterator)
6060
.def("GetDirectoryList", &LuaMan::DirectoryList, luabind::return_stl_iterator)
6161
.def("GetFileList", &LuaMan::FileList, luabind::return_stl_iterator)
62+
.def("FileExists", &LuaMan::FileExists)
6263
.def("FileOpen", &LuaMan::FileOpen)
6364
.def("FileClose", &LuaMan::FileClose)
6465
.def("FileReadLine", &LuaMan::FileReadLine)
@@ -522,6 +523,17 @@ namespace RTE {
522523
return m_FileOrDirectoryPaths;
523524
}
524525

526+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
527+
528+
bool LuaMan::FileExists(const std::string &fileName) {
529+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(fileName);
530+
if ((fullPath.find("..") == std::string::npos) && (fullPath.find(System::GetModulePackageExtension()) != std::string::npos)) {
531+
return std::filesystem::exists(fullPath);
532+
}
533+
534+
return false;
535+
}
536+
525537
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
526538

527539
int LuaMan::FileOpen(const std::string &fileName, const std::string &accessMode) {

Managers/LuaMan.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ namespace RTE {
194194
/// <returns>A vector of the files in relativeDirectory.</returns>
195195
const std::vector<std::string> & FileList(const std::string &relativeDirectory);
196196

197+
/// <summary>
198+
/// Returns whether or not the specified file exists. You can only check for files inside .rte folders in the working directory.
199+
/// </summary>
200+
/// <param name="fileName">Path to the file. All paths are made absolute by adding current working directory to the specified path.</param>
201+
/// <returns>Whether or not the specified file exists.</returns>
202+
bool FileExists(const std::string &fileName);
203+
197204
/// <summary>
198205
/// Opens a file or creates one if it does not exist, depending on access mode. You can open files only inside .rte folders in the working directly. You can't open more that c_MaxOpenFiles file simultaneously.
199206
/// On Linux will attempt to open a file case insensitively.

0 commit comments

Comments
 (0)