Skip to content
Open
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
52 changes: 52 additions & 0 deletions engine/Poseidon/IO/Streams/QBStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ std::string NormalizeAddonBankPath(const char* name)

return collapsed.substr(sep + 1);
}

bool IsContentRoot(const std::string& part)
{
return EqualPathComponent(part, "addons", 6) || EqualPathComponent(part, "anims", 5) ||
EqualPathComponent(part, "dta", 3);
}

// The game-root forms of a collapsed path: the path itself and, when its first folder is not a
// content root, the rest of it. That first folder names where the pack was installed (Res, or the
// author's mod folder), and the pack may sit in any enabled mod instead.
std::vector<std::string> GameRootForms(const std::string& collapsed)
{
std::vector<std::string> forms;
if (collapsed.empty())
return forms;
forms.push_back(collapsed);
const size_t sep = collapsed.find('\\');
if (sep != std::string::npos && !IsContentRoot(collapsed.substr(0, sep)))
forms.push_back(collapsed.substr(sep + 1));
return forms;
}
} // namespace

std::string ResolveModOverride(const char* relPath)
Expand All @@ -197,6 +218,37 @@ std::string ResolveModOverride(const char* relPath)
return context.resolved;
}

std::string ResolveGameRootFolder(const char* folder, const char* file)
{
const std::string path = std::string(folder) + "\\" + file;
const std::string collapsed = CollapseParentDirs(path.c_str());
const size_t fileLen = strlen(file) + 1;
for (const std::string& form : GameRootForms(collapsed))
{
if (QIFStream::FileExists(form.c_str()))
return form.substr(0, form.size() - fileLen + 1);
const std::string modCopy = ResolveModOverride(form.c_str());
if (!modCopy.empty())
return modCopy.substr(0, modCopy.size() - fileLen + 1);
}
if (!GUseFileBanks)
return {};
for (const std::string& form : GameRootForms(collapsed))
{
const size_t sep = form.find('\\');
if (sep == std::string::npos)
continue;
const std::string root = form.substr(0, sep);
if (!EqualPathComponent(root, "addons", 6) && !EqualPathComponent(root, "dta", 3))
continue;
const std::string bankPath = form.substr(sep + 1);
QFBank* bank = QIFStreamB::AutoBank(bankPath.c_str());
if (bank && bank->FileExists(bankPath.c_str() + bank->GetPrefix().GetLength()))
return bankPath.substr(0, bankPath.size() - fileLen + 1);
}
return {};
}

QFBank::QFBank()
{
EXCLUSIVE();
Expand Down
6 changes: 6 additions & 0 deletions engine/Poseidon/IO/Streams/QBStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ extern RString GFileBankPrefix;
// Resolve a base-relative path to an enabled mod's copy of the same file, "" if none.
std::string ResolveModOverride(const char* relPath);

// Where a game-root folder holding `file` is found, for paths leaving their prefix through ".." as
// island cutscenes[] entries do: loose in the game root or an enabled mod, else packed in an addons or
// dta bank, also with a leading install folder other than a content root (Res, or a pack's own mod
// folder) dropped. Returns the folder with a trailing separator, in a form QIFStreamB opens; "" if none.
std::string ResolveGameRootFolder(const char* folder, const char* file);

} // namespace Poseidon

using ::Poseidon::QIFStreamB;
Expand Down
38 changes: 31 additions & 7 deletions engine/Poseidon/UI/OptionsUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <string.h>
#include <optional>
#include <string>
#include <Poseidon/Foundation/Common/Filenames.hpp>
#include <Poseidon/Foundation/Common/FltOpts.hpp>
#include <Poseidon/Foundation/Common/GamePaths.hpp>
#include <Poseidon/Foundation/Containers/Array.hpp>
Expand Down Expand Up @@ -988,11 +989,20 @@ static bool FindCutsceneRootCallback(RStringB dir, void* ctx)
return true;
}

RString RootAnchoredCutsceneName(RString name)
{
// A cutscenes[] entry starting with a separator starts at the game root, like one leaving anims
// through "..".
if (name[0] == '\\' || name[0] == '/')
return RString("..") + name;
return name;
}

RString ResolveCutsceneAnimsSubdir(RString world, RString name)
{
// A "..\addons\..." cutscene is a packed-bank path resolved through the base anims/ mount; only a
// bare cutscene name lives in a mod's Anims/ folder, hosted like any other mission type.
if (strpbrk(name, "\\/"))
// A cutscene leaving anims through ".." is a game-root path; one inside anims may live in a mod's
// Anims/ folder, hosted like any other mission.
if (strstr(name, ".."))
return GetAnimsDir();
char rel[512];
snprintf(rel, sizeof(rel), "Anims\\%s.%s\\mission.sqm", (const char*)name, (const char*)world);
Expand All @@ -1003,6 +1013,20 @@ RString ResolveCutsceneAnimsSubdir(RString world, RString name)
return GetAnimsDir();
}

