Skip to content

Commit

Permalink
Raise NuGet package version
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchPlaysBF2 committed Mar 11, 2023
2 parents d31c5c4 + ed73600 commit 67a2e88
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 10 deletions.
39 changes: 37 additions & 2 deletions src/GameDataReader.Tests/Battlefield1942/GameDataReadersTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.IO;
using FluentAssertions;
using GameDataReader.Battlefield1942.Reader;
using GameDataReader.Common.Refractor.V1.Files;
using NUnit.Framework;

namespace GameDataReader.Tests.Battlefield1942;
Expand All @@ -11,11 +13,11 @@ public class GameDataReadersTests

[Explicit("Only run this test on a real Windows machine, for end-to-end testing.")]
[Test]
public void ReadActivePlayer_DoesNotThrowLocally()
public void GameDataReaders_Bf1942_ReadActivePlayer_DoesNotThrowLocally()
{
var player = Bf1942DataReader.ReadActivePlayer();

Console.WriteLine($"Player Name: {player.OnlineName}");
Console.WriteLine($"[Bf1942_ReadActivePlayer_DoesNotThrowLocally] Player Name: {player.OnlineName}");
}

[Test]
Expand All @@ -37,4 +39,37 @@ public void GameDataReaders_GetAccessor_IsNotSingleton()
bf1942DataReader1.GetHashCode().Should()
.NotBe(bf1942DataReader2.GetHashCode());
}

[Explicit("Only run this test on a real Windows machine, for end-to-end testing.")]
[Test]
public void GameDataReaders_Bf1942_IsTrueProfileFileExists()
{
var GameName = "Battlefield 1942";
var ModName = "bf1942";

var globalRefractorV1ConfigFile = new GlobalRefractorV1ConfigFile(GameName, ModName);
var filePath = globalRefractorV1ConfigFile.GetFilePath();

Console.WriteLine($"[Bf1942_IsTrueProfileFileExists] File Path: {filePath}");

Assert.IsTrue(File.Exists(filePath));
}

[Explicit("Only run this test on a real Windows machine, for end-to-end testing.")]
[Test]
public void GameDataReaders_Bf1942_IsTrueGeneralOptionsFileExists()
{
var GameName = "Battlefield 1942";
var ModName = "bf1942";

var globalRefractorV1ConfigFile = new GlobalRefractorV1ConfigFile(GameName, ModName);
var activeProfileName = globalRefractorV1ConfigFile.GetCurrentlyActiveProfileName();

var profileRefractorV1ConfigFile = new ProfileRefractorV1ConfigFile(GameName, ModName, activeProfileName);
var filePath = profileRefractorV1ConfigFile.GetFilePath();

Console.WriteLine($"[Bf1942_IsTrueGeneralOptionsFileExists] File Path: {filePath}");

Assert.IsTrue(File.Exists(filePath));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.IO;
using FluentAssertions;
using GameDataReader.BattlefieldVietnam.Reader;
using GameDataReader.Common.Refractor.V1.Files;
using NUnit.Framework;

namespace GameDataReader.Tests.BattlefieldVietnam;
Expand All @@ -11,11 +13,11 @@ public class GameDataReadersTests

[Explicit("Only run this test on a real Windows machine, for end-to-end testing.")]
[Test]
public void ReadActivePlayer_DoesNotThrowLocally()
public void GameDataReaders_BfVietnam_ReadActivePlayer_DoesNotThrowLocally()
{
var player = BfVietnamDataReader.ReadActivePlayer();

Console.WriteLine($"Player Name: {player.OnlineName}");
Console.WriteLine($"[BfVietnam_ReadActivePlayer_DoesNotThrowLocally] Player Name: {player.OnlineName}");
}

[Test]
Expand All @@ -37,4 +39,37 @@ public void GameDataReaders_GetAccessor_IsNotSingleton()
bfVietnamDataReader1.GetHashCode().Should()
.NotBe(bfVietnamDataReader2.GetHashCode());
}

[Explicit("Only run this test on a real Windows machine, for end-to-end testing.")]
[Test]
public void GameDataReaders_BfVietnam_IsTrueProfileFileExists()
{
var GameName = "Battlefield Vietnam";
var ModName = "BfVietnam";

var globalRefractorV1ConfigFile = new GlobalRefractorV1ConfigFile(GameName, ModName);
var filePath = globalRefractorV1ConfigFile.GetFilePath();

Console.WriteLine($"[BfVietnam_IsTrueProfileFileExists] File Path: {filePath}");

Assert.IsTrue(File.Exists(filePath));
}

[Explicit("Only run this test on a real Windows machine, for end-to-end testing.")]
[Test]
public void GameDataReaders_BfVietnam_IsTrueGeneralOptionsFileExists()
{
var GameName = "Battlefield Vietnam";
var ModName = "BfVietnam";

var globalRefractorV1ConfigFile = new GlobalRefractorV1ConfigFile(GameName, ModName);
var activeProfileName = globalRefractorV1ConfigFile.GetCurrentlyActiveProfileName();

var profileRefractorV1ConfigFile = new ProfileRefractorV1ConfigFile(GameName, ModName, activeProfileName);
var filePath = profileRefractorV1ConfigFile.GetFilePath();

Console.WriteLine($"[BfVietnam_IsTrueGeneralOptionsFileExists] File Path: {filePath}");

Assert.IsTrue(File.Exists(filePath));
}
}
14 changes: 13 additions & 1 deletion src/GameDataReader/Common/Files/LineBasedConfigFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GameDataReader.Common.Parsing;
using System.Text;

