{"id":22282788,"url":"https://github.com/zrong/lua","last_synced_at":"2025-07-28T21:32:01.324Z","repository":{"id":11841676,"uuid":"14398447","full_name":"zrong/lua","owner":"zrong","description":"A lua library by zengrong.net","archived":false,"fork":false,"pushed_at":"2020-04-09T10:28:37.000Z","size":359,"stargazers_count":166,"open_issues_count":2,"forks_count":78,"subscribers_count":18,"default_branch":"master","last_synced_at":"2023-11-07T21:16:52.344Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zrong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-14T15:12:10.000Z","updated_at":"2023-10-13T10:43:34.000Z","dependencies_parsed_at":"2022-09-23T02:41:52.883Z","dependency_job_id":null,"html_url":"https://github.com/zrong/lua","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zrong%2Flua","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zrong%2Flua/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zrong%2Flua/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zrong%2Flua/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zrong","download_url":"https://codeload.github.com/zrong/lua/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227957359,"owners_count":17847212,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-03T16:36:07.779Z","updated_at":"2024-12-03T16:36:09.220Z","avatar_url":"https://github.com/zrong.png","language":"Lua","readme":"A lua library by [zengrong.net][2]\n\n# Dependences\n\nFollowing libraries are dependented:\n\n* [lpack][3]\n* [BitOp][4]\n* [LuaSocket][6]\n\n# 1. For pure lua\n\nFor some reason in my server development, I moved some packages that my wrote in to **zr** package (They were in **cc** package before).\n\n# 2. For [quick-cocos2d-x][10]\n\nAll of the dependences were included in [quick-cocos2d-x][10].\n\nThis library has already been merged into [quick-cocos2d-x framework][1].\n\nIn [quick-cocos2d-x framework][1], these librares still use \"cc\" package name, and I won't update them.\n\n# 3. Usage\n\nYou can import them by `lib.zrong.init`, it can import all of zrong's packages into a global table named `zr` , and also import some necessary global functions:\n\n``` lua\nrequire(\"lib.zrong.init\")\n```\n\nor if you have had these necessary global functions, you can require them in your code selectively:\n\n``` lua\nutils = require(\"lib.zrong.zr.utils.init\")\nnet = {}\nnet.SocketTCP = require(\"lib.zrong.zr.net.SocketTCP\")\n```\n\nThe necessary global functions are:\n\n## In functions.lua\n\n- class\n- import\n- iskindof\n\n# 4. API list\n\n- [zr.utils.Gettext](#Gettext)\n- [zr.utils.ByteArray](#ByteArray)\n- [zr.utils.ByteArrayVarint](#ByteArrayVarint)\n- [zr.net.SocketTCP](#SocketTCP)\n- [zr.log](#log)\n\t\n\u003ca name=\"Gettext\"\u003e\n\n## zr.utils.Gettext\n\nA detailed example about [GNU gettext][9] and [Poedit][8] (in chinese): \u003chttps://blog.zengrong.net/post/using_gettext_in_lua/\u003e\n\nUsage:\n\n``` lua\nlocal Gettext = require(\"utils.Gettext\")\n\n-- Use lua io, cannot use in Android\nlocal fd,err=io.open(\"main.mo\",\"rb\")\nif not fd then return nil,err end\nlocal raw_data=fd:read(\"*all\")\nfd:close()\n\nlocal mo_data=assert(Gettext.parseData(raw_data))\nprint(mo_data[\"hello\"])\n-- 你好\nprint(mo_data[\"world\"])\n-- nil\n\n-- Then you'll get a kind of gettext function:\nlocal gettext= Gettext.gettext(raw_data)\nprint(gettext(\"hello\"))\n-- 你好\nprint(gettext(\"world\"))\n-- world\n\n-- With a slight modification this will be ready-to-use for the xgettext tool:\n\n_ = Gettext.gettext(raw_data)\nprint(_(\"hello\"))\nprint(_(\"world\"))\n```\n\n\u003ca name=\"ByteArray\"\u003e\n\n## zr.utils.ByteArray\n\nIt can serialize bytes stream like ActionScript [flash.utils.ByteArray][5]\n\nIt depends on [lpack][3].\n\nUsage:\n\n``` lua\nlocal ByteArray = zr.utils.ByteArray\n-- use lpack to write a pack\nlocal __pack = string.pack(\"\u003cbihP2\", 0x59, 11, 1101, \"\", \"中文\")\n\n-- create a ByteArray\nlocal __ba = ByteArray.new()\n\n-- ByteArray can write a lpack buffer directly\n__ba:writeBuf(__pack)\n\n-- remember, lua array started from 1\n__ba:setPos(1)\n\n-- now, you can read it like actionscript\nprint(\"ba.len:\", __ba:getLen())\nprint(\"ba.readByte:\", __ba:readByte())\nprint(\"ba.readInt:\", __ba:readInt())\nprint(\"ba.readShort:\", __ba:readShort())\nprint(\"ba.readString:\", __ba:readStringUShort())\nprint(\"ba.available:\", __ba:getAvailable())\n-- dump it\nprint(\"ba.toString(16):\", __ba:toString(16))\n\n-- create a ByteArray\nlocal __ba2 = ByteArray.new()\n\n-- you can write some values like actionscript\n-- also, you can use chaining calls.\n__ba2:writeByte(0x59)\n    :writeInt(11)\n    :writeShort(1101)\n-- write a empty string\n__ba2:writeStringUShort(\"\")\n-- write some chinese string\n__ba2:writeStringUShort(\"中文\")\n\n-- dump it\nprint(\"ba2.toString(10):\", __ba2:toString(10))\n```\n\nAbove codes will print like these:\n\n![print result][51]\n\n\u003ca name=\"ByteArrayVarint\"\u003e\n\n## zr.utils.ByteArrayVarint\n\nByteArrayVarint depends on [BitOP][4].\n\nByteArrayVarint implements [the Varint encoding in google protocol buffer][7].\n\nSee following:\n\n\u003eTo understand your simple protocol buffer encoding, you first need to understand varints. Varints are a method of serializing integers using one or more bytes. Smaller numbers take a smaller number of bytes.\n\u003e\n\u003eEach byte in a varint, except the last byte, has the most significant bit (msb) set – this indicates that there are further bytes to come. The lower 7 bits of each byte are used to store the two's complement representation of the number in groups of 7 bits, least significant group first.\n\nYour can use these methods(and all ByteArray methods) in ByteArrayVarint:\n\n|Method Name|Description|\n|----|----|\n|ByteArrayVarint.readUVInt()|read a unsigned varint int|\n|ByteArrayVarint.writeUVInt()|write a unsigned varint int|\n|ByteArrayVarint.readVInt()|read varint int|\n|ByteArrayVarint.writeVInt()|write varint int|\n|ByteArrayVarint.readStringUVInt()|read a string preceding a unsigned varint int|\n|ByteArrayVarint.writeStringUVInt()|write a string preceding a unsigned varint int|\n\nOn account of a [BitOP][4] limitation, ByteArrayVarint will read a unsigned int as a **minus**.\n\n\u003ca name=\"SocketTCP\"\u003e\n\n## zr.net.SocketTCP\n\nThe SocketTCP depends on [LuaSocket][6]\n\nUsage:\n\n``` lua\nlocal SocketTCP = zr.net.SocketTCP\nlocal ByteArray = zr.utils.ByteArray\n\nsocket = SocketTCP.new(\"127.0.0.1\", 12001, false)\nsocket:addEventListener(SocketTCP.EVENT_CONNECTED, onStatus)\nsocket:addEventListener(SocketTCP.EVENT_CLOSE, onStatus)\nsocket:addEventListener(SocketTCP.EVENT_CLOSED, onStatus)\nsocket:addEventListener(SocketTCP.EVENT_CONNECT_FAILURE, onStatus)\nsocket:addEventListener(SocketTCP.EVENT_DATA, onData)\n\nsocket:send(ByteArray.new():writeByte(0x59):getPack())\n\nfunction onStatus(__event)\n    echoInfo(\"socket status: %s\", __event.name)\nend\n\nfunction onData(__event)\n    echoInfo(\"socket status: %s, data:%s\", __event.name, ByteArray.toString(__event.data))\nend\n```\n\n\u003ca name=\"log\"\u003e\n\n## zr.log\n\n`zr.log` package is very similar to python logging package.\n\n``` lua\nlocal logFilePath = 'log.txt'\nlocal flogh = nil\nlocal logFileHandler = io.open(logFilePath, 'w+b')\n\n-- FileHandler can accept a file handler or a file name.\nif logFileHandler then\n    flogh = zr.log.FileHandler.new(logFileHandler, nil, true, true)\n    flogh.filename = logFilePath\nelse\n    flogh = zr.log.FileHandler.new(logFilePath, 'w+b', true, true)\nend\n\nlocal echo = print\n-- A logger can accept one or more handler.\nlog = zr.log.Logger.new(zr.log.Logger.NOTSET, \n    zr.log.PrintHandler.new(echo), \n    flogh)\nlog:debug('You name?', 'zrong')\nlog:debug('My name is %s.', 'zrong')\n\n-- Following contents will appear in console and log.txt.\n-- [5.3278] You name?  zrong\n-- [5.3279] My name is zrong.\n```\n\n# 5. LICENSE\n\nBSD 3-Clause License\n\nCopyright (c) 2018, Jacky Tsang\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this\n  list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n  this list of conditions and the following disclaimer in the documentation\n  and/or other materials provided with the distribution.\n\n* Neither the name of the copyright holder nor the names of its\n  contributors may be used to endorse or promote products derived from\n  this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n\n[1]: https://github.com/chukong/quick-cocos2d-x/tree/develop/framework\n[2]: https://zengrong.net\n[3]: http://underpop.free.fr/l/lua/lpack/\n[4]: http://bitop.luajit.org/index.html\n[5]: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html\n[6]: http://w3.impa.br/~diego/software/luasocket/\n[7]: https://developers.google.com/protocol-buffers/docs/encoding\n[8]: http://www.poedit.net/\n[9]: http://www.gnu.org/software/gettext/\n[10]: https://github.com/chukong/quick-cocos2d-x\n[51]: ./luabytearray.png\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzrong%2Flua","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzrong%2Flua","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzrong%2Flua/lists"}