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

Improve handling of mod loading failures #3981

Merged
merged 3 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
77 changes: 62 additions & 15 deletions src/modding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ static std::vector<std::string> mod_names_list;
static std::vector<Sha256> mod_hash_list;


static void addLoadedMod(std::string modname, std::string filename);
static void addLoadedMod(std::string modname, std::string filename, const std::string& fullRealPath);
static bool hasLoadedModRealPath(const std::string& fullRealPath);


static inline std::vector<std::string> split(std::string const &str, std::string const &sep)
Expand Down Expand Up @@ -107,27 +108,65 @@ size_t addSubdirs(const char *basedir, const char *subdir, const bool appendToPa
{
size_t numAddedMods = 0;
const WzString subdir_platformDependent = convertToPlatformDependentPath(subdir);
std::string tmpFullModRealPath;
WZ_PHYSFS_enumerateFiles(subdir, [&](const char *i) -> bool {
#ifdef DEBUG
debug(LOG_NEVER, "Examining subdir: [%s]", i);
#endif // DEBUG
if (i[0] != '.' && (!checkList || std::find(checkList->begin(), checkList->end(), i) != checkList->end()))
{
char tmpstr[PATH_MAX];
snprintf(tmpstr, sizeof(tmpstr), "%s%s%s%s", basedir, subdir_platformDependent.toUtf8().c_str(), PHYSFS_getDirSeparator(), i);
// platform-dependent notation
tmpFullModRealPath = basedir;
tmpFullModRealPath += subdir_platformDependent.toUtf8();
tmpFullModRealPath += PHYSFS_getDirSeparator();
tmpFullModRealPath += i;
#ifdef DEBUG
debug(LOG_NEVER, "Adding [%s] to search path", tmpstr);
debug(LOG_NEVER, "Adding [%s] to search path", tmpFullModRealPath.c_str());
#endif // DEBUG
if (addToModList)
if (hasLoadedModRealPath(tmpFullModRealPath))
{
std::string filename = astringf("%s/%s", subdir, i); // platform-independent notation
addLoadedMod(i, std::move(filename));
char buf[256];
snprintf(buf, sizeof(buf), "mod: %s", i);
addDumpInfo(buf);
debug(LOG_INFO, "Already loaded: %s, skipping", tmpFullModRealPath.c_str());
return true; // continue
}
if (PHYSFS_mount(tmpFullModRealPath.c_str(), NULL, appendToPath) != 0) // platform-dependent notation
{
numAddedMods++;
if (addToModList)
{
std::string filename = astringf("%s/%s", subdir, i); // platform-independent notation
addLoadedMod(i, std::move(filename), tmpFullModRealPath);
char buf[256];
snprintf(buf, sizeof(buf), "mod: %s", i);
addDumpInfo(buf);
}
}
else
{
// failed to mount mod
code_part log_level = LOG_WZ;
#if defined(WZ_PHYSFS_2_1_OR_GREATER)
auto errorCode = PHYSFS_getLastErrorCode();
switch (errorCode)
{
case PHYSFS_ERR_CORRUPT:
log_level = LOG_ERROR;
break;
case PHYSFS_ERR_NOT_FOUND:
default:
log_level = LOG_WZ;
break;
}
const char* pErrStr = PHYSFS_getErrorByCode(errorCode);
#else
const char* pErrStr = WZ_PHYSFS_getLastError();
#endif
if (!pErrStr)
{
pErrStr = "<unknown error>";
}
debug(log_level, "Failed to load mod from path: %s, %s", tmpFullModRealPath.c_str(), pErrStr);

}
PHYSFS_mount(tmpstr, NULL, appendToPath); // platform-dependent notation
numAddedMods++;
}
return true; // continue
});
Expand Down Expand Up @@ -198,15 +237,22 @@ bool hasCampaignMods()
return !campaign_mods.empty();
}

static void addLoadedMod(std::string modname, std::string filename)
static void addLoadedMod(std::string modname, std::string filename, const std::string& fullRealPath)
{
// Note, findHashOfFile won't work right now, since the search paths aren't set up until after all calls to addSubdirs, see rebuildSearchPath in init.cpp.
loaded_mods.emplace_back(std::move(modname), std::move(filename));
loaded_mods.emplace_back(std::move(modname), std::move(filename), fullRealPath);
mod_list.clear();
mod_names_list.clear();
mod_hash_list.clear();
}

static bool hasLoadedModRealPath(const std::string& fullRealPath)
{
return std::any_of(loaded_mods.begin(), loaded_mods.end(), [fullRealPath](const WzMods::LoadedMod& loadedMod) -> bool {
return loadedMod.fullRealPath == fullRealPath;
});
}

void clearLoadedMods()
{
loaded_mods.clear();
Expand Down Expand Up @@ -252,9 +298,10 @@ std::vector<std::string> const &getModNamesList()
return mod_names_list;
}

WzMods::LoadedMod::LoadedMod(const std::string& name, const std::string& filename)
WzMods::LoadedMod::LoadedMod(const std::string& name, const std::string& filename, const std::string& fullRealPath)
: name(name)
, filename(filename)
, fullRealPath(fullRealPath)
{ }

Sha256& WzMods::LoadedMod::getHash()
Expand Down
3 changes: 2 additions & 1 deletion src/modding.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ struct LoadedMod
{
std::string name;
std::string filename;
std::string fullRealPath;

LoadedMod(const std::string& name, const std::string& filename);
LoadedMod(const std::string& name, const std::string& filename, const std::string& fullRealPath);
Sha256& getHash();

private:
Expand Down
Loading