CutsceneLocation ResolveCutsceneLocation(RString world, RString name)
{
if (!strstr(name, ".."))
return {ResolveCutsceneAnimsSubdir(world, name), name};

const char* intro = Foundation::GetFilenameExt(name);
const RString parent = GetAnimsDir() + RString(name, static_cast<int>(intro - (const char*)name));
const std::string folder =
ResolveGameRootFolder(parent, RString(intro) + RString(".") + world + RString("\\mission.sqm"));
if (folder.empty())
return {GetAnimsDir(), name};
return {RString(folder.c_str()), RString(intro)};
}

RString CreateSingleMissionBank(RString filename)
{
// suppose filename is without extension (.pbo)
Expand Down Expand Up @@ -2243,12 +2267,12 @@ void StartRandomCutscene(RString world)
int n = cls.GetSize();
int i = toIntFloor(n * GRandGen.RandomValue());

RString name = cls[i];
RString name = RootAnchoredCutsceneName(cls[i]);

// A mod that ships Anims/<name>.<world> hosts the cutscene from its own root; else the base
// anims/ bank. Clear the base dir first so SetMission's own reads resolve from the same place.
// Clear the base dir first so SetMission's own reads resolve from the located folder.
SetBaseDirectory("");
SetMission(world, name, ResolveCutsceneAnimsSubdir(world, name));
const CutsceneLocation location = ResolveCutsceneLocation(world, name);
SetMission(world, location.name, location.subdir);

ParseIntro();

Expand Down
9 changes: 9 additions & 0 deletions engine/Poseidon/UI/OptionsUICommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ void AddDeadIdentity(RString identity);
void SetMissionDisplayName(RString campaign, RString battle, RString mission, RString displayName);
void AddMission(RString campaign, RString battle, RString mission, RString displayName);
void StartRandomCutscene(RString world);
RString RootAnchoredCutsceneName(RString name);
RString ResolveCutsceneAnimsSubdir(RString world, RString name);

// Where a CfgWorlds cutscenes[] entry lives: SetMission(world, name, subdir) opens it.
struct CutsceneLocation
{
RString subdir;
RString name;
};
CutsceneLocation ResolveCutsceneLocation(RString world, RString name);
RString GetCampaignMissionDirectory(RString campaign, RString mission);

struct CountedString : SerializeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,35 @@ TEST_CASE("QIFStreamB FileExist - mod path escaping its prefix", "[qstream][path
std::filesystem::remove_all(root, ec);
}

// ResolveGameRootFolder finds a game-root folder in an enabled mod, with a leading install folder that is
// not a content root (Res, or a pack's own mod folder name) dropped; plain FileExist does not.
TEST_CASE("ResolveGameRootFolder - game-root folder found in an enabled mod", "[qstream][path][mod]")
{
const std::filesystem::path root = std::filesystem::temp_directory_path() / "cwr_gamerootfolder";
const std::filesystem::path modDir = root / "@islands";
const std::filesystem::path animDir = modDir / "AddOns" / "Isle_Anim" / "intro.isle";
const char payload[] = "sqm";

std::error_code ec;
std::filesystem::remove_all(root, ec);
REQUIRE(std::filesystem::create_directories(animDir, ec));
REQUIRE(Poseidon::WriteFileUtf8((animDir / "mission.sqm").string().c_str(), payload, sizeof(payload)));

Poseidon::ModSystem::SetModPath(modDir.string().c_str());

const std::string expected = modDir.string() + "/addons/Isle_Anim/";
CHECK(Poseidon::ResolveGameRootFolder("anims/..\\addons\\Isle_Anim", "intro.isle\\mission.sqm") == expected);
CHECK(Poseidon::ResolveGameRootFolder("anims/..\\Res\\addons\\Isle_Anim", "intro.isle\\mission.sqm") == expected);
CHECK(Poseidon::ResolveGameRootFolder("anims/..\\@OldName\\addons\\Isle_Anim", "intro.isle\\mission.sqm") ==
expected);
CHECK(Poseidon::ResolveGameRootFolder("anims/..\\dta\\Isle_Anim", "intro.isle\\mission.sqm").empty());
CHECK(Poseidon::ResolveGameRootFolder("addons\\Isle_Anim", "intro.isle\\mission.sqm").empty());
CHECK_FALSE(QIFStreamB::FileExist("anims/..\\Res\\addons\\Isle_Anim\\intro.isle\\mission.sqm"));

Poseidon::ModSystem::SetModPath("");
std::filesystem::remove_all(root, ec);
}

TEST_CASE("QIFStreamB FileExist - filesystem fallback", "[qstream][path][bankexist]")
{
REQUIRE_FIXTURE("qstream/test_input.txt");
Expand Down
Loading
Loading