Пример #1
0
 Assembly _universe_AssemblyResolve(object sender, IKVM.Reflection.ResolveEventArgs args) {
     AssemblyName name = new AssemblyName(args.Name);
     AssemblyName previousMatch = null;
     int previousMatchLevel = 0;
     foreach (Assembly asm in _universe.GetAssemblies()) {
         if (Match(asm.GetName(), name, ref previousMatch, ref previousMatchLevel)) {
             return asm;
         }
     }
     foreach (string file in FindAssemblyPath(name.Name + ".dll")) {
         if (Match(AssemblyName.GetAssemblyName(file), name, ref previousMatch, ref previousMatchLevel)) {
             return LoadFile(file);
         }
     }
     if (args.RequestingAssembly != null) {
         string path = Path.Combine(Path.GetDirectoryName(args.RequestingAssembly.Location), name.Name + ".dll");
         if (File.Exists(path) && Match(AssemblyName.GetAssemblyName(path), name, ref previousMatch, ref previousMatchLevel)) {
             return LoadFile(path);
         }
     }
     string hintpath;
     if (_hintpaths.TryGetValue(name.FullName, out hintpath)) {
         string path = Path.Combine(hintpath, name.Name + ".dll");
         if (File.Exists(path) && Match(AssemblyName.GetAssemblyName(path), name, ref previousMatch, ref previousMatchLevel)) {
             return LoadFile(path);
         }
     }
     if (previousMatch != null) {
         if (previousMatchLevel == 2) {
             ConsoleOps.Warning("assuming assembly reference '{0}' matches '{1}', you may need to supply runtime policy", previousMatch.FullName, name.FullName);
             return LoadFile(new Uri(previousMatch.CodeBase).LocalPath);
         } else if (args.RequestingAssembly != null) {
             ConsoleOps.Error(true, "Assembly '{0}' uses '{1}' which has a higher version than referenced assembly '{2}'", args.RequestingAssembly.FullName, name.FullName, previousMatch.FullName);
         } else {
             ConsoleOps.Error(true, "Assembly '{0}' was requested which is a higher version than referenced assembly '{1}'", name.FullName, previousMatch.FullName);
         }
     } else {
         ConsoleOps.Error(true, "unable to find assembly '{0}' {1}", args.Name, args.RequestingAssembly != null ? string.Format("    (a dependency of '{0}')", args.RequestingAssembly.FullName) : string.Empty);
     }
     return null;
 }
Пример #2
0
 bool Match(AssemblyName assemblyDef, AssemblyName assemblyRef, ref AssemblyName bestMatch, ref int bestMatchLevel) {
     // Match levels:
     //   0 = no match
     //   1 = lower version match (i.e. not a suitable match, but used in error reporting: something was found but the version was too low)
     //   2 = higher version potential match (i.e. we can use this version, but if it is available the exact match will be preferred)
     //
     // If we find a perfect match, bestMatch is not changed but we return true to signal that the search can end right now.
     AssemblyComparisonResult result;
     _universe.CompareAssemblyIdentity(assemblyRef.FullName, false, assemblyDef.FullName, true, out result);
     switch (result) {
         case AssemblyComparisonResult.EquivalentFullMatch:
         case AssemblyComparisonResult.EquivalentPartialMatch:
         case AssemblyComparisonResult.EquivalentFXUnified:
         case AssemblyComparisonResult.EquivalentPartialFXUnified:
         case AssemblyComparisonResult.EquivalentPartialWeakNamed:
         case AssemblyComparisonResult.EquivalentWeakNamed:
             return true;
         case AssemblyComparisonResult.NonEquivalentPartialVersion:
         case AssemblyComparisonResult.NonEquivalentVersion:
             if (bestMatchLevel < 1) {
                 bestMatchLevel = 1;
                 bestMatch = assemblyDef;
             }
             return false;
         case AssemblyComparisonResult.EquivalentUnified:
         case AssemblyComparisonResult.EquivalentPartialUnified:
             if (bestMatchLevel < 2) {
                 bestMatchLevel = 2;
                 bestMatch = assemblyDef;
             }
             return false;
         case AssemblyComparisonResult.NonEquivalent:
         case AssemblyComparisonResult.Unknown:
             return false;
         default:
             throw new NotImplementedException();
     }
 }
Пример #3
0
		public static bool ReferenceMatchesDefinition(AssemblyName reference, AssemblyName definition)
		{
			// HACK use the real AssemblyName to implement the (broken) ReferenceMatchesDefinition method
			return System.Reflection.AssemblyName.ReferenceMatchesDefinition(new System.Reflection.AssemblyName(reference.FullName), new System.Reflection.AssemblyName(definition.FullName));
		}
Пример #4
0
		private void SetKeyPair (AssemblyName aname)
		{
			if (keyfile != null) {
				if (!File.Exists (keyfile)) {
					Report (1044, String.Format ("Couldn't open '{0}' key file.", keyfile));
				}

				using (FileStream fs = File.OpenRead (keyfile)) {
					byte[] data = new byte [fs.Length];
					try {
						fs.Read (data, 0, data.Length);

						if (delaysign == DelaySign.Yes) {
							SetPublicKey (aname, data);
						} else {
							CryptoConvert.FromCapiPrivateKeyBlob (data);
							aname.KeyPair = new StrongNameKeyPair (data);
						}
					}
					catch (CryptographicException) {
						if (delaysign != DelaySign.Yes) {
							if (data.Length == 16) {
								// error # is different for ECMA key
								Report (1019, "Could not strongname the assembly. " + 
									"ECMA key can only be used to delay-sign assemblies");
							} else {
								Report (1028, String.Format ("Key file {0}' is missing it's private key " +
									"or couldn't be decoded.", keyfile));
							}
						} else {
							Report (1044, String.Format ("Couldn't decode '{0}' key file.", keyfile));
						}
					}
					fs.Close ();
				}
			} else if (keyname != null) {
				// delay-sign doesn't apply to key containers
				aname.KeyPair = new StrongNameKeyPair (keyname);
			}
		}
