Utils.lua.txt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. local parentOffset = csp or ""
  18. local propertyOffset = parentOffset .. "\t"
  19. local str = ""
  20. str = str .. "{" .. "\n"
  21. for k, v in pairs(tab) do
  22. if type(v)=="table" then
  23. str = str .. propertyOffset.. k .. " = " .. Utils:Table2String(v, propertyOffset) .. ",\n"
  24. else
  25. str = str .. propertyOffset.. k .. " = '" .. v .. "',\n"
  26. end
  27. end
  28. str = string.sub(str, 1, string.len(str) - string.len(",\n")) .. "\n"
  29. str = str .. parentOffset .. "}"
  30. return str
  31. end
  32. function Utils:TableDeepCopy(tSrcTable)
  33. local tSearchTable = {}
  34. local function _copy(tSrcTable)
  35. if type(tSrcTable) ~= "table" then
  36. return tSrcTable
  37. end
  38. local tNewTable = {}
  39. tSearchTable[tSrcTable] = tNewTable
  40. for k, v in pairs(tSrcTable) do
  41. tNewTable[_copy(k)] = _copy(v)
  42. end
  43. return setmetatable(tNewTable, getmetatable(tSrcTable))
  44. end
  45. return _copy(tSrcTable)
  46. end
  47. function Utils:TimeNow()
  48. local nTimeNow = os.time()
  49. local szTimeFormat = os.date("%Y-%m-%d %H:%M:%S", nTimeNow)
  50. return nTimeNow, szTimeFormat
  51. end
  52. function Utils:FormatHMS(nTime)
  53. local nHour = 0
  54. local nMin = 0
  55. local nSecond = 0
  56. nHour = math.floor(nTime / 3600)
  57. nMin = math.floor((nTime - nHour * 3600) / 60)
  58. nSecond = math.floor(nTime % 60)
  59. local szTime = ""
  60. szTime = szTime .. string.format("%02d:", nHour)
  61. szTime = szTime .. string.format("%02d:", nMin)
  62. szTime = szTime .. string.format("%02d", nSecond)
  63. return szTime
  64. end
  65. function Utils:Clamp(value, nMaxValue, nMinValue)
  66. if value < nMinValue then
  67. return nMinValue
  68. end
  69. if value > nMaxValue then
  70. return nMaxValue
  71. end
  72. return value
  73. end
  74. do -- 队列
  75. local QueueLogic = {}
  76. function QueueLogic:Length()
  77. return self.last - self.first
  78. end
  79. function QueueLogic:Enqueue(val)
  80. self[self.last] = val
  81. self.last = self.last + 1
  82. end
  83. function QueueLogic:Dequeue()
  84. if (self:Length() <= 0) then
  85. return nil
  86. end
  87. local ret = self[self.first]
  88. self[self.first] = nil
  89. self.first = self.first + 1
  90. return ret
  91. end
  92. function QueueLogic:Peek()
  93. if (self:Length() <= 0) then
  94. return nil
  95. end
  96. return self[self.first]
  97. end
  98. function Utils:CreateQueue()
  99. local queue = {}
  100. setmetatable(queue, {__index = QueueLogic})
  101. queue.first = 0
  102. queue.last = 0
  103. return queue
  104. end
  105. end
  106. return Utils