修复给虚函数分配名字时未考虑到不要与每个虚函数所在命名空间不冲突的bug

before-split
walon 2025-05-23 19:48:55 +08:00
parent 5de469c4a6
commit 5f4083066b
4 changed files with 37 additions and 4 deletions

View File

@ -22,6 +22,8 @@ namespace Obfuz.ObfusPasses.SymbolObfus
string GetNewName(MethodDef methodDef, string originalName);
string GetNewName(VirtualMethodGroup virtualMethodGroup, string originalName);
string GetNewName(ParamDef param, string originalName);
string GetNewName(FieldDef fieldDef, string originalName);

View File

@ -2,7 +2,9 @@
{
public interface INameScope
{
void AddPreservedName(string name);
bool AddPreservedName(string name);
bool IsNamePreserved(string name);
string GetNewName(string originalName, bool reuse);
}

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.Assertions;
namespace Obfuz.ObfusPasses.SymbolObfus.NameMakers
{
@ -77,7 +78,29 @@ namespace Obfuz.ObfusPasses.SymbolObfus.NameMakers
public string GetNewName(MethodDef methodDef, string originalName)
{
return (methodDef.IsVirtual ? ">" : "") + GetDefaultNewName(methodDef.DeclaringType, originalName);
Assert.IsFalse(methodDef.IsVirtual);
return GetDefaultNewName(methodDef.DeclaringType, originalName);
}
public string GetNewName(VirtualMethodGroup virtualMethodGroup, string originalName)
{
var scope = GetNameScope(virtualMethodGroup);
while (true)
{
string newName = scope.GetNewName(originalName, false);
if (virtualMethodGroup.methods.Any(m => GetNameScope(m).IsNamePreserved(newName)))
{
continue;
}
else
{
foreach (var method in virtualMethodGroup.methods)
{
GetNameScope(method).AddPreservedName(newName);
}
return newName;
}
}
}
public virtual string GetNewName(ParamDef param, string originalName)

View File

@ -11,12 +11,18 @@ namespace Obfuz.ObfusPasses.SymbolObfus.NameMakers
private readonly HashSet<string> _preservedNames = new HashSet<string>();
public void AddPreservedName(string name)
public bool AddPreservedName(string name)
{
if (!string.IsNullOrEmpty(name))
{
_preservedNames.Add(name);
return _preservedNames.Add(name);
}
return false;
}
public bool IsNamePreserved(string name)
{
return _preservedNames.Contains(name);
}