Пример #5
0
        /// <summary>
        /// Generates the stub .exe file for starting the app
        /// </summary>
        /// <param name="config"></param>
        static void GenerateExe(Config config)
        {
            var u = new Universe();
            var aName = new AssemblyName(Path.GetFileNameWithoutExtension(new FileInfo(config.Output).Name));

            if (config.FileInfoVersion != null) {
                aName.Version = config.FileInfoVersion;
            }

            var ab = u.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save, Path.GetDirectoryName(config.Output));
            ab.DefineVersionInfoResource(config.FileInfoProduct, config.FileInfoProductVersion, config.FileInfoCompany, config.FileInfoCopyright, config.FileInfoTrademark);
            var mb = ab.DefineDynamicModule(config.Output, aName.Name + (aName.Name.EndsWith(".exe") ? string.Empty : ".exe"));
            var tb = mb.DefineType("PythonMain", IKVM.Reflection.TypeAttributes.Public);

            if (!string.IsNullOrEmpty(config.Win32Icon)) {
                ab.__DefineIconResource(File.ReadAllBytes(config.Win32Icon));
            }

            MethodBuilder assemblyResolveMethod = null;
            ILGenerator gen = null;

            if (config.Standalone) {
                ConsoleOps.Info("Generating stand alone executable");
                config.Embed = true;

                foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies()) {
                    var n = new AssemblyName(a.FullName);
                    if (!a.IsDynamic && a.EntryPoint == null && (n.Name.StartsWith("IronPython") || n.Name == "Microsoft.Dynamic" || n.Name == "Microsoft.Scripting")) {
                        ConsoleOps.Info("\tEmbedded {0} {1}", n.Name, n.Version);
                        var f = new FileStream(a.Location, FileMode.Open, FileAccess.Read);
                        mb.DefineManifestResource("Dll." + n.Name, f, IKVM.Reflection.ResourceAttributes.Public);
                    }
                }

                // we currently do no error checking on what is passed in to the assemblyresolve event handler
                assemblyResolveMethod = tb.DefineMethod("AssemblyResolve", MethodAttributes.Public | MethodAttributes.Static, u.Import(typeof(System.Reflection.Assembly)), new IKVM.Reflection.Type[] { u.Import(typeof(System.Object)), u.Import(typeof(System.ResolveEventArgs)) });
                gen = assemblyResolveMethod.GetILGenerator();
                var s = gen.DeclareLocal(u.Import(typeof(System.IO.Stream))); // resource stream

                gen.Emit(OpCodes.Ldnull);
                gen.Emit(OpCodes.Stloc, s);
                var d = gen.DeclareLocal(u.Import(typeof(byte[]))); // data buffer;
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("GetEntryAssembly"), Type.EmptyTypes);
                gen.Emit(OpCodes.Ldstr, "Dll.");
                gen.Emit(OpCodes.Ldarg_1);    // The event args
                gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.ResolveEventArgs)).GetMethod("get_Name"), Type.EmptyTypes);
                gen.Emit(OpCodes.Newobj, u.Import(typeof(System.Reflection.AssemblyName)).GetConstructor(new IKVM.Reflection.Type[] { u.Import(typeof(string)) }));
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.AssemblyName)).GetMethod("get_Name"), Type.EmptyTypes);
                gen.EmitCall(OpCodes.Call, u.Import(typeof(string)).GetMethod("Concat", new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(string)) }), Type.EmptyTypes);
                gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.Reflection.Assembly)).GetMethod("GetManifestResourceStream", new IKVM.Reflection.Type[] { u.Import(typeof(string)) }), Type.EmptyTypes);
                gen.Emit(OpCodes.Stloc, s);
                gen.Emit(OpCodes.Ldloc, s);
                gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.IO.Stream)).GetMethod("get_Length"), Type.EmptyTypes);
                gen.Emit(OpCodes.Newarr, u.Import(typeof(System.Byte)));
                gen.Emit(OpCodes.Stloc, d);
                gen.Emit(OpCodes.Ldloc, s);
                gen.Emit(OpCodes.Ldloc, d);
                gen.Emit(OpCodes.Ldc_I4_0);
                gen.Emit(OpCodes.Ldloc, s);
                gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.IO.Stream)).GetMethod("get_Length"), Type.EmptyTypes);
                gen.Emit(OpCodes.Conv_I4);
                gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.IO.Stream)).GetMethod("Read", new IKVM.Reflection.Type[] { u.Import(typeof(byte[])), u.Import(typeof(int)), u.Import(typeof(int)) }), Type.EmptyTypes);
                gen.Emit(OpCodes.Pop);
                gen.Emit(OpCodes.Ldloc, d);
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("Load", new IKVM.Reflection.Type[] { u.Import(typeof(byte[])) }), Type.EmptyTypes);
                gen.Emit(OpCodes.Ret);

                // generate a static constructor to assign the AssemblyResolve handler (otherwise it tries to use IronPython before it adds the handler)
                // the other way of handling this would be to move the call to InitializeModule into a separate method.
                var staticConstructor = tb.DefineConstructor(MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, Type.EmptyTypes);
                gen = staticConstructor.GetILGenerator();
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.AppDomain)).GetMethod("get_CurrentDomain"), Type.EmptyTypes);
                gen.Emit(OpCodes.Ldnull);
                gen.Emit(OpCodes.Ldftn, assemblyResolveMethod);
                gen.Emit(OpCodes.Newobj, u.Import(typeof(System.ResolveEventHandler)).GetConstructor(new IKVM.Reflection.Type[] { u.Import(typeof(object)), u.Import(typeof(System.IntPtr)) }));
                gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.AppDomain)).GetMethod("add_AssemblyResolve"), Type.EmptyTypes);
                gen.Emit(OpCodes.Ret);
            }

            var mainMethod = tb.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, u.Import(typeof(int)), Type.EmptyTypes);
            if (config.Target == PEFileKinds.WindowApplication && config.UseMta) {
                mainMethod.SetCustomAttribute(u.Import(typeof(System.MTAThreadAttribute)).GetConstructor(Type.EmptyTypes), new byte[0]);
            } else if (config.Target == PEFileKinds.WindowApplication) {
                mainMethod.SetCustomAttribute(u.Import(typeof(System.STAThreadAttribute)).GetConstructor(Type.EmptyTypes), new byte[0]);
            }

            gen = mainMethod.GetILGenerator();

            // variables for saving original working directory and return code of script
            var strVar = gen.DeclareLocal(u.Import(typeof(string)));
            var intVar = gen.DeclareLocal(u.Import(typeof(int)));

            Label tryStart = gen.BeginExceptionBlock();

            // get the ScriptCode assembly...
            if (config.Embed) {
                // put the generated DLL into the resources for the stub exe
                var mem = new MemoryStream();
                var rw = new ResourceWriter(mem);
                rw.AddResource("IPDll." + Path.GetFileNameWithoutExtension(config.Output) + ".dll", File.ReadAllBytes(config.Output + ".dll"));
                rw.Generate();
                mem.Position = 0;
                mb.DefineManifestResource("IPDll.resources", mem, ResourceAttributes.Public);
                File.Delete(config.Output + ".dll");

                // generate code to load the resource
                gen.Emit(OpCodes.Ldstr, "IPDll");
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("GetEntryAssembly"), Type.EmptyTypes);
                gen.Emit(OpCodes.Newobj, u.Import(typeof(System.Resources.ResourceManager)).GetConstructor(new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(System.Reflection.Assembly)) }));
                gen.Emit(OpCodes.Ldstr, "IPDll." + Path.GetFileNameWithoutExtension(config.Output) + ".dll");
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Resources.ResourceManager)).GetMethod("GetObject", new IKVM.Reflection.Type[] { u.Import(typeof(string)) }), Type.EmptyTypes);
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("Load", new IKVM.Reflection.Type[] { u.Import(typeof(byte[])) }), Type.EmptyTypes);
            } else {

                // save current working directory
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Environment)).GetMethod("get_CurrentDirectory"), Type.EmptyTypes);
                gen.Emit(OpCodes.Stloc, strVar);
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("GetEntryAssembly"), Type.EmptyTypes);
                gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.Reflection.Assembly)).GetMethod("get_Location"), Type.EmptyTypes);
                gen.Emit(OpCodes.Newobj, u.Import(typeof(System.IO.FileInfo)).GetConstructor(new IKVM.Reflection.Type[] { u.Import(typeof(string)) }));
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.IO.FileInfo)).GetMethod("get_Directory"), Type.EmptyTypes);
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.IO.DirectoryInfo)).GetMethod("get_FullName"), Type.EmptyTypes);
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Environment)).GetMethod("set_CurrentDirectory"), Type.EmptyTypes);
                gen.Emit(OpCodes.Ldstr, config.Output + ".dll");
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.IO.Path)).GetMethod("GetFullPath", new IKVM.Reflection.Type[] { u.Import(typeof(string)) }), Type.EmptyTypes);
                // result of GetFullPath stays on the stack during the restore of the
                // original working directory

                // restore original working directory
                gen.Emit(OpCodes.Ldloc, strVar);
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Environment)).GetMethod("set_CurrentDirectory"), Type.EmptyTypes);

                // for the LoadFile() call, the full path of the assembly is still is on the stack
                // as the result from the call to GetFullPath()
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("LoadFile", new IKVM.Reflection.Type[] { u.Import(typeof(string)) }), Type.EmptyTypes);
            }

            // emit module name
            gen.Emit(OpCodes.Ldstr, "__main__");  // main module name
            gen.Emit(OpCodes.Ldnull);             // no references
            gen.Emit(OpCodes.Ldc_I4_0);           // don't ignore environment variables for engine startup

            // call InitializeModule
            // (this will also run the script)
            // and put the return code on the stack
            gen.EmitCall(OpCodes.Call, u.Import(typeof(PythonOps)).GetMethod("InitializeModuleEx"), Type.EmptyTypes);
            gen.Emit(OpCodes.Stloc, intVar);

            gen.BeginCatchBlock(u.Import(typeof(Exception)));

            if (config.Target == PEFileKinds.ConsoleApplication) {
                gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.Exception)).GetMethod("get_Message", Type.EmptyTypes), Type.EmptyTypes);
                gen.Emit(OpCodes.Stloc, strVar);
                gen.Emit(OpCodes.Ldstr, config.ErrorMessageFormat);
                gen.Emit(OpCodes.Ldloc, strVar);
                gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Console)).GetMethod("WriteLine", new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(string)) }), Type.EmptyTypes);
            } else {
                // what do we want to do in the case of a Windows app, show a MessageBox?
            }

            gen.Emit(OpCodes.Ldc_I4, -1); // return code is -1 to show failure
            gen.Emit(OpCodes.Stloc, intVar);

            gen.EndExceptionBlock();

            gen.Emit(OpCodes.Ldloc, intVar);
            gen.Emit(OpCodes.Ret);

            tb.CreateType();
            ab.SetEntryPoint(mainMethod, config.Target);
            string fileName = aName.Name.EndsWith(".exe") ? aName.Name : aName.Name + ".exe";
            ab.Save(fileName, config.Platform, config.Machine);
        }
