util.lua.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. -- Tencent is pleased to support the open source community by making xLua available.
  2. -- Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  3. -- Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  4. -- http://opensource.org/licenses/MIT
  5. -- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  6. local unpack = unpack or table.unpack
  7. local function async_to_sync(async_func, callback_pos)
  8. return function(...)
  9. local _co = coroutine.running() or error ('this function must be run in coroutine')
  10. local rets
  11. local waiting = false
  12. local function cb_func(...)
  13. if waiting then
  14. assert(coroutine.resume(_co, ...))
  15. else
  16. rets = {...}
  17. end
  18. end
  19. local params = {...}
  20. table.insert(params, callback_pos or (#params + 1), cb_func)
  21. async_func(unpack(params))
  22. if rets == nil then
  23. waiting = true
  24. rets = {coroutine.yield()}
  25. end
  26. return unpack(rets)
  27. end
  28. end
  29. local function coroutine_call(func)
  30. return function(...)
  31. local co = coroutine.create(func)
  32. assert(coroutine.resume(co, ...))
  33. end
  34. end
  35. local move_end = {}
  36. local generator_mt = {
  37. __index = {
  38. MoveNext = function(self)
  39. self.Current = self.co()
  40. if self.Current == move_end then
  41. self.Current = nil
  42. return false
  43. else
  44. return true
  45. end
  46. end;
  47. Reset = function(self)
  48. self.co = coroutine.wrap(self.w_func)
  49. end
  50. }
  51. }
  52. local function cs_generator(func, ...)
  53. local params = {...}
  54. local generator = setmetatable({
  55. w_func = function()
  56. func(unpack(params))
  57. return move_end
  58. end
  59. }, generator_mt)
  60. generator:Reset()
  61. return generator
  62. end
  63. local function loadpackage(...)
  64. for _, loader in ipairs(package.searchers) do
  65. local func = loader(...)
  66. if type(func) == 'function' then
  67. return func
  68. end
  69. end
  70. end
  71. local function auto_id_map()
  72. local hotfix_id_map = require 'hotfix_id_map'
  73. local org_hotfix = xlua.hotfix
  74. xlua.hotfix = function(cs, field, func)
  75. local map_info_of_type = hotfix_id_map[typeof(cs):ToString()]
  76. if map_info_of_type then
  77. if func == nil then func = false end
  78. local tbl = (type(field) == 'table') and field or {[field] = func}
  79. for k, v in pairs(tbl) do
  80. local map_info_of_methods = map_info_of_type[k]
  81. local f = type(v) == 'function' and v or nil
  82. for _, id in ipairs(map_info_of_methods or {}) do
  83. CS.XLua.HotfixDelegateBridge.Set(id, f)
  84. end
  85. --CS.XLua.HotfixDelegateBridge.Set(
  86. end
  87. xlua.private_accessible(cs)
  88. else
  89. return org_hotfix(cs, field, func)
  90. end
  91. end
  92. end
  93. --和xlua.hotfix的区别是:这个可以调用原来的函数
  94. local function hotfix_ex(cs, field, func)
  95. assert(type(field) == 'string' and type(func) == 'function', 'invalid argument: #2 string needed, #3 function needed!')
  96. local function func_after(...)
  97. xlua.hotfix(cs, field, nil)
  98. local ret = {func(...)}
  99. xlua.hotfix(cs, field, func_after)
  100. return unpack(ret)
  101. end
  102. xlua.hotfix(cs, field, func_after)
  103. end
  104. local function bind(func, obj)
  105. return function(...)
  106. return func(obj, ...)
  107. end
  108. end
  109. --为了兼容luajit,lua53版本直接用|操作符即可
  110. local enum_or_op = debug.getmetatable(CS.System.Reflection.BindingFlags.Public).__bor
  111. local enum_or_op_ex = function(first, ...)
  112. for _, e in ipairs({...}) do
  113. first = enum_or_op(first, e)
  114. end
  115. return first
  116. end
  117. -- description: 直接用C#函数创建delegate
  118. local function createdelegate(delegate_cls, obj, impl_cls, method_name, parameter_type_list)
  119. local flag = enum_or_op_ex(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.NonPublic,
  120. CS.System.Reflection.BindingFlags.Instance, CS.System.Reflection.BindingFlags.Static)
  121. local m = parameter_type_list and typeof(impl_cls):GetMethod(method_name, flag, nil, parameter_type_list, nil)
  122. or typeof(impl_cls):GetMethod(method_name, flag)
  123. return CS.System.Delegate.CreateDelegate(typeof(delegate_cls), obj, m)
  124. end
  125. local function state(csobj, state)
  126. local csobj_mt = getmetatable(csobj)
  127. for k, v in pairs(csobj_mt) do rawset(state, k, v) end
  128. local csobj_index, csobj_newindex = state.__index, state.__newindex
  129. state.__index = function(obj, k)
  130. return rawget(state, k) or csobj_index(obj, k)
  131. end
  132. state.__newindex = function(obj, k, v)
  133. if rawget(state, k) ~= nil then
  134. rawset(state, k, v)
  135. else
  136. csobj_newindex(obj, k, v)
  137. end
  138. end
  139. debug.setmetatable(csobj, state)
  140. return state
  141. end
  142. local function print_func_ref_by_csharp()
  143. local registry = debug.getregistry()
  144. for k, v in pairs(registry) do
  145. if type(k) == 'number' and type(v) == 'function' and registry[v] == k then
  146. local info = debug.getinfo(v)
  147. print(string.format('%s:%d', info.short_src, info.linedefined))
  148. end
  149. end
  150. end
  151. return {
  152. async_to_sync = async_to_sync,
  153. coroutine_call = coroutine_call,
  154. cs_generator = cs_generator,
  155. loadpackage = loadpackage,
  156. auto_id_map = auto_id_map,
  157. hotfix_ex = hotfix_ex,
  158. bind = bind,
  159. createdelegate = createdelegate,
  160. state = state,
  161. print_func_ref_by_csharp = print_func_ref_by_csharp,
  162. }