private void CreateAssemblyDefinition(String sAssemblyName, AssemblyKind akAssemblyKind)
 {
     assemblyName = sAssemblyName;
     assemblyKind = akAssemblyKind;
     assemblyDefinition = AssemblyFactory.DefineAssembly(sAssemblyName, TargetRuntime.NET_2_0, akAssemblyKind);
     mainModule = assemblyDefinition.MainModule;
 }
Esempio n. 2
0
        public MetadataWriter(AssemblyDefinition asm, MetadataRoot root,
                              AssemblyKind kind, TargetRuntime rt, BinaryWriter writer)
        {
            m_assembly     = asm;
            m_root         = root;
            m_runtime      = rt;
            m_imgWriter    = new ImageWriter(this, kind, writer);
            m_binaryWriter = m_imgWriter.GetTextWriter();

            m_stringCache  = new Hashtable();
            m_stringWriter = new MemoryBinaryWriter(Encoding.UTF8);
            m_stringWriter.Write((byte)0);

            m_guidCache  = new Hashtable();
            m_guidWriter = new MemoryBinaryWriter();

            m_usCache  = new Hashtable();
            m_usWriter = new MemoryBinaryWriter(Encoding.Unicode);
            m_usWriter.Write((byte)0);

            m_blobCache  = new Hashtable(ByteArrayEqualityComparer.Instance, ByteArrayEqualityComparer.Instance);
            m_blobWriter = new MemoryBinaryWriter();
            m_blobWriter.Write((byte)0);

            m_tWriter     = new MemoryBinaryWriter();
            m_tableWriter = new MetadataTableWriter(this, m_tWriter);

            m_cilWriter = new MemoryBinaryWriter();

            m_fieldDataWriter = new MemoryBinaryWriter();
            m_resWriter       = new MemoryBinaryWriter();
        }
        /// <summary>
        /// compile the C# code contained in the array of input streams. Where each
        /// stream is a text string containing C# code lines delimited by New
        /// </summary>
        /// <param name="sourceText"></param>
        /// <param name="outputKind"></param>
        /// <param name="assemblyName"></param>
        /// <returns></returns>
        public static Tuple <Assembly, IEnumerable <string> > CompileSource(
            string[] sourceText,
            PortableExecutableReference anotherReference,
            AssemblyKind assemKind = AssemblyKind.DynamicallyLinkedLibrary,
            string assemblyName    = null)
        {
            var      errmsgs = new List <string>();
            Assembly assem   = null;

            var treeArray      = SyntaxFactoryExt.ParseToSyntaxTree(sourceText);
            var compileOptions = new CSharpCompilationOptions(assemKind.ToOutputKind( ));

            if (assemblyName == null)
            {
                assemblyName = "gen" + Guid.NewGuid().ToString().Replace("-", "")
                               + ".dll";
            }

            // get a "PortableExecutableReference" that identifies each referenced
            // assembly.
            // ( To get that reference, pass an instance of an object from each
            //   assembly to an extension method. )
            var mscorlib =
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            var windowsBase           = (new FileFormatException()).GetReference();
            var systemData            = (new DataTable()).GetReference();
            var presentationCore      = (DragAction.Cancel).GetReference();
            var presentationFramework = (new EventTrigger()).GetReference();
            var diagnostics           =
                (new System.Diagnostics.BooleanSwitch("abc", "efg")).GetReference();
            var systemCore = typeof(ImmutableArrayExtensions).GetReference();
            var autocoder  = typeof(EnhancedDataTable).GetReference();

            // run the compiler, using the parsed syntax trees as input.
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName: assemblyName,
                options: compileOptions,
                syntaxTrees: treeArray,
                references: new[] { mscorlib, windowsBase, presentationCore,
                                    presentationFramework, diagnostics, systemData,
                                    systemCore, autocoder, anotherReference }
                );

            using (var ms = new MemoryStream())
            {
                var rv = compilation.Emit(ms);
                foreach (var item in rv.Diagnostics)
                {
                    var s1 = item.ToString();
                    errmsgs.Add(s1);
                }

                if (rv.Success == true)
                {
                    assem = Assembly.Load(ms.GetBuffer());
                }
            }

            return(new Tuple <Assembly, IEnumerable <string> >(assem, errmsgs));
        }
