Ejemplo n.º 1
0
        internal static void IgnoreVisibility(Microsoft.CodeAnalysis.Scripting.Script script)
        {
            var options = (CSharpCompilationOptions)script.GetCompilation().Options;

            var metadataImportOptionsProperty = typeof(CompilationOptions).GetProperty("MetadataImportOptions", BindingFlags.Instance | BindingFlags.Public);

            metadataImportOptionsProperty.SetValue(options, MetadataImportOptions.All);

            var        topLevelBinderFlagsProperty = typeof(CSharpCompilationOptions).GetProperty("TopLevelBinderFlags", BindingFlags.Instance | BindingFlags.NonPublic);
            var        flags = (uint)topLevelBinderFlagsProperty.GetValue(options);
            const uint IgnoreAccessibility = 1u << 22;

            flags |= IgnoreAccessibility;
            topLevelBinderFlagsProperty.SetValue(options, flags);
        }
Ejemplo n.º 2
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;
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Sets up the new script.
        /// </summary>
        /// <param name="scriptContents">The contents of the script.</param>
        private void Initialize(string scriptContents)
        {
            var imports = _importParser.GetImportsFromCode(scriptContents);

            var options = ScriptOptions.Default
                          .AddImports(imports)
                          .AddReferences(this._referencedAssemblies);

            this._script = CSharpScript.Create(scriptContents, options, typeof(IGlobal));
            var errors = this._script.Compile();

            if (errors != null && errors.Any())
            {
                throw new Exception(@"Failed to compile script.

Following errors detected: 
" + errors.Select(d => d.ToString()).Aggregate((s1, s2) => s1.Any() ? "" : "   * " + s1 + Environment.NewLine + "   * " + s2));
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 新建脚本
 /// </summary>
 /// <param name="code">脚本语言代码</param>
 public Script(string code)
 {
     script = CSharpScript.Create(code, ScriptOptions.Default, typeof(Globals));
     script.Compile();
 }
Ejemplo n.º 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);
        }
    }
Ejemplo n.º 6
0
 public abstract Compilation CreateSubmission(Script script);
Ejemplo n.º 7
0
        internal Script(ScriptCompiler compiler, ScriptBuilder builder, string code, ScriptOptions options, Type globalsTypeOpt, Script previousOpt)
        {
            Debug.Assert(code != null);
            Debug.Assert(options != null);
            Debug.Assert(compiler != null);
            Debug.Assert(builder != null);

            Compiler    = compiler;
            Builder     = builder;
            Previous    = previousOpt;
            Code        = code;
            Options     = options;
            GlobalsType = globalsTypeOpt;
        }
Ejemplo n.º 8
0
 internal Submission(Script script, Lazy <object> input)
 {
     _script     = script;
     _lazyResult = new Lazy <ScriptState>(() => script.Run(input.Value));
 }