Пример #6
0
        /// <summary>
        /// Fully compiles the current model into a static-compiled serialization dll
        /// (the serialization dll still requires protobuf-net for support services).
        /// </summary>
        /// <remarks>A full compilation is restricted to accessing public types / members</remarks>
        /// <returns>An instance of the newly created compiled type-model</returns>
        public TypeModel Compile(CompilerOptions options)
        {
            if (options == null) throw new ArgumentNullException("options");
            string typeName = options.TypeName;
            string path = options.OutputPath;
            BuildAllSerializers();
            Freeze();
            bool save = !Helpers.IsNullOrEmpty(path);
            if (Helpers.IsNullOrEmpty(typeName))
            {
                if (save) throw new ArgumentNullException("typeName");
                typeName = Guid.NewGuid().ToString();
            }


            string assemblyName, moduleName;
            if(path == null)
            {
                assemblyName = typeName;
                moduleName = assemblyName + ".dll";
            }
            else
            {
                assemblyName = new System.IO.FileInfo(System.IO.Path.GetFileNameWithoutExtension(path)).Name;
                moduleName = assemblyName + System.IO.Path.GetExtension(path);
            }

#if FEAT_IKVM
            IKVM.Reflection.AssemblyName an = new IKVM.Reflection.AssemblyName();
            an.Name = assemblyName;
            AssemblyBuilder asm = universe.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save);
            if(!Helpers.IsNullOrEmpty(options.ImageRuntimeVersion) && options.MetaDataVersion != 0)
            {
                asm.__SetImageRuntimeVersion(options.ImageRuntimeVersion, options.MetaDataVersion);
            }
            ModuleBuilder module = asm.DefineDynamicModule(moduleName, path);
#else
            AssemblyName an = new AssemblyName();
            an.Name = assemblyName;
            AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly(an,
                (save ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run)
                );
            ModuleBuilder module = save ? asm.DefineDynamicModule(moduleName, path)
                                        : asm.DefineDynamicModule(moduleName);
#endif

            if (!Helpers.IsNullOrEmpty(options.TargetFrameworkName))
            {
                // get [TargetFramework] from mscorlib/equivalent and burn into the new assembly
                Type versionAttribType = null;
                try
                { // this is best-endeavours only
                    versionAttribType = GetType("System.Runtime.Versioning.TargetFrameworkAttribute", MapType(typeof(string)).Assembly);
                }
                catch { /* don't stress */ }
                if (versionAttribType != null)
                {
                    PropertyInfo[] props;
                    object[] propValues;
                    if (Helpers.IsNullOrEmpty(options.TargetFrameworkDisplayName))
                    {
                        props = new PropertyInfo[0];
                        propValues = new object[0];
                    }
                    else
                    {
                        props = new PropertyInfo[1] { versionAttribType.GetProperty("FrameworkDisplayName") };
                        propValues = new object[1] { options.TargetFrameworkDisplayName };
                    }
                    CustomAttributeBuilder builder = new CustomAttributeBuilder(
                        versionAttribType.GetConstructor(new Type[] { MapType(typeof(string)) }),
                        new object[] { options.TargetFrameworkName },
                        props,
                        propValues);
                    asm.SetCustomAttribute(builder);
                }
            }

            // copy assembly:InternalsVisibleTo
            Type internalsVisibleToAttribType = null;
#if !FX11
            try
            {
                internalsVisibleToAttribType = MapType(typeof(System.Runtime.CompilerServices.InternalsVisibleToAttribute));
            }
            catch { /* best endeavors only */ }
