LuaExtension.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. ----Define Require Function
  2. function RequireBehaviorTreeDesigner(name_)
  3. return require("RequireBehaviorTreeDesigner/" .. name_)
  4. end
  5. function GetTableMsg(tbl, level, filteDefault)
  6. local msg = ""
  7. filteDefault = filteDefault or true
  8. -- 默认过滤关键字(DeleteMe, _class_type)
  9. level = level or 1
  10. local indent_str = ""
  11. for i = 1, level do
  12. indent_str = indent_str .. " "
  13. end
  14. msg = msg .. indent_str .. "{\n"
  15. if (tbl == nil) then
  16. msg = msg .. "nil\n";
  17. else
  18. for k, v in pairs(tbl) do
  19. local item_str = string.format("%s%s = %s\n", indent_str .. " ", tostring(k), tostring(v))
  20. msg = msg .. item_str
  21. if type(v) == "table" and tostring(k) ~= "owner" then
  22. msg = msg .. GetTableMsg(v, level + 1)
  23. -- tostring(v)
  24. end
  25. end
  26. end
  27. msg = msg .. indent_str .. "}\n"
  28. return msg
  29. end
  30. ----打印table里面的值...
  31. function ShowTable (tbl)
  32. ----print(GetTableMsg(tbl))
  33. end
  34. -- 包含缩进的table打印log
  35. function ShowTableIndent(data)
  36. local indent = "│ "
  37. local tableBegin = "├─"
  38. local tableEnd = "└─"
  39. local function print_table(t, i)
  40. local tab = ""
  41. for j = 1, i do
  42. tab = tab .. indent
  43. end
  44. tab = tab .. tableBegin
  45. for k, v in pairs(t) do
  46. if (type(v) == "table") then
  47. ----print(tab .. k)
  48. print_table(v, i + 1)
  49. else
  50. if type(v) == "boolean" then
  51. ----print(tab .. k .. ": " ..((v and "true") or "false"))
  52. elseif type(v) == "nil" then
  53. ----print(tab .. k .. ": " .. "nil")
  54. else
  55. ----print(tab .. k .. ": " .. v)
  56. end
  57. end
  58. end
  59. end
  60. print_table(data, 0)
  61. end
  62. -- lua层初始化类的一个构造器...
  63. function InitCtor(tb_, mt_)
  64. tb_ = tb_ or { };
  65. setmetatable(tb_, mt_);
  66. mt_.__index = mt_;
  67. return tb_;
  68. end
  69. --
  70. function DeepCopy(obj)
  71. local tempTable = { };
  72. local function copy(obj)
  73. if type(obj) ~= "table" then
  74. return obj
  75. elseif tempTable[obj] then
  76. return tempTable[obj]
  77. end
  78. local newTable = { };
  79. tempTable[obj] = newTable;
  80. for index, value in pairs(obj) do
  81. newTable[copy(index)] = copy(value);
  82. end
  83. return setmetatable(newTable, getmetatable(obj))
  84. end
  85. return copy(obj);
  86. end
  87. --浅拷贝,仅拷贝非table数据。.
  88. function ShallowCopy(orig)
  89. local copy
  90. if type(orig) == "table" then
  91. copy = {}
  92. for orig_key, orig_value in pairs(orig) do
  93. if type(orig_value) == "table" then
  94. ----print("table cant ShallowCopy" ..orig_key);
  95. else -- number, string, boolean, etc
  96. copy[orig_key] = orig_value
  97. end
  98. end
  99. else -- number, string, boolean, etc
  100. copy = orig
  101. end
  102. return copy
  103. end
  104. function TableRelease(orig)
  105. local function release(tb)
  106. for k, v in pairs(tb) do
  107. if type(v) == "table" then
  108. release(tb[k]);
  109. end
  110. tb[k] = nil;
  111. end
  112. end
  113. return release(orig);
  114. end
  115. -- 设置table里面的初始字段的值为dv_(default value)...
  116. -- tips:如果有大量table要设置初始值,请使用下面的SetDefaultValue3Agrs来设置
  117. function SetDefaultValue2Agrs(t_, dv_)
  118. local mt = { __index = function() return dv_ end };
  119. setmetatable(t_, mt);
  120. end
  121. -- local mt = {__index = function() return t_[k_] end}
  122. -- function SetDefaultValue3Agrs(t_,k_,dv_)
  123. -- t_[k_] = dv_;
  124. -- setmetatable(t_,mt);
  125. -- end
  126. -- 提供给你一个默认值的原表mt_,然你自己脚本设置此原表 setmetatable(tbMe,mt_)
  127. function SetDefaultValue3Agrs(mt_, k_, dv_)
  128. mt_.__index = function(table, k_)
  129. mt_[k_] = dv_;
  130. return mt_[k_];
  131. end
  132. end
  133. -- 提供允许通过table里面的key来进行排序函数(默认 ASC)...
  134. function Pairs2Key(t_, f_)
  135. local tempTb = { }
  136. for n in pairs(t_) do
  137. tempTb[#tempTb + 1] = n
  138. end
  139. -- ASC or DESC
  140. table.sort(tempTb, f_)
  141. local i = 0
  142. return function()
  143. i = i + 1;
  144. return tempTb[i], t_[tempTb[i]]
  145. end
  146. end
  147. -- 提供字符串分割返回字符串list(如StringSplit("123|wwe|eee", "|")之后,返回的list中有123,wwe,eee三个元素)
  148. function StringSplit(szFullString, szSeparator)
  149. local nSplitArray = { }
  150. -- 长度
  151. local n = string.len(szFullString)
  152. local start = 1
  153. while start <= n do
  154. -- find 'next szSeparator'
  155. local i = string.find(szFullString, szSeparator, start)
  156. if i == nil then
  157. table.insert(nSplitArray, string.sub(szFullString, start, n))
  158. break
  159. end
  160. table.insert(nSplitArray, string.sub(szFullString, start, i - 1))
  161. if i == n then
  162. table.insert(nSplitArray, "")
  163. break
  164. end
  165. start = i + 1
  166. end
  167. return nSplitArray
  168. end
  169. function GetLuaBitwiseFlag(tb_)
  170. return LuaUtilityHelper.LuaBitwise(tb_)
  171. end
  172. function GetLuaBitwiseTable(flag_)
  173. local flagAll = LuaUtilityHelper.LuaBitwiseLuaTable(flag_);
  174. return StringSplit(flagAll,";");
  175. end
  176. local eventGUID = 0;
  177. local eventValue = { };
  178. function EventGUID2Tb(tb_)
  179. eventGUID = eventGUID + 1;
  180. eventValue[eventGUID] = tb_;
  181. return eventGUID;
  182. end
  183. function EventValue2GUID(eventGUID_)
  184. if eventValue[eventGUID_] ~= nil then
  185. local tb = eventValue[eventGUID_];
  186. eventValue[eventGUID_] = nil;
  187. return tb;
  188. end
  189. end
  190. -- 判断函数隐藏参数self是否为空...
  191. function IsSelfNil(self_)
  192. if self_ == nil or self_ == -1 then
  193. return true;
  194. end
  195. return false;
  196. end
  197. --异步分发执行函数
  198. function DispatchAsyn(func_,argsTb_)
  199. local OnCoroutine = function()
  200. Yield(null)
  201. func_(argsTb_);
  202. end
  203. StartCoroutine(OnCoroutine);
  204. end
  205. --注册一个延迟一帧执行的函数.
  206. function RegisterDelayStep(func_)
  207. local coDelay = nil;
  208. local OnDelay = function()
  209. coroutine.step();
  210. func_();
  211. coroutine.stop(coDelay);
  212. end
  213. coDelay = coroutine.start(OnDelay);
  214. return coDelay
  215. end
  216. function CreateEnumTable(tb_, index_)
  217. local enumTb = { };
  218. local enumIndex = index_ or 0;
  219. for k, v in ipairs(tb_) do
  220. enumTb[v] = enumIndex + k;
  221. end
  222. return enumTb;
  223. end
  224. -- 是否是百分比型属性...
  225. function IsGamePropPer(argPropIdx)
  226. if argPropIdx >= EBattleAttribute.Atk and argPropIdx <= EBattleAttribute.Speed then
  227. return false;
  228. elseif argPropIdx == EBattleAttribute.MaxPower then
  229. return false;
  230. elseif argPropIdx == EBattleAttribute.MaxEnnrgy then
  231. return false;
  232. else
  233. return true;
  234. end
  235. end
  236. -- 删除table数组表中元素
  237. function RemoveElementByVal(table, value, removeAll)
  238. local rmCount = 0
  239. for i = 1, #table do
  240. if list[i - rmCount] == value then
  241. table.remove(table, i - rmCount)
  242. if removeAll then
  243. rmCount = rmCount + 1
  244. else
  245. break
  246. end
  247. end
  248. end
  249. end
  250. --取当前的系统时区..
  251. function GetTimeZone(bHour_)
  252. if bHour_ then
  253. return os.difftime(os.time(), os.time(os.date("!*t", os.time())))/3600;
  254. else
  255. return os.difftime(os.time(), os.time(os.date("!*t", os.time())));
  256. end
  257. end
  258. --UTC标准时间转换成当地时间..
  259. function GetLocalTime(utcTime_)
  260. return (utcTime_ + os.difftime(os.time(), os.time(os.date("!*t", os.time()))));
  261. end
  262. -- 把时间X秒,转化为xx天xx时xx分xx秒 的形式..
  263. function ConvertTimeForm(second)
  264. local timeDay = math.floor(second / 86400);
  265. local timeHour = math.fmod(math.floor(second / 3600), 24);
  266. local timeMinute = math.fmod(math.floor(second / 60), 60);
  267. local timeSecond = math.fmod(second, 60);
  268. return timeDay, timeHour, timeMinute, timeSecond;
  269. end
  270. -- 把时间X秒,转化为xx时xx分xx秒 的形式..
  271. function FormatTime(time)
  272. local hour = math.floor(time / 3600);
  273. local minute = math.fmod(math.floor(time / 60), 60);
  274. local second = math.fmod(time, 60);
  275. if hour > 0 then
  276. return string.format("%02d:%02d:%02d", hour, minute, second);
  277. else
  278. return string.format("%02d:%02d", minute, second);
  279. end
  280. end
  281. function FormatTime2(time)
  282. local hour = math.floor(time / 3600);
  283. local minute = math.fmod(math.floor(time / 60), 60);
  284. local second = math.fmod(time, 60);
  285. if hour > 0 then
  286. return string.format("%02d:%02d:%02d", hour, minute, second);
  287. else
  288. return string.format("00:%02d:%02d", minute, second);
  289. end
  290. end
  291. -- 把时间X秒,xx分xx秒 的形式..
  292. function FormatTimeMS(time)
  293. local minute = math.fmod(math.floor(time / 60), 60);
  294. local second = math.fmod(time, 60);
  295. local rtTime = string.format("%02d:%02d", minute, second);
  296. return rtTime;
  297. end
  298. -- 获取table长度(处理#识别不出的表单)
  299. function CountTB(tbl)
  300. local count = 0;
  301. if (tbl) then
  302. for k, v in pairs(tbl) do
  303. count = count + 1;
  304. end
  305. end
  306. return count;
  307. end
  308. --取随机数..
  309. function GetRandomNumber(nMin_, nMax_)
  310. math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6)));
  311. return math.random(nMin_, nMax_);
  312. end
  313. --获取中英文混合字符串的实际字符个数...
  314. function GetActualStringLength(str_)
  315. local lenInByte = #str_
  316. local width = 0
  317. local i = 1
  318. while i <= lenInByte do
  319. local curByte = string.byte(str_, i)
  320. local byteCount = 1
  321. if curByte>0 and curByte<=127 then
  322. byteCount = 1
  323. elseif curByte>=192 and curByte<223 then
  324. byteCount = 2
  325. elseif curByte>=224 and curByte<240 then
  326. byteCount = 3
  327. elseif curByte>=240 and curByte<=247 then
  328. byteCount = 4
  329. end
  330. i = i + byteCount
  331. width = width + 1
  332. end
  333. return width
  334. end
  335. --获取中英文混合字符串中文字符(含标点)的个数(主要用于计算全宽字符的个数)...
  336. function GetChineseCharCount(str_)
  337. local lenInByte = #str_
  338. local width = 0
  339. local i = 1
  340. while i <= lenInByte do
  341. local curByte = string.byte(str_, i)
  342. local byteCount = 1
  343. if curByte>0 and curByte<=127 then
  344. byteCount = 1
  345. elseif curByte>=192 and curByte<223 then
  346. byteCount = 2
  347. elseif curByte>=224 and curByte<240 then
  348. byteCount = 3
  349. width = width + 1
  350. elseif curByte>=240 and curByte<=247 then
  351. byteCount = 4
  352. end
  353. i = i + byteCount
  354. end
  355. return width
  356. end
  357. --utf8字符长度..
  358. local function Utf8CharSize(char_)
  359. if not char_ then
  360. return 0;
  361. elseif char_ > 240 then
  362. return 4;
  363. elseif char_ > 225 then
  364. return 3;
  365. elseif char_ > 192 then
  366. return 2;
  367. else
  368. return 1;
  369. end
  370. end
  371. --utf8字符串长度(数字字母算1个长度)..
  372. function Utf8StrLen(str_)
  373. local len = 0;
  374. local currentIndex = 1;
  375. while currentIndex <= #str_ do
  376. local char = string.byte(str_, currentIndex);
  377. currentIndex = currentIndex + Utf8CharSize(char);
  378. len = len +1;
  379. end
  380. return len;
  381. end
  382. --utf8字符串长度(2个数字字母算1个长度)..
  383. function Utf8StrLen2(str_)
  384. local fullCharCount = GetChineseCharCount(str_);
  385. local totalCharCount = GetActualStringLength(str_);
  386. local nameCharWidth = fullCharCount + (totalCharCount - fullCharCount) / 2;
  387. return nameCharWidth;
  388. end
  389. -- 截取utf8 字符串..
  390. -- str_: 要截取的字符串..
  391. -- startChar_: 开始字符下标,从1开始..
  392. -- numChars_: 要截取的字符长度..
  393. function Utf8StrSub(str_, startChar_, numChars_)
  394. local startIndex = 1;
  395. while startChar_ > 1 do
  396. local char = string.byte(str_, startIndex);
  397. startIndex = startIndex + Utf8CharSize(char);
  398. startChar_ = startChar_ - 1;
  399. end
  400. local currentIndex = startIndex;
  401. while numChars_ > 0 and currentIndex <= #str_ do
  402. local char = string.byte(str_, currentIndex);
  403. currentIndex = currentIndex + Utf8CharSize(char);
  404. numChars_ = numChars_ -1;
  405. end
  406. return str_:sub(startIndex, currentIndex - 1);
  407. end
  408. --取整a_/b_..
  409. function GetMathInteger(a_, b_)
  410. local a = a_;
  411. local b = b_;
  412. local num = 0;
  413. if a < b then
  414. return 0;
  415. elseif b==0 then
  416. return a;
  417. elseif a == b then
  418. return 1;
  419. elseif a > b then
  420. while a >= b do
  421. a = a - b;
  422. num = num + 1;
  423. end
  424. end
  425. return num;
  426. end
  427. --取余a_%b_..
  428. function GetMathRemainder(a_, b_)
  429. local a = a_;
  430. local b = b_;
  431. if a < b then
  432. return a;
  433. elseif b==0 then
  434. return 0;
  435. elseif a == b then
  436. return 0;
  437. elseif a > b then
  438. while a >= b do
  439. a = a - b;
  440. end
  441. end
  442. return a;
  443. end
  444. -- Lua内存记录功能
  445. local preLuaSnapshot = nil
  446. function SnapshotLuaMemory(sender, menu, value)
  447. -- 首先统计Lua内存占用的情况
  448. DebugHelper.LogError("GC前, Lua内存为:"..collectgarbage("count").."Kb")
  449. --collectgarbage()
  450. ------print("GC后, Lua内存为:", collectgarbage("count"))
  451. local snapshot = require "snapshot"
  452. local curLuaSnapshot = snapshot.snapshot()
  453. local ret = {}
  454. local count = 0
  455. if preLuaSnapshot ~= nil then
  456. for k,v in pairs(curLuaSnapshot) do
  457. if preLuaSnapshot[k] == nil then
  458. count = count + 1
  459. ret[k] = v
  460. end
  461. end
  462. end
  463. for k, v in pairs(ret) do
  464. DebugHelper.LogError(tostring(k).." -- "..v);
  465. end
  466. DebugHelper.LogError ("Lua snapshot diff object count is " .. count)
  467. preLuaSnapshot = curLuaSnapshot
  468. end
  469. local profiler = nil;
  470. function StartLuaProfiler()
  471. if profiler == nil then
  472. profiler = require "profiler"
  473. end
  474. if profiler ~= nil then
  475. profiler.start("profiler.txt")
  476. end
  477. end
  478. function StopLuaProfiler()
  479. if profiler ~= nil then
  480. profiler.stop()
  481. end
  482. end
  483. -- 对数值进行四舍五入,如果不是数值则返回 0
  484. -- @function [parent=#math] round
  485. -- @param number value 输入值
  486. -- @return number#number
  487. function math.round(value)
  488. value = tonumber(value) or 0
  489. return math.floor(value + 0.5)
  490. end
  491. --判断table是否为空
  492. function table_is_empty(t)
  493. return _G.next( t ) == nil
  494. end
  495. --A,B夹角[-180 ~ 180]
  496. function GetAngleByPos(p1, p2)
  497. local w = p2.x - p1.x;
  498. local h = p2.y - p1.y;
  499. return math.atan2(w, h)*180/math.pi;
  500. end
  501. --取两点的中间世界坐标
  502. function GetMiddleOfTwoPoints(p1, p2)
  503. local x = p1.x + (p2.x - p1.x) / 2;
  504. local y = p1.y + (p2.y - p1.y) / 2;
  505. local z = p1.z + (p2.z - p1.z) / 2;
  506. return NewVector3(x, y, z);
  507. end
  508. function string.formatbykey(strI18Nkey, ...)
  509. local i18nStr = I18N.SetLanguageValue(strI18Nkey,...)
  510. return i18nStr
  511. end
  512. -- 表中是否含有该value
  513. function IsTableHasValue(tb_,val_)
  514. if tb_ ~= nil then
  515. for k,v in pairs(tb_) do
  516. if v == val_ then
  517. return true
  518. end
  519. end
  520. end
  521. return false
  522. end
  523. --判断字符是否是中文
  524. local function CheckCharIsChinese(char_)
  525. if #char_<3 then
  526. return false;
  527. end
  528. local c1=string.byte(char_,1)
  529. if c1>229 and c1<233 then
  530. return true;
  531. end
  532. local c2=string.byte(char_,2)
  533. local c3=string.byte(char_,3)
  534. local total=c1*256*256+c2*256+c3
  535. if 228*256*256+184*256+128<=total and 233*256*256+190*256+165>=total then
  536. return true;
  537. else
  538. return false;
  539. end
  540. end
  541. --判断字符串是否是全中文
  542. function CheckStringIsChinese(s)
  543. local f = '[%z\1-\127\194-\244][\128-\191]*';
  544. for v in string.gmatch(s,f) do
  545. if not CheckCharIsChinese(v) then
  546. return false;
  547. end
  548. end
  549. return true;
  550. end
  551. --根据分隔符分隔字符串
  552. function SplitFunc( str,reps )
  553. local resultStrList = {}
  554. string.gsub(str,'[^'..reps..']+',function ( w )
  555. table.insert(resultStrList,w)
  556. end)
  557. return resultStrList
  558. end