Esempio n. 4
0
        // TODO:
        // platform:<string>
        // baseaddress:<address>
        // filealign:<n>

        public CompilationOptions(
            string mainTypeName       = AnyMain,
            string scriptClassName    = null,
            AssemblyKind assemblyKind = AssemblyKind.ConsoleApplication,
            bool optimize             = false,
            bool checkOverflow        = false,
            bool isNetModule          = false)
        {
            if (mainTypeName != null && mainTypeName != AnyMain && !OptionsValidator.IsValidFullName(mainTypeName))
            {
                throw new ArgumentException("Invalid type name", "mainTypeName");
            }
            if (scriptClassName != null && !OptionsValidator.IsValidFullName(scriptClassName))
            {
                throw new ArgumentException("Invalid type name", "scriptClassName");
            }
            if (!assemblyKind.IsValid())
            {
                throw new ArgumentOutOfRangeException("assemblyKind");
            }

            ScriptClassName = scriptClassName ?? "Script";
            MainTypeName    = mainTypeName ?? AnyMain;
            AssemblyKind    = assemblyKind;
            Optimize        = optimize;
            CheckOverflow   = checkOverflow;
            IsNetModule     = isNetModule;
        }
Esempio n. 5
0
    static void Import(string ra, AssemblyKind kind)
    {
        ReaderParameters rp      = new ReaderParameters();
        bool             add_pdb = enable_debug && File.Exists(Path.ChangeExtension(ra, "pdb"));

        if (add_pdb)
        {
            rp.ReadSymbols = true;
        }

        rp.InMemory = true;

        var image = ModuleDefinition.ReadModule(ra, rp);

        if (!asm_list.Add(ra))
        {
            return;
        }
        file_list.Add(ra);
        assembly_names.Add(image.Assembly.Name.Name);
        Debug($"Processing {ra} debug {add_pdb}");

        if (add_pdb && kind == AssemblyKind.User)
        {
            file_list.Add(Path.ChangeExtension(ra, "pdb"));
        }

        foreach (var ar in image.AssemblyReferences)
        {
            var resolve = Resolve(ar.Name, out kind);
            Import(resolve, kind);
        }
    }
Esempio n. 6
0
    static string Resolve(string asm_name, out AssemblyKind kind)
    {
        kind = AssemblyKind.User;
        var asm = ResolveUser(asm_name);

        if (asm != null)
        {
            return(asm);
        }

        kind = AssemblyKind.Framework;
        asm  = ResolveFramework(asm_name);
        if (asm != null)
        {
            return(asm);
        }

        kind = AssemblyKind.Bcl;
        asm  = ResolveBcl(asm_name);
        if (asm == null)
        {
            asm = ResolveBclFacade(asm_name);
        }
        if (asm != null)
        {
            return(asm);
        }

        kind = AssemblyKind.None;
        throw new Exception($"Could not resolve {asm_name}");
    }
Esempio n. 7
0
 private void CreateAssemblyDefinition(String sAssemblyName, AssemblyKind akAssemblyKind)
 {
     assemblyName       = sAssemblyName;
     assemblyKind       = akAssemblyKind;
     assemblyDefinition = AssemblyFactory.DefineAssembly(sAssemblyName, TargetRuntime.NET_2_0, akAssemblyKind);
     mainModule         = assemblyDefinition.MainModule;
 }
Esempio n. 8
0
        public MetadataWriter(AssemblyDefinition asm, MetadataRoot root,
			AssemblyKind kind, TargetRuntime rt, BinaryWriter writer)
        {
            m_assembly = asm;
            m_root = root;
            m_runtime = rt;
            m_imgWriter = new ImageWriter (this, kind, writer);
            m_binaryWriter = m_imgWriter.GetTextWriter ();

            m_stringCache = new Hashtable ();
            m_stringWriter = new MemoryBinaryWriter (Encoding.UTF8);
            m_stringWriter.Write ((byte) 0);

            m_guidCache = new Hashtable ();
            m_guidWriter = new MemoryBinaryWriter ();

            m_usCache = new Hashtable ();
            m_usWriter = new MemoryBinaryWriter (Encoding.Unicode);
            m_usWriter.Write ((byte) 0);

            m_blobCache = new Hashtable ();
            m_blobWriter = new MemoryBinaryWriter ();
            m_blobWriter.Write ((byte) 0);

            m_tWriter = new MemoryBinaryWriter ();
            m_tableWriter = new MetadataTableWriter (this, m_tWriter);

            m_cilWriter = new MemoryBinaryWriter ();

            m_fieldDataWriter = new MemoryBinaryWriter ();
            m_resWriter = new MemoryBinaryWriter ();
        }
