Пример #1
0
    Scripting()
    {
        if (Engine.EditorHint)
        {
            return;
        }

        Self = this;

        ScriptOptions = Sc.ScriptOptions.Default.WithReferences(AppDomain.CurrentDomain.GetAssemblies())
                        .AddReferences(Assembly.GetAssembly(typeof(System.Dynamic.DynamicObject)),                      // System.Code
                                       Assembly.GetAssembly(typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo)), // Microsoft.CSharp
                                       Assembly.GetAssembly(typeof(System.Dynamic.ExpandoObject)));                     // System.Dynamic

        Sc.Script CEngine = Cs.Create("", ScriptOptions);
        ConsoleState = CEngine.ContinueWith("using System; using System.Dynamic; using Godot; using static API; using static Items.ID;")
                       .RunAsync().Result;
    }
Пример #2
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);
        }
    }