예제 #1
0
        // Mimicked from https://github.com/kkdevs/Patchwork/blob/master/Patchwork/MonoScript.cs#L124
        public static Assembly Compile(Dictionary <string, byte[]> sources, TextWriter logger = null)
        {
            ReportPrinter reporter = logger == null ? new ConsoleReportPrinter() : new StreamReportPrinter(logger);

            Location.Reset();

            var dllName = $"compiled_{DateTime.Now.Ticks}";

            compiledAssemblies.Add(dllName);

            var ctx = CreateContext(reporter);

            ctx.Settings.SourceFiles.Clear();

            var i = 0;

            SeekableStreamReader GetFile(SourceFile file)
            {
                return(new SeekableStreamReader(new MemoryStream(sources[file.OriginalFullPathName]), Encoding.UTF8));
            }

            foreach (var source in sources)
            {
                ctx.Settings.SourceFiles.Add(new SourceFile(Path.GetFileName(source.Key), source.Key, i, GetFile));
                i++;
            }

            var container = new ModuleContainer(ctx);

            RootContext.ToplevelTypes = container;
            Location.Initialize(ctx.Settings.SourceFiles);

            var session = new ParserSession {
                UseJayGlobalArrays = true, LocatedTokens = new LocatedToken[15000]
            };

            container.EnableRedefinition();

            foreach (var sourceFile in ctx.Settings.SourceFiles)
            {
                var stream = sourceFile.GetInputStream(sourceFile);
                var source = new CompilationSourceFile(container, sourceFile);
                source.EnableRedefinition();
                container.AddTypeContainer(source);
                var parser = new CSharpParser(stream, source, session);
                parser.parse();
            }

            var ass = new AssemblyDefinitionDynamic(container, dllName, $"{dllName}.dll");

            container.SetDeclaringAssembly(ass);

            var importer = new ReflectionImporter(container, ctx.BuiltinTypes);

            ass.Importer = importer;

            var loader = new DynamicLoader(importer, ctx);

            ImportAppdomainAssemblies(a => importer.ImportAssembly(a, container.GlobalRootNamespace));

            loader.LoadReferences(container);
            ass.Create(AppDomain.CurrentDomain, AssemblyBuilderAccess.RunAndSave);
            container.CreateContainer();
            loader.LoadModules(ass, container.GlobalRootNamespace);
            container.InitializePredefinedTypes();
            container.Define();

            if (ctx.Report.Errors > 0)
            {
                logger?.WriteLine("Found errors! Aborting compilation...");
                return(null);
            }

            try
            {
                ass.Resolve();
                ass.Emit();
                container.CloseContainer();
                ass.EmbedResources();
            }
            catch (Exception e)
            {
                logger?.WriteLine($"Failed to compile because {e}");
                return(null);
            }

            return(ass.Builder);
        }
예제 #2
0
    public Assembly DoStaticCompile(IEnumerable <object> sources, string prefix = "compiled_")
    {
        reporter.Reset();
        Location.Reset();
        var ctx = BuildContext(reporter);

        ctx.Settings.SourceFiles.Clear();
        int             i        = 0;
        var             allBytes = new MemoryStream();
        List <Assembly> imports  = new List <Assembly>();

        foreach (var fo in sources)
        {
            Assembly impass = fo as Assembly;
            if (impass != null)
            {
                imports.Add(impass);
                continue;
            }
            var    f    = fo as string;
            byte[] fbuf = fo as byte[];
            if (f != null)
            {
                if (!f.EndsWith(".cs"))
                {
                    continue;
                }
                var bname = (f + "\n").ToBytes();
                allBytes.Write(bname, 0, bname.Length);
                fbuf = File.ReadAllBytes(f);
                allBytes.Write(fbuf, 0, fbuf.Length);
            }
            else
            {
                allBytes.Write(fbuf, 0, fbuf.Length);
                f = null;
            }
            i++;
            ctx.Settings.SourceFiles.Add(new SourceFile(f == null ? "<eval>" : Path.GetFileName(f), f ?? "<eval>", i, (o) =>
            {
                return(new SeekableStreamReader(new MemoryStream(fbuf), Encoding.UTF8));
            }));
        }
        string dllname = prefix + (counter++) + ".dll";

        if (tempdir != null)
        {
            if (hashkey != null)
            {
                var hb = hashkey.ToBytes();
                allBytes.Write(hb, 0, hb.Length);
            }

            var hash = prefix + Ext.HashToString(allBytes.ToArray()).Substring(0, 12).ToLower() + ".dll";
            if (hashkey == null)
            {
                hashkey = hash;
            }

            dllname = Path.Combine(tempdir, hash);
            if (File.Exists(dllname))
            {
                var nam = AssemblyName.GetAssemblyName(dllname);
                unloaded.Remove(nam.Name.ToLower());
                return(Assembly.Load(nam));
            }
        }

        var mod = new ModuleContainer(ctx);

        RootContext.ToplevelTypes = mod;
        Location.Initialize(ctx.Settings.SourceFiles);
        var session = new ParserSession()
        {
            UseJayGlobalArrays = true,
            LocatedTokens      = new LocatedToken[15000]
        };

        mod.EnableRedefinition();
        foreach (var finfo in ctx.Settings.SourceFiles)
        {
            var fs   = finfo.GetInputStream(finfo);
            var csrc = new CompilationSourceFile(mod, finfo);
            csrc.EnableRedefinition();
            mod.AddTypeContainer(csrc);
            var parser = new CSharpParser(fs, csrc, session);
            parser.parse();
        }
        Debug.Log("Defining new assembly " + dllname);
        var ass = new AssemblyDefinitionDynamic(mod, Path.GetFileNameWithoutExtension(dllname), dllname);

        mod.SetDeclaringAssembly(ass);
        var importer = new ReflectionImporter(mod, ctx.BuiltinTypes);

        ass.Importer = importer;
        var loader = new DynamicLoader(importer, ctx);

        ImportAssemblies((a) => importer.ImportAssembly(a, mod.GlobalRootNamespace), prefix);
        foreach (var impa in imports)
        {
            importer.ImportAssembly(impa, mod.GlobalRootNamespace);
        }
        loader.LoadReferences(mod);
        ass.Create(AppDomain.CurrentDomain, AssemblyBuilderAccess.RunAndSave);
        mod.CreateContainer();
        loader.LoadModules(ass, mod.GlobalRootNamespace);
        mod.InitializePredefinedTypes();
        mod.Define();
        if (ctx.Report.Errors > 0)
        {
            tw.WriteLine($"{ctx.Report.Errors} errors, aborting.");
            return(null);
        }
        try
        {
            ass.Resolve();
            ass.Emit();
            mod.CloseContainer();
            ass.EmbedResources();
        }
        catch (Exception ex)
        {
            tw.WriteLine($"Link error: " + ex.ToString());
            return(null);
        }
        if (tempdir != null)
        {
            ass.Save();
        }
        return(ass.Builder);
    }