LuaUtil.lua 10.6 KB
Newer Older
L
linxinfa 已提交
1 2 3 4 5 6
-- region *.lua
-- Date
-- 此文件由[BabeLua]插件自动生成

-- 由于tolua重写了__index方法, 所以不能直接扩展 LuaFramework.Util 只能用名称LuaUtil

L
linxinfa 已提交
7
LuaUtil = LuaUtil or {}
L
linxinfa 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

local ONE_HOUR_SEC = 3600
-- 一天的秒数
local ONE_DAY_SEC = 86400
-- 一周的秒数
local ONE_WEEK_SEC = 604800
-- 两周的秒数
local TWO_WEEKS_SEC = 1209600

LuaUtil.ONE_DAY_SEC = ONE_DAY_SEC
LuaUtil.ONE_WEEK_SEC = ONE_WEEK_SEC
LuaUtil.TWO_WEEKS_SEC = TWO_WEEKS_SEC

local EventDispatcher = EventDispatcher.instance

function LuaUtil.RegistEvent(eventName, handle)
    EventDispatcher:Regist(eventName, handle)
end

function LuaUtil.UnRegistEvent(eventName, handle)
    EventDispatcher:UnRegist(eventName, handle)
end

function LuaUtil.FireEvent(eventName, ...)
    EventDispatcher:DispatchEvent(eventName, ...)
end

-- 获取table长度
function LuaUtil.TableCount(t)
L
linxinfa 已提交
37 38 39
    if nil == t then
        return 0
    end
L
linxinfa 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

    local cnt = 0
    for _ in pairs(t) do
        cnt = cnt + 1
    end
    return cnt
end

local function PrintSpace(p, count)
    for ii = 1, count do
        p("    ")
    end
end

local function PrintTable2(o, f, b, deep)
    if type(f) ~= "function" and f ~= nil then
        logError("expected second argument %s is a function", tostring(f))
    end
    if type(b) ~= "boolean" and b ~= nil then
        logError("expected third argument %s is a boolean", tostring(b))
    end
    p = f or io.write
    b = b or false
L
linxinfa 已提交
63
    if type(o) == "number" or type(o) == "function" or type(o) == "boolean" or type(o) == "nil" then
L
linxinfa 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        p(tostring(o))
    elseif type(o) == "string" then
        p(string.format("%q", o))
    elseif type(o) == "table" then
        p("\n")
        PrintSpace(p, deep)
        p("{\n")
        for k, v in pairs(o) do
            PrintSpace(p, deep)
            if b then
                p("[")
            end
            PrintTable2(k, p, b, deep + 1)
            if b then
                p("]")
            end
            p(" = ")
            PrintTable2(v, p, b, deep + 1)
            p(",\n")
        end
        PrintSpace(p, deep)
        p("}")
    end
end

-- 打印一个lua表 用于调试输出
function LuaUtil.PrintTable(t)
L
linxinfa 已提交
91 92 93 94 95 96 97 98 99
    local printTabStr = ""
    PrintTable2(
        t,
        function(str)
            printTabStr = printTabStr .. str
        end,
        true,
        0
    )
L
linxinfa 已提交
100 101 102 103 104 105 106
    if printTabStr == "" then
        logError("LuaUtil.PrintTable printTabStr is nil")
    end
    log(printTabStr)
end

function LuaUtil.strToDate(s, sTime)
L
linxinfa 已提交
107
    local t = {}
L
linxinfa 已提交
108 109 110 111 112 113
    if sTime then
        t.year, t.month, t.day = s:match("(%d+)-(%d+)-(%d+)")
        t.hour, t.min, t.sec = sTime:match("(%d+):(%d+):(%d+)")
    else
        t.year, t.month, t.day, t.hour, t.min, t.sec = s:match("(%d+)-(%d+)-(%d+)%s+(%d+):(%d+):(%d+)")
    end
L
linxinfa 已提交
114 115 116
    for k, v in pairs(t) do
        t[k] = tonumber(v)
    end
L
linxinfa 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129
    return t
end

function LuaUtil.strToTime(s, sTime)
    return os.time(LuaUtil.strToDate(s, sTime))
end

-- 获取今日指定时间的时间戳
-- clock格式, 例子:5:10:05
-- nowTime:该时间戳用于指定某一天
function LuaUtil.getTodayTimestamp(clock, nowTime)
    local nowTab = os.date("*t", nowTime)
    local _, _, hour, min, sec = string.find(clock, "(%d+):(%d+):(%d+)")
L
linxinfa 已提交
130 131
    local timestamp =
        os.time {
L
linxinfa 已提交
132 133 134 135 136 137 138 139 140 141
        year = nowTab.year,
        month = nowTab.month,
        day = nowTab.day,
        hour = hour,
        min = min,
        sec = sec
    }

    return timestamp
end
L
linxinfa 已提交
142

