コード例 #1
0
        IResolutionScope CreateScopeReference(IAssembly defAsm, ModuleDef defMod)
        {
            if (defAsm == null)
            {
                return(null);
            }
            if (defMod != null && defAsm != null && module.Assembly != null)
            {
                if (UTF8String.CaseInsensitiveEquals(module.Assembly.Name, defAsm.Name))
                {
                    if (UTF8String.CaseInsensitiveEquals(module.Name, defMod.Name))
                    {
                        return(module);
                    }
                    return(module.UpdateRowId(new ModuleRefUser(module, defMod.Name)));
                }
            }
            var pkt = PublicKeyBase.ToPublicKeyToken(defAsm.PublicKeyOrToken);

            if (PublicKeyBase.IsNullOrEmpty2(pkt))
            {
                pkt = null;
            }
            return(module.UpdateRowId(new AssemblyRefUser(defAsm.Name, defAsm.Version, pkt, defAsm.Culture)));
        }
コード例 #2
0
ファイル: AssemblyDef.cs プロジェクト: hmflash/dnlib
		/// <summary>
		/// Finds a module in this assembly
		/// </summary>
		/// <param name="name">Name of module</param>
		/// <returns>A <see cref="ModuleDef"/> instance or <c>null</c> if it wasn't found.</returns>
		public ModuleDef FindModule(UTF8String name) {
			foreach (var module in Modules.GetSafeEnumerable()) {
				if (module == null)
					continue;
				if (UTF8String.CaseInsensitiveEquals(module.Name, name))
					return module;
			}
			return null;
		}
コード例 #3
0
 /// <summary>
 /// Finds a module in this assembly
 /// </summary>
 /// <param name="name">Name of module</param>
 /// <returns>A <see cref="ModuleDef"/> instance or <c>null</c> if it wasn't found.</returns>
 public ModuleDef FindModule(UTF8String name)
 {
     foreach (var module in Modules)
     {
         if (UTF8String.CaseInsensitiveEquals(module.Name, name))
         {
             return(module);
         }
     }
     return(null);
 }
コード例 #4
0
 static bool Equals(IAssembly a, IAssembly b)
 {
     if (a == b)
     {
         return(true);
     }
     if (a == null || b == null)
     {
         return(false);
     }
     return(Utils.Equals(a.Version, b.Version) &&
            PublicKeyBase.TokenEquals(a.PublicKeyOrToken, b.PublicKeyOrToken) &&
            UTF8String.Equals(a.Name, b.Name) &&
            UTF8String.CaseInsensitiveEquals(a.Culture, b.Culture));
 }
コード例 #5
0
        private static ModuleDef FindModule(ModuleDef module, FileDef file)
        {
            if (module == null || file == null)
            {
                return(null);
            }
            if (UTF8String.CaseInsensitiveEquals(module.Name, file.Name))
            {
                return(module);
            }
            var asm = module.Assembly;

            if (asm == null)
            {
                return(null);
            }
            return(asm.FindModule(file.Name));
        }
コード例 #6
0
        /// <summary>
        /// Figures out which of two assembly names is closer to another assembly name
        /// </summary>
        /// <param name="requested">Requested assembly name</param>
        /// <param name="a">First</param>
        /// <param name="b">Second</param>
        /// <returns>-1 if both are equally close, 0 if a is closest, 1 if b is closest</returns>
        public int CompareClosest(AssemblyNameInfo requested, AssemblyNameInfo a, AssemblyNameInfo b)
        {
            if (a == b)
            {
                return(0);
            }
            if (a == null)
            {
                return(1);
            }
            if (b == null)
            {
                return(0);
            }

            // Compare the most important parts first:
            //	1. Assembly simple name
            //	2. Public key token
            //	3. Version
            //	4. Locale

            if (CompareName)
            {
                // If the name only matches one of a or b, return that one.
                bool na = UTF8String.CaseInsensitiveEquals(requested.Name, a.Name);
                bool nb = UTF8String.CaseInsensitiveEquals(requested.Name, b.Name);
                if (na && !nb)
                {
                    return(0);
                }
                if (!na && nb)
                {
                    return(1);
                }
                if (!na && !nb)
                {
                    return(-1);
                }
            }

            if (ComparePublicKeyToken)
            {
                bool pa, pb;
                if (PublicKeyBase.IsNullOrEmpty2(requested.PublicKeyOrToken))
                {
                    // If one of them has a pkt but the other one hasn't, return the one with
                    // no pkt.
                    pa = PublicKeyBase.IsNullOrEmpty2(a.PublicKeyOrToken);
                    pb = PublicKeyBase.IsNullOrEmpty2(b.PublicKeyOrToken);
                }
                else
                {
                    // If one of them has the correct pkt, but the other one has an incorrect
                    // pkt, return the one with the correct pkt.
                    pa = PublicKeyBase.TokenEquals(requested.PublicKeyOrToken, a.PublicKeyOrToken);
                    pb = PublicKeyBase.TokenEquals(requested.PublicKeyOrToken, b.PublicKeyOrToken);
                }
                if (pa && !pb)
                {
                    return(0);
                }
                if (!pa && pb)
                {
                    return(1);
                }
            }

            if (CompareVersion && !Utils.Equals(a.Version, b.Version))
            {
                var rv = Utils.CreateVersionWithNoUndefinedValues(requested.Version);
                if (rv == new Version(0, 0, 0, 0))
                {
                    rv = new Version(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue);
                }
                int va = Utils.CompareTo(a.Version, rv);
                int vb = Utils.CompareTo(b.Version, rv);
                if (va == 0)
                {
                    return(0);                          // vb != 0 so return 0
                }
                if (vb == 0)
                {
                    return(1);                          // va != 0 so return 1
                }
                if (va > 0 && vb < 0)
                {
                    return(0);
                }
                if (va < 0 && vb > 0)
                {
                    return(1);
                }
                // Now either both a and b's version > req version or both are < req version
                if (va > 0)
                {
                    // a.Version and b.Version > req.Version. Pick the one that is closest.
                    return(Utils.CompareTo(a.Version, b.Version) < 0 ? 0 : 1);
                }
                else
                {
                    // a.Version and b.Version < req.Version. Pick the one that is closest.
                    return(Utils.CompareTo(a.Version, b.Version) > 0 ? 0 : 1);
                }
            }

            if (CompareLocale)
            {
                bool la = Utils.LocaleEquals(requested.Locale, a.Locale);
                bool lb = Utils.LocaleEquals(requested.Locale, b.Locale);
                if (la && !lb)
                {
                    return(0);
                }
                if (!la && lb)
                {
                    return(1);
                }
            }

            return(-1);
        }
