noop.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # apilot/stubs/noop.py
  2. """万能 no-op 工厂: 冒充 tolua/cc 渲染类。属性=同款工厂, 调用=返回同款对象。"""
  3. from __future__ import annotations
  4. from typing import Any
  5. _NOOP_RETURN_DEFAULTS: tuple[tuple[tuple, Any], ...] = (
  6. # (参数个数, 返回值) —— 需要非 nil 返回的接口按绑定面清单加
  7. (("getPosition",), (0, 0)),
  8. )
  9. def make_noop_table(name: str = "?", _depth: int = 0) -> Any:
  10. if _depth > 24:
  11. return None
  12. class _Noop:
  13. def __init__(self, n: str) -> None:
  14. self.__name = n
  15. def __getitem__(self, k: str) -> Any:
  16. return make_noop_table(f"{self.__name}.{k}", _depth + 1)
  17. def __getattr__(self, k: str) -> Any:
  18. return self[k]
  19. def __call__(self, *a: Any, **kw: Any) -> Any:
  20. method = self.__name.rsplit(".", 1)[-1]
  21. for names, ret in _NOOP_RETURN_DEFAULTS:
  22. if method in names:
  23. return ret
  24. return make_noop_table(f"{self.__name}()", _depth + 1)
  25. def __repr__(self) -> str:
  26. return f"<noop:{self.__name}>"
  27. return _Noop(name)
  28. class _ReturnZeroPair:
  29. """getPosition 等双值返回的特化包装, M1 暂不启用(绑定面清单核对后启用)。"""
  30. def install_noop_globals(lua: Any) -> None:
  31. """注入 cc/ccui/ccs/ccexp 骨架。真实实现(netClient 等)在装完 no-op 后由各自模块覆盖。"""
  32. lua.execute("""
  33. cc = cc or {}
  34. setmetatable(cc, {__index = function(t, k)
  35. local v = setmetatable({}, {
  36. __index = function(tt, kk)
  37. local fn = function(...) return setmetatable({}, {
  38. __index = function(t3, k3)
  39. return function(...) return nil end
  40. end}) end
  41. return fn end,
  42. __call = function(cls, ...) return setmetatable({}, {
  43. __index = function(o, k)
  44. return function(...) return nil end end}) end})
  45. rawset(t, k, v)
  46. return v
  47. end})
  48. ccui = ccui or cc.Sprite -- 同骨架
  49. ccs = ccs or cc.Sprite
  50. ccexp = ccexp or cc.Sprite
  51. """)