Utils.lua.txt 4.6 KB

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