Skip to content

Commit d2b20e2

Browse files
committed
Refactor LoadFiles method with generic syntax
1 parent 82aa068 commit d2b20e2

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

Mod.Localizer/Program.cs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using System.Globalization;
54
using System.IO;
65
using System.Linq;
76
using System.Reflection;
@@ -25,9 +24,14 @@ internal static class Program
2524
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2625

2726
private static bool _dump = true;
28-
private static string _modFilePath, _sourcePath;
27+
private static string _modFilePath;
2928
private static GameCultures _language = DefaultConfigurations.DefaultLanguage;
3029

30+
/// <summary>
31+
/// Source folder path for processors accessing extra files.
32+
/// </summary>
33+
internal static string SourcePath { get; private set; }
34+
3135
private static void Process()
3236
{
3337
var wrapper = new TmodFileWrapper(typeof(BitsByte).Assembly);
@@ -126,15 +130,15 @@ private static void InnerPatch(TmodFileWrapper.ITmodFile modFile, IEnumerable<Ty
126130
Logger.Info(Strings.Patching, dll);
127131

128132
var module = AssemblyDef.Load(modFile.GetFile(dll)).Modules.Single();
129-
133+
130134
foreach (var processor in processors)
131135
{
132136
try
133137
{
134138
var proc = Activator.CreateInstance(processor, modFile, module, _language);
135-
var tran = LoadFiles(_sourcePath, processor);
139+
var tran = LoadFiles(SourcePath, processor);
136140

137-
processor.GetMethod(nameof(Processor<Content>.PatchContents))?.Invoke(proc, new [] {tran});
141+
processor.GetMethod(nameof(Processor<Content>.PatchContents))?.Invoke(proc, new[] { tran });
138142

139143
}
140144
catch (Exception ex)
@@ -152,40 +156,42 @@ private static void InnerPatch(TmodFileWrapper.ITmodFile modFile, IEnumerable<Ty
152156
}
153157
}
154158

155-
private static object LoadFiles(string folder, Type procType)
159+
private static object LoadFiles(string contentPath, Type processorType)
156160
{
157-
if (!DefaultConfigurations.FolderMapper.ContainsKey(procType))
161+
Debug.Assert(processorType.BaseType != null, "processorType.BaseType != null");
162+
var contentType = processorType.BaseType.GetGenericArguments().Single();
163+
164+
var loadFilesMethod = typeof(Program).GetMethod(nameof(LoadFiles), BindingFlags.NonPublic | BindingFlags.Static, null, new[] { typeof(string) }, null);
165+
loadFilesMethod = loadFilesMethod?.MakeGenericMethod(processorType, contentType);
166+
167+
Debug.Assert(loadFilesMethod != null, nameof(loadFilesMethod) + " != null");
168+
return loadFilesMethod.Invoke(null, new object[] { contentPath });
169+
}
170+
171+
private static IList<TContent> LoadFiles<TProcessor, TContent>(string contentPath)
172+
where TContent : Content
173+
where TProcessor : Processor<TContent>
174+
{
175+
if (!DefaultConfigurations.FolderMapper.ContainsKey(typeof(TProcessor)))
158176
{
159177
return null;
160178
}
161179

162-
Debug.Assert(procType.BaseType != null, "procType.BaseType != null");
163-
var contentType = procType.BaseType.GetGenericArguments().Single();
164-
var listType = typeof(List<>).MakeGenericType(contentType);
165-
166-
var doubleListType = typeof(List<>).MakeGenericType(listType);
180+
var path = Path.Combine(contentPath, DefaultConfigurations.FolderMapper[typeof(TProcessor)]);
167181

168-
var translations = Activator.CreateInstance(doubleListType);
169-
var addMethod = doubleListType.GetMethod(nameof(List<object>.Add));
182+
var list = new List<TContent>();
170183

171-
Debug.Assert(addMethod != null, nameof(addMethod) + " != null");
172-
173-
foreach (var file in Directory.EnumerateFiles(Path.Combine(folder, DefaultConfigurations.FolderMapper[procType]), "*.json"))
184+
foreach (var file in Directory.EnumerateFiles(path, "*.json"))
174185
{
175186
using (var sr = new StreamReader(File.OpenRead(file)))
176187
{
177-
var list = JsonConvert.DeserializeObject(sr.ReadToEnd(), listType);
178-
179-
addMethod.Invoke(translations, new [] {list});
188+
list.AddRange(JsonConvert.DeserializeObject<List<TContent>>(sr.ReadToEnd()));
180189
}
181190
}
182191

183-
return typeof(Program).GetMethod(nameof(CombineLists))?.MakeGenericMethod(contentType).Invoke(null, new[] {translations});
184-
}
192+
Logger.Debug("Loaded from {0}", path);
185193

186-
public static List<T> CombineLists<T>(List<List<T>> list)
187-
{
188-
return list.SelectMany(x => x).ToList();
194+
return list;
189195
}
190196

191197
public static void Main(string[] args)
@@ -225,13 +231,13 @@ public static void Main(string[] args)
225231
.AppendLine()
226232
.Append("================\r\n")
227233
.AppendFormat("{0}: Unhandled Exception\r\nCulture: {1}\r\nException: {2}\r\n",
228-
DateTime.Now,
234+
DateTime.Now,
229235
Thread.CurrentThread.CurrentCulture.Name,
230236
eventArgs.ExceptionObject.ToString())
231237
.Append("================\r\n");
232238

233239
Logger.Error(sb);
234-
240+
235241
Environment.Exit(1);
236242
};
237243

@@ -262,7 +268,7 @@ public static void Main(string[] args)
262268
{
263269
Process();
264270

265-
Logger.Info(Strings.ProcessComplete);
271+
Logger.Fatal(Strings.ProcessComplete);
266272
}
267273

268274
#if DEBUG
@@ -300,7 +306,7 @@ private static bool ParseCliArguments(string[] args)
300306

301307
if (srcOpt.HasValue())
302308
{
303-
_sourcePath = srcOpt.Value();
309+
SourcePath = srcOpt.Value();
304310
}
305311

306312
if (langOpt.HasValue())
@@ -326,7 +332,7 @@ private static bool ParseCliArguments(string[] args)
326332
}
327333

328334
// ReSharper disable once InvertIf
329-
if (string.IsNullOrWhiteSpace(_sourcePath) && !_dump)
335+
if (string.IsNullOrWhiteSpace(SourcePath) && !_dump)
330336
{
331337
Logger.Error(Strings.NoSourceFolderSpecified);
332338
return false;

0 commit comments

Comments
 (0)