#endif   
            if (internalsVisibleToAttribType != null)
            {
                BasicList internalAssemblies = new BasicList(), consideredAssemblies = new BasicList();
                foreach (MetaType metaType in types)
                {
                    Assembly assembly = metaType.Type.Assembly;
                    if (consideredAssemblies.IndexOfReference(assembly) >= 0) continue;
                    consideredAssemblies.Add(assembly);

                    AttributeMap[] assemblyAttribsMap = AttributeMap.Create(this, assembly);
                    for (int i = 0; i < assemblyAttribsMap.Length; i++)
                    {
                        
                        if (assemblyAttribsMap[i].AttributeType != internalsVisibleToAttribType) continue;

                        object privelegedAssemblyObj;
                        assemblyAttribsMap[i].TryGet("AssemblyName", out privelegedAssemblyObj);
                        string privelegedAssemblyName = privelegedAssemblyObj as string;
                        if (privelegedAssemblyName == assemblyName || Helpers.IsNullOrEmpty(privelegedAssemblyName)) continue; // ignore

                        if (internalAssemblies.IndexOf(new StringFinder(privelegedAssemblyName)) >= 0) continue; // seen it before
                        internalAssemblies.Add(privelegedAssemblyName);


                        CustomAttributeBuilder builder = new CustomAttributeBuilder(
                            internalsVisibleToAttribType.GetConstructor(new Type[] { MapType(typeof(string)) }),
                            new object[] { privelegedAssemblyName });
                        asm.SetCustomAttribute(builder);
                    }
                }
            }
            Type baseType = MapType(typeof(TypeModel));
            TypeAttributes typeAttributes = (baseType.Attributes & ~TypeAttributes.Abstract) | TypeAttributes.Sealed;
            if(options.Accessibility == Accessibility.Internal)
            {
                typeAttributes &= ~TypeAttributes.Public;
            }
            
            TypeBuilder type = module.DefineType(typeName, typeAttributes, baseType);
            Compiler.CompilerContext ctx;
            
            int index = 0;
            bool hasInheritance = false;
            SerializerPair[] methodPairs = new SerializerPair[types.Count];
            foreach (MetaType metaType in types)
            {
                MethodBuilder writeMethod = type.DefineMethod("Write"
#if DEBUG
 + metaType.Type.Name
#endif
                    ,
                    MethodAttributes.Private | MethodAttributes.Static, CallingConventions.Standard,
                    MapType(typeof(void)), new Type[] { metaType.Type, MapType(typeof(ProtoWriter)) });

                MethodBuilder readMethod = type.DefineMethod("Read"
#if DEBUG
 + metaType.Type.Name
#endif              
                    ,
                    MethodAttributes.Private | MethodAttributes.Static, CallingConventions.Standard,
                    metaType.Type, new Type[] { metaType.Type, MapType(typeof(ProtoReader)) });

                SerializerPair pair = new SerializerPair(
                    GetKey(metaType.Type, true, false), GetKey(metaType.Type, true, true), metaType,
                    writeMethod, readMethod, writeMethod.GetILGenerator(), readMethod.GetILGenerator());
                methodPairs[index++] = pair;
                if (pair.MetaKey != pair.BaseKey) hasInheritance = true;
            }
            if (hasInheritance)
            {
                Array.Sort(methodPairs);
            }

            Compiler.CompilerContext.ILVersion ilVersion = Compiler.CompilerContext.ILVersion.Net2;
            if (options.MetaDataVersion == 0x10000)
            {
                ilVersion = Compiler.CompilerContext.ILVersion.Net1; // old-school!
            }
            for(index = 0; index < methodPairs.Length ; index++)
            {
                SerializerPair pair = methodPairs[index];
                ctx = new Compiler.CompilerContext(pair.SerializeBody, true, true, methodPairs, this, ilVersion, assemblyName);
                ctx.CheckAccessibility(pair.Deserialize.ReturnType);
                pair.Type.Serializer.EmitWrite(ctx, Compiler.Local.InputValue);
                ctx.Return();

                ctx = new Compiler.CompilerContext(pair.DeserializeBody, true, false, methodPairs, this, ilVersion, assemblyName);
                pair.Type.Serializer.EmitRead(ctx, Compiler.Local.InputValue);
                if (!pair.Type.Serializer.ReturnsValue)
                {
                    ctx.LoadValue(Compiler.Local.InputValue);
                }
                ctx.Return();
            }

            ILGenerator il = Override(type, "GetKeyImpl");
            int knownTypesCategory;
            FieldBuilder knownTypes;
            Type knownTypesLookupType;
            const int KnownTypes_Array = 1, KnownTypes_Dictionary = 2, KnownTypes_Hashtable = 3, KnownTypes_ArrayCutoff = 20;

            if (types.Count <= KnownTypes_ArrayCutoff)
            {
                knownTypesCategory = KnownTypes_Array;
                knownTypesLookupType = MapType(typeof(System.Type[]), true);
            }
            else
            {
#if NO_GENERICS
                knownTypesLookupType = null;
#else
                knownTypesLookupType = MapType(typeof(System.Collections.Generic.Dictionary<System.Type, int>), false);
#endif
                if (knownTypesLookupType == null)
                {
                    knownTypesLookupType = MapType(typeof(Hashtable), true);
                    knownTypesCategory = KnownTypes_Hashtable;
                }
                else
                {
                    knownTypesCategory = KnownTypes_Dictionary; 
                }
            }
            knownTypes = type.DefineField("knownTypes", knownTypesLookupType, FieldAttributes.Private | FieldAttributes.InitOnly | FieldAttributes.Static);
            
            switch(knownTypesCategory)
            {
                case KnownTypes_Array:
                    {
                        il.Emit(OpCodes.Ldsfld, knownTypes);
                        il.Emit(OpCodes.Ldarg_1);
                        // note that Array.IndexOf is not supported under CF
                        il.EmitCall(OpCodes.Callvirt, MapType(typeof(IList)).GetMethod(
                            "IndexOf", new Type[] { MapType(typeof(object)) }), null);
                        if (hasInheritance)
                        {
                            il.DeclareLocal(MapType(typeof(int))); // loc-0
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Stloc_0);

                            BasicList getKeyLabels = new BasicList();
                            int lastKey = -1;
                            for (int i = 0; i < methodPairs.Length; i++)
                            {
                                if (methodPairs[i].MetaKey == methodPairs[i].BaseKey) break;
                                if (lastKey == methodPairs[i].BaseKey)
                                {   // add the last label again
                                    getKeyLabels.Add(getKeyLabels[getKeyLabels.Count - 1]);
                                }
                                else
                                {   // add a new unique label
                                    getKeyLabels.Add(il.DefineLabel());
                                    lastKey = methodPairs[i].BaseKey;
                                }
                            }
                            Label[] subtypeLabels = new Label[getKeyLabels.Count];
                            getKeyLabels.CopyTo(subtypeLabels, 0);

                            il.Emit(OpCodes.Switch, subtypeLabels);
                            il.Emit(OpCodes.Ldloc_0); // not a sub-type; use the original value
                            il.Emit(OpCodes.Ret);

                            lastKey = -1;
                            // now output the different branches per sub-type (not derived type)
                            for (int i = subtypeLabels.Length - 1; i >= 0; i--)
                            {
                                if (lastKey != methodPairs[i].BaseKey)
                                {
                                    lastKey = methodPairs[i].BaseKey;
                                    // find the actual base-index for this base-key (i.e. the index of
                                    // the base-type)
                                    int keyIndex = -1;
                                    for (int j = subtypeLabels.Length; j < methodPairs.Length; j++)
                                    {
                                        if (methodPairs[j].BaseKey == lastKey && methodPairs[j].MetaKey == lastKey)
                                        {
                                            keyIndex = j;
                                            break;
                                        }
                                    }
                                    il.MarkLabel(subtypeLabels[i]);
                                    Compiler.CompilerContext.LoadValue(il, keyIndex);
                                    il.Emit(OpCodes.Ret);
                                }
                            }
                        }
                        else
                        {
                            il.Emit(OpCodes.Ret);
                        }
                    }
                    break;
                case KnownTypes_Dictionary:
                    {
                        LocalBuilder result = il.DeclareLocal(MapType(typeof(int)));
                        Label otherwise = il.DefineLabel();
                        il.Emit(OpCodes.Ldsfld, knownTypes);
                        il.Emit(OpCodes.Ldarg_1);
                        il.Emit(OpCodes.Ldloca_S, result);
                        il.EmitCall(OpCodes.Callvirt, knownTypesLookupType.GetMethod("TryGetValue", BindingFlags.Instance | BindingFlags.Public), null);
                        il.Emit(OpCodes.Brfalse_S, otherwise);
                        il.Emit(OpCodes.Ldloc_S, result);
                        il.Emit(OpCodes.Ret);
                        il.MarkLabel(otherwise);
                        il.Emit(OpCodes.Ldc_I4_M1);
                        il.Emit(OpCodes.Ret);
                    }
                    break;
                case KnownTypes_Hashtable:
                    {
                        Label otherwise = il.DefineLabel();
                        il.Emit(OpCodes.Ldsfld, knownTypes);
                        il.Emit(OpCodes.Ldarg_1);
                        il.EmitCall(OpCodes.Callvirt, knownTypesLookupType.GetProperty("Item").GetGetMethod(), null);
                        il.Emit(OpCodes.Dup);
                        il.Emit(OpCodes.Brfalse_S, otherwise);
#if FX11
                        il.Emit(OpCodes.Unbox, MapType(typeof(int)));
                        il.Emit(OpCodes.Ldobj, MapType(typeof(int)));
#else
                        if (ilVersion == Compiler.CompilerContext.ILVersion.Net1)
                        {
                            il.Emit(OpCodes.Unbox, MapType(typeof(int)));
                            il.Emit(OpCodes.Ldobj, MapType(typeof(int)));
                        }
                        else
                        {
                            il.Emit(OpCodes.Unbox_Any, MapType(typeof(int)));
                        }
#endif
                        il.Emit(OpCodes.Ret);
                        il.MarkLabel(otherwise);
                        il.Emit(OpCodes.Pop);
                        il.Emit(OpCodes.Ldc_I4_M1);
                        il.Emit(OpCodes.Ret);
                    }
                    break;
                default:
                    throw new InvalidOperationException();
            }
            
            il = Override(type, "Serialize");
            ctx = new Compiler.CompilerContext(il, false, true, methodPairs, this, ilVersion, assemblyName);
            // arg0 = this, arg1 = key, arg2=obj, arg3=dest
            Label[] jumpTable = new Label[types.Count];
            for (int i = 0; i < jumpTable.Length; i++) {
                jumpTable[i] = il.DefineLabel();
            }
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Switch, jumpTable);
            ctx.Return();
            for (int i = 0; i < jumpTable.Length; i++)
            {
                SerializerPair pair = methodPairs[i];
                il.MarkLabel(jumpTable[i]);
                il.Emit(OpCodes.Ldarg_2);
                ctx.CastFromObject(pair.Type.Type);
                il.Emit(OpCodes.Ldarg_3);
                il.EmitCall(OpCodes.Call, pair.Serialize, null);
                ctx.Return();
            }

            il = Override(type, "Deserialize");
            ctx = new Compiler.CompilerContext(il, false, false, methodPairs, this, ilVersion, assemblyName);
            // arg0 = this, arg1 = key, arg2=obj, arg3=source
            for (int i = 0; i < jumpTable.Length; i++)
            {
                jumpTable[i] = il.DefineLabel();
            }
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Switch, jumpTable);
            ctx.LoadNullRef();
            ctx.Return();
            for (int i = 0; i < jumpTable.Length; i++)
            {
                SerializerPair pair = methodPairs[i];
                il.MarkLabel(jumpTable[i]);
                Type keyType = pair.Type.Type;
                if (keyType.IsValueType)
                {
                    il.Emit(OpCodes.Ldarg_2);
                    il.Emit(OpCodes.Ldarg_3);
                    il.EmitCall(OpCodes.Call, EmitBoxedSerializer(type, i, keyType, methodPairs, this, ilVersion, assemblyName), null);
                    ctx.Return();
                }
                else
                {
                    il.Emit(OpCodes.Ldarg_2);
                    ctx.CastFromObject(keyType);
                    il.Emit(OpCodes.Ldarg_3);
                    il.EmitCall(OpCodes.Call, pair.Deserialize, null);
                    ctx.Return();
                }
            }
            type.DefineDefaultConstructor(MethodAttributes.Public);
            il = type.DefineTypeInitializer().GetILGenerator();
            switch (knownTypesCategory)
            {
                case KnownTypes_Array:
                    {
                        Compiler.CompilerContext.LoadValue(il, types.Count);
                        il.Emit(OpCodes.Newarr, ctx.MapType(typeof(System.Type)));
                        index = 0;
                        foreach (SerializerPair pair in methodPairs)
                        {
                            il.Emit(OpCodes.Dup);
                            Compiler.CompilerContext.LoadValue(il, index);
                            il.Emit(OpCodes.Ldtoken, pair.Type.Type);
                            il.EmitCall(OpCodes.Call, ctx.MapType(typeof(System.Type)).GetMethod("GetTypeFromHandle"), null);
                            il.Emit(OpCodes.Stelem_Ref);
                            index++;
                        }
                        il.Emit(OpCodes.Stsfld, knownTypes);
                        il.Emit(OpCodes.Ret);
                    }
                    break;
                case KnownTypes_Dictionary:
                    {
                        Compiler.CompilerContext.LoadValue(il, types.Count);
                        LocalBuilder loc = il.DeclareLocal(knownTypesLookupType);
                        il.Emit(OpCodes.Newobj, knownTypesLookupType.GetConstructor(new Type[] { MapType(typeof(int)) }));
                        il.Emit(OpCodes.Stsfld, knownTypes);
                        int typeIndex = 0;
                        foreach (SerializerPair pair in methodPairs)
                        {
                            il.Emit(OpCodes.Ldsfld, knownTypes);
                            il.Emit(OpCodes.Ldtoken, pair.Type.Type);
                            il.EmitCall(OpCodes.Call, ctx.MapType(typeof(System.Type)).GetMethod("GetTypeFromHandle"), null);
                            int keyIndex = typeIndex++, lastKey = pair.BaseKey;
                            if (lastKey != pair.MetaKey) // not a base-type; need to give the index of the base-type
                            {
                                keyIndex = -1; // assume epic fail
                                for (int j = 0; j < methodPairs.Length; j++)
                                {
                                    if (methodPairs[j].BaseKey == lastKey && methodPairs[j].MetaKey == lastKey)
                                    {
                                        keyIndex = j;
                                        break;
                                    }
                                }
                            }
                            Compiler.CompilerContext.LoadValue(il, keyIndex);
                            il.EmitCall(OpCodes.Callvirt, knownTypesLookupType.GetMethod("Add", new Type[] { MapType(typeof(System.Type)), MapType(typeof(int)) }), null);
                        }
                        il.Emit(OpCodes.Ret);
                    }
                    break;
                case KnownTypes_Hashtable:
                    {
                        Compiler.CompilerContext.LoadValue(il, types.Count);
                        il.Emit(OpCodes.Newobj, knownTypesLookupType.GetConstructor(new Type[] { MapType(typeof(int)) }));
                        il.Emit(OpCodes.Stsfld, knownTypes);
                        int typeIndex = 0;
                        foreach (SerializerPair pair in methodPairs)
                        {
                            il.Emit(OpCodes.Ldsfld, knownTypes);
                            il.Emit(OpCodes.Ldtoken, pair.Type.Type);
                            il.EmitCall(OpCodes.Call, ctx.MapType(typeof(System.Type)).GetMethod("GetTypeFromHandle"), null);
                            int keyIndex = typeIndex++, lastKey = pair.BaseKey;
                            if (lastKey != pair.MetaKey) // not a base-type; need to give the index of the base-type
                            {
                                keyIndex = -1; // assume epic fail
                                for (int j = 0; j < methodPairs.Length; j++)
                                {
                                    if (methodPairs[j].BaseKey == lastKey && methodPairs[j].MetaKey == lastKey)
                                    {
                                        keyIndex = j;
                                        break;
                                    }
                                }
                            }
                            Compiler.CompilerContext.LoadValue(il, keyIndex);
                            il.Emit(OpCodes.Box, MapType(typeof(int)));
                            il.EmitCall(OpCodes.Callvirt, knownTypesLookupType.GetMethod("Add", new Type[] { MapType(typeof(object)), MapType(typeof(object)) }), null);
                        }
                        il.Emit(OpCodes.Ret);
                    }
                    break;
                default:
                    throw new InvalidOperationException();
            }
            


            Type finalType = type.CreateType();
            if(!Helpers.IsNullOrEmpty(path))
            {
                asm.Save(path);
                Helpers.DebugWriteLine("Wrote dll:" + path);
            }
