diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/3rdparty/luax/luax.h | 13 | ||||
-rw-r--r-- | src/lua/bit/luaopen_bit.cpp | 21 | ||||
-rw-r--r-- | src/lua/embed/net.lua.h | 27 | ||||
-rw-r--r-- | src/lua/math/luaopen_math.cpp | 10 | ||||
-rw-r--r-- | src/lua/net/luaopen_Buffer.cpp | 191 |
5 files changed, 246 insertions, 16 deletions
diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h index 94cc2e3..69b9669 100644 --- a/src/3rdparty/luax/luax.h +++ b/src/3rdparty/luax/luax.h @@ -61,6 +61,7 @@ #define luax_checknumber luaL_checknumber #define luax_checkinteger luaL_checkinteger #define luax_checkstring luaL_checkstring +#define luax_checklstring luaL_checklstring //#define luax_checkbool luaL_checkinteger inline bool luax_checkbool(lua_State *L, int numArg) { @@ -248,6 +249,18 @@ inline int luax_istype(lua_State* L, int idx, const char* tname) #define luax_istable(L, i) luax_is(table, L, i) #define luax_isnil(L, i) luax_is(nil, L, i) #define luax_isboolean(L, i) luax_is(boolean, L, i) +inline int luax_isinteger(lua_State* L, int i) +{ + if (!luax_isnumber(L, i)) + return 0; + return lua_tonumber(L, i) == lua_tointeger(L, i); +} +inline int luax_isfloat(lua_State* L, int i) +{ + if (!luax_isnumber(L, i)) + return 0; + return lua_tonumber(L, i) != lua_tointeger(L, i); +} /** * To userdata. */ diff --git a/src/lua/bit/luaopen_bit.cpp b/src/lua/bit/luaopen_bit.cpp index fc3c188..e3a7bbb 100644 --- a/src/lua/bit/luaopen_bit.cpp +++ b/src/lua/bit/luaopen_bit.cpp @@ -1,5 +1,6 @@ #include "lua/luax.h" #include "libjin/jin.h" +#include <cstdlib> namespace jin { @@ -61,12 +62,6 @@ namespace lua return 1; } - // return buffer, size - static int l_write(lua_State* L) - { - - } - static const luaL_Reg f[] = { { "AND", l_and }, { "OR" , l_or }, @@ -76,7 +71,18 @@ namespace lua { "RS", l_rshift }, { "INC", l_include }, - { "write", l_write}, + //{ "Buffer", l_newBuffer }, // ײ + + { "buffer", l_buffer }, + { "write", l_write }, + { "shift", l_shift }, + + { "grabstring", l_grabstring }, + { "grabinteger", l_grabinteger }, + { "grabfloat", l_grabfloat }, + { "grabboolean", l_grabboolean }, + + // , offset { 0, 0 } }; @@ -84,6 +90,7 @@ namespace lua int luaopen_bit(lua_State* L) { luax_newlib(L, f); + return 1; } diff --git a/src/lua/embed/net.lua.h b/src/lua/embed/net.lua.h index 73b60c3..3db9264 100644 --- a/src/lua/embed/net.lua.h +++ b/src/lua/embed/net.lua.h @@ -4,10 +4,10 @@ jin.net = jin.net or {} --[[ socketͨŵ -* INT -* FLOAT -* BOOL -* STRING +* INT 32 +* FLOAT 32 +* BOOL 32 +* STRING -- STRINGжҽ磬0β һЭ鶨ӣðЭͳһһluaļ -- s2c_package.lua @@ -29,10 +29,19 @@ local data, size = Socket:receive() local message = jin.net.deserialize(Message.Skill, data, size) ]] -jin.net.INT = 1 -jin.net.FLOAT = 2 -jin.net.BOOL = 3 -jin.net.STRING = 4 +jin.net.dataType = { + INT = 1, + FLOAT = 2, + BOOL = 3, + STRING = 4 +} + +jin.net.dataSize = { + INT = 4, + FLOAT = 4, + BOOL = 4, + STRING = -1, +} jin.net.deserialize = function(prototype, data, size) local message = {} @@ -55,7 +64,7 @@ end -- Э jin.net.decode = function() - + local s = jin.bit.grabstring(buffer, size) end )";
\ No newline at end of file diff --git a/src/lua/math/luaopen_math.cpp b/src/lua/math/luaopen_math.cpp index e4f68df..462e8d1 100644 --- a/src/lua/math/luaopen_math.cpp +++ b/src/lua/math/luaopen_math.cpp @@ -6,7 +6,17 @@ namespace jin namespace lua { + static int l_mod(lua_State* L) + { + int n = luax_checkinteger(L, 1); + int m = luax_checkinteger(L, 2); + int mod = n % m; + luax_pushinteger(L, mod); + return 1; + } + static const luaL_Reg f[] = { + { "mod", l_mod }, { 0, 0 } }; diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp new file mode 100644 index 0000000..6e10f25 --- /dev/null +++ b/src/lua/net/luaopen_Buffer.cpp @@ -0,0 +1,191 @@ +#include "lua/luax.h" +#include "../luaopen_types.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ +namespace net +{ + + class Buffer + { + public: + Buffer(size_t size); + Buffer(const char* data, size_t size); + ~Buffer() + { + } + + void append(char* data, size_t size) + { + + } + + const char* grabString(int offset = 0) + { + int l = offset; + for (; l < size; ++l) + { + if (buffer[l] == 0) + { + + } + } + return nullptr; + } + + int grabInteger(int offset = 0) + { + + } + + float grabfloat(int offset = 0) + { + + } + + bool grabboolean(int offset = 0) + { + + } + + private: + char* buffer; + size_t size; + + }; + + static int l_buffer(lua_State* L) + { + int size = luax_checkinteger(L, 1); + char* buffer = (char*)malloc(size); + memset(buffer, 0, size); + luax_pushlstring(L, buffer, size); + return 1; + } + + // return buffer, size + static int l_write(lua_State* L) + { + const char* data = luax_checklstring(L, 1, NULL); + int len = luax_checkinteger(L, 2); + if (luax_isinteger(L, 3)) + { + int n = luax_checkinteger(L, 3); + int size = len + sizeof(n); + char* buffer = (char*)malloc(size); + memcpy(buffer, data, len); + memcpy(buffer + len, &n, sizeof(n)); + //free((void*)data); + luax_pushlstring(L, buffer, size); + luax_pushinteger(L, size); + return 2; + } + else if (luax_isfloat(L, 3)) + { + float n = luax_checknumber(L, 3); + int size = len + sizeof(n); + char* buffer = (char*)malloc(size); + memcpy(buffer, data, len); + memcpy(buffer + len, &n, sizeof(n)); + //free((void*)data); + luax_pushlstring(L, buffer, size); + luax_pushinteger(L, size); + return 2; + } + else if (luax_isboolean(L, 3)) + { + bool b = luax_checkbool(L, 3); + int size = len + sizeof(b); + char* buffer = (char*)malloc(size); + memcpy(buffer, data, len); + memcpy(buffer + len, &b, sizeof(b)); + //free((void*)data); + luax_pushlstring(L, buffer, size); + luax_pushinteger(L, size); + return 2; + } + else if (luax_isstring(L, 3)) + { + const char* s = luax_checkstring(L, 3); + int l = strlen(s) + 1; // with \0 + int size = len + l; + char* buffer = (char*)malloc(size); + memcpy(buffer, data, len); + memcpy(buffer + len, s, l); + luax_pushlstring(L, buffer, size); + luax_pushinteger(L, size); + return 2; + } + else + { + luax_typerror(L, 3, "number, bool or string"); + return 0; + } + } + + // jin.shift(buffer, shift) + static int l_shift(lua_State* L) + { + const char* buffer = luax_checklstring(L, 1, NULL); + int size = luax_checkinteger(L, 2); + int shift = luax_checkinteger(L, 3); + int ss = size - shift; + luax_pushlstring(L, buffer + shift, ss); + luax_pushinteger(L, ss); + return 2; + } + + // jin.bit.grabstring(buffer, size) + static int l_grabstring(lua_State* L) + { + const char* buffer = luax_checklstring(L, 1, NULL); + int size = luax_checkinteger(L, 2); + int l = 0; + for (; l < size; ++l) + { + if (buffer[l] == 0) + { + int len = l + 1; + char* str = (char*)malloc(len); + memcpy(str, buffer, len); + luax_pushstring(L, str); + luax_pushinteger(L, l); + return 2; + } + } + return 0; + } + + static int l_grabinteger(lua_State* L) + { + const char* buffer = luax_checklstring(L, 1, NULL); + int size = luax_checkinteger(L, 2); + int n = *((int*)buffer); + luax_pushinteger(L, n); + return 1; + } + + static int l_grabfloat(lua_State* L) + { + const char* buffer = luax_checklstring(L, 1, NULL); + int size = luax_checkinteger(L, 2); + float n = *((float*)buffer); + luax_pushnumber(L, n); + return 1; + } + + static int l_grabboolean(lua_State* L) + { + const char* buffer = luax_checklstring(L, 1, NULL); + int size = luax_checkinteger(L, 2); + bool n = *((bool*)buffer); + luax_pushboolean(L, n); + return 1; + } + +} // net +} // lua +} // jin
\ No newline at end of file |