| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- local Utils = {}
- function Utils:GetScriptNameByScriptPath(scriptPath)
- local startIndex = string.find(scriptPath, "%a+.lua")
- local endIndex = string.find(scriptPath, ".lua") - 1
- local scriptName = string.sub(scriptPath, startIndex, endIndex)
- return scriptName
- end
- function Utils:OutputLuaTable(tData, szPrefix)
- if not szPrefix then szPrefix = "" end
- for _, v in pairs(tData) do
- if type(v) == "table" then
- Utils:OutputLuaTable(v, "\t")
- end
- end
- end
- function Utils:Table2String(tab, csp)
- if not tab then
- return
- end
- local parentOffset = csp or ""
- local propertyOffset = parentOffset .. "\t"
- local str = ""
- str = str .. "{" .. "\n"
- for k, v in pairs(tab) do
- if type(v)=="table" then
- str = str .. propertyOffset.. k .. " = " .. Utils:Table2String(v, propertyOffset) .. ",\n"
- else
- -- str = str .. propertyOffset.. k .. " = '" .. v .. "',\n"
- str = str .. propertyOffset.. k .. " = '" .. string.format("%q", v) .. "',\n"
- end
- end
- str = string.sub(str, 1, string.len(str) - string.len(",\n")) .. "\n"
- str = str .. parentOffset .. "}"
- return str
- end
- function Utils:TableDeepCopy(tSrcTable)
- local tSearchTable = {}
- local function _copy(tSrcTable)
- if type(tSrcTable) ~= "table" then
- return tSrcTable
- end
- local tNewTable = {}
- tSearchTable[tSrcTable] = tNewTable
- for k, v in pairs(tSrcTable) do
- tNewTable[_copy(k)] = _copy(v)
- end
- return setmetatable(tNewTable, getmetatable(tSrcTable))
- end
- return _copy(tSrcTable)
- end
- function Utils:GetBytes(szChar)
- if not szChar then
- return 0
- end
- local nCode = string.byte(szChar)
- if nCode < 127 then
- return 1
- elseif nCode <= 223 then
- return 2
- elseif nCode <= 239 then
- return 3
- elseif nCode <= 247 then
- return 4
- else
- return 0
- end
- end
- function Utils:Sub(str, startIndex, endIndex)
- local tempStr = str
- local byteStart = 1 -- string.sub截取的开始位置
- local byteEnd = -1 -- string.sub截取的结束位置
- local index = 0 -- 字符记数
- local bytes = 0 -- 字符的字节记数
- startIndex = math.max(startIndex, 1)
- endIndex = endIndex or -1
- while string.len(tempStr) > 0 do
- if index == startIndex - 1 then
- byteStart = bytes+1;
- elseif index == endIndex then
- byteEnd = bytes;
- break;
- end
- bytes = bytes + Utils:GetBytes(tempStr)
- tempStr = string.sub(str, bytes+1)
- index = index + 1
- end
- return string.sub(str, byteStart, byteEnd)
- end
- function Utils:TimeNow()
- local nTimeNow = os.time()
- local szTimeFormat = os.date("%Y-%m-%d %H:%M:%S", nTimeNow)
- return nTimeNow, szTimeFormat
- end
- function Utils:TimeMonthFormat()
- local nTimeNow = os.time()
- local szTimeFormat = os.date("%Y-%m", nTimeNow)
- return szTimeFormat
- end
- function Utils:FormatHMS(nTime)
- local nHour = 0
- local nMin = 0
- local nSecond = 0
- nHour = math.floor(nTime / 3600)
- nMin = math.floor((nTime - nHour * 3600) / 60)
- nSecond = math.floor(nTime % 60)
- local szTime = ""
- szTime = szTime .. string.format("%02d:", nHour)
- szTime = szTime .. string.format("%02d:", nMin)
- szTime = szTime .. string.format("%02d", nSecond)
- return szTime
- end
- function Utils:Clamp(value, nMaxValue, nMinValue)
- if value < nMinValue then
- return nMinValue
- end
- if value > nMaxValue then
- return nMaxValue
- end
- return value
- end
- -- szJsonPath: /xxx.json todo 待完善
- function Utils:ReadStreamAssetsJson(szJsonPath)
- local szJsonFullPath = CS.Application.streamingAssetsPath .. szJsonPath
- local streamReader = CS.StreamReader(szJsonFullPath)
- -- local szJsonContent =
- end
- do -- 队列
- local QueueLogic = {}
- function QueueLogic:Length()
- return self.last - self.first
- end
- function QueueLogic:Enqueue(val)
- self[self.last] = val
- self.last = self.last + 1
- end
- function QueueLogic:Dequeue()
- if (self:Length() <= 0) then
- return nil
- end
- local ret = self[self.first]
- self[self.first] = nil
- self.first = self.first + 1
- return ret
- end
- function QueueLogic:Peek()
- if (self:Length() <= 0) then
- return nil
- end
- return self[self.first]
- end
- function Utils:CreateQueue()
- local queue = {}
- setmetatable(queue, {__index = QueueLogic})
- queue.first = 0
- queue.last = 0
- return queue
- end
- end
- return Utils
|