Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加HotfixFlag枚举项,允许需要热更的类型忽略特定方法。 #1139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Assets/XLua/Doc/Hotfix_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ xlua.hotfix(CS.HotfixTest, 'Update', function(self)

The premise is that hotfix_id_map.lua.txt is in a directory that can be referenced by require 'hotfix_id_map'.

## Ignore Method
Adding [Hotfix(HotfixFlag.IgnoreThisMethod)] attribute to a method will not GenDelegateBridge for that method and will not hotfix inject that method.
Typically used for methods that cause xlua generation to cause compilation errors. For example methods with Span parameters in the signature.

## Usage suggestions

* Add the Hotfix flag to all types that are most likely to be modified.
Expand Down
4 changes: 4 additions & 0 deletions Assets/XLua/Doc/hotfix.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ xlua.hotfix(CS.HotfixTest, 'Update', function(self)

前提是 `hotfix_id_map.lua.txt` 放到可以通过 `require 'hotfix_id_map'` 引用到的地方。

## 忽略特定方法
在方法上添加[Hotfix(HotfixFlag.IgnoreThisMethod)]特性标记,则不会为该方法生成委托,也不会hotfix inject该方法。
通常用于一些会导致xlua生成造成编译错误的函数。例如签名中含有Span参数的方法。

## 使用建议

* 对所有较大可能变动的类型加上 `Hotfix` 标识;
Expand Down
23 changes: 23 additions & 0 deletions Assets/XLua/Src/Editor/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,28 @@ static bool HasFlag(this HotfixFlag toCheck, HotfixFlag flag)
return (toCheck != HotfixFlag.Stateless) && ((toCheck & flag) == flag);
}

/// <summary>
/// 方法是否由特性标记为忽略生成委托
/// </summary>
/// <param name="member"></param>
/// <returns></returns>
static bool isIgnoreGenDelegateBridgeByAttribute(MemberInfo member)
{
var ca = GetCustomAttribute(member, typeof(HotfixAttribute));
if (ca == null)
{
return false;
}

#if XLUA_GENERAL
var hotfixType = (HotfixFlag)Convert.ToInt32(ca.GetType().GetProperty("Flag").GetValue(ca, null));
#else
var hotfixType = (ca as HotfixAttribute).Flag;
#endif

return hotfixType.HasFlag(HotfixFlag.IgnoreGenDelegateBridge);
}

static void GenDelegateBridge(IEnumerable<Type> types, string save_path, IEnumerable<Type> hotfix_check_types)
{
string filePath = save_path + "DelegatesGensBridge.cs";
Expand Down Expand Up @@ -945,6 +967,7 @@ static void GenDelegateBridge(IEnumerable<Type> types, string save_path, IEnumer
.Cast<MethodBase>()
.Concat(kv.Key.GetConstructors(bindingAttrOfConstructor).Cast<MethodBase>())
.Where(method => !injectByGeneric(method, kv.Value))
.Where(method => !isIgnoreGenDelegateBridgeByAttribute(method))
.Select(method => makeHotfixMethodInfoSimulation(method, kv.Value)));
}
hotfxDelegates = hotfxDelegates.Distinct(comparer).ToList();
Expand Down
35 changes: 35 additions & 0 deletions Assets/XLua/Src/Editor/Hotfix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ enum HotfixFlagInTool
AdaptByDelegate = 64,
IgnoreCompilerGenerated = 128,
NoBaseProxy = 256,
/// <summary>
/// 仅标记在方法上有效。忽略此方法生成委托。
/// </summary>
IgnoreGenDelegateBridge = 512,
/// <summary>
/// 仅标记在方法上有效。忽略此方法注入。
/// </summary>
IgnoreHotfixInject = 1024,
/// <summary>
/// 仅标记在方法上有效。忽略此方法生成委托和注入。
/// </summary>
IgnoreThisMethod = IgnoreGenDelegateBridge | IgnoreHotfixInject,
}

static class ExtentionMethods
Expand Down Expand Up @@ -685,6 +697,25 @@ static bool genericInOut(MethodDefinition method, HotfixFlagInTool hotfixType)
return false;
}

/// <summary>
/// 方法是否由特性标记为忽略注入
/// </summary>
/// <param name="hotfixAttributeType"></param>
/// <param name="method"></param>
/// <returns></returns>
public bool isIgnoreInjectMethodByAttribute(TypeReference hotfixAttributeType, MethodDefinition method)
{
CustomAttribute hotfixAttr = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType == hotfixAttributeType);
HotfixFlagInTool hotfixType = default;
if (hotfixAttr != null)
{
hotfixType = (HotfixFlagInTool)(int)hotfixAttr.ConstructorArguments[0].Value;
}

bool ignore = hotfixType.HasFlag(HotfixFlagInTool.IgnoreHotfixInject);
return ignore;
}

public bool InjectType(TypeReference hotfixAttributeType, TypeDefinition type)
{
foreach(var nestedTypes in type.NestedTypes)
Expand Down Expand Up @@ -747,6 +778,10 @@ public bool InjectType(TypeReference hotfixAttributeType, TypeDefinition type)
{
continue;
}
if (isIgnoreInjectMethodByAttribute(hotfixAttributeType,method))
{
continue;
}
if (method.Name != ".cctor" && !method.IsAbstract && !method.IsPInvokeImpl && method.Body != null && !method.Name.Contains("<"))
{
//Debug.Log(method);
Expand Down
12 changes: 12 additions & 0 deletions Assets/XLua/Src/GenAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ public enum HotfixFlag
AdaptByDelegate = 64,
IgnoreCompilerGenerated = 128,
NoBaseProxy = 256,
/// <summary>
/// 仅标记在方法上有效。忽略此方法生成委托。
/// </summary>
IgnoreGenDelegateBridge = 512,
/// <summary>
/// 仅标记在方法上有效。忽略此方法注入。
/// </summary>
IgnoreHotfixInject = 1024,
/// <summary>
/// 仅标记在方法上有效。忽略此方法生成委托和注入。
/// </summary>
IgnoreThisMethod = IgnoreGenDelegateBridge | IgnoreHotfixInject,
}

public class HotfixAttribute : Attribute
Expand Down
Binary file modified Tools/FilesSignature.exe
Binary file not shown.
Binary file modified Tools/FilesSignature.pdb
Binary file not shown.
Binary file modified Tools/KeyPairsGen.exe
Binary file not shown.
Binary file modified Tools/KeyPairsGen.pdb
Binary file not shown.
Binary file modified Tools/XLua.Mini.dll
Binary file not shown.
Binary file modified Tools/XLua.Mini.pdb
Binary file not shown.
Binary file modified Tools/XLuaGenerate.exe
Binary file not shown.
Binary file modified Tools/XLuaGenerate.pdb
Binary file not shown.
Binary file modified Tools/XLuaHotfixInject.exe
Binary file not shown.
Binary file modified Tools/XLuaHotfixInject.pdb
Binary file not shown.