🔧 build: 调整库

develop
xiaojin 5 years ago
parent b6fc0646f7
commit 35676fc89b

@ -28,5 +28,9 @@ cleanall:
testluaclib:
$(SKYNET_ROOT)3rd/lua/lua $(SKYNET_DEPS)testluaclib.lua
simulation:
$(SKYNET_ROOT)3rd/lua/lua simulation/simulation.lua
#-------------------------------------------------------------#
#-------------------------------------------------------------#

File diff suppressed because it is too large Load Diff

@ -10,7 +10,7 @@ struct ecs_capi
void *(*iter)(struct entity_world *w, int cid, int index);
void (*clear_type)(struct entity_world *w, int cid);
void *(*sibling)(struct entity_world *w, int cid, int index, int slibling_id);
void (*add_sibling)(struct entity_world *w, int cid, int index, int slibling_id, const void *buffer, void *L);
void *(*add_sibling)(struct entity_world *w, int cid, int index, int slibling_id, const void *buffer, void *L);
void (*remove)(struct entity_world *w, int cid, int index, void *L);
};
@ -51,12 +51,12 @@ entity_sibling(struct ecs_context *ctx, int cid, int index, int slibling_id)
return ctx->api->sibling(ctx->world, ctx->cid[cid], index, ctx->cid[slibling_id]);
}
static inline void
static inline void *
entity_add_sibling(struct ecs_context *ctx, int cid, int index, int slibling_id, const void *buffer)
{
check_id_(ctx, cid);
check_id_(ctx, slibling_id);
ctx->api->add_sibling(ctx->world, ctx->cid[cid], index, ctx->cid[slibling_id], buffer, ctx->L);
return ctx->api->add_sibling(ctx->world, ctx->cid[cid], index, ctx->cid[slibling_id], buffer, ctx->L);
}
static inline void

@ -0,0 +1,165 @@
local ecs = require "ecs"
local N = 1
local w = ecs.world()
print("memory:", w:memory())
w:register{
name = "vector",
"x:float",
"y:float",
}
w:register{
name = "mark",
}
w:register{
name = "id",
type = "int",
}
w:register{
name = "object",
type = "lua",
}
w:new{
object = "Hello",
}
local t = {}
for i = 1, N do
w:new{
vector = {
x = 1,
y = 2,
},
}
t[i] = {
x = 1,
y = 2,
}
end
w:update()
local function swap_c()
for v in w:select "vector:update" do
local vec = v.vector
vec.x, vec.y = vec.y, vec.x
end
end
local function swap_lua()
for _, v in ipairs(t) do
v.x, v.y = v.y, v.x
end
end
local function timing(f)
local c = os.clock()
for i = 1, 100 do
f()
end
return os.clock() - c
end
print("memory:", w:memory())
print("CSWAP", timing(swap_c))
print("LUASWAP", timing(swap_lua))
w:new{
vector = {
x = 3,
y = 4,
},
id = 100,
}
table.insert(t, {
x = 3,
y = 4,
})
w:new{
vector = {
x = 5,
y = 6,
},
mark = true,
}
table.insert(t, {
x = 5,
y = 6,
})
w:update()
local context = w:context{"vector", "mark", "id"}
local test = require "ecs.ctest"
local function csum()
return test.sum(context)
end
print("csum = ", csum())
local function luasum()
local s = 0
for v in w:select "vector:in" do
s = s + v.vector.x + v.vector.y
end
return s
end
print("luasum = ", luasum())
local function luanativesum()
local s = 0
for _, v in ipairs(t) do
s = s + v.x + v.y
end
return s
end
print("lnative sum = ", luanativesum())
print("CSUM", timing(csum))
print("LUASUM", timing(luasum))
print("LNATIVESUM", timing(luanativesum))
print "vector:update"
for v in w:select "vector:update" do
local vec = v.vector
print(vec.x, vec.y)
vec.x, vec.y = vec.y, vec.x
end
print "vector:in id?out"
for v in w:select "vector:in id?out" do
print(v.vector.x, v.vector.y, v.id)
if v.id then
v.id = 200
end
end
print "vector:in id:in"
for v in w:select "vector:in id:in" do
print(v.vector.x, v.vector.y, v.id)
end
print "object:update"
for v in w:select "object:update" do
print(v.object)
v.object = v.object .. " world"
end
print "object:in"
for v in w:select "object:in" do
print(v.object)
end

