private async Task<ScriptState<object>> ExecuteOnUIThread(Script<object> script, ScriptState<object> stateOpt)
            {
                return await Task.Factory.StartNew(async () =>
                {
                    try
                    {
                        var task = (stateOpt == null) ?
                            script.RunAsync(_hostObject, CancellationToken.None) :
                            script.ContinueAsync(stateOpt, CancellationToken.None);

                        return await task.ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        // TODO (tomat): format exception
                        Console.Error.WriteLine(e);
                        return null;
                    }
                },
                CancellationToken.None,
                TaskCreationOptions.None,
                s_UIThreadScheduler).Unwrap().ConfigureAwait(false);
            }
            private async Task<ScriptState<object>> ExecuteOnUIThread(Script<object> script, ScriptState<object> stateOpt, bool displayResult)
            {
                return await ((Task<ScriptState<object>>)s_control.Invoke(
                    (Func<Task<ScriptState<object>>>)(async () =>
                    {
                        var task = (stateOpt == null) ?
                            script.RunAsync(_globals, catchException: e => true, cancellationToken: CancellationToken.None) :
                            script.RunFromAsync(stateOpt, catchException: e => true, cancellationToken: CancellationToken.None);

                        var newState = await task.ConfigureAwait(false);

                        if (newState.Exception != null)
                        {
                            DisplayException(newState.Exception);
                        }
                        else if (displayResult && newState.Script.HasReturnValue())
                        {
                            _globals.Print(newState.ReturnValue);
                        }

                        return newState;

                    }))).ConfigureAwait(false);
            }
 private static async Task<ScriptState<object>> ExecuteOnUIThread(Script<object> script)
 {
     return await (await _dispatcher.InvokeAsync(
         async () =>
         {
             try
             {
                 var task = script.RunAsync();
                 return await task.ConfigureAwait(false);
             }
             catch (FileLoadException e) when (e.InnerException is NotSupportedException)
             {
                 Console.Error.WriteLine(e.InnerException.Message);
                 return null;
             }
             catch (Exception e)
             {
                 e.Dump();
                 return null;
             }
         })).ConfigureAwait(false);
 }
 private async Task<ScriptState<object>> ExecuteOnUIThread(Script<object> script, ScriptState<object> stateOpt)
 {
     return await ((Task<ScriptState<object>>)s_control.Invoke(
         (Func<Task<ScriptState<object>>>)(async () =>
         {
             try
             {
                 var task = (stateOpt == null) ?
                     script.RunAsync(_globals, CancellationToken.None) :
                     script.ContinueAsync(stateOpt, CancellationToken.None);
                 return await task.ConfigureAwait(false);
             }
             catch (FileLoadException e) when (e.InnerException is InteractiveAssemblyLoaderException)
             {
                 Console.Error.WriteLine(e.InnerException.Message);
                 return null;
             }
             catch (Exception e)
             {
                 // TODO (tomat): format exception
                 Console.Error.WriteLine(e);
                 return null;
             }
         }))).ConfigureAwait(false);
 }
