Utils.lua.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. local Utils = {}
  2. function Utils:GetScriptNameByScriptPath(scriptPath)
  3. local startIndex = string.find(scriptPath, "%a+.lua")
  4. local endIndex = string.find(scriptPath, ".lua") - 1
  5. local scriptName = string.sub(scriptPath, startIndex, endIndex)
  6. return scriptName
  7. end
  8. function Utils:OutputLuaTable(tData, szPrefix)
  9. if not szPrefix then szPrefix = "" end
  10. for _, v in pairs(tData) do
  11. if type(v) == "table" then
  12. Utils:OutputLuaTable(v, "\t")
  13. end
  14. end
  15. end
  16. function Utils:Table2String(tab, csp)
  17. if not tab then
  18. return
  19. end
  20. local parentOffset = csp or ""
  21. local propertyOffset = parentOffset .. "\t"
  22. local str = ""
  23. str = str .. "{" .. "\n"
  24. for k, v in pairs(tab) do
  25. local t = type(v)
  26. if k=="__index" then
  27. elseif t=="table" then
  28. str = str .. propertyOffset.. k .. " = " .. Utils:Table2String(v, propertyOffset) .. ",\n"
  29. elseif t == "number" then
  30. str = str .. propertyOffset.. k .. " = '" .. v .. "',\n"
  31. elseif t == "userdata" or t == "function" then
  32. str = str .. propertyOffset.. k .. " = '" .. t .. "',\n"
  33. else
  34. -- str = str .. propertyOffset.. k .. " = '" .. t .. "',\n"
  35. -- str = str .. propertyOffset.. k .. " = '" .. v .. "',\n"
  36. str = str .. propertyOffset.. k .. " = '" .. string.format("%q", v) .. "',\n"
  37. end
  38. end
  39. local metatable = getmetatable(tab)
  40. if metatable ~= nil then
  41. str = str .. propertyOffset.. " meta = " .. Utils:Table2String(metatable, propertyOffset) .. ",\n"
  42. -- str = str .. propertyOffset.. "meta = " .. ",\n"
  43. end
  44. str = string.sub(str, 1, string.len(str) - string.len(",\n")) .. "\n"
  45. str = str .. parentOffset .. "}"
  46. return str
  47. end
  48. function Utils:TableDeepCopy(tSrcTable)
  49. local tSearchTable = {}
  50. local function _copy(tSrcTable)
  51. if type(tSrcTable) ~= "table" then
  52. return tSrcTable
  53. end
  54. local tNewTable = {}
  55. tSearchTable[tSrcTable] = tNewTable
  56. for k, v in pairs(tSrcTable) do
  57. tNewTable[_copy(k)] = _copy(v)
  58. end
  59. return setmetatable(tNewTable, getmetatable(tSrcTable))
  60. end
  61. return _copy(tSrcTable)
  62. end
  63. function Utils:GetBytes(szChar)
  64. if not szChar then
  65. return 0
  66. end
  67. local nCode = string.byte(szChar)
  68. if nCode < 127 then
  69. return 1
  70. elseif nCode <= 223 then
  71. return 2
  72. elseif nCode <= 239 then
  73. return 3
  74. elseif nCode <= 247 then
  75. return 4
  76. else
  77. return 0
  78. end
  79. end
  80. function Utils:Sub(str, startIndex, endIndex)
  81. local tempStr = str
  82. local byteStart = 1 -- string.sub截取的开始位置
  83. local byteEnd = -1 -- string.sub截取的结束位置
  84. local index = 0 -- 字符记数
  85. local bytes = 0 -- 字符的字节记数
  86. startIndex = math.max(startIndex, 1)
  87. endIndex = endIndex or -1
  88. while string.len(tempStr) > 0 do
  89. if index == startIndex - 1 then
  90. byteStart = bytes+1;
  91. elseif index == endIndex then
  92. byteEnd = bytes;
  93. break;
  94. end
  95. bytes = bytes + Utils:GetBytes(tempStr)
  96. tempStr = string.sub(str, bytes+1)
  97. index = index + 1
  98. end
  99. return string.sub(str, byteStart, byteEnd)
  100. end
  101. function Utils:TimeNow()
  102. local nTimeNow = os.time()
  103. local szTimeFormat = os.date("%Y-%m-%d %H:%M:%S", nTimeNow)
  104. return nTimeNow, szTimeFormat
  105. end
  106. function Utils:TimeMonthFormat()
  107. local nTimeNow = os.time()
  108. local szTimeFormat = os.date("%Y-%m", nTimeNow)
  109. return szTimeFormat
  110. end
  111. function Utils:FormatHMS(nTime)
  112. local nHour = 0
  113. local nMin = 0
  114. local nSecond = 0
  115. nHour = math.floor(nTime / 3600)
  116. nMin = math.floor((nTime - nHour * 3600) / 60)
  117. nSecond = math.floor(nTime % 60)
  118. local szTime = ""
  119. szTime = szTime .. string.format("%02d:", nHour)
  120. szTime = szTime .. string.format("%02d:", nMin)
  121. szTime = szTime .. string.format("%02d", nSecond)
  122. return szTime
  123. end
  124. function Utils:Clamp(value, nMaxValue, nMinValue)
  125. if value < nMinValue then
  126. return nMinValue
  127. end
  128. if value > nMaxValue then
  129. return nMaxValue
  130. end
  131. return value
  132. end
  133. -- szJsonPath: /xxx.json todo 待完善
  134. function Utils:ReadStreamAssetsJson(szJsonPath)
  135. local szJsonFullPath = CS.Application.streamingAssetsPath .. szJsonPath
  136. local streamReader = CS.StreamReader(szJsonFullPath)
  137. -- local szJsonContent =
  138. end
  139. do -- 队列
  140. local QueueLogic = {}
  141. function QueueLogic:Length()
  142. return self.last - self.first
  143. end
  144. function QueueLogic:Enqueue(val)
  145. self[self.last] = val
  146. self.last = self.last + 1
  147. end
  148. function QueueLogic:Dequeue()
  149. if (self:Length() <= 0) then
  150. return nil
  151. end
  152. local ret = self[self.first]
  153. self[self.first] = nil
  154. self.first = self.first + 1
  155. return ret
  156. end
  157. function QueueLogic:Peek()
  158. if (self:Length() <= 0) then
  159. return nil
  160. end
  161. return self[self.first]
  162. end
  163. function Utils:CreateQueue()
  164. local queue = {}
  165. setmetatable(queue, {__index = QueueLogic})
  166. queue.first = 0
  167. queue.last = 0
  168. return queue
  169. end
  170. end
  171. function Utils:PCallOrError(func)
  172. local status, error = pcall(func)
  173. if not status then
  174. CS.ZLog.Log.Error(error)
  175. end
  176. end
  177. return Utils