L
linxinfa 已提交
143 144 145 146
-- 通过距零点的秒数获取时间戳
function LuaUtil.GetTodayTimestampBySec(sec)
    local H, M, S = LuaUtil.GetTimeHMS(sec)
    local nowTab = os.date("*t", os.time())
L
linxinfa 已提交
147
    local timestamp = os.time {year = nowTab.year, month = nowTab.month, day = nowTab.day, hour = H, min = M, sec = S}
L
linxinfa 已提交
148 149 150 151 152 153 154

    return timestamp
end

-- 将秒数转换为 小时, 分, 秒(只限于一天)
function LuaUtil.GetTimeHMS(sec)
    local H = math.floor(sec / ONE_HOUR_SEC)
L
linxinfa 已提交
155
    local L = sec - (H * ONE_HOUR_SEC)
L
linxinfa 已提交
156
    local M = math.floor(L / 60)
L
linxinfa 已提交
157
    local S = L - (M * 60)
L
linxinfa 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

    return H, M, S
end

-- 将秒数转换为 小时, 分, 秒(只限于一天)
function LuaUtil.GetFormatTime(sec)
    local H, M, S = LuaUtil.GetTimeHMS(sec)
    local str = ""
    if H ~= nil and H > 0 then
        str = str .. tostring(H) .. Util.GetSperateResStr(14639, 1)
    end
    if M ~= nil and M > 0 then
        str = str .. " " .. tostring(M) .. Util.GetSperateResStr(14639, 2)
    end
    if S ~= nil and S > 0 then
        str = str .. " " .. tostring(S) .. Util.GetSperateResStr(14639, 3)
    end
    return str
end

-- 获取今天的秒数(相对于0点)
function LuaUtil.GetTodaySec()
    local cur_time = os.time()
    local nowTab = os.date("*t", os.time())
L
linxinfa 已提交
182
    local zero_time = os.time {year = nowTab.year, month = nowTab.month, day = nowTab.day, hour = 0, min = 0, sec = 0}
L
linxinfa 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    return cur_time - zero_time
end

function LuaUtil.GetDefaultTimeStr(sec)
    local H, M, S = LuaUtil.GetTimeHMS(sec)
    return string.format("%02d:%02d:%02d", H, M, S)
end

--[[ 秒数转年月日时分 ]]
function LuaUtil.Second2DataTime(second)
    return os.date("%Y-%m-%d %H:%M", math.floor(second))
end
--[[ 秒数转月日时分 ]]
function LuaUtil.Second2MonthDateTime(second)
    return os.date("%m-%d %H:%M", math.floor(second))
end
--[[ 秒数转年月日 ]]
function LuaUtil.Second2Date(second)
    return os.date("%Y-%m-%d", math.floor(second))
end
--[[ 秒数转时间的table ]]
function LuaUtil.Second2DateTable(second)
    return os.date("*t", math.floor(second))
end

-- utils for string
function LuaUtil.SplitString(fullString, separator)
    local startIndex = 1
    local splitIndex = 1
L
linxinfa 已提交
212
    local splitArray = {}
L
linxinfa 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    if not fullString or not separator or string.len(fullString) == 0 then
        return splitArray
    end
    while true do
        local lastIndex = string.find(fullString, separator, startIndex)
        if not lastIndex then
            splitArray[splitIndex] = string.sub(fullString, startIndex, string.len(fullString))
            break
        end
        splitArray[splitIndex] = string.sub(fullString, startIndex, lastIndex - 1)
        startIndex = lastIndex + string.len(separator)
        splitIndex = splitIndex + 1
    end
    return splitArray
end

function LuaUtil.StringToNumberList(fromString, separator)
L
linxinfa 已提交
230
    local retList = {}
L
linxinfa 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    if fromString ~= nil and string.len(fromString) > 0 then
        local tempTable = LuaUtil.SplitString(fromString, separator)
        for k, v in pairs(tempTable) do
            if v ~= nil and string.len(v) then
                table.insert(retList, tonumber(v))
            end
        end
    end
    return retList
end
-- end of utils for string

function LuaUtil.ToNumber(strContent)
    local ret = 0
    if strContent then
        ret = tonumber(strContent)
    end
    return ret
end

function LuaUtil.IsStrNullOrEmpty(str)
    if str == nil or str == "" then
        return true
    end
    return false
end

function LuaUtil.GetTableValue(prototab, key, default)
    if nil ~= prototab[key] then
        return prototab[key]
    else
        return default
    end
end

-- utf8字符串截取
function LuaUtil.sub_chars(s, len)
L
linxinfa 已提交
268
    local ss = {}
L
linxinfa 已提交
269 270 271 272 273 274
    if len > #s then
        return s
    end
    local k = 1
    while true do
        len = len - 1
L
linxinfa 已提交
275 276 277
        if len < 0 then
            break
        end