Exemple #5
0
    public static bool LoadGamemode(string Name)
    {
        UnloadGamemode();

        Directory ModeDir = new Directory();

        if (ModeDir.FileExists($"user://Gamemodes/{Name}/{Name}.json"))
        {
            Console.Log($"Found gamemode '{Name}', loading");

            GmConfigClass Config;
            {
                File ConfigFile = new File();
                ConfigFile.Open($"user://Gamemodes/{Name}/{Name}.json", 1);
                try
                {
                    Config = Newtonsoft.Json.JsonConvert.DeserializeObject <GmConfigClass>(ConfigFile.GetAsText());
                    ConfigFile.Close();
                }
                catch (Newtonsoft.Json.JsonReaderException)
                {
                    ConfigFile.Close();
                    Console.ThrowLog($"Failed to parse config file for gamemode '{Name}'");
                    return(false);
                }
            }
            if (Config.MainScript == null)
            {
                Console.ThrowLog($"The gamemode '{Name}' did not specify a path for MainScript");
                return(false);
            }

            File ScriptFile = new File();
            if (!ScriptFile.FileExists($"user://Gamemodes/{Name}/{Config.MainScript}"))
            {
                Console.ThrowLog($"Specified MainScript '{Config.MainScript}' for gamemode '{Name}' does not exist");
                return(false);
            }
            ScriptFile.Open($"user://Gamemodes/{Name}/{Config.MainScript}", 1);
            Sc.Script Engine = Cs.Create(ScriptFile.GetAsText(),
                                         ScriptOptions.WithSourceResolver(new Microsoft.CodeAnalysis.SourceFileResolver(ImmutableArray <string> .Empty, $"{OS.GetUserDataDir()}/Gamemodes/{Name}"))
                                         .WithEmitDebugInformation(true)
                                         .WithFilePath($"{OS.GetUserDataDir()}/Gamemodes/{Name}")
                                         .WithFileEncoding(System.Text.Encoding.UTF8));             //NOTE Hardcoding UTF8 should work for now
            ScriptFile.Close();

            object Returned = null;
            try
            {
                Sc.ScriptState State = Engine.RunAsync().Result;
                Returned = State.ReturnValue;
            }
            catch (Exception Err)
            {
                Console.ThrowLog($"Error executing gamemode '{Name}': {Err.Message}");
                return(false);
            }

            if (Returned is Gamemode)
            {
                GamemodeName = Name;
                Game.Mode    = Returned as Gamemode;
                Game.Mode.SetName("Gamemode");
                Game.Mode.Self     = Game.Mode;
                Game.Mode.LoadPath = $"{OS.GetUserDataDir()}/Gamemodes/{Name}";
                Game.Mode.OwnName  = Name;
                Game.Self.AddChild(Game.Mode);
                return(true);
            }
            else
            {
                Console.ThrowLog($"Gamemode script '{Name}' did not return a valid Gamemode instance, unloading");
                return(false);
            }
        }
        else
        {
            Console.ThrowPrint($"No gamemode named '{Name}'");
            return(false);
        }
    }
            private async Task<ScriptState<object>> ExecuteOnUIThread(Script<object> script, ScriptState<object> stateOpt)
            {
                return await Task.Factory.StartNew(async () =>
                {
                    try
                    {
                        var task = (stateOpt == null) ?
                            script.RunAsync(_globals, CancellationToken.None) :
                            script.ContinueAsync(stateOpt, CancellationToken.None);

                        return await task.ConfigureAwait(false);
                    }
                    catch (FileLoadException e) when (e.InnerException is InteractiveAssemblyLoaderException)
                    {
                        Console.Error.WriteLine(e.InnerException.Message);
                        return null;
                    }
                    catch (Exception e)
                    {
                        // TODO (tomat): format exception
                        Console.Error.WriteLine(e);
                        return null;
                    }
                },
                CancellationToken.None,
                TaskCreationOptions.None,
                s_UIThreadScheduler).Unwrap().ConfigureAwait(false);
            }
            private async Task<ExecuteResult> ExecuteOnUIThread(TaskResult result, Script<object> script)
            {
                var executeResult = await Task.Factory.StartNew(async () =>
                    {
                        try
                        {
                            var state = result.State;
                            var globals = state ?? (object)_hostObject;
                            state = script.RunAsync(globals, CancellationToken.None);
                            var value = await state.ReturnValue.ConfigureAwait(false);
                            return new ExecuteResult(result.With(state), value, null, true);
                        }
                        catch (Exception e)
                        {
                            return new ExecuteResult(result, null, e, false);
                        }
                    },
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    s_UIThreadScheduler).Unwrap().ConfigureAwait(false);

                var exception = executeResult.Exception;
                if (exception != null)
                {
                    // TODO (tomat): format exception
                    Console.Error.WriteLine(exception);
                }

                return executeResult;
            }
 private object ExecuteInner(Script<object> script)
 {
     var globals = _lastResult != null ? (object)_lastResult : (object)_hostObject;
     var result = script.RunAsync(globals, CancellationToken.None); // TODO
     _lastResult = result;
     var task = result.ReturnValue;
     return task.Result; // For now, submissions are assumed to be synchronous.
 }
Exemple #9
0
 internal Submission(Script <T> script, Lazy <object> input)
 {
     _script     = script;
     _lazyResult = new Lazy <ScriptState <T> >(() => script.RunAsync(input.Value, CancellationToken.None));
 }