开源软件名称(OpenSource Name):zrong/lua开源软件地址(OpenSource Url):https://github.com/zrong/lua开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):A lua library by zengrong.net DependencesFollowing libraries are dependented: 1. For pure luaFor some reason in my server development, I moved some packages that my wrote in to zr package (They were in cc package before). quick-cocos2d-x2. ForAll of the dependences were included in quick-cocos2d-x. This library has already been merged into quick-cocos2d-x framework. In quick-cocos2d-x framework, these librares still use "cc" package name, and I won't update them. 3. UsageYou can import them by require("lib.zrong.init") or if you have had these necessary global functions, you can require them in your code selectively: utils = require("lib.zrong.zr.utils.init")
net = {}
net.SocketTCP = require("lib.zrong.zr.net.SocketTCP") The necessary global functions are: In functions.lua
4. API listzr.utils.GettextA detailed example about GNU gettext and Poedit (in chinese): https://blog.zengrong.net/post/using_gettext_in_lua/ Usage: 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.ByteArrayIt can serialize bytes stream like ActionScript flash.utils.ByteArray It depends on lpack. Usage: local ByteArray = zr.utils.ByteArray
-- use lpack to write a pack
local __pack = string.pack("<bihP2", 0x59, 11, 1101, "", "中文")
-- create a ByteArray
local __ba = ByteArray.new()
-- ByteArray can write a lpack buffer directly
__ba:writeBuf(__pack)
-- remember, lua array started from 1
__ba:setPos(1)
-- now, you can read it like actionscript
print("ba.len:", __ba:getLen())
print("ba.readByte:", __ba:readByte())
print("ba.readInt:", __ba:readInt())
print("ba.readShort:", __ba:readShort())
print("ba.readString:", __ba:readStringUShort())
print("ba.available:", __ba:getAvailable())
-- dump it
print("ba.toString(16):", __ba:toString(16))
-- create a ByteArray
local __ba2 = ByteArray.new()
-- you can write some values like actionscript
-- also, you can use chaining calls.
__ba2:writeByte(0x59)
:writeInt(11)
:writeShort(1101)
-- write a empty string
__ba2:writeStringUShort("")
-- write some chinese string
__ba2:writeStringUShort("中文")
-- dump it
print("ba2.toString(10):", __ba2:toString(10)) Above codes will print like these: zr.utils.ByteArrayVarintByteArrayVarint depends on BitOP. ByteArrayVarint implements the Varint encoding in google protocol buffer. See following:
Your can use these methods(and all ByteArray methods) in ByteArrayVarint:
On account of a BitOP limitation, ByteArrayVarint will read a unsigned int as a minus. zr.net.SocketTCPThe SocketTCP depends on LuaSocket Usage: local SocketTCP = zr.net.SocketTCP
local ByteArray = zr.utils.ByteArray
socket = 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)
end
function onData(__event)
echoInfo("socket status: %s, data:%s", __event.name, ByteArray.toString(__event.data))
end zr.log
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)
end
local 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. LICENSEBSD 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:
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. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论