|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using BIS.Core.Config; |
| 7 | +using BIS.Core.Streams; |
| 8 | +using BIS.PBO; |
| 9 | +using Microsoft.Win32; |
| 10 | + |
| 11 | +namespace Helper |
| 12 | +{ |
| 13 | + static class ConfigHelper |
| 14 | + { |
| 15 | + public static IEnumerable<T> Get<T>(this IEnumerable<ParamFile> files, string name) where T : ParamEntry |
| 16 | + { |
| 17 | + return files.Where(f => f.Root != null).Select(f => f.Root).Get<T>(name); |
| 18 | + } |
| 19 | + |
| 20 | + public static IEnumerable<T> Get<T>(this IEnumerable<ParamClass> entries) where T : ParamEntry |
| 21 | + { |
| 22 | + return entries.SelectMany(e => e.Entries.OfType<T>()); |
| 23 | + } |
| 24 | + |
| 25 | + public static IEnumerable<T> Get<T>(this IEnumerable<ParamClass> entries, string name) where T : ParamEntry |
| 26 | + { |
| 27 | + return entries.SelectMany(e => e.Entries.OfType<T>()).Where(e => string.Equals(e.Name, name, StringComparison.OrdinalIgnoreCase)); |
| 28 | + } |
| 29 | + |
| 30 | + public static IEnumerable<T> Get<T>(this ParamClass entry, string name) where T : ParamEntry |
| 31 | + { |
| 32 | + return entry.Entries.OfType<T>().Where(e => string.Equals(e.Name, name, StringComparison.OrdinalIgnoreCase)); |
| 33 | + } |
| 34 | + |
| 35 | + public static IEnumerable<T> Get<T>(this ParamClass entry) where T : ParamEntry |
| 36 | + { |
| 37 | + return entry.Entries.OfType<T>(); |
| 38 | + } |
| 39 | + |
| 40 | + public static T Get<T>(this ParamClass entry, string name, IEnumerable<ParamClass> cfgClassRoot) where T : ParamEntry |
| 41 | + { |
| 42 | + var value = entry.Get<T>(name).FirstOrDefault(); |
| 43 | + if (value != null) |
| 44 | + { |
| 45 | + return value; |
| 46 | + } |
| 47 | + if (!string.IsNullOrEmpty(entry.BaseClassName)) |
| 48 | + { |
| 49 | + return |
| 50 | + cfgClassRoot |
| 51 | + .Get<ParamClass>(entry.BaseClassName) |
| 52 | + .Select(e => e.Get<T>(name, cfgClassRoot)) |
| 53 | + .Where(v => v != null) |
| 54 | + .FirstOrDefault(); |
| 55 | + } |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + public static T GetValue<T>(this ParamClass entry, string name, IEnumerable<ParamClass> classes) |
| 60 | + { |
| 61 | + var value = entry.Get<ParamValue>(name, classes); |
| 62 | + if (value != null) |
| 63 | + { |
| 64 | + return value.Get<T>(); |
| 65 | + } |
| 66 | + return default(T); |
| 67 | + } |
| 68 | + |
| 69 | + public static T[] GetArray<T>(this ParamClass entry, string name, IEnumerable<ParamClass> classes) |
| 70 | + { |
| 71 | + return entry.Get<ParamArray>(name, classes)?.ToArray<T>(); |
| 72 | + } |
| 73 | + |
| 74 | + public static bool Inherits(this ParamClass entry, string wantedBaseClassName, IEnumerable<ParamClass> cfgClassRoot) |
| 75 | + { |
| 76 | + if (!string.IsNullOrEmpty(entry.BaseClassName)) |
| 77 | + { |
| 78 | + if (string.Equals(wantedBaseClassName, entry.BaseClassName)) |
| 79 | + { |
| 80 | + return true; |
| 81 | + } |
| 82 | + return cfgClassRoot |
| 83 | + .Get<ParamClass>(entry.BaseClassName) |
| 84 | + .Any(c => c.Inherits(wantedBaseClassName, cfgClassRoot)); |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + internal static ParamFile ReadParamFile(string file) |
| 91 | + { |
| 92 | + var ext = Path.GetExtension(file); |
| 93 | + ParamFile paramFile; |
| 94 | + if (string.Equals(ext, ".cpp", System.StringComparison.OrdinalIgnoreCase)) |
| 95 | + { |
| 96 | + paramFile = ParseCpp(file); |
| 97 | + } |
| 98 | + else if (string.Equals(ext, ".pbo", System.StringComparison.OrdinalIgnoreCase)) |
| 99 | + { |
| 100 | + var pbo = new PBO(file); |
| 101 | + var configBin = pbo.Files.FirstOrDefault(f => f.FileName == "config.bin"); |
| 102 | + if (configBin != null) |
| 103 | + { |
| 104 | + using (var stream = configBin.OpenRead()) |
| 105 | + { |
| 106 | + paramFile = StreamHelper.Read<ParamFile>(stream); |
| 107 | + } |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + var src = Path.GetTempFileName(); |
| 112 | + var configCpp = pbo.Files.FirstOrDefault(f => f.FileName == "config.cpp"); |
| 113 | + if (configCpp != null) |
| 114 | + { |
| 115 | + using (var streamFile = File.Create(src)) |
| 116 | + using (var stream = configCpp.OpenRead()) |
| 117 | + { |
| 118 | + stream.CopyTo(streamFile); |
| 119 | + } |
| 120 | + paramFile = ParseCpp(src); |
| 121 | + File.Delete(src); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + paramFile = new ParamFile(); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + paramFile = StreamHelper.Read<ParamFile>(file); |
| 132 | + } |
| 133 | + return paramFile; |
| 134 | + } |
| 135 | + |
| 136 | + private static ParamFile ParseCpp(string src) |
| 137 | + { |
| 138 | + ParamFile paramFile; |
| 139 | + var dst = Path.GetTempFileName(); |
| 140 | + Process.Start(new ProcessStartInfo(Path.Combine(GetArma3ToolsPath(), "CfgConvert", "CfgConvert.exe"), $@"-bin -dst ""{dst}"" ""{src}""") |
| 141 | + { |
| 142 | + WindowStyle = ProcessWindowStyle.Hidden, |
| 143 | + CreateNoWindow = true, |
| 144 | + UseShellExecute = false |
| 145 | + }).WaitForExit(); |
| 146 | + paramFile = StreamHelper.Read<ParamFile>(dst); |
| 147 | + File.Delete(dst); |
| 148 | + return paramFile; |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + internal static string GetArma3ToolsPath() |
| 153 | + { |
| 154 | + string path = ""; |
| 155 | +#pragma warning disable CA1416 // Valider la compatibilité de la plateforme |
| 156 | + using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 233800")) |
| 157 | + { |
| 158 | + if (key != null) |
| 159 | + { |
| 160 | + path = (key.GetValue("InstallLocation") as string) ?? path; |
| 161 | + } |
| 162 | + } |
| 163 | +#pragma warning restore CA1416 // Valider la compatibilité de la plateforme |
| 164 | + if (string.IsNullOrEmpty(path)) |
| 165 | + { |
| 166 | + 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."); |
| 167 | + } |
| 168 | + return path; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments