Skip to content

Commit 7f674cd

Browse files
authored
Fix for backward compatibility with mod loader (#9)
* fix for backward compatibility with ModLoader * Fix for crash when script creation from file fails.
1 parent ba095df commit 7f674cd

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

source/CScriptEngine.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -983,27 +983,33 @@ namespace CLEO
983983
}
984984

985985
// [game root]\cleo
986-
std::string scriptsDir = CFileMgr::ms_rootDirName;
986+
/*std::string scriptsDir = CFileMgr::ms_rootDirName;
987987
if (!scriptsDir.empty() && scriptsDir.back() != '\\') scriptsDir.push_back('\\');
988-
scriptsDir += "cleo";
988+
scriptsDir += "cleo";*/
989+
std::string scriptsDir = "cleo"; // TODO: restore to absolute path when ModLoader is updated to support CLEO5
989990

990991
TRACE("Searching for cleo scripts");
991992

992-
FilesWalk(scriptsDir.c_str(), cs_ext, [this](const char* fullPath, const char* filename) {
993-
auto cs = LoadScript(fullPath);
994-
cs->SetDebugMode(NativeScriptsDebugMode); // inherit from global state
993+
CCustomScript* cs = nullptr;
994+
FilesWalk(scriptsDir.c_str(), cs_ext, [&](const char* fullPath, const char* filename) {
995+
cs = LoadScript(fullPath);
995996
});
996997

997-
FilesWalk(scriptsDir.c_str(), cs4_ext, [this](const char* fullPath, const char* filename) {
998-
auto cs = LoadScript(fullPath);
998+
FilesWalk(scriptsDir.c_str(), cs4_ext, [&](const char* fullPath, const char* filename) {
999+
cs = LoadScript(fullPath);
9991000
if (cs) cs->SetCompatibility(CLEO_VER_4);
10001001
});
10011002

1002-
FilesWalk(scriptsDir.c_str(), cs3_ext, [this](const char* fullPath, const char* filename) {
1003-
auto cs = LoadScript(fullPath);
1003+
FilesWalk(scriptsDir.c_str(), cs3_ext, [&](const char* fullPath, const char* filename) {
1004+
cs = LoadScript(fullPath);
10041005
if (cs) cs->SetCompatibility(CLEO_VER_3);
10051006
});
10061007

1008+
if (cs != nullptr)
1009+
{
1010+
cs->SetDebugMode(NativeScriptsDebugMode); // inherit from global state
1011+
}
1012+
10071013
for (void* func : GetInstance().GetCallbacks(eCallbackId::ScriptsLoaded))
10081014
{
10091015
typedef void WINAPI callback(void);

source/FileEnumerator.h

+35-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
template<typename T>
55
void FilesWalk(const char* directory, const char* extension, T callback)
66
{
7-
try
7+
/*try
88
{
99
for (auto& it : std::filesystem::directory_iterator(directory))
1010
{
@@ -28,5 +28,39 @@ void FilesWalk(const char* directory, const char* extension, T callback)
2828
catch (const std::exception& ex)
2929
{
3030
TRACE("Error while iterating directory: %s", ex.what());
31+
}*/
32+
33+
// Re-implemented with raw search APIs for compatibility with ModLoader.
34+
// The ModLoader should be updated anyway to solve potential file access problems in more advanced Cleo scripts
35+
36+
std::string pattern = directory;
37+
if(!pattern.empty() && pattern.back() != '\\') pattern.push_back('\\');
38+
39+
std::string_view baseDir = pattern;
40+
41+
pattern.push_back('*');
42+
if (extension != nullptr) pattern.append(extension);
43+
44+
WIN32_FIND_DATA wfd = { 0 };
45+
HANDLE hSearch = FindFirstFile(pattern.c_str(), &wfd);
46+
47+
if (hSearch == INVALID_HANDLE_VALUE)
48+
{
49+
TRACE("No files found in: %s", pattern.c_str());
50+
return;
3151
}
52+
do
53+
{
54+
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
55+
{
56+
continue; // skip directories
57+
}
58+
59+
//auto result = std::filesystem::absolute(std::string(baseDir) + wfd.cFileName);
60+
auto result = std::filesystem::path(std::string(baseDir) + wfd.cFileName); // ModLoader supports only relative paths...
61+
callback(result.string().c_str(), result.filename().string().c_str());
62+
63+
} while (FindNextFile(hSearch, &wfd));
64+
65+
FindClose(hSearch);
3266
}

0 commit comments

Comments
 (0)