Skip to content
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
9 changes: 4 additions & 5 deletions Source.Engine/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public FSReturnCode LoadSearchPaths(ref CFSSearchPathsInit initInfo) {

initInfo.ModPath = initInfo.DirectoryName;

#if !GMOD_DLL
const string GAMEINFOPATH_TOKEN = "|gameinfo_path|";
const string BASESOURCEPATHS_TOKEN = "|all_source_engine_paths|";

Expand Down Expand Up @@ -76,13 +77,12 @@ public FSReturnCode LoadSearchPaths(ref CFSSearchPathsInit initInfo) {
string[] pathIDs = cur.Name.Split('+');
for (int i = 0; i < pathIDs.Length; i++) {
pathIDs[i] = pathIDs[i].Trim();
#if GMOD_DLL
initInfo.FileSystem.AddSearchPath(absSearchPath, pathIDs[i], SearchPathAdd.ToTail, PathGroupName.GModCore);
#else
initInfo.FileSystem.AddSearchPath(absSearchPath, pathIDs[i]);
#endif
}
}
#else
initInfo.FileSystem.GMOD_SetupDefaultPaths(baseDir, initInfo.DirectoryName);
#endif

initInfo.FileSystem.MarkPathIDByRequestOnly("executable_path", true);
initInfo.FileSystem.MarkPathIDByRequestOnly("gamebin", true);
Expand All @@ -92,7 +92,6 @@ public FSReturnCode LoadSearchPaths(ref CFSSearchPathsInit initInfo) {
initInfo.FileSystem.MarkPathIDByRequestOnly("mod_write", true);

#if GMOD_DLL
initInfo.FileSystem.GMOD_SetupDefaultPaths(baseDir, initInfo.DirectoryName);
initInfo.FileSystem.DoFilesystemRefresh();
#endif

Expand Down
47 changes: 43 additions & 4 deletions Source.Filesystem/BaseFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,13 +1062,52 @@ public void AddVPKFileFromPath(ReadOnlySpan<char> vpk, ReadOnlySpan<char> path,
AddVPKFile(vpk, path, (SearchPathAdd)id, PathGroupName.Default);
}

private void GMOD_AddPath(string path, PathGroupName group, params ReadOnlySpan<string> pathIDs) {
foreach (string pathID in pathIDs)
AddSearchPath(path, pathID, SearchPathAdd.ToTail, group);
}

private void GMOD_AddVPK(string vpk, PathGroupName group, params ReadOnlySpan<string> pathIDs) {
if (!File.Exists($"{vpk}_dir.vpk"))
return;
GMOD_AddPath($"{vpk}.vpk", group, pathIDs);
}

public void GMOD_SetupDefaultPaths(ReadOnlySpan<char> path, ReadOnlySpan<char> game) {
string workshop = Path.Combine(new string(game), "workshop");
AddSearchPath(new AddonSearchPath(g_AddonFileSystem, workshop), "GAME", SearchPathAdd.ToHead, PathGroupName.GModCore);
AddSearchPath(new AddonSearchPath(g_AddonFileSystem, workshop), "workshop", SearchPathAdd.ToHead, PathGroupName.GModCore);
AddSearchPath(new AddonSearchPath(g_AddonFileSystem, workshop), "thirdparty", SearchPathAdd.ToHead, PathGroupName.GModCore);
string baseDir = new(path);
string gameDir = new(game);
bool vpkBuild = FileExists("vpk_build", "BASE_PATH");

GMOD_AddPath(Path.Combine(gameDir, "bin"), PathGroupName.EngineCore, "GAMEBIN");

string workshop = Path.Combine(gameDir, "workshop");
AddSearchPath(new AddonSearchPath(g_AddonFileSystem, workshop), "GAME", SearchPathAdd.ToTail, PathGroupName.GModCore);
AddSearchPath(new AddonSearchPath(g_AddonFileSystem, workshop), "workshop", SearchPathAdd.ToTail, PathGroupName.GModCore);
AddSearchPath(new AddonSearchPath(g_AddonFileSystem, workshop), "thirdparty", SearchPathAdd.ToTail, PathGroupName.GModCore);

GMOD_AddVPK(Path.Combine(gameDir, "garrysmod"), PathGroupName.GModCore, "MOD", "GAME", "garrysmod");
if (vpkBuild)
GMOD_AddPath(Path.Combine(gameDir, "overrides"), PathGroupName.GModCore, "GAME", "garrysmod");
GMOD_AddPath(gameDir, PathGroupName.GModCore, "MOD", "MOD_WRITE", "DEFAULT_WRITE_PATH", "GAME", "GAME_WRITE", "garrysmod");

MarkPathIDByRequestOnly("workshop", true);
MarkPathIDByRequestOnly("thirdparty", true);
MarkPathIDByRequestOnly("garrysmod", true);

string sourceEngine = Path.Combine(baseDir, "sourceengine");
foreach (string vpk in (ReadOnlySpan<string>)["hl2_misc", "hl2_sound_misc", "hl2_sound_vo_english", "hl2_textures"])
GMOD_AddVPK(Path.Combine(sourceEngine, vpk), PathGroupName.SourceSDK, "GAME");
GMOD_AddPath(sourceEngine, PathGroupName.SourceSDK, "GAME");

string platform = Path.Combine(baseDir, "platform");
GMOD_AddVPK(Path.Combine(platform, "platform_misc"), PathGroupName.SourceSDK, "GAME", "PLATFORM");
GMOD_AddPath(platform, PathGroupName.SourceSDK, "GAME", "PLATFORM");

GMOD_AddPath(Path.Combine(gameDir, "download"), PathGroupName.Downloads, "GAME", "DOWNLOAD");

GMOD_AddVPK(Path.Combine(gameDir, "fallbacks"), PathGroupName.Fallbacks, "GAME", "MOD");
if (vpkBuild)
GMOD_AddPath(Path.Combine(gameDir, "fallbacks"), PathGroupName.Fallbacks, "GAME", "MOD");
}

public void GMOD_FixPathCase(Span<char> a) {
Expand Down
32 changes: 31 additions & 1 deletion Source.Filesystem/GarrysMod/GameDepotSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ void UnmountGame(ref IGameDepotSystem.Information game) {
game.Mounted = false;
}

static string FallbackVPK(string folder) => Path.Combine(AppContext.BaseDirectory, "sourceengine", $"content_{folder}_dir.vpk");

static void MountFallback(ref IGameDepotSystem.Information game) {
string vpk = FallbackVPK(game.Folder);
if (!File.Exists(vpk)) return;
Msg($"Mounting game '{game.Title}' as fallback ({game.Folder}, {(uint)game.AppID})\n");
g_FullFileSystem.AddSearchPath(vpk, "GAME", groupName: PathGroupName.GameContent);
g_FullFileSystem.AddSearchPath(vpk, game.Folder, groupName: PathGroupName.GameContent);
g_FullFileSystem.MarkPathIDByRequestOnly(game.Folder, true);
}

static void UnmountFallback(ref IGameDepotSystem.Information game) {
string vpk = FallbackVPK(game.Folder);
if (!File.Exists(vpk)) return;
g_FullFileSystem.RemoveSearchPath(vpk, "GAME");
g_FullFileSystem.RemoveSearchPath(vpk, game.Folder);
}

static void MountContentDir(string absDir, ReadOnlySpan<char> mountID) {
if (!Directory.Exists(absDir)) return;
foreach (string vpk in Directory.EnumerateFiles(absDir, "*_dir.vpk")) {
Expand Down Expand Up @@ -97,6 +115,12 @@ public void Refresh() {
if (game.Enabled)
MountGame(ref game);
}

for (int i = 0; i < games.Length; i++) {
ref IGameDepotSystem.Information game = ref games[i];
if (!game.Mounted && game.Bundled)
MountFallback(ref game);
}
}

private void LoadMountPrefs() {
Expand Down Expand Up @@ -126,7 +150,11 @@ private void LoadGameList() {
return;

foreach (KeyValues game in manifest) {
string[] extra = new string(game.GetString("extra")).Split(' ', StringSplitOptions.RemoveEmptyEntries);
List<string> extra = [];
KeyValues? extraKV = game.FindKey("extra");
if (extraKV != null)
foreach (KeyValues folder in extraKV)
extra.Add(new(folder.GetString()));
AppId_t appID = (AppId_t)uint.Parse(game.Name);
Games.Add(new IGameDepotSystem.Information {
AppID = appID,
Expand All @@ -150,6 +178,8 @@ private void UnmountAll() {
ref IGameDepotSystem.Information game = ref games[i];
if (game.Mounted)
UnmountGame(ref game);
else if (game.Bundled)
UnmountFallback(ref game);
}
}

Expand Down