コード例 #7
0
        /// <summary>
        ///     Figures out which of two assembly names is closer to another assembly name
        /// </summary>
        /// <param name="requested">Requested assembly name</param>
        /// <param name="a">First</param>
        /// <param name="b">Second</param>
        /// <returns>
        ///     -1 if both are equally close, 0 if <paramref name="a" /> is closest, 1 if
        ///     <paramref name="b" /> is closest
        /// </returns>
        public int CompareClosest(IAssembly requested, IAssembly a, IAssembly b)
        {
            if (a == b)
            {
                return(0);
            }
            if (a == null)
            {
                return(!CompareName ? 1 : UTF8String.CaseInsensitiveEquals(requested.Name, b.Name) ? 1 : 0);
            }
            if (b == null)
            {
                return(!CompareName ? 0 : UTF8String.CaseInsensitiveEquals(requested.Name, a.Name) ? 0 : 1);
            }

            // Compare the most important parts first:
            //	1. Assembly simple name
            //	2. Public key token
            //	3. Version
            //	4. Locale
            //	5. Content type

            if (CompareName)
            {
                // If the name only matches one of a or b, return that one.
                var na = UTF8String.CaseInsensitiveEquals(requested.Name, a.Name);
                var nb = UTF8String.CaseInsensitiveEquals(requested.Name, b.Name);
                if (na && !nb)
                {
                    return(0);
                }
                if (!na && nb)
                {
                    return(1);
                }
                if (!na && !nb)
                {
                    return(-1);
                }
            }

            if (ComparePublicKeyToken)
            {
                bool pa, pb;
                if (PublicKeyBase.IsNullOrEmpty2(requested.PublicKeyOrToken))
                {
                    // If one of them has a pkt but the other one hasn't, return the one with
                    // no pkt.
                    pa = PublicKeyBase.IsNullOrEmpty2(a.PublicKeyOrToken);
                    pb = PublicKeyBase.IsNullOrEmpty2(b.PublicKeyOrToken);
                }
                else
                {
                    // If one of them has the correct pkt, but the other one has an incorrect
                    // pkt, return the one with the correct pkt.
                    pa = PublicKeyBase.TokenEquals(requested.PublicKeyOrToken, a.PublicKeyOrToken);
                    pb = PublicKeyBase.TokenEquals(requested.PublicKeyOrToken, b.PublicKeyOrToken);
                }
                if (pa && !pb)
                {
                    return(0);
                }
                if (!pa && pb)
                {
                    return(1);
                }
            }

            if (CompareVersion && !Utils.Equals(a.Version, b.Version))
            {
                var rv = Utils.CreateVersionWithNoUndefinedValues(requested.Version);
                if (rv == new Version(0, 0, 0, 0))
                {
                    rv = new Version(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue);
                }
                var va = Utils.CompareTo(a.Version, rv);
                var vb = Utils.CompareTo(b.Version, rv);
                if (va == 0)
                {
                    return(0); // vb != 0 so return 0
                }
                if (vb == 0)
                {
                    return(1); // va != 0 so return 1
                }
                if (va > 0 && vb < 0)
                {
                    return(0);
                }
                if (va < 0 && vb > 0)
                {
                    return(1);
                }
                // Now either both a and b's version > req version or both are < req version
                if (va > 0)
                {
                    return(Utils.CompareTo(a.Version, b.Version) < 0 ? 0 : 1);
                }
                return(Utils.CompareTo(a.Version, b.Version) > 0 ? 0 : 1);
            }

            if (CompareCulture)
            {
                var la = Utils.LocaleEquals(requested.Culture, a.Culture);
                var lb = Utils.LocaleEquals(requested.Culture, b.Culture);
                if (la && !lb)
                {
                    return(0);
                }
                if (!la && lb)
                {
                    return(1);
                }
            }

            if (CompareContentType)
            {
                var ca = requested.ContentType == a.ContentType;
                var cb = requested.ContentType == b.ContentType;
                if (ca && !cb)
                {
                    return(0);
                }
                if (!ca && cb)
                {
                    return(1);
                }
            }

            return(-1);
        }