#if FEAT_IKVM
            return null;
#else
            return (TypeModel)Activator.CreateInstance(finalType);
#endif
        }
Пример #7
0
		private static bool CheckReferencedAssemblies (AssemblyName an)
		{
			try {
				Assembly a = ReflectionOnlyLoadFrom (an.CodeBase);
				AssemblyName corlib = GetCorlibName ();

				foreach (AssemblyName ref_an in a.GetReferencedAssemblies ()) {
					if (ref_an.Name == corlib.Name) // Just do a string compare so we can install on diff versions
						continue;
					byte [] pt = ref_an.GetPublicKeyToken ();
					if (pt == null || pt.Length == 0) {
						WriteLine ("Assembly " + ref_an.Name + " is not strong named.");
						return false;
					}
				}
			} catch (Exception e) {
				WriteLine (e.ToString ()); // This should be removed pre beta3
				return false;
			}

			return true;
		}
Пример #8
0
		private AssemblyBuilder DefineDynamicAssemblyImpl(AssemblyName name, AssemblyBuilderAccess access, string dir, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions)
		{
			AssemblyBuilder asm = new AssemblyBuilder(this, name, dir, requiredPermissions, optionalPermissions, refusedPermissions);
			assemblies.Add(asm);
			return asm;
 		}
Пример #9
0
 public AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, string dir, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions)
 {
     return(DefineDynamicAssemblyImpl(name, access, dir, requiredPermissions, optionalPermissions, refusedPermissions));
 }
Пример #10
0
		private static bool ParsePublicKey(string str, out string publicKeyToken)
		{
			if (str == null)
			{
				publicKeyToken = null;
				return false;
			}
			// HACK use AssemblyName to convert PublicKey to PublicKeyToken
			byte[] token = new AssemblyName("Foo, PublicKey=" + str).GetPublicKeyToken();
			StringBuilder sb = new StringBuilder(token.Length * 2);
			for (int i = 0; i < token.Length; i++)
			{
				sb.AppendFormat("{0:x2}", token[i]);
			}
			publicKeyToken = sb.ToString();
			return true;
		}
Пример #11
0
 public AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, string dir)
 {
     return(DefineDynamicAssemblyImpl(name, access, dir, null, null, null));
 }
