Skip to content

Commit

Permalink
Merge pull request #32 from jetelain/helper
Browse files Browse the repository at this point in the history
Config Helper Application
  • Loading branch information
jetelain authored Nov 11, 2022
2 parents 0d2535d + 37b792d commit 8bef422
Show file tree
Hide file tree
Showing 45 changed files with 2,531 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Helper/Helper.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Helper", "Helper\Helper.csproj", "{CED6453D-664A-4A60-AEDF-5F0C6B53EC3D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BIS.Core", "..\..\bis-file-formats\BIS.Core\BIS.Core.csproj", "{D248C687-C704-4658-8134-8329ACE4F6EF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelperUI", "HelperUI\HelperUI.csproj", "{109A4D15-7DDF-4F55-B03B-9183D1B22652}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BIS.PBO", "..\..\bis-file-formats\BIS.PBO\BIS.PBO.csproj", "{CC24C932-60B5-40E4-A426-651F456C0C6C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CED6453D-664A-4A60-AEDF-5F0C6B53EC3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CED6453D-664A-4A60-AEDF-5F0C6B53EC3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CED6453D-664A-4A60-AEDF-5F0C6B53EC3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CED6453D-664A-4A60-AEDF-5F0C6B53EC3D}.Release|Any CPU.Build.0 = Release|Any CPU
{D248C687-C704-4658-8134-8329ACE4F6EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D248C687-C704-4658-8134-8329ACE4F6EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D248C687-C704-4658-8134-8329ACE4F6EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D248C687-C704-4658-8134-8329ACE4F6EF}.Release|Any CPU.Build.0 = Release|Any CPU
{109A4D15-7DDF-4F55-B03B-9183D1B22652}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{109A4D15-7DDF-4F55-B03B-9183D1B22652}.Debug|Any CPU.Build.0 = Debug|Any CPU
{109A4D15-7DDF-4F55-B03B-9183D1B22652}.Release|Any CPU.ActiveCfg = Release|Any CPU
{109A4D15-7DDF-4F55-B03B-9183D1B22652}.Release|Any CPU.Build.0 = Release|Any CPU
{CC24C932-60B5-40E4-A426-651F456C0C6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC24C932-60B5-40E4-A426-651F456C0C6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC24C932-60B5-40E4-A426-651F456C0C6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC24C932-60B5-40E4-A426-651F456C0C6C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FCAC46E7-E1BF-4662-8E57-A0130EC80F0F}
EndGlobalSection
EndGlobal
171 changes: 171 additions & 0 deletions Helper/Helper/Detector/ConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using BIS.Core.Config;
using BIS.Core.Streams;
using BIS.PBO;
using Microsoft.Win32;

namespace Helper
{
static class ConfigHelper
{
public static IEnumerable<T> Get<T>(this IEnumerable<ParamFile> files, string name) where T : ParamEntry
{
return files.Where(f => f.Root != null).Select(f => f.Root).Get<T>(name);
}

public static IEnumerable<T> Get<T>(this IEnumerable<ParamClass> entries) where T : ParamEntry
{
return entries.SelectMany(e => e.Entries.OfType<T>());
}

public static IEnumerable<T> Get<T>(this IEnumerable<ParamClass> entries, string name) where T : ParamEntry
{
return entries.SelectMany(e => e.Entries.OfType<T>()).Where(e => string.Equals(e.Name, name, StringComparison.OrdinalIgnoreCase));
}

public static IEnumerable<T> Get<T>(this ParamClass entry, string name) where T : ParamEntry
{
return entry.Entries.OfType<T>().Where(e => string.Equals(e.Name, name, StringComparison.OrdinalIgnoreCase));
}

public static IEnumerable<T> Get<T>(this ParamClass entry) where T : ParamEntry
{
return entry.Entries.OfType<T>();
}

public static T Get<T>(this ParamClass entry, string name, IEnumerable<ParamClass> cfgClassRoot) where T : ParamEntry
{
var value = entry.Get<T>(name).FirstOrDefault();
if (value != null)
{
return value;
}
if (!string.IsNullOrEmpty(entry.BaseClassName))
{
return
cfgClassRoot
.Get<ParamClass>(entry.BaseClassName)
.Select(e => e.Get<T>(name, cfgClassRoot))
.Where(v => v != null)
.FirstOrDefault();
}
return null;
}

public static T GetValue<T>(this ParamClass entry, string name, IEnumerable<ParamClass> classes)
{
var value = entry.Get<ParamValue>(name, classes);
if (value != null)
{
return value.Get<T>();
}
return default(T);
}

public static T[] GetArray<T>(this ParamClass entry, string name, IEnumerable<ParamClass> classes)
{
return entry.Get<ParamArray>(name, classes)?.ToArray<T>();
}

public static bool Inherits(this ParamClass entry, string wantedBaseClassName, IEnumerable<ParamClass> cfgClassRoot)
{
if (!string.IsNullOrEmpty(entry.BaseClassName))
{
if (string.Equals(wantedBaseClassName, entry.BaseClassName))
{
return true;
}
return cfgClassRoot
.Get<ParamClass>(entry.BaseClassName)
.Any(c => c.Inherits(wantedBaseClassName, cfgClassRoot));
}
return false;
}


internal static ParamFile ReadParamFile(string file)
{
var ext = Path.GetExtension(file);
ParamFile paramFile;
if (string.Equals(ext, ".cpp", System.StringComparison.OrdinalIgnoreCase))
{
paramFile = ParseCpp(file);
}
else if (string.Equals(ext, ".pbo", System.StringComparison.OrdinalIgnoreCase))
{
var pbo = new PBO(file);
var configBin = pbo.Files.FirstOrDefault(f => f.FileName == "config.bin");
if (configBin != null)
{
using (var stream = configBin.OpenRead())
{
paramFile = StreamHelper.Read<ParamFile>(stream);
}
}
else
{
var src = Path.GetTempFileName();
var configCpp = pbo.Files.FirstOrDefault(f => f.FileName == "config.cpp");
if (configCpp != null)
{
using (var streamFile = File.Create(src))
using (var stream = configCpp.OpenRead())
{
stream.CopyTo(streamFile);
}
paramFile = ParseCpp(src);
File.Delete(src);
}
else
{
paramFile = new ParamFile();
}
}
}
else
{
paramFile = StreamHelper.Read<ParamFile>(file);
}
return paramFile;
}

private static ParamFile ParseCpp(string src)
{
ParamFile paramFile;
var dst = Path.GetTempFileName();
Process.Start(new ProcessStartInfo(Path.Combine(GetArma3ToolsPath(), "CfgConvert", "CfgConvert.exe"), $@"-bin -dst ""{dst}"" ""{src}""")
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}).WaitForExit();
paramFile = StreamHelper.Read<ParamFile>(dst);
File.Delete(dst);
return paramFile;

}

internal static string GetArma3ToolsPath()
{
string path = "";
#pragma warning disable CA1416 // Valider la compatibilité de la plateforme
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 233800"))
{
if (key != null)
{
path = (key.GetValue("InstallLocation") as string) ?? path;
}
}
#pragma warning restore CA1416 // Valider la compatibilité de la plateforme
if (string.IsNullOrEmpty(path))
{
throw new ApplicationException("Arma 3 Tools are not installed.\r\nUnable to process a text config file.\r\nPlease install Arma 3 Tools with Steam, or open a binarized config file.");
}
return path;
}
}
}
19 changes: 19 additions & 0 deletions Helper/Helper/Detector/DetectedConfigInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Diagnostics;

namespace Helper.Detector
{
[DebuggerDisplay("{ClassName}")]
public class DetectedConfigInfo
{
public string ClassRoot { get; set; }
public string ClassName { get; set; }
public string P3dModel { get; set; }
public Dictionary<string, string> HiddenSelections { get; set; } = new Dictionary<string, string>();
public string DisplayName { get; set; }
public string FileName { get; set; }
public string Definition { get; internal set; }

public string GetHiddenSelection(string name) => HiddenSelections.TryGetValue(name, out var v) ? v : null;
}
}
12 changes: 12 additions & 0 deletions Helper/Helper/Detector/DetectedHiddenSelection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Diagnostics;

namespace Helper
{
[DebuggerDisplay("{Name}")]
public class DetectedHiddenSelection
{
public string Name { get; set; }
public List<DetectedHiddenSelectionValue> Values { get; set; }
}
}
11 changes: 11 additions & 0 deletions Helper/Helper/Detector/DetectedHiddenSelectionValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Diagnostics;

namespace Helper
{
[DebuggerDisplay("{SuggestedName}")]
public class DetectedHiddenSelectionValue
{
public string Value { get; set; }
public string SuggestedName { get; set; }
}
}
18 changes: 18 additions & 0 deletions Helper/Helper/Detector/DetectedModelInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Diagnostics;
using Helper.Detector;

namespace Helper
{
[DebuggerDisplay("{Name}")]
public class DetectedModelInfo
{
public string Name { get; set; }
public string P3dModel { get; set; }
public List<DetectedConfigInfo> Configs { get; set; }
public List<DetectedHiddenSelection> HiddenSelections { get; set; }
public string ClassRoot { get; set; }
public List<string> FileNames { get; set; }
public string PackageName { get; set; }
}
}
Loading

0 comments on commit 8bef422

Please sign in to comment.