Esempio n. 9
0
        void Import(string ra, AssemblyKind kind)
        {
            if (!asm_map.Add(ra))
            {
                return;
            }
            ReaderParameters rp      = new ReaderParameters();
            bool             add_pdb = enable_debug && File.Exists(Path.ChangeExtension(ra, "pdb"));

            if (add_pdb)
            {
                rp.ReadSymbols = true;
                // Facades do not have symbols
                rp.ThrowIfSymbolsAreNotMatching = false;
                rp.SymbolReaderProvider         = new DefaultSymbolReaderProvider(false);
            }

            var resolver = new DefaultAssemblyResolver();

            root_search_paths.ForEach(resolver.AddSearchDirectory);
            resolver.AddSearchDirectory(bcl_facades_prefix);
            resolver.AddSearchDirectory(bcl_prefix);
            resolver.AddSearchDirectory(framework_prefix);
            rp.AssemblyResolver = resolver;

            rp.InMemory = true;
            var image = ModuleDefinition.ReadModule(ra, rp);

            file_list.Add(ra);
            //Debug ($"Processing {ra} debug {add_pdb}");

            var data = new AssemblyData()
            {
                name = image.Assembly.Name.Name, src_path = ra
            };

            assemblies.Add(data);

            if (add_pdb && (kind == AssemblyKind.User || kind == AssemblyKind.Framework))
            {
                file_list.Add(Path.ChangeExtension(ra, "pdb"));
                assemblies_with_dbg_info.Add(Path.ChangeExtension(ra, "pdb"));
            }

            foreach (var ar in image.AssemblyReferences)
            {
                // Resolve using root search paths first
                var resolved = image.AssemblyResolver.Resolve(ar, rp);

                var searchName = resolved?.MainModule.FileName ?? ar.Name;

                var resolve = Resolve(searchName, out kind);
                Import(resolve, kind);
            }
        }
Esempio n. 10
0
 public static AssemblyDefinition DefineAssembly(string assemblyName, string moduleName, TargetRuntime rt, AssemblyKind kind)
 {
     AssemblyNameDefinition asmName = new AssemblyNameDefinition ();
     asmName.Name = assemblyName;
     AssemblyDefinition asm = new AssemblyDefinition (asmName);
     asm.Runtime = rt;
     asm.Kind = kind;
     ModuleDefinition main = new ModuleDefinition (moduleName, asm, true);
     asm.Modules.Add (main);
     return asm;
 }
Esempio n. 11
0
        public ImageWriter(MetadataWriter writer, AssemblyKind kind, BinaryWriter bw)
        {
            m_mdWriter= writer;
            m_img = writer.GetMetadataRoot ().GetImage ();
            m_kind = kind;
            m_binaryWriter = bw;

            m_textWriter = new MemoryBinaryWriter ();
            m_textWriter.BaseStream.Position = 80;
            m_relocWriter = new MemoryBinaryWriter ();
        }
Esempio n. 12
0
        public ImageWriter(MetadataWriter writer, AssemblyKind kind, BinaryWriter bw)
        {
            m_mdWriter     = writer;
            m_img          = writer.GetMetadataRoot().GetImage();
            m_kind         = kind;
            m_binaryWriter = bw;

            m_textWriter = new MemoryBinaryWriter();
            m_textWriter.BaseStream.Position = 80;
            m_relocWriter = new MemoryBinaryWriter();
        }
Esempio n. 13
0
        static string GetTypeString(AssemblyKind kind)
        {
            switch (kind)
            {
            case AssemblyKind.Console:
                return(GettextCatalog.GetString("Console application"));

            case AssemblyKind.Dll:
                return(GettextCatalog.GetString("Library"));

            case AssemblyKind.Windows:
                return(GettextCatalog.GetString("Application"));
            }
            return(GettextCatalog.GetString("Unknown"));
        }
Esempio n. 14
0
    static void Import(string ra, AssemblyKind kind)
    {
        if (string.IsNullOrEmpty(ra))
        {
            return;
        }

        if (!asm_map.Add(ra))
        {
            return;
        }
        ReaderParameters rp      = new ReaderParameters();
        bool             add_pdb = enable_debug && File.Exists(Path.ChangeExtension(ra, "pdb"));

        if (add_pdb)
        {
            rp.ReadSymbols = true;
        }

        rp.InMemory = true;
        var image = ModuleDefinition.ReadModule(ra, rp);

        file_list.Add(ra);
        Debug($"Processing {ra} debug {add_pdb}");

        var data = new AssemblyData()
        {
            name = image.Assembly.Name.Name, src_path = ra
        };

        assemblies.Add(data);

        if (add_pdb && kind == AssemblyKind.User)
        {
            file_list.Add(Path.ChangeExtension(ra, "pdb"));
        }

        foreach (var ar in image.AssemblyReferences)
        {
            var resolve = Resolve(ar.Name, out kind);
            if (kind != AssemblyKind.None)
            {
                Import(resolve, kind);
            }
        }
    }
