community-network / Battlefield-rich-presence

Show your current Battlefield server in your Discord activity
MIT License
20 stars 2 forks source link

BF1942 and BFV process detection and con file dynamic file path lookup #4

Closed AnomalousNicole closed 1 year ago

AnomalousNicole commented 1 year ago

Recently, I installed Battlefield Rich Presence and noticed that it did not show game info in Discord while playing BF1942 and BFV. I cloned the repos for Battlefield Rich Presence and GameDataReader and debugged the code to figure out what was happening.

BF1942 had the following Battlefield Rich Presence issue that prevented game information from displaying in my profile in Discord.

Issue: Battlefield Rich Presence was not able to find the BF1942 game process using a RegEx to find the running process.

Fix: I modified Battlefield Rich Presence to look for any processes named "BF1942" and if one match is found then a GameInfo object will be returned from the IsRunning method.

Testing: After the fix above was applied to Battlefield Rich Presence, I built a debug version of Battlefield Rich Presence and ran BF1942 again. This time when BF1942 was run, Battlefield Rich Presence was displayed in my Discord profile. However, it only displayed the "In the menus" status in my profile. When I debugged the Battlefield Rich Presence further, I was able to determine that GameDataReader was passing back the message "Couldn't find configuration data. Is the game installed?" to Battlefield Rich Presence. This is what led me to debug the GameDataReader code.

BF1942 and BFV each had the GameDataReader issue below that was causing the con files for each game to not be found correctly.

Issue: GameDataReader was not able to find the GeneralOptions.con and Profile.con files located within the "mods\bf1942\settings" and "Mods\BfVietnam\settings" folders in each game install folder. GameDataReader was looking in the "%LOCALAPPDATA%\VirtualStore\Program Files (x86)\EA Games\Battlefield 1942\Mods\bf1942\settings" and "%LOCALAPPDATA%\VirtualStore\Program Files (x86)\EA Games\Battlefield Vietnam\Mods\BfVietnam\settings" folders for each game. These folders are used when BF1942 or BFV are installed under Program Files (x86) and the game does not have permission to write to the game install folder. However, when BF1942 or BFV is not installed under Program Files (x86) each game uses the "mods\bf1942\settings" or "Mods\BfVietnam\settings" located in that game's install folder.

Fix: I modified GameDataReader to dynamically lookup the path where the BF1942 or BFV processes are running from. Those paths are then used to check if the GeneralOptions.con and Profile.con files exist within the "mods\bf1942\settings" and "Mods\BfVietnam\settings" folders in each game install folder. If the GeneralOptions.con and Profile.con files are not found, then GameDataReader will check for the existence of the con files in the "%LOCALAPPDATA%\VirtualStore\Program Files (x86)\EA Games\" for each game.

Testing: After the fix above was applied to GameDataReader, I built a debug version of GameDataReader and then copied the GameDataReader DLL to the debug folder for Battlefield Rich Presence. I then ran Battlefield Rich Presence and also launched BF1942 and connected to a multiplayer server. Battlefield Rich Presence then correctly displayed the game and server information in my Discord profile. I repeated the same test for BFV and Battlefield Rich Presence also displayed the game and server information in Discord.

Note: The fix I created for Battlefield Rich Presence is dependent upon the fix for GameDataReader. In each program I have created my fixes in a branch called "bf1492-bfv-fixes". I have submitted pull requests on both the Battlefield Rich Presence and GameDataReader GitHub repo pages.

Below are screenshots of Battlefield Rich Presence displaying game information in Discord for Battlefield 1942 and Battlefield Vietnam.

Battlefield 1942 BattlefieldRichPresence-BF1942

Battlefield Vietnam BattlefieldRichPresence-BFV

Hopefully, the above information has been helpful in explaining the issues that were fixed in each program. Please let me know if more information is needed.

cetteup commented 1 year ago

Just to see why the regex-based process detection is failing, could you please post the exact title if your game window?

You can use the get-active-window-title.exe bundled with this release under tools: https://github.com/cetteup/0xQWERTY-client/releases/tag/v1.0.6

AnomalousNicole commented 1 year ago

Here are two examples of the window title for the BF1942 process.

This is the window title from the latest official version of the BF1942.exe: BF1942 (Ver: Tue, 19 Oct 2004 14:58:45)

This is the window title from the latest master server patch version of the BF1942.exe: BF1942 (Ver: Henk, 23 Feb. 2022)

cetteup commented 1 year ago

Not sure why Henk would change that (and not follow the original pattern), but we can just update the regular expression.


^(?:(?: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]+)?\)))
cetteup commented 1 year ago

@johnbrechbuhl85 Feel tree to just update the regex and force push the branch to only include that change. I can also do it on main, but then you'd not be listed as a contributor.

cetteup commented 1 year ago

Thanks! I cherry-picked the commit related to the regex from the PR branch. I'll create a new release with the bugfix.

AnomalousNicole commented 1 year ago

@cetteup I have reverted the BF1942 process lookup and updated the regular expression used to match the BF1942 process window title. I tested the change with the BF1942 exe files from 2004 and 2022. I will admit, I am not very experienced when it comes to using regular expressions and that is why I went the way of using the Process.GetProcessesByName call. While testing the latest changes, I did discover another bug with GameDataReader in relation to the playerName that gets passed back to Battlefield Rich Presence. BF1942 and BFV use ANSI character encoding for the CON files. In one of my player names, I use the registered trademark symbol "®". When GameDataReader passes back the player name with the "®" symbol in it, the URL that gets built by Battlefield Rich Presence does not work correctly. I was able to fix that issue in GameDataReader by using the System.Text.Encoding.CodePages NuGet package and making the change below in the ReadConfigFile method in LineBasedConfigFile.

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var configLines = File.ReadAllLines(filePath, Encoding.GetEncoding("windows-1252"));

BF1942 Player Name In CON File: lookoutriddick® Player Name Passed Back From GameDataReader: lookoutriddick�

Non working URL: https://api.bflist.io/bf1942/v1/players/lookoutriddick%EF%BF%BD/server

Working URL: https://api.bflist.io/bf1942/v1/players/lookoutriddick®/server

cetteup commented 1 year ago

This seems to be an issue that needs to be fixed over in GameDataReader. Would you open another PR there?

AnomalousNicole commented 1 year ago

Sure, I will open another Pull Request on the GameDataReader page.