Skip to content

Commit

Permalink
Fix for backward compatibility with mod loader (#9)
Browse files Browse the repository at this point in the history
* fix for backward compatibility with ModLoader
* Fix for crash when script creation from file fails.
  • Loading branch information
MiranDMC authored Oct 27, 2023
1 parent ba095df commit 7f674cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
24 changes: 15 additions & 9 deletions source/CScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,27 +983,33 @@ namespace CLEO
}

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

TRACE("Searching for cleo scripts");

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

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

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

if (cs != nullptr)
{
cs->SetDebugMode(NativeScriptsDebugMode); // inherit from global state
}

for (void* func : GetInstance().GetCallbacks(eCallbackId::ScriptsLoaded))
{
typedef void WINAPI callback(void);
Expand Down
36 changes: 35 additions & 1 deletion source/FileEnumerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
template<typename T>
void FilesWalk(const char* directory, const char* extension, T callback)
{
try
/*try
{
for (auto& it : std::filesystem::directory_iterator(directory))
{
Expand All @@ -28,5 +28,39 @@ void FilesWalk(const char* directory, const char* extension, T callback)
catch (const std::exception& ex)
{
TRACE("Error while iterating directory: %s", ex.what());
}*/

// Re-implemented with raw search APIs for compatibility with ModLoader.
// The ModLoader should be updated anyway to solve potential file access problems in more advanced Cleo scripts

std::string pattern = directory;
if(!pattern.empty() && pattern.back() != '\\') pattern.push_back('\\');

std::string_view baseDir = pattern;

pattern.push_back('*');
if (extension != nullptr) pattern.append(extension);

WIN32_FIND_DATA wfd = { 0 };
HANDLE hSearch = FindFirstFile(pattern.c_str(), &wfd);

if (hSearch == INVALID_HANDLE_VALUE)
{
TRACE("No files found in: %s", pattern.c_str());
return;
}
do
{
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
continue; // skip directories
}

//auto result = std::filesystem::absolute(std::string(baseDir) + wfd.cFileName);
auto result = std::filesystem::path(std::string(baseDir) + wfd.cFileName); // ModLoader supports only relative paths...
callback(result.string().c_str(), result.filename().string().c_str());

} while (FindNextFile(hSearch, &wfd));

FindClose(hSearch);
}

0 comments on commit 7f674cd

Please sign in to comment.