@@ -16,7 +16,41 @@ namespace CSLoader
16
16
{
17
17
public class Loader
18
18
{
19
+ [ System . Diagnostics . Conditional ( "DEBUG_FILE" ) ]
20
+ private static void Log ( string text ) {
19
21
22
+ var message = $ "{ DateTime . Now . Ticks } : { text } \n ";
23
+ System . IO . File . AppendAllText ( "log.txt" , message ) ;
24
+ }
25
+
26
+ private static Assembly MyResolveEventHandler ( object sender , ResolveEventArgs args )
27
+ {
28
+ Assembly asm = null ;
29
+ Log ( "MyResolveEventHandler" + paths . Count . ToString ( ) ) ;
30
+ foreach ( var path in paths )
31
+ {
32
+ try
33
+ {
34
+ var p = path + "\\ " + args . Name + ".dll" ;
35
+ Log ( p ) ;
36
+
37
+ asm = Assembly . LoadFile ( p ) ;
38
+ if ( asm != null )
39
+ {
40
+ return asm ;
41
+ } else {
42
+ Log ( "NULL" ) ;
43
+ }
44
+ }
45
+ catch ( Exception ex )
46
+ {
47
+ Log ( "EX! " + ex . Message ) ;
48
+
49
+ }
50
+ }
51
+
52
+ return asm ;
53
+ }
20
54
public static void Main ( string [ ] args )
21
55
{
22
56
@@ -26,7 +60,9 @@ public static void Main(string[] args)
26
60
27
61
static Loader ( )
28
62
{
29
- AssemblyLoadContext . Default . Resolving += Context_Resolving ;
63
+ Log ( "start" ) ;
64
+ AppDomain . CurrentDomain . AssemblyResolve += new ResolveEventHandler ( MyResolveEventHandler ) ;
65
+ AppDomain . CurrentDomain . TypeResolve += new ResolveEventHandler ( MyResolveEventHandler ) ;
30
66
Init ( ) ;
31
67
}
32
68
@@ -74,7 +110,6 @@ public static ReflectFunction[] GetFunctionsInternal()
74
110
{
75
111
return loader . Functions ( ) ;
76
112
}
77
-
78
113
public static void GetFunctions ( ref int count , IntPtr p )
79
114
{
80
115
var f = loader . Functions ( ) ;
@@ -189,11 +224,14 @@ public bool LoadFromSourceFunctions(string[] source)
189
224
190
225
string assemblyName = Path . GetRandomFileName ( ) ;
191
226
192
- MetadataReference [ ] references = new MetadataReference [ ]
193
- {
194
- MetadataReference . CreateFromFile ( typeof ( object ) . GetTypeInfo ( ) . Assembly . Location ) ,
195
- MetadataReference . CreateFromFile ( typeof ( Enumerable ) . GetTypeInfo ( ) . Assembly . Location )
196
- } ;
227
+ MetadataReference [ ] references ;
228
+
229
+ var mainPath = Path . GetDirectoryName ( typeof ( object ) . GetTypeInfo ( ) . Assembly . Location ) + "/" ;
230
+ var assemblyFiles = System . IO . Directory . GetFiles ( mainPath , "*.dll" ) ;
231
+
232
+ assemblyFiles = assemblyFiles . Concat ( System . IO . Directory . GetFiles ( AppDomain . CurrentDomain . BaseDirectory , "*.dll" ) ) . Distinct ( ) . ToArray ( ) ;
233
+
234
+ references = assemblyFiles . Select ( x=> MetadataReference . CreateFromFile ( x ) ) . ToArray ( ) ;
197
235
198
236
CSharpCompilation compilation = CSharpCompilation . Create (
199
237
assemblyName ,
@@ -213,15 +251,13 @@ public bool LoadFromSourceFunctions(string[] source)
213
251
214
252
foreach ( Diagnostic diagnostic in failures )
215
253
{
216
- Console . Error . WriteLine ( "{0 }: {1}" , diagnostic . Id , diagnostic . GetMessage ( ) ) ;
254
+ Log ( $ " { diagnostic . Id } : { diagnostic . GetMessage ( ) } " ) ;
217
255
}
218
256
}
219
257
else
220
258
{
221
259
ms . Seek ( 0 , SeekOrigin . Begin ) ;
222
-
223
- AssemblyLoadContext context = AssemblyLoadContext . Default ;
224
- assembly = context . LoadFromStream ( ms ) ;
260
+ assembly = Assembly . Load ( ms . ToArray ( ) ) ;
225
261
}
226
262
}
227
263
@@ -240,7 +276,6 @@ public bool LoadFromSourceFunctions(string[] source)
240
276
241
277
public static bool LoadFromAssembly ( string assemblyFile )
242
278
{
243
- AssemblyLoadContext context = AssemblyLoadContext . Default ;
244
279
Assembly asm = null ;
245
280
246
281
string path = System . IO . Path . GetDirectoryName ( assemblyFile ) ;
@@ -252,7 +287,7 @@ public static bool LoadFromAssembly(string assemblyFile)
252
287
253
288
try
254
289
{
255
- asm = context . LoadFromAssemblyPath ( assemblyFile ) ;
290
+ asm = Assembly . LoadFile ( assemblyFile ) ;
256
291
}
257
292
catch ( Exception )
258
293
{
@@ -261,7 +296,7 @@ public static bool LoadFromAssembly(string assemblyFile)
261
296
{
262
297
try
263
298
{
264
- asm = context . LoadFromAssemblyName ( new AssemblyName ( System . IO . Path . GetFileNameWithoutExtension ( assemblyFile ) ) ) ;
299
+ asm = Assembly . Load ( new AssemblyName ( System . IO . Path . GetFileNameWithoutExtension ( assemblyFile ) ) ) ;
265
300
}
266
301
catch ( Exception )
267
302
{
@@ -283,30 +318,6 @@ public static bool LoadFromAssembly(string assemblyFile)
283
318
return false ;
284
319
}
285
320
286
- private static Assembly Context_Resolving ( AssemblyLoadContext context , AssemblyName name )
287
- {
288
- Assembly asm = null ;
289
-
290
- foreach ( var path in paths )
291
- {
292
- try
293
- {
294
- asm = context . LoadFromAssemblyPath ( path + "\\ " + name . Name + ".dll" ) ;
295
-
296
- if ( asm != null )
297
- {
298
- return asm ;
299
- }
300
- }
301
- catch ( Exception ex )
302
- {
303
- Console . Error . WriteLine ( ex . Message ) ;
304
- }
305
- }
306
-
307
- return asm ;
308
- }
309
-
310
321
public static bool LoadFromAssemblyC ( [ System . Runtime . InteropServices . MarshalAs ( System . Runtime . InteropServices . UnmanagedType . LPWStr ) ] string assemblyFile )
311
322
{
312
323
return LoadFromAssembly ( assemblyFile ) ;
@@ -377,7 +388,7 @@ public void LoadFunctions(Assembly assembly)
377
388
}
378
389
catch ( Exception ex )
379
390
{
380
- Console . Error . WriteLine ( ex . Message ) ;
391
+ Log ( ex . Message ) ;
381
392
}
382
393
383
394
return null ;
0 commit comments