Пример #12
0
		private AssemblyName ReadCustomAttributesFromTemplateFile (string templateFile, AssemblyName aname)
		{
			// LAMESPEC: according to MSDN, the template assembly must have a
			// strong name but this is not enforced
			var asm = universe.LoadFile (templateFile);

			// Create missing assemblies, we don't want to load them!
			// Code taken from ikdasm
			var names = new HashSet<string> ();
			AssemblyName[] assembly_refs = asm.ManifestModule.__GetReferencedAssemblies ();

			var resolved_assemblies = new Assembly [assembly_refs.Length];
			for (int i = 0; i < resolved_assemblies.Length; i++) {
				string name = assembly_refs [i].Name;

				while (names.Contains (name)) {
					name = name + "_" + i;
				}
				names.Add (name);
				resolved_assemblies [i] = universe.CreateMissingAssembly (assembly_refs [i].FullName);
			}
			asm.ManifestModule.__ResolveReferencedAssemblies (resolved_assemblies);

			foreach (var attr_data in asm.__GetCustomAttributes (null, false)) {
				string asm_name = attr_data.AttributeType.Assembly.GetName ().Name;
				if (asm_name != "mscorlib")
					continue;

				switch (attr_data.AttributeType.FullName) {
					case "System.Reflection.AssemblyKeyFileAttribute": {
						if (keyfile != null)
							// ignore if specified on command line
							continue;

						// / AssemblyKeyFileAttribute .ctor(string keyFile)
						string key_file_value = (string) attr_data.ConstructorArguments [0].Value;

						if (!String.IsNullOrEmpty (key_file_value))
							keyfile = Path.Combine (Path.GetDirectoryName (templateFile), key_file_value);
					}
					break;

					case "System.Reflection.AssemblyDelaySignAttribute": {
						if (delaysign != DelaySign.NotSet)
							// ignore if specified on command line
							continue;

						// AssemblyDelaySignAttribute .ctor(bool delaySign)
						bool delay_sign_value = (bool) attr_data.ConstructorArguments [0].Value;
						delaysign = delay_sign_value ? DelaySign.Yes : DelaySign.No;
					}
					break;

					case "System.Reflection.AssemblyKeyNameAttribute": {
						if (keyname != null)
							// ignore if specified on command line
							continue;

						// AssemblyKeyNameAttribute .ctor(string keyName)
						string key_name_value = (string) attr_data.ConstructorArguments [0].Value;

						// ignore null or zero-length keyname
						if (!String.IsNullOrEmpty (key_name_value))
							keyname = key_name_value;
					}
					break;

					case "System.Reflection.AssemblyTitleAttribute": {
						if (title != null)
							// ignore if specified on command line
							continue;

						// AssemblyTitleAttribute .ctor(string title)
						string title_value = (string) attr_data.ConstructorArguments [0].Value;

						if (!String.IsNullOrEmpty (title_value))
							title = title_value;
					}
					break;

					case "System.Reflection.AssemblyDescriptionAttribute": {
						if (description != null)
							// ignore if specified on command line
							continue;

						// AssemblyDescriptionAttribute .ctor(string description)
						string description_value = (string) attr_data.ConstructorArguments [0].Value;

						if (!String.IsNullOrEmpty (description_value))
							description = description_value;
					}
					break;

					case "System.Reflection.AssemblyProductAttribute": {
						if (product != null)
							// ignore if specified on command line
							continue;

						// AssemblyProductAttribute .ctor(string product)
						string product_value = (string) attr_data.ConstructorArguments [0].Value;

						if (!String.IsNullOrEmpty (product_value))
							product = product_value;
					}
					break;

					case "System.Reflection.AssemblyCompanyAttribute": {
						if (company != null)
							// ignore if specified on command line
							continue;

						// AssemblyCompanyAttribute .ctor(string company)
						string company_value = (string) attr_data.ConstructorArguments [0].Value;

						if (!String.IsNullOrEmpty (company_value))
							company = company_value;

					}
					break;

					case "System.Reflection.AssemblyCopyrightAttribute": {
						if (copyright != null)
							// ignore if specified on command line
							continue;

						// AssemblyCopyrightAttribute .ctor(string copyright)
						string copyright_value = (string) attr_data.ConstructorArguments [0].Value;

						if (!String.IsNullOrEmpty (copyright_value))
							copyright = copyright_value;
					}
					break;

					case "System.Reflection.AssemblyTrademarkAttribute": {
						if (trademark != null)
							// ignore if specified on command line
							continue;

						// AssemblyTrademarkAttribute .ctor(string trademark)
						string trademark_value = (string) attr_data.ConstructorArguments [0].Value;

						if (!String.IsNullOrEmpty (trademark_value))
							trademark = trademark_value;
					}
					break;
				}
			}

			var asm_name_for_template_file = asm.GetName ();
			aname.Version = asm_name_for_template_file.Version;
			aname.HashAlgorithm = asm_name_for_template_file.HashAlgorithm;

			return aname;
		}
Пример #13
0
		private void DoIt () {
			AssemblyName aname = new AssemblyName ();
			aname.Name = Path.GetFileNameWithoutExtension (outFile);
			if (culture != null)
				aname.CultureInfo = new CultureInfo (culture);

			string fileName = Path.GetFileName (outFile);

			AssemblyBuilder ab;
			
			/*
			 * Emit Manifest
			 * */

			if (isTemplateFile)
				aname = ReadCustomAttributesFromTemplateFile (templateFile, aname);

			if (!String.IsNullOrEmpty (title))
				AddCattr (typeof (System.Reflection.AssemblyTitleAttribute), title);
			if (!String.IsNullOrEmpty (description))
				AddCattr (typeof (System.Reflection.AssemblyDescriptionAttribute), description);
			if (!String.IsNullOrEmpty (company))
				AddCattr (typeof (System.Reflection.AssemblyCompanyAttribute), company);
			if (!String.IsNullOrEmpty (product))
				AddCattr (typeof (System.Reflection.AssemblyProductAttribute), product);
			if (!String.IsNullOrEmpty (copyright))
				AddCattr (typeof (System.Reflection.AssemblyCopyrightAttribute), copyright);
			if (!String.IsNullOrEmpty (trademark))
				AddCattr (typeof (System.Reflection.AssemblyTrademarkAttribute), trademark);

			SetKeyPair (aname);

			if (fileName != outFile)
				ab = universe.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (outFile));
			else
				ab = universe.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Save);

			foreach (CustomAttributeBuilder cb in cattrs)
				ab.SetCustomAttribute (cb);

			/*
			 * Emit modules
			 */

			foreach (ModuleInfo mod in inputFiles) {
				if (mod.target != null) {
					File.Copy (mod.fileName, mod.target, true);
					mod.fileName = mod.target;
				}

				bool isAssembly = false;
				try {
					AssemblyName.GetAssemblyName (mod.fileName);
					isAssembly = true;
				}
				catch (Exception) {
				}

				if (isAssembly)
					ReportWarning (1020, "Ignoring included assembly '" + mod.fileName + "'");
				else
				ab.__AddModule (universe.OpenRawModule(mod.fileName));
			}

			/*
			 * Set entry point
			 */

			if (entryPoint != null) {
				string mainClass = entryPoint.Substring (0, entryPoint.LastIndexOf ('.'));
				string mainMethod = entryPoint.Substring (entryPoint.LastIndexOf ('.') + 1);

				MethodInfo mainMethodInfo = null;

				try {
					IKVM.Reflection.Type mainType = ab.GetType (mainClass);
					if (mainType != null)
						mainMethodInfo = mainType.GetMethod (mainMethod);
				}
				catch (Exception ex) {
					Console.WriteLine (ex);
				}
				if (mainMethodInfo != null)
					ab.SetEntryPoint (mainMethodInfo);
				else
					Report (1037, "Unable to find the entry point method '" + entryPoint + "'");
			}

			/*
			 * Emit resources
			 */

			ab.DefineVersionInfoResource ();

			if (win32IconFile != null) {
				try {
					ab.__DefineIconResource (File.ReadAllBytes (win32IconFile));
				}
				catch (Exception ex) {
					Report (1031, "Error reading icon '" + win32IconFile + "' --" + ex);
				}
			}

			if (win32ResFile != null) {
				try {
					ab.DefineUnmanagedResource (win32ResFile);
				}
				catch (Exception ex) {
					Report (1019, "Metadata failure creating assembly -- " + ex);
				}
			}

			ModuleBuilder mainModule = null;

			foreach (ResourceInfo res in resources) {
				if (res.name == null)
					res.name = Path.GetFileName (res.fileName);

				foreach (ResourceInfo res2 in resources)
					if ((res != res2) && (res.name == res2.name))
						Report (1046, String.Format ("Resource identifier '{0}' has already been used in this assembly", res.name));

				if (res.isEmbedded) {
					if (mainModule == null) {
						mainModule = ab.DefineDynamicModule (fileName, fileName, false);
					}

					Stream stream = new MemoryStream (File.ReadAllBytes (res.fileName));

					mainModule.DefineManifestResource (res.name, stream, res.isPrivate ? ResourceAttributes.Private : ResourceAttributes.Public);
				}
				else {
					if (res.target != null) {
						File.Copy (res.fileName, res.target, true);
						res.fileName = res.target;
					}
					
					// AddResourceFile must receive a file name and not a path.
					// Drop directory and give warning if we have a path.
					var resourceFileName = Path.GetFileName(res.fileName);
					if (Path.GetDirectoryName (res.fileName) != null || Path.IsPathRooted(res.fileName)) {
						ReportWarning (99999, 
							String.Format ("Path '{0}' in the resource name is not supported. Using just file name '{1}'",
								res.fileName,
								resourceFileName));
					}

					ab.AddResourceFile (res.name, resourceFileName,
							res.isPrivate ? ResourceAttributes.Private : ResourceAttributes.Public);
				}
			}

			PortableExecutableKinds pekind = PortableExecutableKinds.ILOnly;
			ImageFileMachine machine;

			switch (platform) {
			case Platform.X86:
				pekind |= PortableExecutableKinds.Required32Bit;
				machine = ImageFileMachine.I386;
				break;
			case Platform.X64:
				pekind |= PortableExecutableKinds.PE32Plus;
				machine = ImageFileMachine.AMD64;
				break;
			case Platform.IA64:
				machine = ImageFileMachine.IA64;
				break;
			case Platform.AnyCPU32Preferred:
				pekind |= PortableExecutableKinds.Preferred32Bit;
				machine = ImageFileMachine.I386;
				break;
			case Platform.Arm:
				machine = ImageFileMachine.ARM;
				break;
			case Platform.AnyCPU:
			default:
				machine = ImageFileMachine.I386;
				break;
			}

			try {
				ab.Save (fileName, pekind, machine);
			}
			catch (Exception ex) {
				Report (1019, "Metadata failure creating assembly -- " + ex);
			}
		}
