Skip to content

Commit

Permalink
add bf5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
zefanjajobse committed Aug 17, 2022
1 parent e272ed6 commit 4035c8d
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 31 deletions.
4 changes: 4 additions & 0 deletions Battlefield rich presence/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static ServerInfo getBf5CurrentServer(string playerName)
webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
string postData = json_serializer.Serialize(new { data = jwtData });
string data = webClient.UploadString(new Uri("https://api.gametools.network/currentserver/bf5"), "POST", postData);
if (data == "{}")
{
throw new Exception("not in a server");
}
return json_serializer.Deserialize<ServerInfo>(data);
}
}
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 @@ -47,6 +47,9 @@
<setting name="bfbc2" serializeAs="String">
<value />
</setting>
<setting name="bf5" serializeAs="String">
<value />
</setting>
</BattlefieldRichPresence.Properties.Settings>
</userSettings>
</configuration>
36 changes: 27 additions & 9 deletions Battlefield rich presence/ChangePrensence/Frostbite3.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using BattlefieldRichPresence.Structs;
using DiscordRPC;

Expand All @@ -8,15 +9,20 @@ internal class Frostbite3
{
public static void Update(DiscordRpcClient client, DateTime startTime, GameInfo gameInfo, ServerInfo serverInfo)
{
ServerInfo extraInfo = Api.GetServerInfo(gameInfo.ShortName, serverInfo.Name);
ServerInfo extraInfo = serverInfo;

if (gameInfo.Game != Resources.Statics.Game.Bf5)
{
extraInfo = Api.GetServerInfo(gameInfo.ShortName, serverInfo.Name);
}

serverInfo.MaxPlayers = extraInfo.MaxPlayers;
string state = serverInfo.GetPlayerCountString();
state += $" - {extraInfo.MapLabel}";

//Set the rich presence
//Call this as many times as you want and anywhere in your code.
client.SetPresence(new RichPresence
RichPresence presence = new RichPresence
{
Details = $"{serverInfo.Name}",
State = state,
Expand All @@ -26,17 +32,29 @@ public static void Update(DiscordRpcClient client, DateTime startTime, GameInfo
},
Assets = new Assets
{
LargeImageKey = gameInfo.ShortName,
LargeImageKey = gameInfo.ShortName.ToLower(),
LargeImageText = gameInfo.FullName,
SmallImageKey = "gt",
SmallImageText = "Battlefield rich presence"
},
Buttons = new[] //$"{textBox10.Text}"
{
new Button { Label = "Join", Url = $"https://joinme.click/g/{gameInfo.ShortName}/{extraInfo.GameId}" },
new Button { Label = "View server", Url = $"https://gametools.network/servers/{gameInfo.ShortName}/gameid/{extraInfo.GameId}/pc" }
}
});
};

List<Button> buttons = new List<Button>();

String api_name = gameInfo.ShortName.ToLower();

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

buttons.Add(new Button { Label = "View server", Url = $"https://gametools.network/servers/{api_name}/gameid/{extraInfo.GameId}/pc" });

presence.Buttons = buttons.ToArray();
client.SetPresence(presence);
}
}
}
2 changes: 1 addition & 1 deletion Battlefield rich presence/ChangePrensence/OlderTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void Update(DiscordRpcClient client, DateTime startTime, GameInfo
},
Assets = new Assets
{
LargeImageKey = gameInfo.ShortName,
LargeImageKey = gameInfo.ShortName.ToLower(),
LargeImageText = gameInfo.FullName,
SmallImageKey = "gt",
SmallImageText = "Battlefield rich presence"
Expand Down
2 changes: 2 additions & 0 deletions Battlefield rich presence/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void Refresh()
Bf3 = Settings.Default.bf3,
Bf4 = Settings.Default.bf4,
Bfh = Settings.Default.bfh,
Bf5 = Settings.Default.bf5
};
}

Expand All @@ -38,6 +39,7 @@ public void Update()
Settings.Default.bf3 = PlayerNames.Bf3;
Settings.Default.bf4 = PlayerNames.Bf4;
Settings.Default.bfh = PlayerNames.Bfh;
Settings.Default.bf5 = PlayerNames.Bf5;
Settings.Default.Save();
}
}
Expand Down
21 changes: 17 additions & 4 deletions Battlefield rich presence/DiscordPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void UpdatePresenceInMenu(GameInfo gameInfo)
},
Assets = new Assets
{
LargeImageKey = gameInfo.ShortName,
LargeImageKey = gameInfo.ShortName.ToLower(),
LargeImageText = gameInfo.FullName,
SmallImageKey = "gt",
SmallImageText = "Battlefield rich presence"
Expand All @@ -83,7 +83,7 @@ private void UpdatePresenceStatusUnknown(GameInfo gameInfo, string reason)
},
Assets = new Assets
{
LargeImageKey = gameInfo.ShortName,
LargeImageKey = gameInfo.ShortName.ToLower(),
LargeImageText = gameInfo.FullName,
SmallImageKey = "gt",
SmallImageText = "Battlefield rich presence"
Expand Down Expand Up @@ -144,12 +144,25 @@ public void Update()
}
}
}
else if (gameInfo.Game == Statics.Game.Bf5)
{
try
{
var playerName = (string)_config.PlayerNames[gameInfo.ShortName];
ServerInfo serverInfo = Api.getBf5CurrentServer(playerName);
UpdatePresence(gameInfo, serverInfo);
}
catch (Exception)
{
UpdatePresenceInMenu(gameInfo);
}
}
else if (gameInfo.IsRunning && gameInfo.Game == Statics.Game.Bf2)
{
try
{
var playerName = GameDataReaders.Bf2.ReadActivePlayer().OnlineName;
ServerInfo serverInfo = Api.OldTitleServerInfo(playerName, gameInfo.ShortName);
ServerInfo serverInfo = Api.OldTitleServerInfo(playerName, gameInfo.ShortName.ToLower());
UpdatePresence(gameInfo, serverInfo);
}
catch (Exception)
Expand All @@ -162,7 +175,7 @@ public void Update()
try
{
var playerName = (string)_config.PlayerNames[gameInfo.ShortName];
ServerInfo serverInfo = Api.OldTitleServerInfo(playerName, gameInfo.ShortName);
ServerInfo serverInfo = Api.OldTitleServerInfo(playerName, gameInfo.ShortName.ToLower());
UpdatePresence(gameInfo, serverInfo);
}
catch (Exception)
Expand Down
7 changes: 4 additions & 3 deletions Battlefield rich presence/EditWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public EditWindow()
InitializeComponent();
_config = new Config();
_current = Clone(_config.PlayerNames);
GameSelector.DataSource = Statics.BflistDotIoGames;
GameSelector.DataSource = Statics.nameChangeUiGames;
FormBorderStyle = FormBorderStyle.FixedSingle;
FormClosing += EditFormClosing;
}
Expand All @@ -31,13 +31,14 @@ private Structs.GamesPlayerName Clone(Structs.GamesPlayerName PlayerNames)
Bf3 = PlayerNames.Bf3,
Bf4 = PlayerNames.Bf4,
Bfh = PlayerNames.Bfh,
Bf5 = PlayerNames.Bf5
};
}