Esempio n. 15
0
    static string Resolve(string asm_name, out AssemblyKind kind)
    {
        kind = AssemblyKind.User;
        var asm = ResolveUser(asm_name);

        if (asm != null)
        {
            return(asm);
        }

        kind = AssemblyKind.Framework;
        asm  = ResolveFramework(asm_name);
        if (asm != null)
        {
            return(asm);
        }

        kind = AssemblyKind.Bcl;
        asm  = ResolveBcl(asm_name);
        if (asm == null)
        {
            asm = ResolveBclFacade(asm_name);
        }
        if (asm != null)
        {
            return(asm);
        }

        kind = AssemblyKind.User;
        var assembly = _assemblies.SingleOrDefault(_ => _.GetName().Name == asm_name);

        if (assembly != null)
        {
            return(new Uri(assembly.CodeBase).AbsolutePath);
        }

        //while (!System.Diagnostics.Debugger.IsAttached) System.Threading.Thread.Sleep(20);

        //var probeDirectories = GetDefaultProbeDirectories();


        kind = AssemblyKind.None;
        //throw new Exception ($"Could not resolve {asm_name}");
        return(string.Empty);
    }
Esempio n. 16
0
 public static OutputKind ToOutputKind(this AssemblyKind assemblyKind)
 {
     if (assemblyKind == AssemblyKind.ConsoleApplication)
     {
         return(OutputKind.ConsoleApplication);
     }
     else if (assemblyKind == AssemblyKind.WindowsApplication)
     {
         return(OutputKind.WindowsApplication);
     }
     else if (assemblyKind == AssemblyKind.DynamicallyLinkedLibrary)
     {
         return(OutputKind.DynamicallyLinkedLibrary);
     }
     else
     {
         throw new Exception("unexpected AssemblyKind");
     }
 }
 private bool IsEnhanced(AssemblyKind kind)
 {
     return(!IsRaw(kind));
 }
Esempio n. 18
0
 public CompilationOptions Copy(
     string mainTypeName = null,
     string globalTypeName = null,
     AssemblyKind? assemblyKind = null,
     bool? optimize = null,
     bool? checkOverflow = null,
     bool? isNetModule = null)
 {
     return new CompilationOptions(
         mainTypeName ?? this.MainTypeName,
         globalTypeName ?? this.ScriptClassName,
         assemblyKind ?? this.AssemblyKind,
         optimize ?? this.Optimize,
         checkOverflow ?? this.CheckOverflow,
         isNetModule ?? this.IsNetModule
     );
 }
 private bool IsRaw(AssemblyKind kind)
 {
     return(kind == AssemblyKind.RawNoDebugInformation || kind == AssemblyKind.RawWithDebugInformation);
 }
Esempio n. 20
0
 public AssemblyInfo(string precompiledDllPath)
 {
     assemblyKind            = AssemblyKind.PrecompiledAssembly;
     this.precompiledDllPath = precompiledDllPath;
 }
Esempio n. 21
0
 public AssemblyInfo(Assembly assembly, string asmdefPath)
 {
     assemblyKind    = AssemblyKind.Asmdef;
     this.assembly   = assembly;
     this.asmdefPath = asmdefPath;
 }
Esempio n. 22
0
 public CecilAssemblyBuilder(String sAssemblyName, AssemblyKind akAssemblyKind)
 {
     CreateAssemblyDefinition(sAssemblyName, akAssemblyKind);
 }
        public static AssemblyDefinition DefineAssembly(string assemblyName, string moduleName, TargetRuntime rt, AssemblyKind kind)
        {
            AssemblyNameDefinition asmName = new AssemblyNameDefinition();

            asmName.Name = assemblyName;
            AssemblyDefinition asm = new AssemblyDefinition(asmName);

            asm.Runtime = rt;
            asm.Kind    = kind;
            ModuleDefinition main = new ModuleDefinition(moduleName, asm, true);

            asm.Modules.Add(main);
            return(asm);
        }
