Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zrong/lua
A lua library by zengrong.net
https://github.com/zrong/lua
Last synced: about 2 months ago
JSON representation
A lua library by zengrong.net
- Host: GitHub
- URL: https://github.com/zrong/lua
- Owner: zrong
- License: bsd-3-clause
- Created: 2013-11-14T15:12:10.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2020-04-09T10:28:37.000Z (almost 5 years ago)
- Last Synced: 2023-11-07T21:16:52.344Z (about 1 year ago)
- Language: Lua
- Size: 351 KB
- Stars: 166
- Watchers: 18
- Forks: 78
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A lua library by [zengrong.net][2]
# Dependences
Following libraries are dependented:
* [lpack][3]
* [BitOp][4]
* [LuaSocket][6]# 1. For pure lua
For some reason in my server development, I moved some packages that my wrote in to **zr** package (They were in **cc** package before).
# 2. For [quick-cocos2d-x][10]
All of the dependences were included in [quick-cocos2d-x][10].
This library has already been merged into [quick-cocos2d-x framework][1].
In [quick-cocos2d-x framework][1], these librares still use "cc" package name, and I won't update them.
# 3. Usage
You 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:
``` lua
require("lib.zrong.init")
```or if you have had these necessary global functions, you can require them in your code selectively:
``` lua
utils = require("lib.zrong.zr.utils.init")
net = {}
net.SocketTCP = require("lib.zrong.zr.net.SocketTCP")
```The necessary global functions are:
## In functions.lua
- class
- import
- iskindof# 4. API list
- [zr.utils.Gettext](#Gettext)
- [zr.utils.ByteArray](#ByteArray)
- [zr.utils.ByteArrayVarint](#ByteArrayVarint)
- [zr.net.SocketTCP](#SocketTCP)
- [zr.log](#log)
## zr.utils.Gettext
A detailed example about [GNU gettext][9] and [Poedit][8] (in chinese):
Usage:
``` lua
local Gettext = require("utils.Gettext")-- Use lua io, cannot use in Android
local fd,err=io.open("main.mo","rb")
if not fd then return nil,err end
local raw_data=fd:read("*all")
fd:close()local mo_data=assert(Gettext.parseData(raw_data))
print(mo_data["hello"])
-- 你好
print(mo_data["world"])
-- nil-- Then you'll get a kind of gettext function:
local gettext= Gettext.gettext(raw_data)
print(gettext("hello"))
-- 你好
print(gettext("world"))
-- world-- With a slight modification this will be ready-to-use for the xgettext tool:
_ = Gettext.gettext(raw_data)
print(_("hello"))
print(_("world"))
```## zr.utils.ByteArray
It can serialize bytes stream like ActionScript [flash.utils.ByteArray][5]
It depends on [lpack][3].
Usage:
``` lua
local ByteArray = zr.utils.ByteArray
-- use lpack to write a pack
local __pack = string.pack("## zr.utils.ByteArrayVarint
ByteArrayVarint depends on [BitOP][4].
ByteArrayVarint implements [the Varint encoding in google protocol buffer][7].
See following:
>To 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.
>
>Each 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.Your can use these methods(and all ByteArray methods) in ByteArrayVarint:
|Method Name|Description|
|----|----|
|ByteArrayVarint.readUVInt()|read a unsigned varint int|
|ByteArrayVarint.writeUVInt()|write a unsigned varint int|
|ByteArrayVarint.readVInt()|read varint int|
|ByteArrayVarint.writeVInt()|write varint int|
|ByteArrayVarint.readStringUVInt()|read a string preceding a unsigned varint int|
|ByteArrayVarint.writeStringUVInt()|write a string preceding a unsigned varint int|On account of a [BitOP][4] limitation, ByteArrayVarint will read a unsigned int as a **minus**.
## zr.net.SocketTCP
The SocketTCP depends on [LuaSocket][6]
Usage:
``` lua
local SocketTCP = zr.net.SocketTCP
local ByteArray = zr.utils.ByteArraysocket = SocketTCP.new("127.0.0.1", 12001, false)
socket:addEventListener(SocketTCP.EVENT_CONNECTED, onStatus)
socket:addEventListener(SocketTCP.EVENT_CLOSE, onStatus)
socket:addEventListener(SocketTCP.EVENT_CLOSED, onStatus)
socket:addEventListener(SocketTCP.EVENT_CONNECT_FAILURE, onStatus)
socket:addEventListener(SocketTCP.EVENT_DATA, onData)socket:send(ByteArray.new():writeByte(0x59):getPack())
function onStatus(__event)
echoInfo("socket status: %s", __event.name)
endfunction onData(__event)
echoInfo("socket status: %s, data:%s", __event.name, ByteArray.toString(__event.data))
end
```## zr.log
`zr.log` package is very similar to python logging package.
``` lua
local logFilePath = 'log.txt'
local flogh = nil
local logFileHandler = io.open(logFilePath, 'w+b')-- FileHandler can accept a file handler or a file name.
if logFileHandler then
flogh = zr.log.FileHandler.new(logFileHandler, nil, true, true)
flogh.filename = logFilePath
else
flogh = zr.log.FileHandler.new(logFilePath, 'w+b', true, true)
endlocal echo = print
-- A logger can accept one or more handler.
log = zr.log.Logger.new(zr.log.Logger.NOTSET,
zr.log.PrintHandler.new(echo),
flogh)
log:debug('You name?', 'zrong')
log:debug('My name is %s.', 'zrong')-- Following contents will appear in console and log.txt.
-- [5.3278] You name? zrong
-- [5.3279] My name is zrong.
```# 5. LICENSE
BSD 3-Clause License
Copyright (c) 2018, Jacky Tsang
All rights reserved.Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.[1]: https://github.com/chukong/quick-cocos2d-x/tree/develop/framework
[2]: https://zengrong.net
[3]: http://underpop.free.fr/l/lua/lpack/
[4]: http://bitop.luajit.org/index.html
[5]: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html
[6]: http://w3.impa.br/~diego/software/luasocket/
[7]: https://developers.google.com/protocol-buffers/docs/encoding
[8]: http://www.poedit.net/
[9]: http://www.gnu.org/software/gettext/
[10]: https://github.com/chukong/quick-cocos2d-x
[51]: ./luabytearray.png