namespace GameDataReader.Common.Files;

Expand Down Expand Up @@ -27,7 +28,18 @@ public ISettingResolver ReadConfigFile()
"Couldn't find configuration data. Is the game installed?\r\n" +
$"{typeof(T).FullName} not found at location: {filePath}");

var configLines = File.ReadAllLines(filePath);
//BF1942 and BFVietnam CON files use ANSI/windows-1252 character encoding
var configLines = new List<string>();
if (filePath.ToLower().Contains("bf1942") || filePath.ToLower().Contains("bfvietnam"))
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
configLines = File.ReadAllLines(filePath, Encoding.GetEncoding("windows-1252")).ToList();
}
else
{
configLines = File.ReadAllLines(filePath).ToList();
}

var resolver = new LineBasedSettingResolver(configLines, GetParsePattern());
return resolver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public GlobalRefractorV1ConfigFile(string gameName, string modName)

public override string GetFilePath()
{
var appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return $@"{appData}\VirtualStore\Program Files (x86)\EA GAMES\{GameName}\Mods\{_modName}\Settings\Profile.con";
var conFile = $@"\Mods\{_modName}\Settings\Profile.con";
conFile = RefractorV1PathResolver.GetConFilePath(conFile, _modName, GameName);
return conFile;
}

public string GetCurrentlyActiveProfileName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public ProfileRefractorV1ConfigFile(string gameName, string modName, string prof

public override string GetFilePath()
{
var appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return $@"{appData}\VirtualStore\Program Files (x86)\EA GAMES\{GameName}\Mods\{_modName}\Settings\Profiles\{_profileName}\GeneralOptions.con";
var conFile = $@"\Mods\{_modName}\Settings\Profiles\{_profileName}\GeneralOptions.con";
conFile = RefractorV1PathResolver.GetConFilePath(conFile, _modName, GameName);
return conFile;
}

public string GetPlayerName()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Microsoft.Win32;
using System.Diagnostics;

namespace GameDataReader.Common.Refractor.V1.Files;

public static class RefractorV1PathResolver
{
public static string GetConFilePath(string conFile, string modName, string gameName)
{
// Use the path from the Registry, if the con file exists within the game installation path
var conFilePath = GetPathFromRegistry(conFile, gameName);
if (File.Exists(conFilePath))
{
return conFilePath;
}

// Use the path from the game process, if the con file exists within the game process path
conFilePath = GetPathFromGameProcess(conFile, modName);
if (File.Exists(conFilePath))
{
return conFilePath;
}

// Use the path from AppData, if the con file exists within the AppData path
conFilePath = GetPathFromAppData(conFile, gameName);
if (File.Exists(conFilePath))
{
return conFilePath;
}

return string.Empty;
}

public static string GetPathFromRegistry(string conFile, string gameName)
{
var conFilePath = string.Empty;

try
{
// Since BF1942 and BFV are 32bit games, the game's install path needs to be read from Wow6432Node
var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
var registryPath = $@"SOFTWARE\Wow6432Node\EA GAMES\{gameName}";

baseKey = baseKey.OpenSubKey(registryPath);

var value = baseKey?.GetValue("GAMEDIR");
if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
{
conFilePath = $@"{value}{conFile}";
if (File.Exists(conFilePath))
{
return conFilePath;
}
}

conFilePath = string.Empty;
return conFilePath;
}
catch (Exception)
{
return conFilePath;
}
}

public static string GetPathFromGameProcess(string conFile, string processName)
{
var conFilePath = string.Empty;

var foundProcess = Process.GetProcessesByName(processName).FirstOrDefault();
if (foundProcess?.MainModule != null)
{
conFilePath = Path.GetDirectoryName(foundProcess.MainModule.FileName);
}

conFilePath = $@"{conFilePath}{conFile}";
if (File.Exists(conFilePath))
{
return conFilePath;
}

conFilePath = string.Empty;
return conFilePath;
}

public static string GetPathFromAppData(string conFile, string gameName)
{
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

// Since BF1942 and BFV are 32bit games, the game's AppData path needs to be read from Program Files (x86)
var conFilePath = $@"{appDataPath}\VirtualStore\Program Files (x86)\EA GAMES\{gameName}{conFile}";
if (File.Exists(conFilePath))
{
return conFilePath;
}

conFilePath = string.Empty;
return conFilePath;
}
}
4 changes: 3 additions & 1 deletion src/GameDataReader/GameDataReader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>0.0.3</PackageVersion>
<PackageVersion>0.0.4</PackageVersion>
<Title>GameDataReader</Title>
<Authors>TwitchPlaysBF2</Authors>
<Description>NuGet library providing simple access to game settings. Reading data from games like Battlefield 2 - i.e. to access player data from BF2 specific .con configuration files. Open to extension.</Description>
Expand All @@ -18,6 +18,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 67a2e88

Please sign in to comment.