@ -1,30 +1,63 @@
-- https://github.com/cloudwu/luaecs
local ecs = require "ecs.core"
local function get_attrib(opt, inout)
local desc = {}
if opt == "?" then
desc.opt = true
else
assert(opt == ":")
end
if inout == "in" then
desc.r = true
elseif inout == "out" then
desc.w = true
elseif inout == "update" then
desc.r = true
desc.w = true
else
assert(inout == "temp")
end
return desc
end
local function cache_world(obj, k)
local c = {
typenames = {},
id = 0,
each = {},
select = {},
}
local function cache_each(each, key)
local tc = assert(c.typenames[key])
local type_desc = {
id = tc.id,
}
for i, _ in ipairs(tc) do
type_desc[i] = tc[i]
local function gen_select_pat(pat)
local typenames = c.typenames
local desc = {}
local idx = 1
for key, opt, inout in pat:gmatch "([_%w]+)([:?])(%l+)" do
local tc = assert(typenames[key])
local a = get_attrib(opt, inout)
a.name = tc.name
a.id = tc.id
a.type = tc.type
local n = #tc
for i = 1, #tc do
a[i] = tc[i]
end
desc[idx] = a
idx = idx + 1
end
each[key] = k:_simpleiter(type_desc)
return each[key]
return desc
end
setmetatable(c.each, {
local function cache_select(cache, pat)
local pat_desc = gen_select_pat(pat)
cache[pat] = k:_groupiter(pat_desc)
return cache[pat]
end
setmetatable(c.select, {
__mode = "kv",
__index = cache_each,
__index = cache_select,
})
obj[k] = c
return c
end
@ -96,7 +129,11 @@ do -- newtype
for i, v in ipairs(typeclass) do
c[i] = align(c, parse(v))
end
if c.size > 0 then
if typeclass.type == "lua" then
assert(c.size == 0)
c.size = ecs._LUAOBJECT
c.islua = true
elseif c.size > 0 then
align_struct(c, typeclass[1][1])
local pack = "!4="
for i = 1, #c do
@ -110,6 +147,7 @@ do -- newtype
c.type = t
c.size = typesize[t]
c.pack = typepack[t]
c[1] = {t, "v", 0}
else
c.tag = true
end
@ -132,7 +170,9 @@ function M:new(obj)
if not tc then
error("Invalid key : " .. k)
end
if tc.tag then
if tc.islua then
self:_addcomponent(eid, tc.id, v)
elseif tc.tag then
self:_addcomponent(eid, tc.id)
elseif tc.type then
self:_addcomponent(eid, tc.id, string.pack(tc.pack, mapbool[v] or v))
@ -159,11 +199,8 @@ function M:context(t)
return self:_context(id)
end
function M:each(name)
local ctx = context[self]
local typenames = ctx.typenames
local c = assert(typenames[name])
return ctx.each[name]()
function M:select(pat)
return context[self].select[pat]()
end
return ecs

@ -0,0 +1,50 @@
local socket = require 'socket'
local crypt = require "skynet.crypt"
local function encode_token(token)
return string.format("%s@%s:%s:%s", crypt.base64encode(token.user), crypt.base64encode(token.worldId),
crypt.base64encode(token.pass), crypt.base64encode(token.pf))
end
local token = {
pf = '1010',
user = 'teefe',
pwd = '123456789',
worldId = 1,
}
return function(login_host, login_port)
local sock = socket.connect(login_host or '127.0.0.1', login_port or 9527)
local challenge = crypt.base64decode(sock:receive('*l'))
local clientkey = crypt.randomkey()
sock:send(crypt.base64encode(crypt.dhexchange(clientkey)) .. '\n')
local line = crypt.base64decode(sock:receive '*l')
local secret = crypt.dhsecret(line, clientkey)
local hmac = crypt.hmac64(challenge, secret)
-- 5. 回应服务器的握手挑战码,确认握手正常
sock:send(crypt.base64encode(hmac) .. '\n')
-- 6. DES加密发送 token串
local etoken = crypt.desencode(secret, encode_token(token))
sock:send(crypt.base64encode(etoken) .. '\n')
-- 服务器解密后调用定制的auth和login handler处理客户端请求
-- 7. 从服务器读取 登录结果: 状态码 和subid
local result = sock:receive '*l'
local code = tonumber(string.sub(result, 1, 3))
assert(code == 200, "auth error:" .. tostring(code))
sock:close()
local substr = crypt.base64decode(string.sub(result, 5))
local ip, port, uid, subid = substr:match("([^:]+):([^:]+)@([^@]+)@(.+)")
return {
ip = ip,
port = port,
uid = uid,
worldId = token.worldId,
secret = secret,
subid = subid,
}
end

@ -0,0 +1,86 @@
assert(_VERSION == "Lua 5.4")
package.path = "lualib/?.lua;skynet/lualib/?.lua;simulation/?.lua"
package.cpath = "skynet/luaclib/?.so;luaclib/?.so"
local auth = require "auth"
local ok, atoken = pcall(auth)
if not ok then
print("please check: login auth err", atoken)
return
end
local termfx = require "termfx"
local ui = require "simpleui"
termfx.init()
termfx.inputmode(termfx.input.ALT + termfx.input.MOUSE)
termfx.outputmode(termfx.output.COL256)
local function main(token)
local sock = require ("network")()
local sok = sock:connect(token.ip, token.port)
assert(sok)
local output = {}
local mok, err = pcall(function()
local msg_list = {}
local input
local string = {}
while true do
termfx.clear(termfx.color.WHITE, termfx.color.BLACK)
local w, h = termfx.size()
local width = math.floor(w / 2)
ui.box(2, 2, width, h - 10)
ui.box(w / 2, 2, width, h - 10)
termfx.printat(2, h - 1, input)
termfx.printat(2, h, table.concat(string, ""))
termfx.present()
local evt = termfx.pollevent(100)
if evt then
if evt.type == 'key' then
if evt.key == termfx.key.CTRL_C then
break
elseif evt.key == termfx.key.BACKSPACE2 then
table.remove(string)
elseif evt.key == termfx.key.ENTER then
input = table.concat(string, '')
string = {}
pcall(act_when_input, input)
elseif evt.key == termfx.key.SPACE then
table.insert(string, ' ')
else
table.insert(string, evt.char)
end
end
else
local success, err = sock:update()
assert(success, err)
local r, msgid, msg = recv_msg()
if r == true then
table.insert(msg_list, {MN[msgid] or msgid, inspect(msg)})
elseif r == false then
p('网络断开,结束客户端', msgid)
break
end
end
end
end)
termfx.shutdown()
if not mok then
print("Error: ", err)
end
print(':\n', table.concat(output, '\n'))
end
main(atoken)
Loading…
Cancel
Save