Skip to content
This repository was archived by the owner on Dec 21, 2019. It is now read-only.

Commit 5ed2e3d

Browse files
committed
Add C# Scripting [.NET 4.6]
1 parent 471ff5e commit 5ed2e3d

13 files changed

+1541
-0
lines changed

Rocket.Scripting.CSharp/App.config

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
5+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /></startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-1.2.3.0" newVersion="1.2.3.0" />
11+
</dependentAssembly>
12+
<dependentAssembly>
13+
<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
14+
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
15+
</dependentAssembly>
16+
<dependentAssembly>
17+
<assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
18+
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
19+
</dependentAssembly>
20+
<dependentAssembly>
21+
<assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
22+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
23+
</dependentAssembly>
24+
<dependentAssembly>
25+
<assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
26+
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
27+
</dependentAssembly>
28+
<dependentAssembly>
29+
<assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
30+
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
31+
</dependentAssembly>
32+
<dependentAssembly>
33+
<assemblyIdentity name="System.Console" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
34+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
35+
</dependentAssembly>
36+
<dependentAssembly>
37+
<assemblyIdentity name="System.Diagnostics.StackTrace" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
38+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
39+
</dependentAssembly>
40+
<dependentAssembly>
41+
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
42+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
43+
</dependentAssembly>
44+
<dependentAssembly>
45+
<assemblyIdentity name="System.Reflection.Metadata" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
46+
<bindingRedirect oldVersion="0.0.0.0-1.4.3.0" newVersion="1.4.3.0" />
47+
</dependentAssembly>
48+
<dependentAssembly>
49+
<assemblyIdentity name="System.Diagnostics.FileVersionInfo" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
50+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
51+
</dependentAssembly>
52+
<dependentAssembly>
53+
<assemblyIdentity name="Microsoft.CodeAnalysis.Scripting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
54+
<bindingRedirect oldVersion="0.0.0.0-2.8.0.0" newVersion="2.8.0.0" />
55+
</dependentAssembly>
56+
<dependentAssembly>
57+
<assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral" />
58+
<bindingRedirect oldVersion="0.0.0.0-2.8.0.0" newVersion="2.8.0.0" />
59+
</dependentAssembly>
60+
<dependentAssembly>
61+
<assemblyIdentity name="Microsoft.CodeAnalysis.CSharp.Scripting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
62+
<bindingRedirect oldVersion="0.0.0.0-2.8.0.0" newVersion="2.8.0.0" />
63+
</dependentAssembly>
64+
</assemblyBinding>
65+
</runtime>
66+
</configuration>
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Rocket.API.DependencyInjection;
2+
using Rocket.Core.DependencyInjection;
3+
4+
namespace Rocket.Scripting.CSharp
5+
{
6+
[DontAutoRegister]
7+
public class CSharpPlugin: ScriptPlugin
8+
{
9+
public CSharpPlugin(ScriptPluginMeta pluginMeta, IDependencyContainer container, ScriptingProvider provider, CSharpScriptContext context) : base(pluginMeta, container, provider, context)
10+
{
11+
}
12+
13+
protected override bool OnActivate()
14+
{
15+
return true;
16+
}
17+
18+
protected override bool OnDeactivate()
19+
{
20+
return true;
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using Rocket.API.DependencyInjection;
3+
4+
namespace Rocket.Scripting.CSharp
5+
{
6+
public class CSharpScriptContext : IScriptContext
7+
{
8+
public CSharpScriptContext(IDependencyContainer container, IEvaluator evaluator)
9+
{
10+
Evaluator = evaluator;
11+
Container = container;
12+
}
13+
14+
public IDependencyContainer Container { get; }
15+
public IEvaluator Evaluator { get; }
16+
17+
public void ExposeType(string name, Type type)
18+
{
19+
Evaluator.ReferenceAssemblyByNamespace(type.Namespace);
20+
}
21+
22+
public void SetGlobalVariable(string name, object value)
23+
{
24+
Evaluator.SetValue(name, value);
25+
}
26+
27+
public void SetGlobalFunction(string name, Delegate function)
28+
{
29+
Evaluator.SetValue(name, function);
30+
}
31+
32+
public ScriptResult Eval(string expression)
33+
{
34+
Evaluator.LoadCode(expression);
35+
36+
//todo
37+
return new ScriptResult(ScriptExecutionResult.Success);
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Rocket.API.DependencyInjection;
2+
using Rocket.API.Plugins;
3+
using Rocket.Core.Plugins;
4+
5+
namespace Rocket.Scripting.CSharp
6+
{
7+
public class CSharpScriptingProviderPlugin : Plugin
8+
{
9+
public CSharpScriptingProviderPlugin(IDependencyContainer container) : base("C# Scripting", container)
10+
{
11+
}
12+
13+
protected override void OnLoad(bool isFromReload)
14+
{
15+
base.OnLoad(isFromReload);
16+
if (!isFromReload)
17+
{
18+
Container.Resolve<IPluginManager>("csharp_plugins").Init();
19+
}
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// Allgemeine Informationen über eine Assembly werden über die folgenden
5+
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
6+
// die einer Assembly zugeordnet sind.
7+
[assembly: AssemblyTitle("Rocket.Scripting.CSharp")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("Rocket.Scripting.CSharp")]
12+
[assembly: AssemblyCopyright("Copyright © 2018")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
17+
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
18+
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
19+
[assembly: ComVisible(false)]
20+
21+
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
22+
[assembly: Guid("2fa79532-b649-4769-ae89-eb0c2a24b411")]
23+
24+
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
25+
//
26+
// Hauptversion
27+
// Nebenversion
28+
// Buildnummer
29+
// Revision
30+
//
31+
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
32+
// übernehmen, indem Sie "*" eingeben:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Rocket.API.DependencyInjection;
2+
using Rocket.API.Plugins;
3+
4+
namespace Rocket.Scripting.CSharp.Properties
5+
{
6+
public class DependencyRegistrator : IDependencyRegistrator
7+
{
8+
public void Register(IDependencyContainer container, IDependencyResolver resolver)
9+
{
10+
container.RegisterSingletonType<IPluginManager, CSharpScriptingProvider>("csharp_plugins");
11+
var instance = (CSharpScriptingProvider) container.Resolve<IPluginManager>("csharp_plugins");
12+
container.RegisterSingletonInstance<IScriptingProvider>(instance, "csharp_plugins");
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)