private object?Invoke(string funcName, CallInfo callInfo, object?[] args) { int posCount = callInfo.ArgumentCount - callInfo.ArgumentNames.Count; var argNames = Enumerable.Repeat((string?)null, posCount).Concat(callInfo.ArgumentNames).ToArray(); Invoker GetInvoker(bool construct) { var code = new StringWriter(); code.WriteLine($"#line 1 \"{FileName}\""); code.Write("return (global::Int19h.Bannerlord.CSharp.Scripting.Script.Invoker)(args => "); if (construct) { code.Write("new "); } code.Write($"{funcName}("); for (int i = 0; i < args.Length; ++i) { if (i != 0) { code.Write(", "); } var argName = argNames[i]; if (argName != null) { code.Write($"{argName}: "); } code.Write($"args[{i}]"); } code.WriteLine("));"); var script = State.Script.ContinueWith <Invoker>(code.ToString()); Scripts.IgnoreVisibility(script); try { return(script.RunAsync().GetAwaiter().GetResult().ReturnValue); } catch (CompilationErrorException) { if (construct) { return(GetInvoker(false)); } else { throw; } } } var invoker = GetInvoker(false); var oldScriptPath = ScriptGlobals.ScriptPath; ScriptGlobals.ScriptPath = FileName; args = args.Select(arg => ScriptArgument.Wrap(arg)).ToArray(); try { invoker(args); return(null); } catch (TargetInvocationException ex) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); throw; } finally { ScriptGlobals.ScriptPath = oldScriptPath; } }
public Script(string name) { Name = name; var script = CSharpScript.Create <Action <object?[]> >($"#load \"{FileName}\"", Scripts.GetScriptOptions()); Scripts.IgnoreVisibility(script); State = script.RunAsync().GetAwaiter().GetResult(); }