Utils.lua.txt 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. return Utils