private void EditFormClosing(object sender, FormClosingEventArgs e)
{
bool hasChanges = false;
foreach (Statics.Game game in Statics.BflistDotIoGames)
foreach (Statics.Game game in Statics.nameChangeUiGames)
{
if (!string.Equals(_current[game.ToString()], _config.PlayerNames[game.ToString()]))
{
Expand Down Expand Up @@ -83,7 +84,7 @@ private void PlayerNameBox_TextChanged(object sender, EventArgs e)

private void ChangeAllButton_Click(object sender, EventArgs e)
{
foreach (Statics.Game game in Statics.BflistDotIoGames)
foreach (Statics.Game game in Statics.nameChangeUiGames)
{
_current[game.ToString()] = PlayerNameBox.Text;
Save();
Expand Down
12 changes: 12 additions & 0 deletions 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 @@ -23,5 +23,8 @@
<Setting Name="bfbc2" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="bf5" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
34 changes: 20 additions & 14 deletions Battlefield rich presence/Resources/Statics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ public enum Game
Bf3,
Bf4,
Bfh,
Bf1
Bf1,
Bf5
}
// 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)|(?'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{3}, \d+ \w{3} \d+ [:0-9]+\)))$";
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{3}, \d+ \w{3} \d+ [:0-9]+\)))$";
public static readonly Dictionary<Game, string> ShortGameName = new Dictionary<Game, string>
{
{ Game.Bf1942, "bf1942" },
{ Game.Bfvietnam, "bfvietnam" },
{ Game.Bf2, "bf2" },
{ Game.Bf2142, "bf2142" },
{ Game.Bfbc2, "bfbc2" },
{ Game.Bf3, "bf3" },
{ Game.Bf4, "bf4" },
{ Game.Bfh, "bfh" },
{ Game.Bf1, "bf1" },
{ Game.Bf1942, "Bf1942" },
{ Game.Bfvietnam, "Bfvietnam" },
{ Game.Bf2, "Bf2" },
{ Game.Bf2142, "Bf2142" },
{ Game.Bfbc2, "Bfbc2" },
{ Game.Bf3, "Bf3" },
{ Game.Bf4, "Bf4" },
{ Game.Bfh, "Bfh" },
{ Game.Bf1, "Bf1" },
{ Game.Bf5, "Bf5" }
};
public static readonly Dictionary<Game, string> FullGameName = new Dictionary<Game, string>
{
Expand All @@ -42,21 +44,24 @@ public enum Game
{ Game.Bf4, "Battlefield 4" },
{ Game.Bfh, "Battlefield Hardline" },
{ Game.Bf1, "Battlefield 1" },
{ Game.Bf5, "Battlefield V" }
};
public static readonly List<Game> BflistDotIoGames = new List<Game>
public static readonly List<Game> nameChangeUiGames = new List<Game>
{
Game.Bf1942,
Game.Bfvietnam,
Game.Bf2142,
Game.Bfbc2,
Game.Bf3,
Game.Bf4,
Game.Bfh
Game.Bfh,
Game.Bf5
};
public static readonly List<Game> Frostbite3Games = new List<Game>
{
Game.Bf4,
Game.Bf1
Game.Bf1,
Game.Bf5
};
public static readonly List<Game> JoinmeDotClickGames = new List<Game>
{
Expand Down Expand Up @@ -87,6 +92,7 @@ public enum Game
{ Game.Bf4, "998710324922437702" },
{ Game.Bfh, "999057759876161587" },
{ Game.Bf1, "998710285605019708" },
{ Game.Bf5, "1009379254926053458" }
};
}
}
1 change: 1 addition & 0 deletions Battlefield rich presence/Structs/GamesPlayerName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal class GamesPlayerName
public string Bf3 { get; set; }
public string Bf4 { get; set; }
public string Bfh { get; set; }
public string Bf5 { get; set; }

public object this[string propertyName]
{
Expand Down

0 comments on commit 4035c8d

Please sign in to comment.