Skip to content

Commit

Permalink
Add a fallback for unknown gamemodes (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
IncPlusPlus authored Jan 8, 2022
1 parent c31ab2c commit 4f81f8c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
5 changes: 4 additions & 1 deletion titanfall2-rp/GameDetailsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public static (string, string, Timestamps?, Assets? assets) GetMultiplayerDetail
DateTime gameOpenTimestamp)
{
var mpStats = tf2Api.GetMultiPlayerGameStats();
var gameDetails = tf2Api.GetGameMode().ToFriendlyString();
var gameMode = tf2Api.GetGameMode();
var gameDetails = gameMode == GameMode.UNKNOWN_GAME_MODE
? $"Game mode: {tf2Api.GetGameModeCodeName()}"
: gameMode.ToFriendlyString();
var gameState = mpStats.GetGameState();
var timestamps = new Timestamps(gameOpenTimestamp);
var map = Map.FromName(tf2Api.GetMultiplayerMapName());
Expand Down
16 changes: 16 additions & 0 deletions titanfall2-rp/MpGameStats/UnknownGameMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Process.NET;

namespace titanfall2_rp.MpGameStats
{
public class UnknownGameMode : MpStats
{
public UnknownGameMode(Titanfall2Api tf2Api, ProcessSharp processSharp) : base(tf2Api, processSharp)
{
}

public override string GetGameState()
{
return "In a match";
}
}
}
8 changes: 4 additions & 4 deletions titanfall2-rp/MpStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public Faction GetCurrentFaction()
/// more appropriate string.
/// </summary>
/// <returns>a string representing the current state of the match</returns>
public virtual String GetGameState()
public virtual string GetGameState()
{
return $"Score: {GetMyTeamScore()} - {GetEnemyTeamScore()}";
}
Expand Down Expand Up @@ -262,15 +262,15 @@ public static MpStats Of(Titanfall2Api titanfall2Api, ProcessSharp sharp)
GameMode.turbo_ttdm => new TitanBrawl(titanfall2Api, sharp),
GameMode.alts => new LastTitanStanding(titanfall2Api, sharp),
GameMode.turbo_lts => new LastTitanStanding(titanfall2Api, sharp),
_ => ReportGameModeFailure(gameMode)
_ => ReportGameModeFailure(gameMode, titanfall2Api, sharp)
};
}

private static MpStats ReportGameModeFailure(GameMode gameMode)
private static MpStats ReportGameModeFailure(GameMode gameMode, Titanfall2Api titanfall2Api, ProcessSharp sharp)
{
var e = new ArgumentOutOfRangeException(nameof(gameMode), gameMode, $"Unknown game mode '{gameMode}'.");
SegmentManager.SegmentManager.TrackEvent(TrackableEvent.GameplayInfoFailure, e);
throw e;
return new UnknownGameMode(titanfall2Api, sharp);
}

/// <summary>
Expand Down
12 changes: 8 additions & 4 deletions titanfall2-rp/Titanfall2API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ public string GetGameModeName()
return GetGameMode().ToFriendlyString();
}

public GameMode GetGameMode()
public string GetGameModeCodeName()
{
_ensureInit();
string gameModeCodeName = _sharp!.Memory.Read(EngineDllBaseAddress + 0x13984088, Encoding.UTF8, 15);
return GameModeMethods.GetGameMode(gameModeCodeName);
return _sharp!.Memory.Read(EngineDllBaseAddress + 0x13984088, Encoding.UTF8, 15);
}

public GameMode GetGameMode()
{
return GameModeMethods.GetGameMode(GetGameModeCodeName());
}

/// <returns>the name of the multiplayer map being played</returns>
Expand Down Expand Up @@ -209,7 +213,7 @@ private void _populateFields(ProcessSharp sharp)
ServerDllBaseAddress = GetModuleBaseAddress(sharp.Native, "server.dll");
IsNorthstarClient = System.Diagnostics.Process.GetProcessById(sharp.Native.Id)
.GetModules()
.Any(module => module.ModuleName.Equals("Northstar.dll"));
.Any(module => module.ModuleName?.Equals("Northstar.dll") ?? false);
}
}
}
13 changes: 9 additions & 4 deletions titanfall2-rp/enums/GameMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ namespace titanfall2_rp.enums
[SuppressMessage("ReSharper", "IdentifierTypo")]
public enum GameMode
{
/// <summary>
/// This enum value represents a game mode that is unrecognized
/// </summary>
UNKNOWN_GAME_MODE,

/// <summary>
/// Coliseum
/// </summary>
Expand Down Expand Up @@ -267,9 +272,8 @@ public static GameMode GetGameMode(string gameModeCodeName)
{
var parseSuccess = Enum.TryParse(typeof(GameMode), gameModeCodeName, true, out var mode);
if (!parseSuccess)
throw new ArgumentException("Unrecognized game mode '" + gameModeCodeName + "'.");
return (GameMode)(mode ??
throw new ArgumentException("Unrecognized game mode '" + gameModeCodeName + "'."));
return GameMode.UNKNOWN_GAME_MODE;
return (GameMode)((mode) ?? GameMode.UNKNOWN_GAME_MODE);
}

/// <summary>
Expand Down Expand Up @@ -314,7 +318,8 @@ public static string ToFriendlyString(this GameMode gameMode)
GameMode.fastball => "Fastball",
GameMode.hs => "Hide and Seek",
GameMode.ctf_comp => "Competitive CTF",
_ => throw new ArgumentOutOfRangeException(nameof(gameMode), gameMode, null)
_ => throw new ArgumentOutOfRangeException(nameof(gameMode), gameMode,
"No friendly string for gamemode")
};
}
}
Expand Down

0 comments on commit 4f81f8c

Please sign in to comment.