Esempio n. 24
0
        // TODO:
        // platform:<string>
        // baseaddress:<address>
        // filealign:<n>

        public CompilationOptions(
            string mainTypeName = AnyMain,
            string scriptClassName = null,
            AssemblyKind assemblyKind = AssemblyKind.ConsoleApplication,
            bool optimize = false,
            bool checkOverflow = false,
            bool isNetModule = false)
        {
            if (mainTypeName != null && mainTypeName != AnyMain && !OptionsValidator.IsValidFullName(mainTypeName))
            {
                throw new ArgumentException("Invalid type name", "mainTypeName");
            }
            if (scriptClassName != null && !OptionsValidator.IsValidFullName(scriptClassName))
            {
                throw new ArgumentException("Invalid type name", "scriptClassName");
            }
            if (!assemblyKind.IsValid())
            {
                throw new ArgumentOutOfRangeException("assemblyKind");
            }

            ScriptClassName = scriptClassName ?? "Script";
            MainTypeName = mainTypeName ?? AnyMain;
            AssemblyKind = assemblyKind;
            Optimize = optimize;
            CheckOverflow = checkOverflow;
            IsNetModule = isNetModule;
        }
 public static AssemblyDefinition DefineAssembly(string name, AssemblyKind kind)
 {
     return(DefineAssembly(name, name, CurrentRuntime(), kind));
 }
		public static AssemblyDefinition DefineAssembly (string name, TargetRuntime rt, AssemblyKind kind)
		{
			return DefineAssembly (name, name, rt, kind);
		}
 public CecilAssemblyBuilder(String sAssemblyName, AssemblyKind akAssemblyKind)
 {
     CreateAssemblyDefinition(sAssemblyName, akAssemblyKind);
 }
		public static AssemblyDefinition DefineAssembly (string name, AssemblyKind kind)
		{
			return DefineAssembly (name, name, CurrentRuntime (), kind);
		}
Esempio n. 29
0
    private static void Import(string ra, AssemblyKind kind)
    {
        if (!asm_map.Add(ra))
        {
            return;
        }
        var rp      = new ReaderParameters();
        var add_pdb = enable_debug && File.Exists(Path.ChangeExtension(ra, "pdb"));

        if (add_pdb)
        {
            rp.ReadSymbols = true;
            // Facades do not have symbols
            rp.ThrowIfSymbolsAreNotMatching = false;
            rp.SymbolReaderProvider         = new DefaultSymbolReaderProvider(false);
        }

        var resolver = new DefaultAssemblyResolver();

        root_search_paths.ForEach(resolver.AddSearchDirectory);
        resolver.AddSearchDirectory(bcl_facades_prefix);
        resolver.AddSearchDirectory(bcl_prefix);
        resolver.AddSearchDirectory(core_prefix);
        rp.AssemblyResolver = resolver;

        rp.InMemory = true;
        var image = ModuleDefinition.ReadModule(ra, rp);

        file_list.Add(ra);
        Debug($"Processing {ra} debug {add_pdb}");

        var data = new AssemblyData {
            name = image.Assembly.Name.Name, src_path = ra
        };

        assemblies.Add(data);

        if (add_pdb && kind == AssemblyKind.User)
        {
            file_list.Add(Path.ChangeExtension(ra, "pdb"));
        }

        foreach (var ar in image.AssemblyReferences)
        {
            // Resolve using root search paths first
            AssemblyDefinition resolved = null;

            try
            {
                resolved = image.AssemblyResolver.Resolve(ar, rp);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message, ex.InnerException);
            }

            var searchName = resolved?.MainModule.FileName ?? ar.Name;

            var resolve = Resolve(searchName, out kind);
            Import(resolve, kind);
        }
    }
Esempio n. 30
0
 public EmitOptions()
 {
     this.AssemblyKind = AssemblyKind.ConsoleApplication;
 }
 private bool IsColoured(AssemblyKind kind)
 {
     return(kind == AssemblyKind.ColouredMinimalDebugInformation || kind == AssemblyKind.ColouredFullDebugInformation);
 }
 public static AssemblyDefinition DefineAssembly(string name, TargetRuntime rt, AssemblyKind kind)
 {
     return(DefineAssembly(name, name, rt, kind));
 }
 public EmitOptions()
 {
     this.AssemblyKind = AssemblyKind.ConsoleApplication;
 }
		static string GetTypeString (AssemblyKind kind)
		{
			switch (kind) {
			case AssemblyKind.Console:
				return GettextCatalog.GetString ("Console application");
			case AssemblyKind.Dll:
				return GettextCatalog.GetString ("Library");
			case AssemblyKind.Windows:
				return GettextCatalog.GetString ("Application");
			}
			return GettextCatalog.GetString ("Unknown");
		}