Skip to content

Commit

Permalink
解决bug:当采用反射方式注册枚举值时,如果一个枚举有多个相同的值,比如A,B都是1,那么在lua里头访问B将会为空
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Jul 18, 2019
1 parent 65caeb1 commit e8a5fc1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 1 addition & 4 deletions Assets/XLua/Src/Editor/Template/LuaEnumWrap.tpl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ namespace XLua.CSObjectWrap
Utils.RegisterObject(L, translator, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
<%end)%>
<%else%>
foreach(var e in System.Enum.GetValues(typeof(<%=CsFullTypeName(type)%>)))
{
Utils.RegisterObject(L, translator, Utils.CLS_IDX, e.ToString(), e);
}
Utils.RegisterEnumType(L, typeof(<%=CsFullTypeName(type)%>));
<%end%>
Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom);

Expand Down
5 changes: 1 addition & 4 deletions Assets/XLua/Src/Editor/Template/LuaEnumWrapGCM.tpl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ namespace XLua
Utils.RegisterObject(L, this, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
<%end)%>
<%else%>
foreach(var e in System.Enum.GetValues(typeof(<%=CsFullTypeName(type)%>)))
{
Utils.RegisterObject(L, this, Utils.CLS_IDX, e.ToString(), e);
}
Utils.RegisterEnumType(L, typeof(<%=CsFullTypeName(type)%>));
<%end%>
Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom<%=v_type_name%>);

Expand Down
12 changes: 11 additions & 1 deletion Assets/XLua/Src/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,17 @@ public static void loadUpvalue(RealStatePtr L, Type type, string metafunc, int n
}
}

public static void MakePrivateAccessible(RealStatePtr L, Type type)
public static void RegisterEnumType(RealStatePtr L, Type type)
{
ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
foreach (var name in Enum.GetNames(type))
{
RegisterObject(L, translator, Utils.CLS_IDX, name, Enum.Parse(type, name));
}
}


public static void MakePrivateAccessible(RealStatePtr L, Type type)
{
LuaAPI.lua_checkstack(L, 20);

Expand Down

0 comments on commit e8a5fc1

Please sign in to comment.