Skip to content

Commit

Permalink
Remove unused code, update bin path on macos/linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan committed Oct 11, 2024
1 parent 2f914c3 commit 3ed497d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 77 deletions.
139 changes: 64 additions & 75 deletions GmodInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ private static bool TryGetSteamVDFPath(out string vdfPath)
return false;
}

private static bool TryGetMountedBeta(out bool isX64)
{
if (TryGetSteamVDFPath(out string vdfPath))
{
try
{
string? vdfDirPath = Path.GetDirectoryName(vdfPath);
if (vdfDirPath != null)
{
string gmodManifiestPath = Path.Join(vdfDirPath, "appmanifest_4000.acf");
if (File.Exists(gmodManifiestPath))
{
FileStream gmodManifestFile = File.OpenRead(gmodManifiestPath);
VdfDeserializer deserializer = new();
dynamic result = deserializer.Deserialize(gmodManifestFile);

isX64 = result?.AppState?.UserConfig?.BetaKey == "x86-64";
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Could not find Garry's Mod manifest, assuming branch\n" + ex.Message);
}
}

isX64 = false;
return false;
}

internal static bool TryGetGmodPath(out string gmodPath, bool toBin = true)
{
try
Expand Down Expand Up @@ -127,14 +157,39 @@ internal static bool TryGetGmodPath(out string gmodPath, bool toBin = true)

if (toBin)
{
string gmodPathX64 = Path.Combine(gmodPath, "bin/win64/gmod.exe");
string gmodPathX86 = Path.Combine(gmodPath, "bin/gmod.exe");
if (File.Exists(gmodPathX64))
gmodPath = gmodPathX64;
else if (File.Exists(gmodPathX86))
gmodPath = gmodPathX86;
bool gotBeta = TryGetMountedBeta(out bool isX64);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (gotBeta)
{
gmodPath = isX64
? Path.Combine(gmodPath, "bin/win64/gmod.exe")
: Path.Combine(gmodPath, "bin/gmod.exe");
}
else
{
// get x64 bin path in priority
string gmodPathX64 = Path.Combine(gmodPath, "bin/win64/gmod.exe");
string gmodPathX86 = Path.Combine(gmodPath, "bin/gmod.exe");
if (File.Exists(gmodPathX64))
gmodPath = gmodPathX64;
else if (File.Exists(gmodPathX86))
gmodPath = gmodPathX86;
}

if (!File.Exists(gmodPath))
gmodPath = Path.Combine(gmodPath, "hl2.exe");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
gmodPath = Path.Combine(gmodPath, "hl2_osx");
}
else
gmodPath = Path.Combine(gmodPath, "hl2.exe");
{
// TODO: Figure out where and what the linux bin is called
// also figure out how to handle srcds
gmodPath = Path.Combine(gmodPath, "hl2");
}
}

return true;
Expand All @@ -154,51 +209,6 @@ internal static bool TryGetGmodPath(out string gmodPath, bool toBin = true)
}
}

/// <summary>
/// Tries to parse the current user gmod console keys, and use them further for minimizing GTerm
/// </summary>
/// <returns>Found console keys</returns>
internal static List<ConsoleKey> GetConsoleBindings()
{
try
{
List<ConsoleKey> consoleTriggerKeys = [];

if (!TryGetGmodPath(out string gmodBinPath)) return consoleTriggerKeys;

int? index = gmodBinPath.IndexOf("GarrysMod");
if (index == null || index == -1) return consoleTriggerKeys;

string baseGmodPath = gmodBinPath.Substring(0, index.Value + "GarrysMod".Length);
string cfgPath = Path.Combine(baseGmodPath, "garrysmod/cfg/config.cfg");
if (!File.Exists(cfgPath)) return consoleTriggerKeys;

string[] cfgLines = File.ReadAllLines(cfgPath);
foreach (string cfgLine in cfgLines)
{
string[] lineChunks = cfgLine.Split(' ')
.Where(s => !string.IsNullOrWhiteSpace(s))
.Select(s => s.Replace("\"", string.Empty).Trim())
.ToArray();

if (lineChunks.Length >= 3 && lineChunks[0] == "bind" && lineChunks[2].Contains("toggleconsole", StringComparison.CurrentCulture))
{
string keyName = lineChunks[1].ToUpper()[1] + lineChunks[1].Substring(1).ToLower();
if (Enum.TryParse(keyName, out ConsoleKey key))
consoleTriggerKeys.Add(key);
}
}

LocalLogger.WriteLine("Found console bindings: ", string.Join("\t", consoleTriggerKeys.Select(t => t.ToString())));
return consoleTriggerKeys;
}
catch (Exception ex)
{
LocalLogger.WriteLine("Could not get Gmod console bindings: ", ex.Message);
return [];
}
}

private static string GetBinaryFileName(bool isX64)
{
string moduleName = "gmsv_xconsole_";
Expand Down Expand Up @@ -255,29 +265,8 @@ private static bool IsGmodX64(string gmodBinPath)
bool isX64 = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || gmodBinPath.Contains("win64", StringComparison.CurrentCulture);

// Fetch the gmod manifest to make a safer assumption of the current branch
if (TryGetSteamVDFPath(out string vdfPath))
{
try
{
string? vdfDirPath = Path.GetDirectoryName(vdfPath);
if (vdfDirPath != null)
{
string gmodManifiestPath = Path.Join(vdfDirPath, "appmanifest_4000.acf");
if (File.Exists(gmodManifiestPath))
{
FileStream gmodManifestFile = File.OpenRead(gmodManifiestPath);
VdfDeserializer deserializer = new();
dynamic result = deserializer.Deserialize(gmodManifestFile);

isX64 = result?.AppState?.UserConfig?.BetaKey == "x86-64";
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Could not find Garry's Mod manifest, assuming branch\n" + ex.Message);
}
}
if (TryGetMountedBeta(out bool isBetaX64))
isX64 = isBetaX64;

return isX64;
}
Expand Down
2 changes: 0 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ private static void ProcessUserInput()
{
Console.WriteLine(); // on UNIX this prevents a terminal deadlock

List<ConsoleKey> gmodConsoleKeys = GmodInterop.GetConsoleBindings();
while (true)
{
if (!Console.KeyAvailable) {
Expand Down Expand Up @@ -214,7 +213,6 @@ private static void ProcessUserInput()

break;

case ConsoleKey key when gmodConsoleKeys.Contains(key):
case ConsoleKey.Escape:
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
IntPtr hWndConsole = Win32Extensions.GetConsoleWindow();
Expand Down

0 comments on commit 3ed497d

Please sign in to comment.