Skip to content

Commit

Permalink
Stop depending on strings to be in English (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
IncPlusPlus authored Oct 17, 2021
1 parent 4e6e98e commit 2d354aa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
2 changes: 1 addition & 1 deletion titanfall2-rp/GameDetailsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static (string, string, Timestamps?, Assets? assets) GetMultiplayerDetail
var assets = new Assets
{
LargeImageKey = tf2Api.GetMultiplayerMapName(),
LargeImageText = tf2Api.GetFriendlyMapName(),
LargeImageText = tf2Api.GetMultiplayerMapName(),
SmallImageKey = playerInTitan ? tf2Api.GetTitan().GetAssetName() : mpStats.GetCurrentFaction().GetAssetName(),
SmallImageText = playerInTitan ? tf2Api.GetTitan().ToFriendlyString() : mpStats.GetCurrentFaction().ToFriendlyString(),
};
Expand Down
12 changes: 7 additions & 5 deletions titanfall2-rp/PresenceUpdateThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Timers;
using DiscordRPC;
using log4net;
using titanfall2_rp.enums;
using titanfall2_rp.SegmentManager;
using titanfall2_rp.updater;

Expand All @@ -18,7 +19,8 @@ public class PresenceUpdateThread
private readonly Titanfall2Api _tf2Api;
private readonly EventWaitHandle _userExitEvent;

public PresenceUpdateThread(DiscordRpcClient discordRpcClient, Titanfall2Api tf2Api, AutoResetEvent userExitEvent)
public PresenceUpdateThread(DiscordRpcClient discordRpcClient, Titanfall2Api tf2Api,
AutoResetEvent userExitEvent)
{
_discordRpcClient = discordRpcClient;
_tf2Api = tf2Api;
Expand Down Expand Up @@ -116,7 +118,7 @@ private static (string, string, Timestamps?, Assets? assets) GetDetailsAndState(
Timestamps? timestamps = null;
Assets? assets = null;

if (tf2Api.GetGameModeAndMapName().Equals("Main Menu"))
if (tf2Api.GetMultiplayerMapName() == "")
{
gameDetails = "Main Menu";
timestamps = new Timestamps(ProcessNetApi.StartTimestamp);
Expand All @@ -126,10 +128,10 @@ private static (string, string, Timestamps?, Assets? assets) GetDetailsAndState(
gameDetails = "In a lobby";
timestamps = new Timestamps(ProcessNetApi.StartTimestamp);
}
else if (tf2Api.GetGameModeName().Contains("Campaign"))
else if (tf2Api.GetGameMode() == GameMode.solo)
{
gameDetails = "Campaign (" + tf2Api.GetSinglePlayerDifficulty() + ")";
gameState = tf2Api.GetFriendlyMapName();
gameDetails = $"{tf2Api.GetGameModeName()} ({tf2Api.GetSinglePlayerDifficulty()})";
gameState = tf2Api.GetSinglePlayerMapName();
timestamps = new Timestamps(ProcessNetApi.StartTimestamp);
assets = GameDetailsProvider.GetSinglePlayerAssets(tf2Api);
}
Expand Down
20 changes: 3 additions & 17 deletions titanfall2-rp/Titanfall2API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Specialized;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using log4net;
Expand All @@ -18,7 +17,6 @@ namespace titanfall2_rp
public class Titanfall2Api
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod()!.DeclaringType);
private static readonly Regex GameModeAndMapRegex = new Regex("Playing (.*) on (.*)");
private ProcessSharp? _sharp;
public IntPtr EngineDllBaseAddress { get; private set; }
public IntPtr ClientDllBaseAddress { get; private set; }
Expand Down Expand Up @@ -76,24 +74,10 @@ public string GetGameModeAndMapName()
return _sharp!.Memory.Read(EngineDllBaseAddress + 0x1397AC46, Encoding.UTF8, 50);
}

public string GetFriendlyMapName()
{
_ensureInit();
var gameModeAndMapName = GetGameModeAndMapName();
var m = GameModeAndMapRegex.Match(gameModeAndMapName);
return m.Success
? m.Groups[2].Value
: throw new ApplicationException($"Failed to recognize map name from string '{gameModeAndMapName}'.");
}

public string GetGameModeName()
{
_ensureInit();
var gameModeAndMapName = GetGameModeAndMapName();
var m = GameModeAndMapRegex.Match(gameModeAndMapName);
return m.Success
? m.Groups[1].Value
: throw new ApplicationException($"Failed to recognize game mode from string '{gameModeAndMapName}'.");
return GetGameMode().ToFriendlyString();
}

public GameMode GetGameMode()
Expand All @@ -103,6 +87,8 @@ public GameMode GetGameMode()
return GameModeMethods.GetGameMode(gameModeCodeName);
}

/// <returns>the name of the multiplayer map being played</returns>
/// <remarks>this also works for SP maps and when on the main menu, the string is empty</remarks>
public string GetMultiplayerMapName()
{
_ensureInit();
Expand Down

0 comments on commit 2d354aa

Please sign in to comment.