public void RunScript(string source) { _activeProcessorCount++; Task.Run(() => { try { var processor = CreateProcessor(); var result = processor.Run(source); if (ScriptContextManipulator.ThrownRuntimeError(processor) && ScriptOutAdapter.Translate(result) is ScriptRuntimeException runtimeException) { throw runtimeException; } } catch (Exception ex) { Console.WriteLine("An error occurred during script execution: " + ex.Message); } _activeProcessorCount--; }); }
private static ScriptProcessor CreateProcessor() { var processor = new ScriptProcessor(_prototypeBuffer); ScriptContextManipulator.SetCallbackExecuteMethod(processor, ExecuteMethod); return(processor); }
private ScriptProcessor CreateProcessor() { if (_prototypeBuffer == null) { InitializePrototypeBuffer(); } var processor = new ScriptProcessor(_prototypeBuffer); ScriptContextManipulator.SetCallbackExecuteMethod(processor, ExecuteMethod); return(processor); }
public static void RunScript(string scriptFile) { if (_apiClasses == null) { InitializeApiClasses(); } if (_prototypeBuffer == null) { InitializePrototypeBuffer(); } ActiveProcessorCount++; Task.Run(() => { try { var gamemode = GameInstance.GetService <GameModeManager>().ActiveGameMode; var source = Encoding.UTF8.GetString(gamemode.FileLoader.GetFile(gamemode.GetScriptFilePath(scriptFile), false).Data); var processor = CreateProcessor(); var result = processor.Run(source); if (ScriptContextManipulator.ThrownRuntimeError(processor)) { var exObj = ScriptOutAdapter.Translate(result); var runtimeException = exObj as ScriptRuntimeException; if (runtimeException != null) { throw runtimeException; } } } catch (ArgumentNullException) { var message = $"Failed to run script \"{scriptFile}\""; GameLogger.Instance.Log(MessageType.Error, message); GameInstance.GetService <NotificationBar>().PushNotification(NotificationKind.Error, message); } catch (ScriptRuntimeException ex) { var message = $"Script execution failed at runtime. {ex.Type} ({scriptFile}, L{ex.Line}): {ex.Message}"; GameLogger.Instance.Log(MessageType.Error, message); GameInstance.GetService <NotificationBar>().PushNotification(NotificationKind.Error, message); } ActiveProcessorCount--; }); }
public void ObjectTranslateTest() { var processor = new ScriptProcessor(); ScriptContextManipulator.AddPrototype(processor, typeof(Pokemon)); processor.Run("var p = new Pokemon(); p.SetName(\"Pika\");"); object objp = ScriptContextManipulator.GetVariableTranslated(processor, "p"); Assert.IsTrue(objp.GetType() == typeof(Pokemon)); Pokemon p = (Pokemon)objp; Assert.AreEqual("Pikachu", p.OriginalName); Assert.AreEqual("Pika", p.Name); }