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

调用private_accessible方法,某些情况下无法访问内部嵌套类的私有成员 #622

Closed
yaogunfantuan opened this issue Jul 26, 2019 · 9 comments

Comments

@yaogunfantuan
Copy link

public void PrivateAccessible(RealStatePtr L, Type type)
{
    if (!privateAccessibleFlags.Contains(type)) //未处理
     {
         privateAccessibleFlags.Add(type);
         if (typeIdMap.ContainsKey(type)) //loaded
          {
              Utils.MakePrivateAccessible(L, type);
          }
     }
 }

在调用private_accessible时候,如果目标类是一个内部嵌套类,那么在PrivateAccessible中会因为typeIdMap中不存在对应的type,而无法调用MakePrivateAccessible。typeIdMap中添加key只存在于调用import_type方法和push等几个方法中。如果lua中从未调用过此类方法将会导致无法访问内部类的私有成员。
临时解决方案为在private_accessible前,先improt_type下目标嵌套类。
根本原因为在import_type到外部类时候会在TryDelayWrapLoader中直接对其内部类进行TryDelayWrapLoader生成对应的lua Table,而在TryDelayWrapLoader方法中缺少了对typeIdMap中添加对应内部类的key的操作。

@yaogunfantuan yaogunfantuan changed the title 内部嵌套类在某些情况下无法访问私有成员 调用private_accessible方法,某些情况下无法访问内部嵌套类的私有成员 Jul 26, 2019
@chexiongsheng
Copy link
Collaborator

这会递归处理public的内嵌类,你这个内嵌类本身也是私有的么?

@yaogunfantuan
Copy link
Author

内嵌类是public,但是这个内嵌类有一个私有类型,这样会访问不到这个私有类型

@yaogunfantuan
Copy link
Author

也就是在typeIdMap.ContainsKey(type)这句时候会被判定为不存在

@chexiongsheng
Copy link
Collaborator

内嵌类里还有个私有的内嵌类?

@yaogunfantuan
Copy link
Author

public class A{
    public class B{
        private string m_StringField = "Test"
    }
}

xlua.private_accessible(CS.A.B)
然后m_StringField就会获取不到值

@chexiongsheng
Copy link
Collaborator

就你这例子亲测可以。是不是版本太老了?

@yaogunfantuan
Copy link
Author

using UnityEngine;
using XLua;


public class TestA {
    [Hotfix]
    public class TestB {
        private string m_PrivateText = "hello!";
        public string m_PublicText = "hello!";
        public void TestCall() {
            Debug.Log("Hello world");
        }
    }
}


public class test : MonoBehaviour
{
    LuaEnv luaenv = new LuaEnv();
    void Start()
    {
        luaenv.DoString(@"
                require('xlua.util')
                xlua.private_accessible(CS.TestA.TestB)

                xlua.hotfix(CS.TestA.TestB, 'TestCall', function(self)
                    print(self.m_PublicText);
                    print(self.m_PrivateText);
                end)
        ");
        TestA.TestB testB = new TestA.TestB();
        testB.TestCall();
    }
}

out:
LUA: hello!
LUA: nil

版本:v2.1.14

@yaogunfantuan
Copy link
Author

@chexiongsheng 您好,这个问题请问现在是已经被确认还是?上面是我自己的测试代码是不通过的。

@chexiongsheng
Copy link
Collaborator

是问题,已经修复

onlyfeng added a commit to onlyfeng/xLua that referenced this issue Oct 26, 2019
* upstream/master: (24 commits)
  当方法返回指针时过滤掉 (Tencent#660)
  更新iOS cmake toolchain以支持macOS 10.15 (Tencent#657)
  修复生成扩展方法未判断方法是否过时 (Tencent#649)
  删除unity2018下的Generate Minimize Code选项
  * fix LazyReflectionCall with overloaded methods (Tencent#638)
  update: [98989] cmake在osx平台添加一个预编译定义,为了能在Emmy-Lua在ide下调试时能够加载cpath (Tencent#629)
  示例hotfix配置修改为GetTypes,否则照抄这个非public类型不被注入
  直接通过CS.XX访问一个内嵌类时,会在加载外层类的时候顺便加载了内嵌类,但没有建立type到id的映射,导致xlua.private_accessible认为改内嵌类并未加载,fix Tencent#622
  fix error display when StaticLuaCallbacks::ImportType throws
  unity 2018以后版本的兼容性问题的新解决办法。
  支持通过代码来过滤不需生成代码的方法,主要用于解决2018以后unity运行和编译用的mono不一致的问题。
  非Assembly-CSharp程序集注入时,out参数处理有误
  这里有可能是通过反射得到未设置泛型的类型导致lua脚本报错生成代码失败 (Tencent#617)
  unity的.net 4下,object obj = new sbyte[] { 1, 2 };obj is byte[]会判断为true,修改为兼容性更好的写法,防止sbyte[] 在.net 4下push到lua变成字符串
  自动配置CSharpCallLua列表示例里头,过滤掉未实例化的泛型和引用了editor的类型
  解决bug:当采用反射方式注册枚举值时,如果一个枚举有多个相同的值,比如A,B都是1,那么在lua里头访问B将会为空
  最小生成模式枚举生成代码报错
  最小生成模式枚举生成代码报错
  fix dependence alert
  chunkname 应和脚本文件名一致(否则IDE调试不了) (Tencent#600)
  ...

# Conflicts:
#	build/CMakeLists.txt
#	build/cmake/ios.toolchain.cmake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants