Created
February 24, 2015 05:40
-
-
Save Toyz/ce5197e7124503ce80c9 to your computer and use it in GitHub Desktop.
Just a Simple Script Engine implation to use IronPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using BDScript.Overrides; | |
using IronPython.Hosting; | |
namespace BDScript | |
{ | |
public class ScriptEngine | |
{ | |
#region Static ScripEngine Instance | |
public static ScriptEngine Instance | |
{ | |
get { return _instance ?? (_instance = new ScriptEngine()); } | |
set { _instance = value; } | |
} | |
private static ScriptEngine _instance; | |
#endregion | |
public string Classname { get; private set; } | |
public Microsoft.Scripting.Hosting.ScriptEngine PythonScriptEngine { get; private set; } | |
public dynamic Scope { get; private set; } | |
public EventRaisingStreamWriter EventRaisingStreamWriter { get; private set; } | |
private readonly List<Assembly> _loadedAssemblysList; | |
private readonly Dictionary<string, dynamic> _loadedScriptsObjects; | |
public ScriptEngine(string classname = "Script") | |
{ | |
Classname = classname; | |
var ms = new MemoryStream(); | |
EventRaisingStreamWriter = new EventRaisingStreamWriter(ms); | |
_loadedAssemblysList = new List<Assembly>(); | |
_loadedScriptsObjects = new Dictionary<string, dynamic>(); | |
PythonScriptEngine = Python.CreateEngine(); | |
PythonScriptEngine.Runtime.IO.SetOutput(ms, EventRaisingStreamWriter); | |
PythonScriptEngine.Runtime.IO.SetErrorOutput(ms, EventRaisingStreamWriter); | |
} | |
public void ChangeClassName(string className) | |
{ | |
Classname = className; | |
} | |
public void AddAssemblyByType(Type item) | |
{ | |
var ass = Assembly.GetAssembly(item); | |
if (_loadedAssemblysList.Contains(ass)) | |
{ | |
return; | |
} | |
_loadedAssemblysList.Add(ass); | |
PythonScriptEngine.Runtime.LoadAssembly(ass); | |
} | |
public void AddAssemblyByType(List<Type> items) | |
{ | |
foreach (var ass in items.Select(Assembly.GetAssembly).Where(ass => !_loadedAssemblysList.Contains(ass))) | |
{ | |
_loadedAssemblysList.Add(ass); | |
PythonScriptEngine.Runtime.LoadAssembly(ass); | |
} | |
} | |
public void AddAssembly(Assembly item) | |
{ | |
if (_loadedAssemblysList.Contains(item)) | |
{ | |
return; | |
} | |
_loadedAssemblysList.Add(item); | |
PythonScriptEngine.Runtime.LoadAssembly(item); | |
} | |
public void AddAssembly(List<Assembly> items) | |
{ | |
foreach (var item in items.Where(item => !_loadedAssemblysList.Contains(item))) | |
{ | |
_loadedAssemblysList.Add(item); | |
PythonScriptEngine.Runtime.LoadAssembly(item); | |
} | |
} | |
public List<Assembly> GetLoadedAssemblies() | |
{ | |
return _loadedAssemblysList; | |
} | |
public Dictionary<string, dynamic> GetLoadedFiles() | |
{ | |
return _loadedScriptsObjects; | |
} | |
public bool AssemblyLoaded(Assembly item) | |
{ | |
return _loadedAssemblysList.Contains(item); | |
} | |
public bool AssemblyLoaded(Type item) | |
{ | |
return _loadedAssemblysList.Contains(Assembly.GetAssembly(item)); | |
} | |
public dynamic RunScript(string path, string method, params object[] param) | |
{ | |
Scope = PythonScriptEngine.CreateScope(); | |
Scope.owner = this; | |
PythonScriptEngine.ExecuteFile(path, Scope); | |
var scriptClass = PythonScriptEngine.Operations.CreateInstance(Scope.GetVariable(Classname)); | |
return param == null | |
? PythonScriptEngine.Operations.InvokeMember(scriptClass, method) | |
: PythonScriptEngine.Operations.InvokeMember(scriptClass, method, param); | |
} | |
public dynamic RunString(string content, string method, params object[] param) | |
{ | |
Scope = PythonScriptEngine.CreateScope(); | |
Scope.owner = this; | |
PythonScriptEngine.Execute(content, Scope); | |
var scriptClass = PythonScriptEngine.Operations.CreateInstance(Scope.GetVariable(Classname)); | |
return param == null | |
? PythonScriptEngine.Operations.InvokeMember(scriptClass, method) | |
: PythonScriptEngine.Operations.InvokeMember(scriptClass, method, param); | |
} | |
public dynamic RunScriptGetScope(string path) | |
{ | |
dynamic item; | |
_loadedScriptsObjects.TryGetValue(path, out item); | |
if (item != null) return item; | |
Scope = PythonScriptEngine.CreateScope(); | |
Scope.owner = this; | |
PythonScriptEngine.ExecuteFile(path, Scope); | |
var scriptClass = PythonScriptEngine.Operations.CreateInstance(Scope.GetVariable(Classname)); | |
_loadedScriptsObjects.Add(path, scriptClass); | |
return scriptClass; | |
} | |
public dynamic RunStringGetScope(string content) | |
{ | |
Scope = PythonScriptEngine.CreateScope(); | |
Scope.owner = this; | |
PythonScriptEngine.Execute(content, Scope); | |
var scriptClass = PythonScriptEngine.Operations.CreateInstance(Scope.GetVariable(Classname)); | |
return scriptClass; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment