public INsMember GetOrAddPreservedMember(IMemberDef source)
            {
                var name = source.Name;

                if (_byOld.TryGetValue(name, out var result))
                {
                    return(result);
                }
                result = new MetadataElement(this, name);
                _byOld.Add(name, result);
                _byNew.Add(name, result);
                return(result);
            }
            public INsMember GetOrAddMember(IMemberDef source, Func <string> generator)
            {
                var name = source.Name;

                if (_byOld.TryGetValue(name, out var member))
                {
                    return(member);
                }
                var newName = generator?.Invoke();

                if (newName == null)
                {
                    return(null);
                }
                var result = new MetadataElement(this, newName);

                _byOld.Add(name, result);
                _byNew.Add(newName, result);
                return(result);
            }
示例#3
0
        protected override void Handle(NetfuserEvent ev)
        {
            switch (ev)
            {
            case NetfuserEvent.Initialize _:
                if (Options.Generator == null)
                {
                    Options.Generator = Context.DebugMode ? (NameGenerator) new NameGenerator.Debug() : new NameGenerator.Hash();
                }
                break;

            case NetfuserEvent.TypeSkeletonsImported e:
                if ((_options.Type & MetaType.Method) != 0)
                {
                    _nameChains = DnextFactory.BuildVNameChains(Context.Plugin <IVTablePlugin>().VTables,
                                                                Context.MappedTypes.Values.Select(m => m.Source), m =>
                                                                !Context.MappedTypes.TryGetValue(m.DeclaringType.CreateKey(), out var tm) ||
                                                                DontRename(tm, m));
                }

                // build scopes for types with preserved names first
                foreach (var tm in Context.MappedTypes.Values.Where(m =>
                                                                    _ns.IsPreserved(m, MetaType.NamespaceAndType)))
                {
                    Prepopulate(tm);
                }
                if (_options.NamespaceMangling == NamespaceMangling.Distribute)
                {
                    var count = (int)Math.Sqrt(Context.MappedTypes.Count);
                    for (var i = 0; i < count; i++)
                    {
                        GetNode(_root, null, i.ToString());
                    }
                }

                // build scopes for the remaining types and their preserved member names
                foreach (var tm in Context.MappedTypes.Values.Where(m =>
                                                                    !_ns.IsPreserved(m, MetaType.NamespaceAndType)))
                {
                    Prepopulate(tm);
                }

                if ((_options.Type & MetaType.Method) != 0)
                {
                    // add virtual renamable methods before regular methods
                    // we use fake scope here
                    var scope = new MetadataElement(null, string.Empty);

                    foreach (var c in _nameChains.All())
                    {
                        if (!c.DontRename)
                        {
                            c.NewName = NameGenerator(scope, null, c.Name,
                                                      n => MatchesTypeOrMemberName(c.Types, n));
                        }
                    }
                }

                break;

            case NetfuserEvent.CreateMethod cm when(_options.Type& MetaType.Method) != 0:
                cm.Target.Name = GetMangledName(cm.TypeMapping, cm.Source);

                break;

            case NetfuserEvent.CreateField cm when(_options.Type& MetaType.Field) != 0:
                cm.Target.Name = GetMangledName(cm.TypeMapping, cm.Source);

                break;

            case NetfuserEvent.CreateProperty cm when(_options.Type& MetaType.Property) != 0:
                cm.Target.Name = GetMangledName(cm.TypeMapping, cm.Source);

                break;

            case NetfuserEvent.CreateEvent cm when(_options.Type& MetaType.Event) != 0:
                cm.Target.Name = GetMangledName(cm.TypeMapping, cm.Source);

                break;

            case NetfuserEvent.CreateMethodParameter cm when(_options.Type& MetaType.Method) != 0:
                // fast and easy way to check if we should rename parameter names:
                // if renaming didn't happen for this method, there's little sense to rename its parameters.
                // someone may even rely on parameter names [theoretically], as they are available via reflection
                // However, we allow one exception - RTSpecialNames (.ctors), as they are never renamed
                var method = cm.Source.DeclaringMethod;

                if (!method.IsRuntimeSpecialName && GetMangledName(cm.TypeMapping, method) == method.Name)
                {
                    break;
                }

                cm.Target.Name = _options.Generator.Generate(this, cm.Source);
                break;

            case NetfuserEvent.CreateGenericParameter cm:
                method = cm.Source.DeclaringMethod;
                if (method != null)
                {
                    if (!method.IsRuntimeSpecialName && GetMangledName(cm.TypeMapping, method) == method.Name)
                    {
                        break;
                    }
                }
                else if (cm.Source.DeclaringType != null)
                {
                    if (GetNode(cm.TypeMapping).NewName == cm.Source.DeclaringType.Name)
                    {
                        break;
                    }
                }
                else
                {
                    throw new NotSupportedException();
                }

                cm.Target.Name = _options.Generator.Generate(this, cm.Source);
                break;

            case NetfuserEvent.ResourcesDeclared rde:
                var rs       = new MetadataElement(null, string.Empty);
                var newNames = new HashSet <string>(Context.MappedResources.Values.Select(m => (string)m.Target.Name));
                foreach (var rm in Context.MappedResources.Values.Where(m => !_ns.IsPreserved(m)))
                {
                    var oldName = rm.Target.Name;
                    var newName = NameGenerator(rs, null, rm.Source.Name, n => newNames.Contains(n));
                    if (newName != oldName)
                    {
                        newNames.Remove(oldName);
                        newNames.Add(newName);
                        rm.Target.Name = newName;
                    }
                }
                break;
            }
        }
 public MetadataElement(MetadataElement parent, string name)
 {
     Parent  = parent;
     NewName = name;
 }