Skip to content

Commit

Permalink
bf2042 support
Browse files Browse the repository at this point in the history
  • Loading branch information
zefanjajobse committed Mar 10, 2023
1 parent d09a5ae commit 213baab
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Battlefield rich presence/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static ServerInfo OldTitleServerInfo(string unescapedPlayerName, string g
return JsonConvert.DeserializeObject<ServerInfo> (responseContent);
}

public static ServerInfo GetBf5CurrentServer(string playerName)
public static ServerInfo GetCurrentServer(string playerName, Resources.Statics.Game game_name)
{
var payload = new
{
Expand All @@ -40,7 +40,8 @@ public static ServerInfo GetBf5CurrentServer(string playerName)
string jwtData = Jwt.Create(dataString);
string stringPayload = JsonConvert.SerializeObject(new { data = jwtData });
StringContent httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
HttpResponseMessage httpResponse = new HttpClient().PostAsync("https://api.gametools.network/currentserver/bf5", httpContent).Result;
var url = $"https://api.gametools.network/currentserver/{Resources.Statics.ShortGameName[game_name].ToLower()}";
HttpResponseMessage httpResponse = new HttpClient().PostAsync(url, httpContent).Result;
string responseContent = httpResponse.Content.ReadAsStringAsync().Result;
if (responseContent == "{}")
{
Expand Down
3 changes: 3 additions & 0 deletions Battlefield rich presence/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<setting name="GatherServerInfo" serializeAs="String">
<value>False</value>
</setting>
<setting name="bf2042" serializeAs="String">
<value />
</setting>
</BattlefieldRichPresence.Properties.Settings>
</userSettings>
</configuration>
17 changes: 13 additions & 4 deletions Battlefield rich presence/ChangePrensence/Frostbite3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void Update(DiscordRpcClient client, DateTime startTime, GameInfo
{
ServerInfo extraInfo = serverInfo;

if (gameInfo.Game != Resources.Statics.Game.Bf5)
if (!Resources.Statics.NewTitles.Contains(gameInfo.Game))
{
extraInfo = Api.GetServerInfo(gameInfo.ShortName, serverInfo.Name);
}
Expand Down Expand Up @@ -43,15 +43,24 @@ public static void Update(DiscordRpcClient client, DateTime startTime, GameInfo

String apiName = gameInfo.ShortName.ToLower();

if (gameInfo.Game != Resources.Statics.Game.Bf5)
if (!Resources.Statics.NewTitles.Contains(gameInfo.Game))
{
buttons.Add(new Button { Label = "Join", Url = $"https://joinme.click/g/{gameInfo.ShortName.ToLower()}/{extraInfo.GameId}" });
} else
}

if (gameInfo.Game == Resources.Statics.Game.Bf5)
{
apiName = "bfv";
}

buttons.Add(new Button { Label = "View server", Url = $"https://gametools.network/servers/{apiName}/gameid/{extraInfo.GameId}/pc" });
if (gameInfo.Game == Resources.Statics.Game.Bf2042)
{
buttons.Add(new Button { Label = "View server", Url = $"https://gametools.network/servers/{apiName}/serverid/{extraInfo.ServerId}/pc" });
} else
{
buttons.Add(new Button { Label = "View server", Url = $"https://gametools.network/servers/{apiName}/gameid/{extraInfo.GameId}/pc" });
}


presence.Buttons = buttons.ToArray();
client.SetPresence(presence);
Expand Down
4 changes: 3 additions & 1 deletion Battlefield rich presence/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void Refresh()
Bf3 = Settings.Default.bf3,
Bf4 = Settings.Default.bf4,
Bfh = Settings.Default.bfh,
Bf5 = Settings.Default.bf5
Bf5 = Settings.Default.bf5,
Bf2042 = Settings.Default.bf2042
};
}

Expand All @@ -45,6 +46,7 @@ public void Update()
Settings.Default.bf4 = PlayerNames.Bf4;
Settings.Default.bfh = PlayerNames.Bfh;
Settings.Default.bf5 = PlayerNames.Bf5;
Settings.Default.bf2042 = PlayerNames.Bf2042;
Settings.Default.Save();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Battlefield rich presence/DiscordPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ public void Update()
}
}
}
else if (gameInfo.Game == Statics.Game.Bf5)
else if (Statics.NewTitles.Contains(gameInfo.Game))
{
try
{
var playerName = (string)_config.PlayerNames[gameInfo.ShortName];
ServerInfo serverInfo = Api.GetBf5CurrentServer(playerName);
ServerInfo serverInfo = Api.GetCurrentServer(playerName, gameInfo.Game);
UpdatePresence(gameInfo, serverInfo);
}
catch (Exception)
Expand Down
3 changes: 2 additions & 1 deletion Battlefield rich presence/EditWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ private Structs.GamesPlayerName Clone(Structs.GamesPlayerName playerNames)
Bf3 = playerNames.Bf3,
Bf4 = playerNames.Bf4,
Bfh = playerNames.Bfh,
Bf5 = playerNames.Bf5
Bf5 = playerNames.Bf5,
Bf2042 = playerNames.Bf2042
};
}

