Skip to content

Commit

Permalink
Configure INI parser to support bethesda ini files (#535)
Browse files Browse the repository at this point in the history
* Configure INI parser to support bethesda ini files.

Configuration taken from Mutagen:
https://github.com/Noggog/Mutagen/blob/369a0549fab90593cb130b3889f988228f8ecf98/Mutagen.Bethesda.Core/Archives/DI/GetArchiveIniListings.cs#L38-L46

* Add test for INI parsing
  • Loading branch information
Al12rs authored Aug 9, 2023
1 parent 80be8ed commit 46d6ea7
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 1 deletion.
7 changes: 7 additions & 0 deletions NexusMods.App.sln
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NexusMods.Networking.Downlo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NexusMods.Abstractions", "src\NexusMods.Abstractions\NexusMods.Abstractions.csproj", "{1E20E979-5F04-44FD-BE07-54B6686ECB13}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NexusMods.Games.Generic.Tests", "tests\Games\NexusMods.Games.Generic.Tests\NexusMods.Games.Generic.Tests.csproj", "{AA95B93F-23AC-46D5-83B3-2E7AE4BD309C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -301,6 +303,10 @@ Global
{1E20E979-5F04-44FD-BE07-54B6686ECB13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E20E979-5F04-44FD-BE07-54B6686ECB13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E20E979-5F04-44FD-BE07-54B6686ECB13}.Release|Any CPU.Build.0 = Release|Any CPU
{AA95B93F-23AC-46D5-83B3-2E7AE4BD309C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA95B93F-23AC-46D5-83B3-2E7AE4BD309C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA95B93F-23AC-46D5-83B3-2E7AE4BD309C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA95B93F-23AC-46D5-83B3-2E7AE4BD309C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -354,6 +360,7 @@ Global
{3FBDEE15-9892-40EF-9593-6353068FAF48} = {D7E9D8F5-8AC8-4ADA-B219-C549084AD84C}
{09B037AB-07BB-4154-95FD-6EA2E55C4568} = {897C4198-884F-448A-B0B0-C2A6D971EAE0}
{1E20E979-5F04-44FD-BE07-54B6686ECB13} = {E7BAE287-D505-4D6D-A090-665A64309B2D}
{AA95B93F-23AC-46D5-83B3-2E7AE4BD309C} = {05B06AC1-7F2B-492F-983E-5BC63CDBF20D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9F9F8352-34DD-42C0-8564-EE9AF34A3501}
Expand Down
13 changes: 12 additions & 1 deletion src/Games/NexusMods.Games.Generic/FileAnalyzers/IniAnalzyer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using IniParser;
using IniParser.Model.Configuration;
using IniParser.Parser;
using NexusMods.DataModel.Abstractions;
using NexusMods.DataModel.Abstractions.Ids;
Expand All @@ -15,11 +16,21 @@ public class IniAnalzyer : IFileAnalyzer
public FileAnalyzerId Id { get; } = FileAnalyzerId.New("904bca7b-fbd6-4350-b4e2-6fdbd034ec76", 1);
public IEnumerable<FileType> FileTypes => new[] { FileType.INI };

public static readonly IniParserConfiguration Config = new()
{
AllowDuplicateKeys = true,
AllowDuplicateSections = true,
AllowKeysWithoutSection = true,
AllowCreateSectionsOnFly = true,
CaseInsensitive = true,
SkipInvalidLines = true,
};

#pragma warning disable CS1998
public async IAsyncEnumerable<IFileAnalysisData> AnalyzeAsync(FileAnalyzerInfo info, [EnumeratorCancellation] CancellationToken token = default)
#pragma warning restore CS1998
{
var data = new StreamIniDataParser(new IniDataParser()).ReadData(new StreamReader(info.Stream));
var data = new StreamIniDataParser(new IniDataParser(Config)).ReadData(new StreamReader(info.Stream));
var sections = data.Sections.Select(s => s.SectionName).ToHashSet();
var keys = data.Global.Select(k => k.KeyName)
.Concat(data.Sections.SelectMany(d => d.Keys).Select(kv => kv.KeyName))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
keyWithoutSection="SomeString"

[PROXY]
EnableProxyLibrary=false
InitProxyFunctions=true
ProxyLibrary=other_d3d9.dll

# hashtagcomment

[EmptySection]

[GLOBAL]
emptyKey=
duplicateKey=2
duplicateKey=3
UsePatchSpeedhackWithoutGraphics=false
UseDefferedRendering=true
IgnoreCreationKit=true
IsEnderal=false

[GLOBAL]
duplicate=section

[PERFORMANCE]
SpeedHack=true
EnableOcclusionCulling=true

[MULTIGPU]
DisableCloudsShadowInReflections=false

[MEMORY]
ExpandSystemMemoryX64=false
ReduceSystemMemoryUsage=true
DisableDriverMemoryManager=false
DisablePreloadToVRAM=false
EnableUnsafeMemoryHacks=false
ReservedMemorySizeMb=64
VideoMemorySizeMb=2000
EnableCompression=false
AutodetectVideoMemorySize=false

[THREADS]
DataSyncMode=0
PriorityMode=0

[MULTIHEAD]
ForceVideoAdapterIndex=false
VideoAdapterIndex=0

[WINDOW]
ForceBorderless=false
ForceBorderlessFullscreen=false

[ENGINE]
ForceAnisotropicFiltering=true
MaxAnisotropy=16
ForceLodBias=false
LodBias=0.0
AddDisplaySuperSamplingResolutions=false
EnableVSync=false
VSyncSkipNumFrames=0

[LIMITER]
WaitBusyRenderer=false
EnableFPSLimit=false
FPSLimit=10.0

[INPUT]
//shift
KeyCombination=16
//f12
KeyUseEffect=123
//home
KeyFPSLimit=36
//num / 106
KeyShowFPS=106
//print screen
KeyScreenshot=44
//enter
KeyEditor=13
//f4
KeyFreeVRAM=115
//B
KeyBruteForce=66

[ADAPTIVEQUALITY]
Enable=false
Quality=1
DesiredFPS=20.0

[ANTIALIASING]
EnableEdgeAA=false
EnableTemporalAA=false
EnableSubPixelAA=false

[FIX]
FixGameBugs=true
FixParallaxBugs=true
FixParallaxTerrain=false
FixAliasedTextures=true
IgnoreInventory=true
FixTintGamma=true
RemoveBlur=false
RemoveRadialBlur=false
FixSubSurfaceScattering=true
FixSkyReflection=true
FixCursorVisibility=true
FixLag=false
GenerateGrassNormals=true

[LONGEXPOSURE]
EnableLongExposureMode=false
Time=1.0
BlendMax=0.0

[FILE]
ScreenshotFormat=0

[GUI]
HighResolutionScaling=true
29 changes: 29 additions & 0 deletions tests/Games/NexusMods.Games.Generic.Tests/IniParsingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FluentAssertions;
using IniParser;
using IniParser.Parser;
using NexusMods.Games.Generic.FileAnalyzers;
using NexusMods.Paths;
using Xunit;

namespace NexusMods.Games.FOMOD.Tests;

public class IniParsingTests
{
private readonly IFileSystem _fs;

public IniParsingTests(IFileSystem fs)
{
_fs = fs;
}

[Fact]
public void ParseIniTest()
{
var assetsPath = _fs.GetKnownPath(KnownPath.EntryDirectory).Combine("Assets");
var testIniPath = assetsPath.Combine("IniSamples/enblocal.ini");
var config = IniAnalzyer.Config;
var parser = new FileIniDataParser(new IniDataParser(config));
var act = () => parser.ReadFile(testIniPath.ToString());
act.Should().NotThrow();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Games\NexusMods.Games.Generic\NexusMods.Games.Generic.csproj" />
<ProjectReference Include="..\NexusMods.Games.TestFramework\NexusMods.Games.TestFramework.csproj" />
</ItemGroup>


<ItemGroup>
<None Update="Assets\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions tests/Games/NexusMods.Games.Generic.Tests/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NexusMods.Common;
using NexusMods.Games.TestFramework;
using Xunit.DependencyInjection;
using Xunit.DependencyInjection.Logging;

namespace NexusMods.Games.Generic.Tests;

public class Startup
{
public void ConfigureServices(IServiceCollection container)
{
container
.AddDefaultServicesForTesting()
.AddGenericGameSupport()
.Validate();
}

// ReSharper disable once UnusedMember.Global
public void Configure(ILoggerFactory loggerFactory, ITestOutputHelperAccessor accessor) =>
loggerFactory.AddProvider(new XunitTestOutputLoggerProvider(accessor, delegate { return true;}));
}

0 comments on commit 46d6ea7

Please sign in to comment.