Пример #14
0
		public AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, string dir)
		{
			return DefineDynamicAssemblyImpl(name, access, dir, null, null, null);
		}
Пример #15
0
		// modified copy from sn
		private static VerificationResult VerifyStrongName (AssemblyName an, string assemblyFile)
		{
			byte [] publicKey = StrongNameManager.GetMappedPublicKey (an.GetPublicKeyToken ());
			if ((publicKey == null) || (publicKey.Length < 12)) {
				// no mapping
				publicKey = an.GetPublicKey ();
				if ((publicKey == null) || (publicKey.Length < 12))
					return VerificationResult.WeakNamed;
			}

			// Note: MustVerify is based on the original token (by design). Public key
			// remapping won't affect if the assembly is verified or not.
			if (StrongNameManager.MustVerify (new System.Reflection.AssemblyName (an.FullName))) {
				RSA rsa = CryptoConvert.FromCapiPublicKeyBlob (publicKey, 12);
				StrongName sn = new StrongName (rsa);
				if (sn.Verify (assemblyFile)) {
					return VerificationResult.StrongNamed;
				} else {
					return VerificationResult.DelaySigned;
				}
			} else {
				return VerificationResult.Skipped;
			}
		}
Пример #16
0
		public AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, string dir, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions)
		{
			return DefineDynamicAssemblyImpl(name, access, dir, requiredPermissions, optionalPermissions, refusedPermissions);
		}
Пример #17
0
 public AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access)
 {
     return(new AssemblyBuilder(this, name, null, null));
 }
Пример #18
0
		internal void RenameAssembly(Assembly assembly, AssemblyName oldName)
		{
			List<string> remove = new List<string>();
			foreach (KeyValuePair<string, Assembly> kv in assembliesByName)
			{
				if (kv.Value == assembly)
				{
					remove.Add(kv.Key);
				}
			}
			foreach (string key in remove)
			{
				assembliesByName.Remove(key);
			}
		}
Пример #19
0
 public AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, IEnumerable <CustomAttributeBuilder> assemblyAttributes)
 {
     return(new AssemblyBuilder(this, name, null, assemblyAttributes));
 }
Пример #20
0
		public static bool ReferenceMatchesDefinition(AssemblyName reference, AssemblyName definition)
		{
			return System.Reflection.AssemblyName.ReferenceMatchesDefinition(reference.name, definition.name);
		}
Пример #21
0
 public AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, string dir)
 {
     return(new AssemblyBuilder(this, name, dir, null));
 }
		internal ModuleBuilder CreateModuleBuilder()
		{
			AssemblyName name = new AssemblyName();
			name.Name = assemblyName;
			if (options.keyPair != null)
			{
				name.KeyPair = options.keyPair;
			}
			else if (options.publicKey != null)
			{
				name.SetPublicKey(options.publicKey);
			}
			name.Version = options.version;
			assemblyBuilder = 
				StaticCompiler.Universe
					.DefineDynamicAssembly(name, AssemblyBuilderAccess.ReflectionOnly, assemblyDir);
			ModuleBuilder moduleBuilder;
			moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName, assemblyFile, this.EmitDebugInfo);
			if(this.EmitStackTraceInfo)
			{
				AttributeHelper.SetSourceFile(moduleBuilder, null);
			}
			if(this.EmitDebugInfo || this.EmitStackTraceInfo)
			{
				CustomAttributeBuilder debugAttr = new CustomAttributeBuilder(JVM.Import(typeof(DebuggableAttribute)).GetConstructor(new Type[] { Types.Boolean, Types.Boolean }), new object[] { true, this.EmitDebugInfo });
				assemblyBuilder.SetCustomAttribute(debugAttr);
			}
			AttributeHelper.SetRuntimeCompatibilityAttribute(assemblyBuilder);
			if(options.baseAddress != 0)
			{
				moduleBuilder.__ImageBase = options.baseAddress;
			}
			if(options.fileAlignment != 0)
			{
				moduleBuilder.__FileAlignment = options.fileAlignment;
			}
			if(options.highentropyva)
			{
				moduleBuilder.__DllCharacteristics |= DllCharacteristics.HighEntropyVA;
			}
			return moduleBuilder;
		}
        /// <summary>
        /// Fully compiles the current model into a static-compiled serialization dll
        /// (the serialization dll still requires protobuf-net for support services).
        /// </summary>
        /// <remarks>A full compilation is restricted to accessing public types / members</remarks>
        /// <returns>An instance of the newly created compiled type-model</returns>
        public TypeModel Compile(CompilerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            string typeName = options.TypeName;
            string path     = options.OutputPath;

            BuildAllSerializers();
            CompileInPlace();
            Freeze();
            bool save = !Helpers.IsNullOrEmpty(path);

            if (Helpers.IsNullOrEmpty(typeName))
            {
                if (save)
                {
                    throw new ArgumentNullException("typeName");
                }
                typeName = Guid.NewGuid().ToString();
            }


            string assemblyName, moduleName;

            if (path == null)
            {
                assemblyName = typeName;
                moduleName   = assemblyName + ".dll";
            }
            else
            {
                assemblyName = new System.IO.FileInfo(System.IO.Path.GetFileNameWithoutExtension(path)).Name;
                moduleName   = assemblyName + System.IO.Path.GetExtension(path);
            }

#if FEAT_IKVM
            IKVM.Reflection.AssemblyName an = new IKVM.Reflection.AssemblyName();
            an.Name = assemblyName;
            AssemblyBuilder asm = universe.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save);
            if (!Helpers.IsNullOrEmpty(options.KeyFile))
            {
                asm.__SetAssemblyKeyPair(new StrongNameKeyPair(File.OpenRead(options.KeyFile)));
            }
            else if (!Helpers.IsNullOrEmpty(options.KeyContainer))
            {
                asm.__SetAssemblyKeyPair(new StrongNameKeyPair(options.KeyContainer));
            }
            else if (!Helpers.IsNullOrEmpty(options.PublicKey))
            {
                asm.__SetAssemblyPublicKey(FromHex(options.PublicKey));
            }
            if (!Helpers.IsNullOrEmpty(options.ImageRuntimeVersion) && options.MetaDataVersion != 0)
            {
                asm.__SetImageRuntimeVersion(options.ImageRuntimeVersion, options.MetaDataVersion);
            }
            ModuleBuilder module = asm.DefineDynamicModule(moduleName, path);
