|
1 | | -using System; |
| 1 | +using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
4 | 4 | using System.Linq; |
|
7 | 7 |
|
8 | 8 | namespace Python.Runtime |
9 | 9 | { |
| 10 | + public class ResolveStreamResouceArgs : EventArgs |
| 11 | + { |
| 12 | + public string Name { get; set; } |
| 13 | + } |
| 14 | + |
10 | 15 | /// <summary> |
11 | 16 | /// This class provides the public interface of the Python runtime. |
12 | 17 | /// </summary> |
13 | 18 | public class PythonEngine : IDisposable |
14 | 19 | { |
| 20 | + public static event Func<ResolveStreamResouceArgs, Stream> StreamResouceResolve; |
| 21 | + |
15 | 22 | private static DelegateManager delegateManager; |
16 | 23 | private static bool initialized; |
17 | 24 | private static IntPtr _pythonHome = IntPtr.Zero; |
@@ -202,8 +209,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true) |
202 | 209 | IntPtr builtins = Runtime.PyEval_GetBuiltins(); |
203 | 210 | Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins); |
204 | 211 |
|
205 | | - Assembly assembly = Assembly.GetExecutingAssembly(); |
206 | | - using (Stream stream = assembly.GetManifestResourceStream("clr.py")) |
| 212 | + using (Stream stream = LoadResource("clr.py")) |
207 | 213 | using (var reader = new StreamReader(stream)) |
208 | 214 | { |
209 | 215 | // add the contents of clr.py to the module |
@@ -534,6 +540,26 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals, |
534 | 540 | } |
535 | 541 | } |
536 | 542 | } |
| 543 | + |
| 544 | + private static Stream LoadResource(string name) |
| 545 | + { |
| 546 | + var args = new ResolveStreamResouceArgs() |
| 547 | + { |
| 548 | + Name = name |
| 549 | + }; |
| 550 | + Stream stream; |
| 551 | + foreach (Func<ResolveStreamResouceArgs, Stream> resolver in StreamResouceResolve.GetInvocationList()) |
| 552 | + { |
| 553 | + stream = resolver(args); |
| 554 | + if (stream != null) |
| 555 | + { |
| 556 | + return stream; |
| 557 | + } |
| 558 | + } |
| 559 | + Assembly assembly = Assembly.GetExecutingAssembly(); |
| 560 | + stream = assembly.GetManifestResourceStream("clr.py"); |
| 561 | + return stream; |
| 562 | + } |
537 | 563 | } |
538 | 564 |
|
539 | 565 | public enum RunFlagType |
|
0 commit comments