|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using CSScriptLibrary; |
| 4 | +using Rocket.API.DependencyInjection; |
| 5 | +using Rocket.API.Plugins; |
| 6 | + |
| 7 | +namespace Rocket.Scripting.CSharp |
| 8 | +{ |
| 9 | + class CSharpScriptingProvider : ScriptingProvider |
| 10 | + { |
| 11 | + public CSharpScriptingProvider(IDependencyContainer container) : base(container) |
| 12 | + { |
| 13 | + } |
| 14 | + |
| 15 | + public override string[] FileTypes => new[] { "cs" }; |
| 16 | + public override string ScriptName => "C#"; |
| 17 | + public override IEnumerable<IPlugin> Plugins { get; } |
| 18 | + private List<IPlugin> _plugins = new List<IPlugin>(); |
| 19 | + |
| 20 | + public override IScriptContext CreateScriptContext(IDependencyContainer container) |
| 21 | + { |
| 22 | + CSharpScriptEngine |
| 23 | + IEvaluator evaluator = CSScript. |
| 24 | + evaluator.ReferenceDomainAssemblies(); |
| 25 | + |
| 26 | + var ctx = new CSharpScriptContext(container, evaluator); |
| 27 | + RegisterContext(ctx); |
| 28 | + return ctx; |
| 29 | + } |
| 30 | + |
| 31 | + public override ScriptResult ExecuteFile(string path, string entryPoint, IDependencyContainer container, ref IScriptContext context, |
| 32 | + ScriptPluginMeta meta, bool createPluginInstanceOnNull = false) |
| 33 | + { |
| 34 | + if (context == null) |
| 35 | + { |
| 36 | + context = CreateScriptContext(container.CreateChildContainer()); |
| 37 | + |
| 38 | + if (createPluginInstanceOnNull) |
| 39 | + { |
| 40 | + var plugin = (CSharpPlugin)GetPlugin(meta.Name); |
| 41 | + if (plugin == null) |
| 42 | + { |
| 43 | + plugin = new CSharpPlugin(meta, context.Container, this, (CSharpScriptContext)context); |
| 44 | + _plugins.Add(plugin); |
| 45 | + } |
| 46 | + |
| 47 | + context.SetGlobalVariable("plugin", plugin); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + var engine = ((CSharpScriptContext)context).Evaluator; |
| 52 | + if (engine == null) |
| 53 | + { |
| 54 | + return new ScriptResult(ScriptExecutionResult.FailedMisc); |
| 55 | + } |
| 56 | + |
| 57 | + engine.LoadFile(path); |
| 58 | + |
| 59 | + /* |
| 60 | + var ret = engine.CallGlobalFunction(entryPoint, context); |
| 61 | + var res = new ScriptResult(ScriptExecutionResult.Success) |
| 62 | + { |
| 63 | + HasReturn = ret is Undefined, |
| 64 | + Return = ret is Null ? null : ret |
| 65 | + }; |
| 66 | + return res; |
| 67 | + */ |
| 68 | + |
| 69 | + return new ScriptResult(ScriptExecutionResult.Success); |
| 70 | + } |
| 71 | + |
| 72 | + protected override void OnInit() |
| 73 | + { |
| 74 | + foreach (var directory in Directory.GetDirectories(WorkingDirectory)) |
| 75 | + { |
| 76 | + var pluginFile = Path.Combine(directory, "plugin.json"); |
| 77 | + if (!File.Exists(pluginFile)) |
| 78 | + continue; |
| 79 | + |
| 80 | + var meta = GetPluginMeta(pluginFile); |
| 81 | + var entryFile = Path.Combine(directory, meta.EntryFile); |
| 82 | + if (!File.Exists(entryFile)) |
| 83 | + throw new FileNotFoundException(null, entryFile); |
| 84 | + |
| 85 | + IScriptContext context = null; |
| 86 | + ExecuteFile(entryFile, meta.EntryPoint, Container, ref context, meta, true); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public override string ServiceName => "C#"; |
| 91 | + } |
| 92 | +} |
0 commit comments