#else
            AssemblyName an = new AssemblyName();
            an.Name = assemblyName;
            AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly(an,
                                                                                (save ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run)
                                                                                );
            ModuleBuilder module = save ? asm.DefineDynamicModule(moduleName, path)
                                        : asm.DefineDynamicModule(moduleName);
#endif

            WriteAssemblyAttributes(options, assemblyName, asm);

            TypeBuilder type = WriteBasicTypeModel(options, typeName, module);

            int              index;
            bool             hasInheritance;
            SerializerPair[] methodPairs;
            Compiler.CompilerContext.ILVersion ilVersion;
            WriteSerializers(options, assemblyName, type, false, null, out index, out hasInheritance, out methodPairs, out ilVersion);

            int              basicIndex          = index;
            bool             basicHasInheritance = hasInheritance;
            SerializerPair[] basicMethodPairs    = methodPairs;
            var              basicIlVersion      = ilVersion;

            WriteSerializers(options, assemblyName, type, true, basicMethodPairs, out index, out hasInheritance, out methodPairs, out ilVersion);

            ILGenerator  il;
            int          knownTypesCategory;
            FieldBuilder knownTypes;
            Type         knownTypesLookupType;
            WriteGetKeyImpl(type, basicHasInheritance, basicMethodPairs, basicIlVersion, assemblyName, out il, out knownTypesCategory, out knownTypes, out knownTypesLookupType);

            // trivial flags
            il = Override(type, "SerializeDateTimeKind").GetILGenerator();
            il.Emit(IncludeDateTimeKind ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
            il.Emit(OpCodes.Ret);
            // end: trivial flags

            Compiler.CompilerContext ctx = WriteSerializeDeserialize(assemblyName, type, basicMethodPairs, methodPairs, basicIlVersion, ref il);

            WriteConstructors(type, ref basicIndex, basicMethodPairs, ref il, knownTypesCategory, knownTypes, knownTypesLookupType, ctx);


            Type finalType = type.CreateType();
            if (!Helpers.IsNullOrEmpty(path))
            {
                asm.Save(path);
                Helpers.DebugWriteLine("Wrote dll:" + path);
                _compiledToPath = path;
            }
#if FEAT_IKVM
            return(null);
#else
            return((TypeModel)Activator.CreateInstance(finalType));
#endif
        }
Пример #24
0
        /// <summary>
        /// Fully compiles the current model into a static-compiled serialization dll
        /// (the serialization dll still requires protobuf-net for support services).
        /// </summary>
        /// <remarks>A full compilation is restricted to accessing public types / members</remarks>
        /// <returns>An instance of the newly created compiled type-model</returns>
        public TypeModel Compile(CompilerOptions options)
        {
            if (options == null) throw new ArgumentNullException("options");
            string typeName = options.TypeName;
            string path = options.OutputPath;
            BuildAllSerializers();
            Freeze();
            bool save = !Helpers.IsNullOrEmpty(path);
            if (Helpers.IsNullOrEmpty(typeName))
            {
                if (save) throw new ArgumentNullException("typeName");
                typeName = Guid.NewGuid().ToString();
            }


            string assemblyName, moduleName;
            if(path == null)
            {
                assemblyName = typeName;
                moduleName = assemblyName + ".dll";
            }
            else
            {
                assemblyName = new System.IO.FileInfo(System.IO.Path.GetFileNameWithoutExtension(path)).Name;
                moduleName = assemblyName + System.IO.Path.GetExtension(path);
            }

#if FEAT_IKVM
            IKVM.Reflection.AssemblyName an = new IKVM.Reflection.AssemblyName();
            an.Name = assemblyName;
            AssemblyBuilder asm = universe.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save);
            if (!Helpers.IsNullOrEmpty(options.KeyFile))
            {
                asm.__SetAssemblyKeyPair(new StrongNameKeyPair(File.OpenRead(options.KeyFile)));
            }
            else if (!Helpers.IsNullOrEmpty(options.KeyContainer))
            {
                asm.__SetAssemblyKeyPair(new StrongNameKeyPair(options.KeyContainer));
            }
            else if (!Helpers.IsNullOrEmpty(options.PublicKey))
            {
                asm.__SetAssemblyPublicKey(FromHex(options.PublicKey));
            }
            if(!Helpers.IsNullOrEmpty(options.ImageRuntimeVersion) && options.MetaDataVersion != 0)
            {
                asm.__SetImageRuntimeVersion(options.ImageRuntimeVersion, options.MetaDataVersion);
            }
            ModuleBuilder module = asm.DefineDynamicModule(moduleName, path);
#else
            AssemblyName an = new AssemblyName();
            an.Name = assemblyName;
            AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly(an,
                (save ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run)
                );
            ModuleBuilder module = save ? asm.DefineDynamicModule(moduleName, path)
                                        : asm.DefineDynamicModule(moduleName);
#endif

            WriteAssemblyAttributes(options, assemblyName, asm);

            TypeBuilder type = WriteBasicTypeModel(options, typeName, module);

            int index;
            bool hasInheritance;
            SerializerPair[] methodPairs;
            Compiler.CompilerContext.ILVersion ilVersion;
            WriteSerializers(options, assemblyName, type, out index, out hasInheritance, out methodPairs, out ilVersion);

            ILGenerator il;
            int knownTypesCategory;
            FieldBuilder knownTypes;
            Type knownTypesLookupType;
            WriteGetKeyImpl(type, hasInheritance, methodPairs, ilVersion, assemblyName, out il, out knownTypesCategory, out knownTypes, out knownTypesLookupType);

            // trivial flags
            il = Override(type, "SerializeDateTimeKind");
            il.Emit(IncludeDateTimeKind ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
            il.Emit(OpCodes.Ret);
            // end: trivial flags

            Compiler.CompilerContext ctx = WriteSerializeDeserialize(assemblyName, type, methodPairs, ilVersion, ref il);

            WriteConstructors(type, ref index, methodPairs, ref il, knownTypesCategory, knownTypes, knownTypesLookupType, ctx);
            


            Type finalType = type.CreateType();
            if(!Helpers.IsNullOrEmpty(path))
            {
                asm.Save(path);
                Helpers.DebugWriteLine("Wrote dll:" + path);
            }
#if FEAT_IKVM
            return null;
#else
            return (TypeModel)Activator.CreateInstance(finalType);
#endif
        }
Пример #25
0
		// copied from /mcs/mcs/codegen.cs
		private void SetPublicKey (AssemblyName an, byte[] strongNameBlob) 
		{
			// check for possible ECMA key
			if (strongNameBlob.Length == 16) {
				// will be rejected if not "the" ECMA key
				an.SetPublicKey (strongNameBlob);
			} else {
				// take it, with or without, a private key
				RSA rsa = CryptoConvert.FromCapiKeyBlob (strongNameBlob);
				// and make sure we only feed the public part to Sys.Ref
				byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob (rsa);
					
				// AssemblyName.SetPublicKey requires an additional header
				byte[] publicKeyHeader = new byte [12] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00 };

				byte[] encodedPublicKey = new byte [12 + publickey.Length];
				Buffer.BlockCopy (publicKeyHeader, 0, encodedPublicKey, 0, 12);
				Buffer.BlockCopy (publickey, 0, encodedPublicKey, 12, publickey.Length);
				an.SetPublicKey (encodedPublicKey);
			}
		}