Skip to content

Commit

Permalink
Add support for loading Thunderstore mods natively (#503)
Browse files Browse the repository at this point in the history
Allows for loading Thunderstore mods directly from a separate directory called `packages`.
  • Loading branch information
GeckoEidechse authored Jul 16, 2023
1 parent 9f9e3a9 commit 0309af1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions NorthstarDLL/mods/modmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ void ModManager::LoadMods()
// ensure dirs exist
fs::remove_all(GetCompiledAssetsPath());
fs::create_directories(GetModFolderPath());
fs::create_directories(GetThunderstoreModFolderPath());
fs::create_directories(GetRemoteModFolderPath());

m_DependencyConstants.clear();
Expand Down Expand Up @@ -616,6 +617,31 @@ void ModManager::LoadMods()
if (fs::exists(dir.path() / "mod.json"))
modDirs.push_back(dir.path());

// Special case for Thunderstore mods dir
std::filesystem::directory_iterator thunderstoreModsDir = fs::directory_iterator(GetThunderstoreModFolderPath());
// Set up regex for `AUTHOR-MOD-VERSION` pattern
std::regex pattern(R"(.*\\([a-zA-Z0-9_]+)-([a-zA-Z0-9_]+)-(\d+\.\d+\.\d+))");
for (fs::directory_entry dir : thunderstoreModsDir)
{
fs::path modsDir = dir.path() / "mods"; // Check for mods folder in the Thunderstore mod
// Use regex to match `AUTHOR-MOD-VERSION` pattern
if (!std::regex_match(dir.path().string(), pattern))
{
spdlog::warn("The following directory did not match 'AUTHOR-MOD-VERSION': {}", modsDir.string());
continue; // skip loading mod that doesn't match
}
if (fs::exists(modsDir) && fs::is_directory(modsDir))
{
for (fs::directory_entry subDir : fs::directory_iterator(modsDir))
{
if (fs::exists(subDir.path() / "mod.json"))
{
modDirs.push_back(subDir.path());
}
}
}
}

for (fs::path modDir : modDirs)
{
// read mod json file
Expand Down Expand Up @@ -1049,6 +1075,10 @@ fs::path GetModFolderPath()
{
return fs::path(GetNorthstarPrefix() + MOD_FOLDER_SUFFIX);
}
fs::path GetThunderstoreModFolderPath()
{
return fs::path(GetNorthstarPrefix() + THUNDERSTORE_MOD_FOLDER_SUFFIX);
}
fs::path GetRemoteModFolderPath()
{
return fs::path(GetNorthstarPrefix() + REMOTE_MOD_FOLDER_SUFFIX);
Expand Down
2 changes: 2 additions & 0 deletions NorthstarDLL/mods/modmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <filesystem>

const std::string MOD_FOLDER_SUFFIX = "/mods";
const std::string THUNDERSTORE_MOD_FOLDER_SUFFIX = "/packages";
const std::string REMOTE_MOD_FOLDER_SUFFIX = "/runtime/remote/mods";
const fs::path MOD_OVERRIDE_DIR = "mod";
const std::string COMPILED_ASSETS_SUFFIX = "/runtime/compiled";
Expand Down Expand Up @@ -176,6 +177,7 @@ class ModManager

fs::path GetModFolderPath();
fs::path GetRemoteModFolderPath();
fs::path GetThunderstoreModFolderPath();
fs::path GetCompiledAssetsPath();

extern ModManager* g_pModManager;

0 comments on commit 0309af1

Please sign in to comment.