1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
- using System . Globalization ;
5
4
using System . IO ;
6
5
using System . Linq ;
7
6
using System . Reflection ;
@@ -25,9 +24,14 @@ internal static class Program
25
24
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
26
25
27
26
private static bool _dump = true ;
28
- private static string _modFilePath , _sourcePath ;
27
+ private static string _modFilePath ;
29
28
private static GameCultures _language = DefaultConfigurations . DefaultLanguage ;
30
29
30
+ /// <summary>
31
+ /// Source folder path for processors accessing extra files.
32
+ /// </summary>
33
+ internal static string SourcePath { get ; private set ; }
34
+
31
35
private static void Process ( )
32
36
{
33
37
var wrapper = new TmodFileWrapper ( typeof ( BitsByte ) . Assembly ) ;
@@ -126,15 +130,15 @@ private static void InnerPatch(TmodFileWrapper.ITmodFile modFile, IEnumerable<Ty
126
130
Logger . Info ( Strings . Patching , dll ) ;
127
131
128
132
var module = AssemblyDef . Load ( modFile . GetFile ( dll ) ) . Modules . Single ( ) ;
129
-
133
+
130
134
foreach ( var processor in processors )
131
135
{
132
136
try
133
137
{
134
138
var proc = Activator . CreateInstance ( processor , modFile , module , _language ) ;
135
- var tran = LoadFiles ( _sourcePath , processor ) ;
139
+ var tran = LoadFiles ( SourcePath , processor ) ;
136
140
137
- processor . GetMethod ( nameof ( Processor < Content > . PatchContents ) ) ? . Invoke ( proc , new [ ] { tran } ) ;
141
+ processor . GetMethod ( nameof ( Processor < Content > . PatchContents ) ) ? . Invoke ( proc , new [ ] { tran } ) ;
138
142
139
143
}
140
144
catch ( Exception ex )
@@ -152,40 +156,42 @@ private static void InnerPatch(TmodFileWrapper.ITmodFile modFile, IEnumerable<Ty
152
156
}
153
157
}
154
158
155
- private static object LoadFiles ( string folder , Type procType )
159
+ private static object LoadFiles ( string contentPath , Type processorType )
156
160
{
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 ) ) )
158
176
{
159
177
return null ;
160
178
}
161
179
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 ) ] ) ;
167
181
168
- var translations = Activator . CreateInstance ( doubleListType ) ;
169
- var addMethod = doubleListType . GetMethod ( nameof ( List < object > . Add ) ) ;
182
+ var list = new List < TContent > ( ) ;
170
183
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" ) )
174
185
{
175
186
using ( var sr = new StreamReader ( File . OpenRead ( file ) ) )
176
187
{
177
- var list = JsonConvert . DeserializeObject ( sr . ReadToEnd ( ) , listType ) ;
178
-
179
- addMethod . Invoke ( translations , new [ ] { list } ) ;
188
+ list . AddRange ( JsonConvert . DeserializeObject < List < TContent > > ( sr . ReadToEnd ( ) ) ) ;
180
189
}
181
190
}
182
191
183
- return typeof ( Program ) . GetMethod ( nameof ( CombineLists ) ) ? . MakeGenericMethod ( contentType ) . Invoke ( null , new [ ] { translations } ) ;
184
- }
192
+ Logger . Debug ( "Loaded from {0}" , path ) ;
185
193
186
- public static List < T > CombineLists < T > ( List < List < T > > list )
187
- {
188
- return list . SelectMany ( x => x ) . ToList ( ) ;
194
+ return list ;
189
195
}
190
196
191
197
public static void Main ( string [ ] args )
@@ -225,13 +231,13 @@ public static void Main(string[] args)
225
231
. AppendLine ( )
226
232
. Append ( "================\r \n " )
227
233
. AppendFormat ( "{0}: Unhandled Exception\r \n Culture: {1}\r \n Exception: {2}\r \n " ,
228
- DateTime . Now ,
234
+ DateTime . Now ,
229
235
Thread . CurrentThread . CurrentCulture . Name ,
230
236
eventArgs . ExceptionObject . ToString ( ) )
231
237
. Append ( "================\r \n " ) ;
232
238
233
239
Logger . Error ( sb ) ;
234
-
240
+
235
241
Environment . Exit ( 1 ) ;
236
242
} ;
237
243
@@ -262,7 +268,7 @@ public static void Main(string[] args)
262
268
{
263
269
Process ( ) ;
264
270
265
- Logger . Info ( Strings . ProcessComplete ) ;
271
+ Logger . Fatal ( Strings . ProcessComplete ) ;
266
272
}
267
273
268
274
#if DEBUG
@@ -300,7 +306,7 @@ private static bool ParseCliArguments(string[] args)
300
306
301
307
if ( srcOpt . HasValue ( ) )
302
308
{
303
- _sourcePath = srcOpt . Value ( ) ;
309
+ SourcePath = srcOpt . Value ( ) ;
304
310
}
305
311
306
312
if ( langOpt . HasValue ( ) )
@@ -326,7 +332,7 @@ private static bool ParseCliArguments(string[] args)
326
332
}
327
333
328
334
// ReSharper disable once InvertIf
329
- if ( string . IsNullOrWhiteSpace ( _sourcePath ) && ! _dump )
335
+ if ( string . IsNullOrWhiteSpace ( SourcePath ) && ! _dump )
330
336
{
331
337
Logger . Error ( Strings . NoSourceFolderSpecified ) ;
332
338
return false ;
0 commit comments