




已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
lua 工具类(二)local tonumber_ = tonumberfunction tonumber(v, base) return tonumber_(v, base) or 0endfunction toint(v) return math.round(tonumber(v)endfunction tobool(v) return (v = nil and v = false)endfunction totable(v) if type(v) = table then v = end return vendfunction clone(object) local lookup_table = local function _copy(object) if type(object) = table then return object elseif lookup_tableobject then return lookup_tableobject end local new_table = lookup_tableobject = new_table for key, value in pairs(object) do new_table_copy(key) = _copy(value) end return setmetatable(new_table, getmetatable(object) end return _copy(object)endfunction class(classname, super) local superType = type(super) local cls if superType = function and superType = table then superType = nil super = nil end if superType = function or (super and super._ctype = 1) then - inherited from native C+ Object cls = if superType = table then - copy fields from super print (superTyper is table); for k,v in pairs(super) do clsk = v end cls._create = super._create cls.super = super else cls._create = super cls.ctor = function() end end cls._cname = classname cls._ctype = 1 function cls.new(.) local instance = cls._create(.) - copy fields from class to native object for k,v in pairs(cls) do instancek = v end instance.class = cls instance:ctor(.) return instance end else - inherited from Lua Object if super then cls = setmetatable(cls, _index = super) cls.super = super else cls = ctor = function() end end cls._cname = classname cls._ctype = 2 - lua cls._index = cls function cls.new(.) local instance = setmetatable(, cls) instance.class = cls instance:ctor(.) return instance end end return clsendfunction import(moduleName, currentModuleName) local currentModuleNameParts local moduleFullName = moduleName local offset = 1 while true do if string.byte(moduleName, offset) = 46 then - . moduleFullName = string.sub(moduleName, offset) if currentModuleNameParts and #currentModuleNameParts 0 then moduleFullName = table.concat(currentModuleNameParts, .) . . . moduleFullName end break end offset = offset + 1 if not currentModuleNameParts then if not currentModuleName then local n,v = debug.getlocal(3, 1) currentModuleName = v end currentModuleNameParts = string.split(currentModuleName, .) end table.remove(currentModuleNameParts, #currentModuleNameParts) end return require(moduleFullName)endfunction handler(target, method) return function(.) return method(target, .) endendfunction math.round(num) return math.floor(num + 0.5)endfunction io.exists(path) local file = io.open(path, r) if file then io.close(file) return true end return falseendfunction io.readfile(path) local file = io.open(path, r) if file then local content = file:read(*a) io.close(file) return content end return nilendfunction io.writefile(path, content, mode) mode = mode or w+b local file = io.open(path, mode) if file then print(file is ok ok ok ok ok ) if file:write(content) = nil then print(file is bad bad bad bad ) return false end io.close(file) return true else return false endendfunction io.pathinfo(path) local pos = string.len(path) local extpos = pos + 1 while pos 0 do local b = string.byte(path, pos) if b = 46 then - 46 = char . extpos = pos elseif b = 47 then - 47 = char / break end pos = pos - 1 end local dirname = string.sub(path, 1, pos) local filename = string.sub(path, pos + 1) extpos = extpos - pos local basename = string.sub(filename, 1, extpos - 1) local extname = string.sub(filename, extpos) return dirname = dirname, filename = filename, basename = basename, extname = extname endfunction io.filesize(path) local size = false local file = io.open(path, r) if file then local current = file:seek() size = file:seek(end) file:seek(set, current) io.close(file) end return sizeendfunction table.nums(t) local count = 0 for k, v in pairs(t) do count = count + 1 end return countendfunction table.keys(t) local keys = if t = nil then return keys; end for k, v in pairs(t) do keys#keys + 1 = k end return keysendfunction table.values(t) local values = if t = nil then return values; end for k, v in pairs(t) do values#values + 1 = v end return valuesendfunction table.containKey( t, key ) for k, v in pairs(t) do if key = k then return true; end end return false;endfunction table.containValue( t, value ) for k, v in pairs(t) do if value = v then return true; end end return false;endfunction table.getKeyByValue( t, value ) for k, v in pairs(t) do if value = v then return k; end endendfunction table.merge(dest, src) for k, v in pairs(src) do destk = v endendfunction arrayContain( array, value) for i=1,#array do if arrayi = value then return true; end end return false;endfunction string.htmlspecialchars(input) for k, v in pairs(string._htmlspecialchars_set) do input = string.gsub(input, k, v) end return inputendstring._htmlspecialchars_set = string._htmlspecialchars_set& = &string._htmlspecialchars_set = "string._htmlspecialchars_set = string._htmlspecialchars_set = >function string.nl2br(input) return string.gsub(input, n, )endfunction string.text2html(input) input = string.gsub(input, t, ) input = string.htmlspecialchars(input) input = string.gsub(input, , ) input = string.nl2br(input) return inputendfunction string.split(str, delimiter) if (delimiter=) then return false end local pos,arr = 0, - for each divider found for st,sp in function() return string.find(str, delimiter, pos, true) end do table.insert(arr, string.sub(str, pos, st - 1) pos = sp + 1 end table.insert(arr, string.sub(str, pos) return arrendfunction string.ltrim(str) return string.gsub(str, tnr+, )endfunction string.rtrim(str) return string.gsub(str, tnr+$, )endfunction string.trim(str) str = string.gsub(str, tnr+, ) return string.gsub(str, tnr+$, )endfunction string.ucfirst(str) return string.upper(string.sub(str, 1, 1) . string.sub(str, 2)end-ignorelocal function urlencodeChar(char) return % . string.format(%02X, string.byte(c)endfunction string.urlencode(str) - convert line endings str = string.gsub(tostring(str), n, rn) - escape all characters but alphanumeric, . and - str = string.gsub(str, (%w%.%- ), urlencodeChar) - convert spaces to + symbols return string.gsub(str, , +)endfunction string.utf8len(str) local len = #str local left = len local cnt = 0 local arr = 0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc while left = 0 do local tmp = string.byte(str, -left) local i = #arr while arri do if tmp = arri then left = left - i break end i = i - 1 end cnt = cnt + 1 end return cntendfunction string.formatNumberThousands(num) local formatted = tostring(tonumber(num) local k while true do formatted, k = string.gsub(formatted, (-?%d+)(%d%d%d), %1,%2) if k = 0 then break end end return formattedendfunction print_lua_table (lua_table, indent) indent = indent or 0 for k, v in pairs(lua_table) do if type(k) = string then k = string.format(%q, k) end local szSuffix = if type(v) = table then szSuffix = end local szPrefix = string.rep( , indent) formatting = szPrefix.k. = .szSuffix if type(v) = table then print(formatting) print_lua_table(v, indent + 1) print(szPrefix.,) else local szValue = if type(v) = string then szValue = string.format(%q, v) else szValue = tostring(v) end print(formatting.szValue.,) end endendlocal function newCounter() local i = 0 return function() - anonymous function i = i + 1 return i endendg_id_generator = newCounter()function getNextID() local nextID nextID = g_id_generator() return nextIDendluautil = ;function luautil.serialize(tName, t, sort_parent, sort_child) local mark= local assign= local function ser_table(tbl,parent) marktbl=parent local tmp= local sortList = ; for k,v in pairs(tbl) do sortList#sortList + 1 = key=k, value=v; end if tostring(parent) = ret then if sort_parent then table.sort(sortList, sort_parent); end else if sort_child then table.sort(sortList, sort_child); end end for i = 1, #sortList do local info = sortListi; local k = info.key; local v = info.value; local key= type(k)=number and .k. or k; if type(v)=table then local dotkey= parent.(type(k)=number and key or .key) if markv then table.insert(assign, n. dotkey.=.markv) else table.insert(tmp, n.key.=.ser_table(v,dotkey) end else if type(v) = string then table.insert(tmp, n. key.=.v.); else table.insert(tmp, n. key.=.tostring(v) . n); end end end return .table.concat(tmp,).n; end return do local . tName . = nn.ser_table(t, tName).table.concat(assign, ).nn return . tName . end end function luautil.split(str, delimiter) if (delimiter=) then return false end local pos,arr = 0, - for each divider found for st,sp in function() return string.find(str, delimiter, pos, true) end do table.insert(arr, string.sub(str, pos, st - 1) pos = sp + 1 end table.insert(arr, string.sub(str, pos) return arr end function luautil.writefile(str, file) os.remove(file); local file=io.open(file,ab); local len = string.len(str); local tbl = luautil.split(str, n); for i = 1, #tbl do file:write(tbli.n); end file:close(); end function luautil.readfile( strData ) local f = loadstring(strData) if f then return f() end endfunction Json2Lua (fileName,filePath, desFilePath) local jsonString = io.readfile(filePath)-myfile:read(*a) t = CJson.decode(jsonString) if t then t = luautil.serialize(fileName,t); end if t then luautil.writefile(t, desFilePath) end end function performWithDelay(callback, delay) local handle handle = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(function() CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(handle) handle = nil callback(); end , delay , false)end- 参数:待分割的字符串,分割字符- 返回:子串表.(含有空串)function lua_string_split(str, split_char) local sub_str_tab = ; while (true) do local pos = string.find(str, split_char); if (not pos) then sub_str_tab#sub_str_tab + 1 = str; break; end local sub_str = string.sub(str, 1, pos - 1); sub_str_tab#sub_str_tab + 1 = sub_str; str = string.sub(str, pos + 1, #str); end return sub_str_tab;end-去除扩展名function stripExtension(filename) local idx = filename:match(.+()%.%w+$) if(idx) then return filename:sub(1, idx-1) else return filename endend-获取路径function stripfilename(filename) return string.match(filename, (.+)/*%.%w+$) -*nix system -return string.match(filename, “(.+)*%.%w+$”) windowsend-获取文件名function strippath(filename) return string.match(filename, .+/(/*%.%w+)$) - *nix system -return string.match(filename, “.+(*%.%w+)$”) *nix systemend- 生产配置文件function genCfg( ) local assets = require(res.Asset) local sceneKey = Scene local commonKey = common local className = textures, atlas, sounds, animations local config = ; for i=1,#assets do local k = assetsi.k; local v = assetsi.v; local pathnames = lua_string_split(v, /); local fileName = pathnames#pathnames; local name = stripExtension(fileName); local pathType = fileName:match(.+%.(%w+)$); local sceneType; local commonType; local cur; for i=1,#pathnames do cur = pathnamesi; - 1. 检查该路径是否是场景 if string.find(cur, sceneKey) then sceneType = cur; if not configsceneType then configsceneType = ; end end if string.find(cur, commonKey) then commonType = cur; if not configcommonType then configcommonType = ; end end - 2. 查找资源类型 - 2.1 是场景资源 if sceneType then for n=1,#className do local t = classNamen; if cur = t then if not configsceneTypet then configsceneTypet = ; end if (t = atlas and pathType = plist) then configsceneTypetname = v; end if t = atlas and pathType = ExportJson then if not configsceneTypejson then configsceneTypejson = end configsceneTypejsonname = v; end if (t = textures) then configsceneTypetname = v; end if (t = animations and pathType = xml) then configsceneTypetname = v; end if (t = sounds) then configsceneTypetname = v; end end end - 2.2 是公用资源 elseif commonType then for n=1,#className do local t = classNamen; if c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 多污染物协同脱附中反吹频率与二次污染生成的阈值关系
- 多学科交叉视角下的热力学-流体力学耦合仿真模型构建
- 复杂电磁环境下车载制波球定向能武器效能衰减机制
- 基于边缘计算的倾角数据安全传输与隐私保护框架构建
- 基于物联网的分布式电能校准网络架构设计及抗干扰策略
- 基于机器视觉的插头座插拔过程异常姿态识别与补偿控制
- 基于数字孪生的花键啮合应力场动态仿真与优化
- 基于数字孪生的夹钳系统全生命周期能效优化模型构建
- 企业财务风险管理识别与评估表
- 项目式学习背景下的小学数学教学评价研究
- 设备故障分析报告范文
- 2024年国家网络安全宣传周网络安全知识培训讲座
- 上海市内分泌科临床质控手册
- 教科版六年级科学上册知识清单(新版)
- 传感器技术与应用电子教案
- 个人出行安全承诺书合同(2篇)
- DB11-T 2021-2022 12345市民服务热线服务与管理规范
- 数学思想方法及其教学课件学习教案
- 安永校招在线测评真题
- 人教版(2024)小学信息科技 三年级 第3课《体验人机交互》教学设计
- 《材料力学性能》课程教学大纲
评论
0/150
提交评论