//interface //constructor public Console(IConsoleTarget target) { if (target == null) { throw new ArgumentException("target cannot be null"); } title = target.ConsoleTitle; this.target = target; var targetScope = target.GetScope(); var locals = new IronPython.Runtime.PythonDictionary(); foreach (var name in targetScope.GetVariableNames()) { locals[name] = targetScope.GetVariable(name); } scope = Py.CreateScope(); scope.SetVariable("_locals", locals); var ops = scope.Engine.Operations; string init_script = System.IO.File.ReadAllText(Util.ResourcePath("PythonScripts/initialize_new_console.py")); Py.Engine.Execute(init_script, scope); python_output_buffer = scope.GetVariable <IronPython.Modules.PythonIOModule.StringIO>("output_buffer"); python_compile_command = scope.GetVariable <IronPython.Runtime.PythonFunction>("compile_command"); python_console_run_code = scope.GetVariable <IronPython.Runtime.PythonFunction>("console_run_code"); output = new LineBuffer(new string[] { ">>> " }); input = new LineBuffer(new string[] { "" }); editor = new StringBuilder(500); multilineEditor = new StringBuilder(500); _currentInputIndex = 0; _editorCursor = 0; }
public static void PopulateWithNewMod(ModContentPack rwmodInfo) { var path = new ComparablePath(rwmodInfo.PythonFolder()); //will throw ex if rwmodInfo is null, which is fine string scriptPath = Path.Combine(path.reconstructedPath, "main.py"); if (!Directory.Exists(path.ToString()) || !File.Exists(scriptPath)) { return; } ScriptSource mainScriptSource = Py.Engine.CreateScriptSourceFromFile(scriptPath); string packageName = PythonMod.MakeHiddenPackageName(rwmodInfo.Identifier); PythonModManager inst = Instance; //getting this after several potential points of failure, to avoid pointless instantiation if (!inst.ordered.TrueForAll(m => m.rwmodInfo != rwmodInfo)) { throw new ArgumentException( "The mod with that ModContentPack has already been added"); } //create and import package var pkg = IronPython.Modules.PythonImport.new_module(DefaultContext.Default, packageName); var pkg_dict = (PythonDictionary)typeof(IronPython.Runtime.PythonModule).InvokeMember("_dict", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, pkg, null); { var __path__ = new IronPython.Runtime.List(); __path__.Add(path.reconstructedPath); pkg_dict["__path__"] = __path__; pkg_dict["__file__"] = scriptPath; SystemModules[packageName] = pkg; } //setup scope ScriptScope scope = Py.CreateScope(); scope.SetVariable("__contentpack__", rwmodInfo); scope.GetModuleContext().AddExtensionType(typeof(StandardScriptingExtensions)); // MAKE MOD OBJECT var mod = new PythonMod(rwmodInfo, packageName, scope, mainScriptSource); inst.ordered.Add(mod); //run main.py try { mainScriptSource.Execute(scope); } catch (Exception e) { string msg = "Exception while loading " + scriptPath + ": " + e.ToString() + "\n" + Py.GetFullErrorMessage(e); Verse.Log.Error(msg); pkg_dict["__error__"] = e; } }
public static TimeSpan WriteTime(int writes, Func <DateTime> measureFunction, Action cleanupFunction, Action <PyScope, int> write) { DateTime start, end; using (var _ = Py.GIL()) { using (var scope = Py.CreateScope()) { cleanupFunction?.Invoke(); start = measureFunction(); for (var i = 0; i < writes; i++) { write?.Invoke(scope, i); } cleanupFunction?.Invoke(); end = measureFunction(); } } return(end - start); }
public static long WriteMem(int writes, Func <long> measureFunction, Action cleanupFunction, Action <PyScope, int> write) { long start, end; using (var state = Py.GIL()) { using (var scope = Py.CreateScope()) { cleanupFunction?.Invoke(); start = measureFunction(); for (var i = 0; i < writes; i++) { write?.Invoke(scope, i); } cleanupFunction?.Invoke(); end = measureFunction(); } } return(end - start); }
public static TimeSpan ReadTime(int reads, Func <DateTime> measureFunction, Action cleanupFunction) { DateTime start, end; var a = 0; using (var state = Py.GIL()) { using (var scope = Py.CreateScope()) { scope.Exec("a = 1"); cleanupFunction?.Invoke(); start = measureFunction(); Read(reads, ref a, scope); cleanupFunction?.Invoke(); end = measureFunction(); scope.Set("a", a); } } return(end - start); }