Expand Down
14 changes: 13 additions & 1 deletion Battlefield rich presence/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Battlefield rich presence/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
<Setting Name="GatherServerInfo" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="bf2042" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
25 changes: 18 additions & 7 deletions Battlefield rich presence/Resources/Statics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public enum Game
Bf4,
Bfh,
Bf1,
Bf5
Bf5,
Bf2042,
}
// Regex group names need to match enum keys, since we need to map the matched group to an enum value
public static readonly string SupportedGamesRegex = @"^(?:(?:battlefield(?:(?'Bf1'\u2122 1)|(?'Bf5'\u2122 V)|(?'Bf3' 3\u2122)|(?'Bf4' 4)|(?'Bfh' Hardline)|(?'Bfbc2': bad company 2)|(?'Bfvietnam' vietnam)))|(?:bf(?:(?'Bf2'2)|(?'Bf2142'2142)) \(v1\.[\.\-0-9]+, pid: [0-9]+\))|(?'Bf1942'bf1942 \(Ver: \w+, \d+ \w+\.? \d+(?: [:0-9]+)?\)))$";
public static readonly string SupportedGamesRegex = @"^(?:(?:battlefield(?:(?'Bf1'\u2122 1)|(?'Bf5'\u2122 V)|(?'Bf2042'\u2122 2042)|(?'Bf3' 3\u2122)|(?'Bf4' 4)|(?'Bfh' Hardline)|(?'Bfbc2': bad company 2)|(?'Bfvietnam' vietnam)))|(?:bf(?:(?'Bf2'2)|(?'Bf2142'2142)) \(v1\.[\.\-0-9]+, pid: [0-9]+\))|(?'Bf1942'bf1942 \(Ver: \w+, \d+ \w+\.? \d+(?: [:0-9]+)?\)))$";
public static readonly Dictionary<Game, string> ShortGameName = new Dictionary<Game, string>
{
{ Game.Bf1942, "Bf1942" },
Expand All @@ -31,7 +32,8 @@ public enum Game
{ Game.Bf4, "Bf4" },
{ Game.Bfh, "Bfh" },
{ Game.Bf1, "Bf1" },
{ Game.Bf5, "Bf5" }
{ Game.Bf5, "Bf5" },
{ Game.Bf2042, "Bf2042" }
};
public static readonly Dictionary<Game, string> FullGameName = new Dictionary<Game, string>
{
Expand All @@ -44,15 +46,22 @@ public enum Game
{ Game.Bf4, "Battlefield 4" },
{ Game.Bfh, "Battlefield Hardline" },
{ Game.Bf1, "Battlefield 1" },
{ Game.Bf5, "Battlefield V" }
{ Game.Bf5, "Battlefield V" },
{ Game.Bf2042, "Battlefield 2042" }
};
public static readonly List<Game> NewTitles = new List<Game>
{
Game.Bf5,
Game.Bf2042,
};
public static readonly List<Game> NameChangeUiGames = new List<Game>
{
Game.Bfbc2,
Game.Bf3,
Game.Bf4,
Game.Bfh,
Game.Bf5
Game.Bf5,
Game.Bf2042
};

public static readonly List<Game> GameDataReaderGames = new List<Game>
Expand All @@ -66,7 +75,8 @@ public enum Game
{
Game.Bf4,
Game.Bf1,
Game.Bf5
Game.Bf5,
Game.Bf2042
};
public static readonly List<Game> JoinmeDotClickGames = new List<Game>
{
Expand Down Expand Up @@ -97,7 +107,8 @@ public enum Game
{ Game.Bf4, "998710324922437702" },
{ Game.Bfh, "999057759876161587" },
{ Game.Bf1, "998710285605019708" },
{ Game.Bf5, "1009379254926053458" }
{ Game.Bf5, "1009379254926053458" },
{ Game.Bf2042, "1083853115545108632" }
};


Expand Down
1 change: 1 addition & 0 deletions Battlefield rich presence/Structs/GamesPlayerName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal class GamesPlayerName
public string Bf4 { get; set; }
public string Bfh { get; set; }
public string Bf5 { get; set; }
public string Bf2042 { get; set; }

public object this[string propertyName]
{
Expand Down
1 change: 1 addition & 0 deletions Battlefield rich presence/Structs/ServerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class ServerInfo
public string MapLabel { get; set; }
public string JoinLinkWeb { get; set; }
public string GameId { get; set; }
public string ServerId { get; set; }
public string GetPlayerCountString()
{
string playerString = $"{NumPlayers}";
Expand Down

0 comments on commit 213baab

Please sign in to comment.