L
linxinfa 已提交
278
        local c = string.byte(s, k)
L
linxinfa 已提交
279 280 281
        if not c then
            break
        end
L
linxinfa 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        if c < 192 then
            table.insert(ss, string.char(c))
            k = k + 1
        elseif c < 224 then
            local c1 = string.byte(s, k + 1)
            if c1 then
                table.insert(ss, string.char(c, c1))
            end
            k = k + 2
        elseif c < 240 then
            local c1 = string.byte(s, k + 1)
            local c2 = string.byte(s, k + 2)
            if c1 and c2 then
                table.insert(ss, string.char(c, c1, c2))
            end
            k = k + 3
        elseif c < 248 then
            local c1 = string.byte(s, k + 1)
            local c2 = string.byte(s, k + 2)
            local c3 = string.byte(s, k + 3)
            if c1 and c2 and c3 then
                table.insert(ss, string.char(c, c1, c2, c3))
            end
            k = k + 4
        elseif c < 252 then
            local c1 = string.byte(s, k + 1)
            local c2 = string.byte(s, k + 2)
            local c3 = string.byte(s, k + 3)
            local c4 = string.byte(s, k + 4)
            if c1 and c2 and c3 and c4 then
                table.insert(ss, string.char(c, c1, c2, c3, c4))
            end
            k = k + 5
        else
            local c1 = string.byte(s, k + 1)
            local c2 = string.byte(s, k + 2)
            local c3 = string.byte(s, k + 3)
            local c4 = string.byte(s, k + 4)
            local c5 = string.byte(s, k + 5)
            if c1 and c2 and c3 and c4 and c5 then
                table.insert(ss, string.char(c, c1, c2, c3, c4, c5))
            end
            k = k + 6
        end
    end
    return table.concat(ss)
end

--[[ 分割字符串 ]]
function LuaUtil.StringSplit(origin_str, separator)
    -- origin_str = "2"
    -- origin_str = "2,3"
    -- origin_str = "2,3,4,5,6,7,8,9"
    local mFindStartIndex = 1
    local mSplitIndex = 1
L
linxinfa 已提交
337
    local mSplitArray = {}
L
linxinfa 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    while true do
        local mFindLastIndex = string.find(origin_str, separator, mFindStartIndex)
        if mFindLastIndex == nil then
            mSplitArray[mSplitIndex] = string.sub(origin_str, mFindStartIndex, string.len(origin_str))
            break
        end

        mSplitArray[mSplitIndex] = string.sub(origin_str, mFindStartIndex, mFindLastIndex - 1)
        mFindStartIndex = mFindLastIndex + string.len(separator)
        mSplitIndex = mSplitIndex + 1
    end
    -- log("LuaUtil.StringSplit origin_str " .. origin_str)
    -- LuaUtil.PrintTable(mSplitArray)
    return mSplitArray
end

L
linxinfa 已提交
354
-- 将秒转为日、时、分、秒
L
linxinfa 已提交
355 356 357 358
function LuaUtil.GetTimeDHMS(sec)
    local D = math.floor(sec / ONE_DAY_SEC)
    local leftHour = sec - D * ONE_DAY_SEC
    local H = math.floor(leftHour / ONE_HOUR_SEC)
L
linxinfa 已提交
359
    local L = leftHour - (H * ONE_HOUR_SEC)
L
linxinfa 已提交
360
    local M = math.floor(L / 60)
L
linxinfa 已提交
361
    local S = L - (M * 60)
L
linxinfa 已提交
362 363 364
    return D, H, M, S
end

林新发's avatar
林新发 已提交
365 366 367 368 369 370 371 372
-- 克隆物体
function LuaUtil.CloneObj(obj)
    local go = GameObject.Instantiate(obj)    
    LuaUtil.SafeActiveObj(go, true)
    go.transform:SetParent(obj.transform.parent, false)
    return go
end

L
linxinfa 已提交
373
-- 判空
L
linxinfa 已提交
374 375 376 377
function LuaUtil.IsNilOrNull(obj)
    return nil == obj or null == obj
end

L
linxinfa 已提交
378
-- 安全销毁
L
linxinfa 已提交
379
function LuaUtil.SafeDestroyObj(obj)
L
linxinfa 已提交
380
    if LuaUtil.IsNilOrNull(obj) then
L
linxinfa 已提交
381 382 383 384 385 386 387
        return
    end
    GameObject.Destroy(obj.gameObject)
end

-- 安全激活或禁用
function LuaUtil.SafeActiveObj(obj, active)
L
linxinfa 已提交
388
    if LuaUtil.IsNilOrNull(obj) then
L
linxinfa 已提交
389
        return
L
linxinfa 已提交
390
    end
L
linxinfa 已提交
391
    obj.gameObject:SetActive(active)
L
linxinfa 已提交
392 393
end
-- endregion