diff options
63 files changed, 522 insertions, 715 deletions
diff --git a/bin/BeatEm/init.lua b/bin/BeatEm/init.lua deleted file mode 100644 index b9cab27..0000000 --- a/bin/BeatEm/init.lua +++ /dev/null @@ -1,14 +0,0 @@ -local BeatEm = { - _NAME = "BeatEm" - _AUTHOR = "Chai", - _DESCRIPTION = [[ -A Fighting game utility for Jin game framework. - ]], - _JIN_REVISION = 101, - _REVISION = 100 - _LISENCE = [[ - - ]] -} - -return BeatEm
\ No newline at end of file diff --git a/bin/BehaviorTree/init.lua b/bin/BehaviorTree/init.lua deleted file mode 100644 index 79863bc..0000000 --- a/bin/BehaviorTree/init.lua +++ /dev/null @@ -1,3 +0,0 @@ -local BehaviorTree = {} - -return BehaviorTrees
\ No newline at end of file diff --git a/bin/EventMsgCenter/EventMsgCenter.lua b/bin/EventMsgCenter/EventMsgCenter.lua deleted file mode 100644 index 7b944c2..0000000 --- a/bin/EventMsgCenter/EventMsgCenter.lua +++ /dev/null @@ -1,55 +0,0 @@ -local EventMsgCenter = {} - --- <event, {callbacks}> -local _broadcast = {} - -EventMsgCenter.registerMsg = function(e, callback, first) - if _broadcast[e] == nil then - _broadcast[e] = {} - end - first = first or false - EventMsgCenter.unregisterMsg(e, callback) - if not first then - table.insert(_broadcast[e], callback) - else - table.insert(_broadcast[e], 1, callback) - end -end - -local removeElement = function(t, e) - for i, v in ipairs(t) do - if v == e then - table.remove(t, i) - break - end - end -end - -EventMsgCenter.unregisterMsg = function(e, callback) - if _broadcast[e] == nil or callback == nil then - return - end - removeElement(_broadcast[e], callback) -end - -EventMsgCenter.unregisterAllMsgByEvent = function(e) - _broadcast[e] = nil -end - -EventMsgCenter.unregisterAllMsg = function() - _broadcast = {} -end - -EventMsgCenter.sendMsg = function(e, ...) - local callbacks = _broadcast[e] - if callbacks == nil then - return - end - for _, f in ipairs(callbacks) do - if f ~= nil then - f(...) - end - end -end - -return EventMsgCenter
\ No newline at end of file diff --git a/bin/EventMsgCenter/Events.lua b/bin/EventMsgCenter/Events.lua deleted file mode 100644 index 3803fd4..0000000 --- a/bin/EventMsgCenter/Events.lua +++ /dev/null @@ -1,21 +0,0 @@ -local events = { - - "Player_Move", - "Player_Change", - "Player_Spawn", - "Player_Kick", - - "" - -} - -function CreatEnumTable(tbl, index) - local enumtbl = {} - local enumindex = index or 0 - for i, v in ipairs(tbl) do - enumtbl[v] = enumindex + i - end - return enumtbl -end - -return CreatEnumTable(events)
\ No newline at end of file diff --git a/bin/EventMsgCenter/README.md b/bin/EventMsgCenter/README.md deleted file mode 100644 index ef3bd92..0000000 --- a/bin/EventMsgCenter/README.md +++ /dev/null @@ -1,2 +0,0 @@ -Event Massage Center - diff --git a/bin/StateMachine/init.lua b/bin/StateMachine/init.lua deleted file mode 100644 index 83daeb7..0000000 --- a/bin/StateMachine/init.lua +++ /dev/null @@ -1,3 +0,0 @@ -local StateMachine = {} - -return StateMachine
\ No newline at end of file diff --git a/bin/Tilemap/init.lua b/bin/Tilemap/init.lua deleted file mode 100644 index e69de29..0000000 --- a/bin/Tilemap/init.lua +++ /dev/null diff --git a/bin/UI/Button.lua b/bin/UI/Button.lua deleted file mode 100644 index 69950b7..0000000 --- a/bin/UI/Button.lua +++ /dev/null @@ -1,5 +0,0 @@ -local Button = {} - - - -return Button
\ No newline at end of file diff --git a/bin/a.ogg b/bin/a.ogg Binary files differdeleted file mode 100644 index 9ba4905..0000000 --- a/bin/a.ogg +++ /dev/null diff --git a/bin/a.png b/bin/a.png Binary files differdeleted file mode 100644 index e3712e5..0000000 --- a/bin/a.png +++ /dev/null diff --git a/bin/a.wav b/bin/a.wav Binary files differdeleted file mode 100644 index 74b2e31..0000000 --- a/bin/a.wav +++ /dev/null diff --git a/bin/anim/README.md b/bin/anim/README.md deleted file mode 100644 index e69de29..0000000 --- a/bin/anim/README.md +++ /dev/null diff --git a/bin/anim/anim.lua b/bin/anim/anim.lua deleted file mode 100644 index a1529c3..0000000 --- a/bin/anim/anim.lua +++ /dev/null @@ -1,11 +0,0 @@ -local anim = { - _DESCRIPTION = [[ - - ]], - _AUTHOR = "Chai", - _JIN_REVISION = 101, - -} - - -return anim
\ No newline at end of file diff --git a/bin/class/class.lua b/bin/class/class.lua deleted file mode 100644 index 6cff4d7..0000000 --- a/bin/class/class.lua +++ /dev/null @@ -1,15 +0,0 @@ -local class = {} - -class.new = function(self, ...) - local c = {} - setmetatable(c, self) - self.__index = self - if self.init then - self.init(...) - end - return c -end - -class.static = {} - -return class
\ No newline at end of file diff --git a/bin/component/component.lua b/bin/component/component.lua deleted file mode 100644 index 2432054..0000000 --- a/bin/component/component.lua +++ /dev/null @@ -1,20 +0,0 @@ -local Component = { - object = nil -} - -Component.new = function(obj) - local component = {} - setmetatable(component, Component) - Component.__index = Component - component:_init(obj) -end - -Component._init = function(self, obj) - self.object = obj -end - -Component.update = function(dt) - -end - -return Component
\ No newline at end of file diff --git a/bin/enum/enum.lua b/bin/enum/enum.lua deleted file mode 100644 index 20a653d..0000000 --- a/bin/enum/enum.lua +++ /dev/null @@ -1,11 +0,0 @@ -local createEnumTable = function(tbl, index) - assert(IsTable(tbl)) - local enumtbl = {} - local enumindex = index or 0 - for i, v in ipairs(tbl) do - enumtbl[v] = enumindex + i - end - return enumtbl -end - -return createEnumTable
\ No newline at end of file diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex 0d065ea..0f2215c 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/bin/jin3d/init.lua b/bin/jin3d/init.lua deleted file mode 100644 index e69de29..0000000 --- a/bin/jin3d/init.lua +++ /dev/null diff --git a/bin/json/README.md b/bin/json/README.md deleted file mode 100644 index e69de29..0000000 --- a/bin/json/README.md +++ /dev/null diff --git a/bin/lightning.png b/bin/lightning.png Binary files differdeleted file mode 100644 index 89d0549..0000000 --- a/bin/lightning.png +++ /dev/null diff --git a/bin/loghelper/init.lua b/bin/loghelper/init.lua deleted file mode 100644 index fc07943..0000000 --- a/bin/loghelper/init.lua +++ /dev/null @@ -1,33 +0,0 @@ --- 不能使用 debug 命名模块,会冲突, --- 要使用其余名字比如 loghelper -local loghelper = {} -io.stdout:setvbuf("no") - -loghelper.LEVEL = { - INFO = 4, - DEBUG = 3, - WARN = 2, - ERROR = 1, - NONE = 0 -} - -local logTag = { - [loghelper.LEVEL.INFO] = "[Info]", - [loghelper.LEVEL.DEBUG] = "[Debug]", - [loghelper.LEVEL.WARN] = "[Warn]", - [loghelper.LEVEL.ERROR] = "[Error]", -} - -loghelper.level = loghelper.LEVEL.INFO - -loghelper.strict = function(level) - loghelper.level = level -end - -loghelper.log = function(level, msg) - if level <= loghelper.level then - print(logTag[level] .. ":" .. msg) - end -end - -return loghelper
\ No newline at end of file diff --git a/bin/main.lua b/bin/main.lua index 48bdd9a..3480016 100644 --- a/bin/main.lua +++ b/bin/main.lua @@ -1,72 +1,3 @@ -local loghelper = require("loghelper") -loghelper.strict(loghelper.LEVEL.INFO) -local EventMsgCenter = require("EventMsgCenter.EventMsgCenter") -local Events = require("EventMsgCenter.Events") -local timer = require("timer.timer") -_G["frame"] = 0 +require "jin-modules.main" -local thread = nil -jin.core.onLoad = function() - thread = jin.thread.Thread("Test", [[ - local t = jin.thread.getThread() - local str = t:demand(2) - print(str) - t:send(3, "back data") - while true do - jin.time.sleep(1) - end - ]]) - thread:start() - EventMsgCenter.registerMsg(Events.Player_Move, function(msg) - print(msg) - end) - timer.every(1.0, function() - loghelper.log(loghelper.LEVEL.INFO, _G["frame"] .. "fps") - EventMsgCenter.sendMsg(Events.Player_Move, _G["frame"]) - _G["frame"] = 0 - if thread:receive(3) then - print(thread:fetch(3)) - end - end) - timer.after(4.0, function() - thread:send(2, "test thread data") - EventMsgCenter.unregisterAllMsgByEvent(Events.Player_Move) - end) - ----------------------------------------------------- --- jin.bit测试 -local size = 0 -local buff = "" --- buff, size = jin.bit.write(buff, size, "test") --- buff, size = jin.bit.write(buff, size, "hello") -buff, size = jin.bit.write(buff, size, "hello,world!") -buff, size = jin.bit.write(buff, size, true) -buff, size = jin.bit.write(buff, size, 250) -local str, len = jin.bit.grabstring(buff, size) -print(str) -buff, size = jin.bit.shift(buff, size, len + 1) -local a = jin.bit.grabboolean(buff, size) -print(a) -end - -jin.core.onEvent = function(e) - if e.type == "quit" then - jin.core.stop() - elseif e.type == "keydown" then - if e.key == "Escape" then - jin.core.stop() - end - end -end - -jin.core.onUpdate = function(dt) - _G["frame"] = _G["frame"] + 1 - timer.update(dt) - - -- loghelper.log(loghelper.LEVEL.WARN, "版本" .. jin.revision()) -end - -jin.core.onDraw = function() - -end
\ No newline at end of file diff --git a/bin/newton/README.md b/bin/newton/README.md deleted file mode 100644 index e69de29..0000000 --- a/bin/newton/README.md +++ /dev/null diff --git a/bin/newton/init.lua b/bin/newton/init.lua deleted file mode 100644 index 41f9f9a..0000000 --- a/bin/newton/init.lua +++ /dev/null @@ -1,9 +0,0 @@ -local newton = {} - -newton.world = {} - -newton.update = function(dt) - -end - -return newton
\ No newline at end of file diff --git a/bin/particle/particle.lua b/bin/particle/particle.lua deleted file mode 100644 index e430da9..0000000 --- a/bin/particle/particle.lua +++ /dev/null @@ -1,5 +0,0 @@ -local particle = {} - - - -return particle
\ No newline at end of file diff --git a/bin/pool/pool.lua b/bin/pool/pool.lua deleted file mode 100644 index 39eb3a6..0000000 --- a/bin/pool/pool.lua +++ /dev/null @@ -1,3 +0,0 @@ -local pool = {} - -return pool
\ No newline at end of file diff --git a/bin/shader/README.md b/bin/shader/README.md deleted file mode 100644 index e69de29..0000000 --- a/bin/shader/README.md +++ /dev/null diff --git a/bin/timer/timer.lua b/bin/timer/timer.lua deleted file mode 100644 index 398f47f..0000000 --- a/bin/timer/timer.lua +++ /dev/null @@ -1,78 +0,0 @@ -local Timer = {} -local MODE = { - NONE = 0, - EVERY = 1, - REPEATS = 2, - AFTER = 3, -} -Timer.timers = {} -local timer = { - new = function(self, _mod, _duration, _count, _callback) - local t = {} - setmetatable(t, self) - self.__index = self - t:_init(_mod, _duration, _count, _callback) - return t - end, - _init = function(self, _mod, _duration, _count, _callback) - self.tick = 0 - self.duration = _duration - self.mode = _mod - self.count = _count - self.callback = _callback - end, - update = function(self, dt) - self.tick = self.tick + dt - if self.tick >= self.duration then - if self.mode == MODE.EVERY then - self.tick = 0 - elseif self.mode == MODE.REPEATS then - self.tick = 0 - self.count = self.count - 1 - if self.count <= 0 then - -- remove this - for i, v in ipairs(Timer.timers) do - if v == self then - table.remove(Timer.timers, i) - break - end - end - end - elseif self.mode == MODE.AFTER then - -- remove this - for i, v in ipairs(Timer.timers) do - if v == self then - table.remove(Timer.timers, i) - break - end - end - end - if self.callback then - self.callback() - end - end - end -} - -Timer.update = function(sec) - for _, t in ipairs(Timer.timers) do - t:update(sec) - end -end - -Timer.every = function(sec, callback) - local t = timer:new(MODE.EVERY, sec, nil, callback) - table.insert(Timer.timers, t) -end - -Timer.repeats = function(sec, rpt, callback) - local t = timer:new(MODE.REPEATS, sec, rpt, callback) - table.insert(Timer.timers, t) -end - -Timer.after = function(sec, callback) - local t = timer:new(MODE.AFTER, sec, nil, callback) - table.insert(Timer.timers, t) -end - -return Timer
\ No newline at end of file diff --git a/bin/treestump.png b/bin/treestump.png Binary files differdeleted file mode 100644 index 45d8d28..0000000 --- a/bin/treestump.png +++ /dev/null diff --git a/bin/treestump_diffuse.png b/bin/treestump_diffuse.png Binary files differdeleted file mode 100644 index 272e6a2..0000000 --- a/bin/treestump_diffuse.png +++ /dev/null diff --git a/bin/treestump_lines.png b/bin/treestump_lines.png Binary files differdeleted file mode 100644 index 293a757..0000000 --- a/bin/treestump_lines.png +++ /dev/null diff --git a/bin/xml/README.md b/bin/xml/README.md deleted file mode 100644 index e69de29..0000000 --- a/bin/xml/README.md +++ /dev/null diff --git a/build/vs2015/jin.vcxproj b/build/vs2015/jin.vcxproj index 5b141be..e016720 100644 --- a/build/vs2015/jin.vcxproj +++ b/build/vs2015/jin.vcxproj @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> -<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> +<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> <Configuration>Debug</Configuration> @@ -27,26 +27,26 @@ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries> - <PlatformToolset>v140</PlatformToolset> + <PlatformToolset>v141</PlatformToolset> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries> - <PlatformToolset>v140</PlatformToolset> + <PlatformToolset>v141</PlatformToolset> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries> - <PlatformToolset>v140</PlatformToolset> + <PlatformToolset>v141</PlatformToolset> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries> - <PlatformToolset>v140</PlatformToolset> + <PlatformToolset>v141</PlatformToolset> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> @@ -219,6 +219,7 @@ <ClCompile Include="..\..\src\lua\luaopen_jin.cpp" /> <ClCompile Include="..\..\src\lua\net\luaopen_Buffer.cpp" /> <ClCompile Include="..\..\src\lua\net\luaopen_net.cpp" /> + <ClCompile Include="..\..\src\lua\net\luaopen_Socket.cpp" /> <ClCompile Include="..\..\src\lua\thread\luaopen_thread.cpp" /> <ClCompile Include="..\..\src\lua\time\luaopen_time.cpp" /> <ClCompile Include="..\..\src\main.cpp" /> @@ -319,7 +320,7 @@ <ClInclude Include="..\..\src\lua\luaopen_jin.h" /> <ClInclude Include="..\..\src\lua\luaopen_types.h" /> <ClInclude Include="..\..\src\lua\luax.h" /> - <ClInclude Include="..\..\src\lua\net\luaopen_Socket.cpp" /> + <ClInclude Include="..\..\src\lua\net\lua_net_Buffer.h" /> <ClInclude Include="resource.h" /> </ItemGroup> <ItemGroup> diff --git a/build/vs2015/jin.vcxproj.filters b/build/vs2015/jin.vcxproj.filters index e9cd37e..e4e14a0 100644 --- a/build/vs2015/jin.vcxproj.filters +++ b/build/vs2015/jin.vcxproj.filters @@ -405,6 +405,9 @@ <ClCompile Include="..\..\src\lua\net\luaopen_Buffer.cpp"> <Filter>src\lua\net</Filter> </ClCompile> + <ClCompile Include="..\..\src\lua\net\luaopen_Socket.cpp"> + <Filter>src\lua\net</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\..\src\3rdparty\lua51\lapi.h"> @@ -695,7 +698,7 @@ <ClInclude Include="..\..\src\lua\embed\net.lua.h"> <Filter>src\lua\embed</Filter> </ClInclude> - <ClInclude Include="..\..\src\lua\net\luaopen_Socket.cpp"> + <ClInclude Include="..\..\src\lua\net\lua_net_Buffer.h"> <Filter>src\lua\net</Filter> </ClInclude> </ItemGroup> diff --git a/res/icon.ico b/res/icon.ico Binary files differindex 9c0209f..2b3fe21 100644 --- a/res/icon.ico +++ b/res/icon.ico diff --git a/res/icon.png b/res/icon.png Binary files differindex 58bc2fa..01e1ca6 100644 --- a/res/icon.png +++ b/res/icon.png diff --git a/res/icon2.ico b/res/icon2.ico Binary files differnew file mode 100644 index 0000000..f4eb980 --- /dev/null +++ b/res/icon2.ico diff --git a/res/icon2.png b/res/icon2.png Binary files differnew file mode 100644 index 0000000..1f8e0e3 --- /dev/null +++ b/res/icon2.png diff --git a/res/icon3.ico b/res/icon3.ico Binary files differnew file mode 100644 index 0000000..8fa9d30 --- /dev/null +++ b/res/icon3.ico diff --git a/res/icon3.png b/res/icon3.png Binary files differnew file mode 100644 index 0000000..8c9a054 --- /dev/null +++ b/res/icon3.png diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h index 69b9669..6ec4dc9 100644 --- a/src/3rdparty/luax/luax.h +++ b/src/3rdparty/luax/luax.h @@ -62,6 +62,8 @@ #define luax_checkinteger luaL_checkinteger #define luax_checkstring luaL_checkstring #define luax_checklstring luaL_checklstring +#define luax_touserdata lua_touserdata +#define luax_tolightuserdata lua_touserdata //#define luax_checkbool luaL_checkinteger inline bool luax_checkbool(lua_State *L, int numArg) { @@ -101,6 +103,17 @@ inline bool luax_checkbool(lua_State *L, int numArg) #define luax_pushlstring lua_pushlstring #define luax_pushinteger lua_pushinteger #define luax_pushboolean lua_pushboolean +#define luax_pushlightuserdata lua_pushlightuserdata + +//inline void luax_pushuserdata(lua_State* L, void* p) +//{ +// /** +// * https://stackoverflow.com/questions/15038796/lua-c-api-push-existing-pointers +// */ +// void** box = (void**)lua_newuserdata(L, sizeof(p)); +// *box = p; +// +//} #define luax_rawseti lua_rawseti @@ -249,6 +262,8 @@ 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) +#define luax_isuserdata lua_isuserdata +#define luax_islightuserdata lua_islightuserdata inline int luax_isinteger(lua_State* L, int i) { if (!luax_isnumber(L, i)) diff --git a/src/libjin/Common/data.h b/src/libjin/Common/data.h new file mode 100644 index 0000000..7fcc389 --- /dev/null +++ b/src/libjin/Common/data.h @@ -0,0 +1,32 @@ +#ifndef __JIN_COMMON_DATA_H +#define __JIN_COMMON_DATA_H + +namespace jin +{ + + class DataBuffer + { + public: + DataBuffer(int n) + : len(n) + { + buffer = new char[len]; + memset(buffer, 0, len); + } + ~DataBuffer() + { + delete[] buffer; + } + char* operator&() + { + return buffer; + } + + private: + char* buffer; + int len; + }; + +} // jin + +#endif
\ No newline at end of file diff --git a/src/libjin/Net/Socket.cpp b/src/libjin/Net/Socket.cpp index b7c621e..7e6fa7d 100644 --- a/src/libjin/Net/Socket.cpp +++ b/src/libjin/Net/Socket.cpp @@ -5,7 +5,7 @@ namespace jin namespace net { - Socket::Socket(SocketInformation info) + Socket::Socket(const SocketInformation& info) : tcpHandle(nullptr) , udpHandle(nullptr) { diff --git a/src/libjin/Net/Socket.h b/src/libjin/Net/Socket.h index fae6bd2..eb00605 100644 --- a/src/libjin/Net/Socket.h +++ b/src/libjin/Net/Socket.h @@ -26,7 +26,7 @@ namespace net class Socket { public: - Socket(SocketInformation socketInformation); + Socket(const SocketInformation& socketInformation); Socket(SocketType type, unsigned short port); Socket(SocketType type, unsigned int address, unsigned short port); Socket(SocketType type, const char* address, unsigned short port); diff --git a/src/libjin/Net/net.cpp b/src/libjin/Net/net.cpp new file mode 100644 index 0000000..db39be7 --- /dev/null +++ b/src/libjin/Net/net.cpp @@ -0,0 +1,24 @@ +#include "Net.h" + +namespace jin +{ +namespace net +{ + + bool Net::initSystem(const SettingBase* setting) + { + #ifdef _WIN32 + #if JIN_NET_TEKCOS + tk_init(); + #endif + #endif + return true; + } + + void Net::quitSystem() + { + + } + +} +} diff --git a/src/libjin/Net/net.h b/src/libjin/Net/net.h new file mode 100644 index 0000000..54ffede --- /dev/null +++ b/src/libjin/Net/net.h @@ -0,0 +1,30 @@ +#ifndef __JIN_NET_H +#define __JIN_NET_H +#include "../modules.h" +#if JIN_MODULES_NET + +#include "../Common/Subsystem.hpp" +#include "Socket.h" + +namespace jin +{ +namespace net +{ + + class Net : public Subsystem<Net> + { + public: + + protected: + Net() {}; + ~Net() {}; + SINGLETON(Net); + bool initSystem(const SettingBase* setting) override; + void quitSystem() override; + }; + +} +} + +#endif // JIN_MODULES_NET +#endif // __JIN_NET_H
\ No newline at end of file diff --git a/src/lua/audio/luaopen_Source.cpp b/src/lua/audio/luaopen_Source.cpp index cff3f7f..10aab4d 100644 --- a/src/lua/audio/luaopen_Source.cpp +++ b/src/lua/audio/luaopen_Source.cpp @@ -11,7 +11,7 @@ namespace lua static inline SDLSource* checkSource(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_SOURCE); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); if (proxy != 0 && proxy != nullptr) return (SDLSource*)proxy->object; return nullptr; @@ -108,7 +108,7 @@ namespace lua int luaopen_Source(lua_State* L) { - luax_newtype(L, TYPE_SOURCE, f); + luax_newtype(L, JIN_AUDIO_SOURCE, f); return 0; } diff --git a/src/lua/audio/luaopen_audio.cpp b/src/lua/audio/luaopen_audio.cpp index 20a6bf4..33f0561 100644 --- a/src/lua/audio/luaopen_audio.cpp +++ b/src/lua/audio/luaopen_audio.cpp @@ -67,9 +67,9 @@ namespace lua Buffer b; fs->read(f, &b); - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_SOURCE, sizeof(Proxy)); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); SDLSource* src = SDLSource::createSource(b.data, b.size); - proxy->bind(src); + proxy->bind(src, JIN_AUDIO_SOURCE); return 1; } diff --git a/src/lua/bit/luaopen_bit.cpp b/src/lua/bit/luaopen_bit.cpp index e3a7bbb..d6c8197 100644 --- a/src/lua/bit/luaopen_bit.cpp +++ b/src/lua/bit/luaopen_bit.cpp @@ -71,17 +71,6 @@ namespace lua { "RS", l_rshift }, { "INC", l_include }, - //{ "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 } diff --git a/src/lua/embed/net.lua.h b/src/lua/embed/net.lua.h index 3db9264..4d89dc7 100644 --- a/src/lua/embed/net.lua.h +++ b/src/lua/embed/net.lua.h @@ -1,70 +1,4 @@ /* net.lua */ static const char* net_lua = R"( jin.net = jin.net or {} - ---[[ -socketͨŵ -* INT 32 -* FLOAT 32 -* BOOL 32 -* STRING -- -STRINGжҽ磬0β -һЭ鶨ӣðЭͳһһluaļ --- s2c_package.lua -local INT = jin.net.dataType.INT -local FLOAT = jin.net.dataType.FLOAT -local BOOL = jin.net.dataType.BOOL -local STRING = jin.net.dataType.STRING -Skill = { - id = INT, - damage = FLOAT, - range = FLOAT, - description = STRING -} -ʱ -local data, size = jin.net.serialize(Message.Skill, message) -Socket:send(data, size) -յʱ -local data, size = Socket:receive() -local message = jin.net.deserialize(Message.Skill, data, size) -]] - -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 = {} - local i = 1 - for k, t in pairs(prototype) do - message[k] = data[i] - i = i + 1 - end - return message -end - -jin.net.serialize = function(prototype, message) - local data = "" - local size = 0 - for i, v in pairs(message) do - data, size = jin.bit.write(data, size, v) - end - return data, size -end - --- Э -jin.net.decode = function() - local s = jin.bit.grabstring(buffer, size) -end - )";
\ No newline at end of file diff --git a/src/lua/graphics/luaopen_Canvas.cpp b/src/lua/graphics/luaopen_Canvas.cpp index 1b76edd..808a977 100644 --- a/src/lua/graphics/luaopen_Canvas.cpp +++ b/src/lua/graphics/luaopen_Canvas.cpp @@ -11,7 +11,7 @@ namespace lua static inline Canvas* checkCanvas(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); if (proxy != nullptr) return (Canvas*)proxy->object; return nullptr; @@ -50,7 +50,7 @@ namespace lua static int l_gc(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); Canvas* canvas = (Canvas*)proxy->object; delete canvas; return 0; @@ -67,7 +67,7 @@ namespace lua int luaopen_Canvas(lua_State* L) { - luax_newtype(L, TYPE_CANVAS, f); + luax_newtype(L, JIN_GRAPHICS_CANVAS, f); return 0; } diff --git a/src/lua/graphics/luaopen_Font.cpp b/src/lua/graphics/luaopen_Font.cpp index c0d4708..3de2981 100644 --- a/src/lua/graphics/luaopen_Font.cpp +++ b/src/lua/graphics/luaopen_Font.cpp @@ -16,7 +16,7 @@ namespace lua static int l_box(lua_State* L) { - Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT); + Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); const char* text = luax_checkstring(L, 2); int fheight = luax_checknumber(L, 3); int spacing = luax_checknumber(L, 4); @@ -36,7 +36,7 @@ namespace lua int luaopen_Font(lua_State* L) { - luax_newtype(L, TYPE_FONT, f); + luax_newtype(L, JIN_GRAPHICS_FONT, f); return 0; } diff --git a/src/lua/graphics/luaopen_Image.cpp b/src/lua/graphics/luaopen_Image.cpp index 546e6b1..0f97b2c 100644 --- a/src/lua/graphics/luaopen_Image.cpp +++ b/src/lua/graphics/luaopen_Image.cpp @@ -11,7 +11,7 @@ namespace lua static inline Texture* checkTexture(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); if (proxy != nullptr) return (Texture*)proxy->object; return nullptr; @@ -63,7 +63,7 @@ namespace lua static int l_gc(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); Texture* img = (Texture*)proxy->object; delete img; return 0; @@ -81,7 +81,7 @@ namespace lua int luaopen_Image(lua_State* L) { - luax_newtype(L, TYPE_IMAGE, f); + luax_newtype(L, JIN_GRAPHICS_IMAGE, f); return 0; } diff --git a/src/lua/graphics/luaopen_JSL.cpp b/src/lua/graphics/luaopen_JSL.cpp index 7c59937..8d25178 100644 --- a/src/lua/graphics/luaopen_JSL.cpp +++ b/src/lua/graphics/luaopen_JSL.cpp @@ -11,7 +11,7 @@ namespace lua static inline JSLProgram* checkJSLProgram(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); if(proxy != nullptr) return (JSLProgram*)proxy->object; return nullptr; @@ -66,14 +66,14 @@ namespace lua } case IMAGE: { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); + Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_IMAGE); Texture* tex = (Texture*)proxy->object; jsl->sendTexture(variable, tex); break; } case CANVAS: { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); + Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS); Canvas* canvas = (Canvas*)proxy->object; jsl->sendCanvas(variable, canvas); break; @@ -121,7 +121,7 @@ namespace lua static int l_gc(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); JSLProgram* jsl = (JSLProgram*)proxy->object; delete jsl; return 0; @@ -138,7 +138,7 @@ namespace lua */ int luaopen_JSL(lua_State* L) { - luax_newtype(L, TYPE_JSL, f); + luax_newtype(L, JIN_GRAPHICS_SHADER, f); return 0; } diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index 2aa68e8..da91c51 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -77,9 +77,9 @@ namespace lua Buffer b; fs->read(f, &b); - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_IMAGE, sizeof(Proxy)); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_IMAGE, sizeof(Proxy)); Texture* img = Texture::createTexture(b.data, b.size); - proxy->bind(img); + proxy->bind(img, JIN_GRAPHICS_IMAGE); return 1; } @@ -89,10 +89,10 @@ namespace lua */ static int l_newShader(lua_State* L) { - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_JSL, sizeof(Proxy)); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); const char* program = luax_checkstring(L, 1); JSLProgram* jsl = JSLProgram::createJSLProgram(program); - proxy->bind(jsl); + proxy->bind(jsl, JIN_GRAPHICS_SHADER); return 1; } @@ -104,9 +104,9 @@ namespace lua { int w = luax_checknumber(L, 1); int h = luax_checknumber(L, 2); - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_CANVAS, sizeof(Proxy)); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_CANVAS, sizeof(Proxy)); Canvas* cvs = Canvas::createCanvas(w, h); - proxy->bind(cvs); + proxy->bind(cvs, JIN_GRAPHICS_CANVAS); return 1; } @@ -145,13 +145,13 @@ namespace lua float sx = luax_optnumber(L, 4, 1); float sy = luax_optnumber(L, 5, 1); float r = luax_optnumber(L, 6, 0); - if (luax_istype(L, 1, TYPE_IMAGE)) + if (luax_istype(L, 1, JIN_GRAPHICS_IMAGE)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); Texture* tex = (Texture*)proxy->object; tex->draw(x, y, sx, sy, r); } - else if (luax_istype(L, 1, TYPE_CANVAS)) + else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); Canvas* p = (Canvas*)proxy->object; @@ -204,7 +204,7 @@ namespace lua Canvas::unbind(); return 0; } - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); Canvas* c = (Canvas*)proxy->object; c->bind(); return 0; @@ -223,7 +223,7 @@ namespace lua JSLProgram::unuse(); return 0; } - if (luax_istype(L, 1, TYPE_JSL)) + if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); JSLProgram* jsl = (JSLProgram*)proxy->object; @@ -389,7 +389,7 @@ namespace lua static int l_newFont(lua_State* L) { - Font* font = (Font*)luax_newinstance(L, TYPE_FONT, sizeof(Font)); + Font* font = (Font*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Font)); const char* path = luax_checkstring(L, 1); Filesystem* fs = Filesystem::get(); Buffer b = {}; @@ -422,7 +422,7 @@ namespace lua context.curFont = context.defaultFont; return 0; } - Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT); + Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); context.curFont = font; return 0; } diff --git a/src/lua/luaopen_jin.cpp b/src/lua/luaopen_jin.cpp index 0a258ec..ad5b193 100644 --- a/src/lua/luaopen_jin.cpp +++ b/src/lua/luaopen_jin.cpp @@ -68,7 +68,7 @@ namespace lua {"mouse", luaopen_mouse}, {"keyboard", luaopen_keyboard}, {"filesystem", luaopen_filesystem}, - //{"net", luaopen_net}, + {"net", luaopen_net}, {"audio", luaopen_audio}, {"joypad", luaopen_joypad}, {"math", luaopen_math}, diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h index 894af48..ba720ee 100644 --- a/src/lua/luaopen_types.h +++ b/src/lua/luaopen_types.h @@ -2,19 +2,20 @@ #define __JIN_M_TYPES_H // graphics module -#define TYPE_IMAGE "Image" -#define TYPE_JSL "Shader" -#define TYPE_CANVAS "Canvas" -#define TYPE_FONT "Font" +#define JIN_GRAPHICS_IMAGE "JIN_GRAPHICS_IMAGE" +#define JIN_GRAPHICS_SHADER "JIN_GRAPHICS_SHADER" +#define JIN_GRAPHICS_CANVAS "JIN_GRAPHICS_CANVAS" +#define JIN_GRAPHICS_FONT "JIN_GRAPHICS_FONT" // audio module -#define TYPE_SOURCE "Source" +#define JIN_AUDIO_SOURCE "JIN_AUDIO_SOURCE" // thread module -#define TYPE_THREAD "Thread" +#define JIN_THREAD_THREAD "JIN_THREAD_THREAD" // network module -#define TYPE_SOCKET "Socket" +#define JIN_NETWORK_SOCKET "JIN_NETWORK_SOCKET" +#define JIN_NETWORK_BUFFER "JIN_NETWORK_BUFFER" namespace jin { @@ -24,12 +25,14 @@ namespace lua class Proxy { public: - inline void bind(const void* obj) + inline void bind(const void* obj, const char* t) { object = obj; + type = t; } - const void* object; + const void* object; // acctual object binded + const char* type; // type name and metatable name }; } diff --git a/src/lua/net/lua_net_Buffer.h b/src/lua/net/lua_net_Buffer.h new file mode 100644 index 0000000..51c7598 --- /dev/null +++ b/src/lua/net/lua_net_Buffer.h @@ -0,0 +1,92 @@ +#ifndef __JIN_LUA_NET_NETBUFFER_H +#define __JIN_LUA_NET_NETBUFFER_H + +#include <cstring> +#include <cstdlib> + +namespace jin +{ +namespace lua +{ +namespace net +{ + + class Buffer + { + public: + Buffer(size_t s = 0) + { + size = s; + buffer = new char[size]; + memset(buffer, 0, size); + } + + Buffer(const char* data, size_t s) + { + size = s; + buffer = new char[size]; + memcpy(buffer, data, size); + } + + ~Buffer() + { + delete[] buffer; + buffer = nullptr; + size = 0; + } + + void append(const void* data, size_t s) + { + if (data == nullptr) + return; + char* buf = buffer; + buffer = new char[size + s]; + memcpy(buffer, buf, size); + memcpy(buffer + size, data, s); + delete[] buf; + size += s; + return; + } + + const char* grabString(int* length, int offset = 0) + { + int l = offset; + for (; l < size; ++l) + { + if (buffer[l] == 0) + break; + } + *length = l - offset + 1; + char* str = (char*)malloc(*length); + memcpy(str, buffer + offset, *length); + return str; + } + + int grabInteger(int* length, int offset = 0) + { + *length = sizeof(int); + return *((int*)buffer); + } + + float grabFloat(int* length, int offset = 0) + { + *length = sizeof(float); + return *((float*)buffer); + } + + bool grabBoolean(int* length, int offset = 0) + { + *length = sizeof(bool); + return *((bool*)buffer); + } + + char* buffer; + size_t size; + + }; + +} // net +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp index 6e10f25..3a8353b 100644 --- a/src/lua/net/luaopen_Buffer.cpp +++ b/src/lua/net/luaopen_Buffer.cpp @@ -1,6 +1,7 @@ #include "lua/luax.h" #include "../luaopen_types.h" #include "libjin/jin.h" +#include "lua_net_Buffer.h" namespace jin { @@ -9,183 +10,120 @@ namespace lua namespace net { - class Buffer + static inline Buffer* checkNetBuffer(lua_State* L) { - 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; + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); + if (proxy != 0 && proxy != nullptr) + return (Buffer*)proxy->object; + return nullptr; } - // return buffer, size - static int l_write(lua_State* L) + // net.Buffer:append(value) -> value_length + static int l_append(lua_State* L) { - const char* data = luax_checklstring(L, 1, NULL); - int len = luax_checkinteger(L, 2); - if (luax_isinteger(L, 3)) + Buffer* buffer = checkNetBuffer(L); + const int vp = 2; + if (luax_isinteger(L, vp)) { - 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); + int n = luax_checkinteger(L, vp); + int size = sizeof(n); + buffer->append(&n, size); luax_pushinteger(L, size); - return 2; + return 1; } - else if (luax_isfloat(L, 3)) + else if (luax_isfloat(L, vp)) { - 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); + float n = luax_checknumber(L, vp); + int size = sizeof(n); + buffer->append(&n, size); luax_pushinteger(L, size); - return 2; + return 1; } - else if (luax_isboolean(L, 3)) + else if (luax_isboolean(L, vp)) { - 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); + bool n = luax_checkbool(L, vp); + int size = sizeof(n); + buffer->append(&n, size); luax_pushinteger(L, size); - return 2; + return 1; } - else if (luax_isstring(L, 3)) + else if (luax_isstring(L, vp)) { - 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); + const char* str = luax_checkstring(L, vp); + int size = strlen(str) + 1; + buffer->append(str, size); luax_pushinteger(L, size); - return 2; + return 1; } else { - luax_typerror(L, 3, "number, bool or string"); + luax_typerror(L, vp, "number, bool or string"); return 0; } } - // jin.shift(buffer, shift) - static int l_shift(lua_State* L) + // net.Buffer:grabString(offset) -> string, length + static int l_grabString(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); + Buffer* buffer = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + const char* str = buffer->grabString(&len, offset); + luax_pushstring(L, str); + luax_pushinteger(L, len); return 2; } - // jin.bit.grabstring(buffer, size) - static int l_grabstring(lua_State* L) + // net.Buffer:grabInteger(offset) -> integer, length + static int l_grabInteger(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; + Buffer* buffer = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + int integer = buffer->grabInteger(&len, offset); + luax_pushinteger(L, integer); + luax_pushinteger(L, len); + return 1; } - static int l_grabinteger(lua_State* L) + static int l_grabFloat(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); + Buffer* buffer = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + float floatv = buffer->grabFloat(&len, offset); + luax_pushnumber(L, floatv); + luax_pushinteger(L, len); return 1; } - static int l_grabfloat(lua_State* L) + static int l_grabBoolean(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); + Buffer* buffer = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + bool boolean = buffer->grabBoolean(&len, offset); + luax_pushboolean(L, boolean); + luax_pushinteger(L, len); return 1; } - static int l_grabboolean(lua_State* L) + static const luaL_Reg netbuffer_function[] = { + { "append", l_append }, + { "grabString", l_grabString }, + { "grabInteger", l_grabInteger }, + { "grabBoolean", l_grabBoolean }, + { "grabFloat", l_grabFloat }, + { 0, 0 } + }; + +} // net + + int luaopen_Buffer(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; + luax_newtype(L, JIN_NETWORK_BUFFER, net::netbuffer_function); + return 0; } -} // net } // lua } // jin
\ No newline at end of file diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp index 7dbfb33..395729a 100644 --- a/src/lua/net/luaopen_Socket.cpp +++ b/src/lua/net/luaopen_Socket.cpp @@ -1,120 +1,109 @@ #include "lua/luax.h" #include "../luaopen_types.h" #include "libjin/jin.h" +#include "lua_net_Buffer.h" namespace jin { namespace lua { - /** - * л - * int - * float - * bool - * - * һmessageһtable - */ - class DenseBuffer - { - public: - DenseBuffer(int len) - { - this->len = len; - buffer = new char[len]; - memset(buffer, 0, len); - } - ~DenseBuffer() - { - delete[] buffer; - } - char* operator&() - { - return buffer; - } - int size() - { - return len; - } - - private: - int len; - char* buffer; - }; - + using namespace jin::net; - static inline Socket* checkSocket(lua_State* L) + const int BUFFER_SIZE = 1024; + + static inline Socket* checkSocket(lua_State* L, int pos = 1) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_SOCKET); + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); if (proxy != 0 && proxy != nullptr) return (Socket*)proxy->object; return nullptr; } - /** - +---------------+ - | message table | -1 - +---------------+ - | ... | -2 - +---------------+ - | ... | ... - */ - static char* serialize(lua_State* L) + static inline net::Buffer* checkNetBuffer(lua_State* L, int pos = 1) { - if (!luax_istable(L, -1)) - luax_typerror(L, -1, "table"); - DenseBuffer* buffer; - + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); + if (proxy != 0 && proxy != nullptr) + return (net::Buffer*)proxy->object; + return nullptr; } + // return net.Buffer static int l_accept(lua_State* L) { Socket* socket = checkSocket(L); - + Socket* client = socket->accept(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + proxy->bind(client, JIN_NETWORK_SOCKET); + return 1; } + // return net.Buffer static int l_receive(lua_State* L) { Socket* socket = checkSocket(L); - + char buffer[BUFFER_SIZE] = {0}; + int size = socket->receive(buffer, BUFFER_SIZE); + net::Buffer* netBuffer = new net::Buffer(buffer, size); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + proxy->bind(netBuffer, JIN_NETWORK_BUFFER); + return 1; } - static int l_receive(lua_State* L) + // Socket:receiveFrom(address, port) + static int l_receiveFrom(lua_State* L) { Socket* socket = checkSocket(L); - + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + char buffer[BUFFER_SIZE]; + int size = socket->receiveFrom(buffer, BUFFER_SIZE, address, port); + net::Buffer* netBuffer = new net::Buffer(buffer, size); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + proxy->bind(netBuffer, JIN_NETWORK_BUFFER); + return 1; } - + + // Socket:send(net.Buffer) -> data_length static int l_send(lua_State* L) { Socket* socket = checkSocket(L); - DenseBuffer buffer(1024); - socket->send(&buffer, buffer.size()); + net::Buffer* buffer = checkNetBuffer(L, 2); + int len = socket->send(buffer->buffer, buffer->size); + luax_pushinteger(L, len); + return 1; } - // thread:sendTo(address, port, table) + // Socket:sendTo(address, port, net.Buffer) static int l_sendTo(lua_State* L) { Socket* socket = checkSocket(L); - + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + net::Buffer* buffer = checkNetBuffer(L, 4); + socket->sendTo(buffer->buffer, buffer->size, address, port); + return 0; } static int l_close(lua_State* L) { Socket* socket = checkSocket(L); - + socket->close(); + return 0; } static int l_configBlocking(lua_State* L) { Socket* socket = checkSocket(L); - + bool blocking = luax_checkbool(L, 2); + socket->configureBlocking(blocking); + return 0; } static const luaL_Reg socket_function[] = { { "accept", l_accept }, { "receive", l_receive }, - { "receiveFrom", l_receive }, + { "receiveFrom", l_receiveFrom }, { "send", l_send }, { "sendTo", l_sendTo }, { "close", l_close }, @@ -124,9 +113,9 @@ namespace lua int luaopen_Socket(lua_State* L) { - luax_newtype(L, TYPE_SOURCE, socket_function); + luax_newtype(L, JIN_NETWORK_SOCKET, socket_function); return 0; } -} -}
\ No newline at end of file +} // lua +} // jin
\ No newline at end of file diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp index 8d67487..4bbb7e8 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -1,31 +1,95 @@ #include "lua/luax.h" #include "libjin/jin.h" +#include "../luaopen_types.h" +#include "lua_net_Buffer.h" namespace jin { namespace lua { + using namespace jin::net; + static int l_initNetwork(lua_State* L) { jin::net::Net::get()->init(); return 1; } + + // jin.net.toSocket(lightuserdata) + static int l_toSocket(lua_State*L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET); + //Proxy * socketProxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + + return 1; + } + + // jin.net.Socket() + static int l_Socket(lua_State* L) + { + const char* socketType = luax_checkstring(L, 1); + SocketInformation info = { 0 }; + if (strcmp(socketType, "TCP") == 0) + info.type = SocketType::TCP; + else if (strcmp(socketType, "UDP") == 0) + info.type = SocketType::UDP; + else + { + luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP"); + return 0; + } + // type, port + if (luax_gettop(L) == 2) + { + info.port = luax_checkinteger(L, 2); + } + // type, address, port + else if (luax_gettop(L) == 3) + { + if (luax_isstring(L, 2)) + info.address = tk_strtohl(luax_checkstring(L, 2)); + else if(luax_isinteger(L, 2)) + info.address = luax_checkinteger(L, 2); + info.port = luax_checkinteger(L, 3); + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + Socket* socket = new Socket(info); + proxy->bind(socket, JIN_NETWORK_SOCKET); + return 1; + } + + // jin.net.Buffer() + static int l_Buffer(lua_State* L) + { + int size = luax_checkinteger(L, 1); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + net::Buffer* buffer = new net::Buffer(); + proxy->bind(buffer, JIN_NETWORK_BUFFER); + return 1; + } static const luaL_Reg f[] = { - { "init", l_initNetwork}, + { "init", l_initNetwork }, + { "toSocket", l_toSocket }, + { "Socket", l_Socket }, + { "Buffer", l_Buffer }, { 0, 0 } }; extern int luaopen_Socket(lua_State* L); + extern int luaopen_Buffer(lua_State* L); // only tcp int luaopen_net(lua_State* L) { + luaopen_Socket(L); + luaopen_Buffer(L); + luax_newlib(L, f); return 1; } -} -}
\ No newline at end of file +} // lua +} // jin
\ No newline at end of file diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp index 4c38899..b16a114 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -28,7 +28,7 @@ namespace jin static inline Thread* checkThread(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_THREAD); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); if (proxy != nullptr) return (Thread*)proxy->object; return nullptr; @@ -41,8 +41,8 @@ namespace jin luax_openlibs(L); luaopen_jin(L); luax_getglobal(L, MODULE_NAME); - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy)); - proxy->bind(thread); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); + proxy->bind(thread, JIN_THREAD_THREAD); luax_setfield(L, -2, "_curThread"); luax_dostring(L, thread->code.c_str()); luax_close(L); @@ -74,21 +74,32 @@ namespace jin { Thread* t = checkThread(L); int slot = luax_checkinteger(L, 2); - if (luax_isnumber(L, 3)) + const int vp = 3; + if (luax_isnumber(L, vp)) { - float real = luax_checknumber(L, 3); + float real = luax_checknumber(L, vp); t->send(slot, real); } - else if (luax_isboolean(L, 3)) + else if (luax_isboolean(L, vp)) { - bool bol = luax_checkbool(L, 3); + bool bol = luax_checkbool(L, vp); t->send(slot, bol); } - else if (luax_isstring(L, 3)) + else if (luax_isstring(L, vp)) { - const char* str = luax_checkstring(L, 3); + const char* str = luax_checkstring(L, vp); t->send(slot, str); } + else if (luax_isuserdata(L, vp)) + { + void* p = luax_touserdata(L, vp); + t->send(slot, p); + } + else if (luax_islightuserdata(L, vp)) + { + void* p = luax_tolightuserdata(L, vp); + t->send(slot, p); + } return 0; } @@ -123,6 +134,13 @@ namespace jin case thread::Thread::Variant::REAL: luax_pushnumber(L, v.real); break; + + case thread::Thread::Variant::POINTER: + Proxy* p = (Proxy*)v.pointer; + Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + proxy->bind(p->object, p->type); + break; + } return 1; } @@ -149,6 +167,13 @@ namespace jin case thread::Thread::Variant::REAL: luax_pushnumber(L, v.real); break; + + case thread::Thread::Variant::POINTER: + Proxy* p = (Proxy*)v.pointer; + Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + proxy->bind(p->object, p->type); + break; + } return 1; } @@ -190,10 +215,10 @@ namespace jin { "isRunning", l_isRunning }, { 0, 0 } }; - + static int luaopen_Thread(lua_State* L) { - luax_newtype(L, TYPE_THREAD, thread_function); + luax_newtype(L, JIN_THREAD_THREAD, thread_function); return 0; } @@ -203,9 +228,9 @@ namespace jin { const char* name = luax_checkstring(L, 1); const char* code = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy)); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); Thread* thread = new Thread(name, code, Thread::threadRunner); - proxy->bind(thread); + proxy->bind(thread, JIN_THREAD_THREAD); return 1; } diff --git a/src/lua/thread/luaopen_thread.cpp b/src/lua/thread/luaopen_thread.cpp index 4c38899..b16a114 100644 --- a/src/lua/thread/luaopen_thread.cpp +++ b/src/lua/thread/luaopen_thread.cpp @@ -28,7 +28,7 @@ namespace jin static inline Thread* checkThread(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_THREAD); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); if (proxy != nullptr) return (Thread*)proxy->object; return nullptr; @@ -41,8 +41,8 @@ namespace jin luax_openlibs(L); luaopen_jin(L); luax_getglobal(L, MODULE_NAME); - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy)); - proxy->bind(thread); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); + proxy->bind(thread, JIN_THREAD_THREAD); luax_setfield(L, -2, "_curThread"); luax_dostring(L, thread->code.c_str()); luax_close(L); @@ -74,21 +74,32 @@ namespace jin { Thread* t = checkThread(L); int slot = luax_checkinteger(L, 2); - if (luax_isnumber(L, 3)) + const int vp = 3; + if (luax_isnumber(L, vp)) { - float real = luax_checknumber(L, 3); + float real = luax_checknumber(L, vp); t->send(slot, real); } - else if (luax_isboolean(L, 3)) + else if (luax_isboolean(L, vp)) { - bool bol = luax_checkbool(L, 3); + bool bol = luax_checkbool(L, vp); t->send(slot, bol); } - else if (luax_isstring(L, 3)) + else if (luax_isstring(L, vp)) { - const char* str = luax_checkstring(L, 3); + const char* str = luax_checkstring(L, vp); t->send(slot, str); } + else if (luax_isuserdata(L, vp)) + { + void* p = luax_touserdata(L, vp); + t->send(slot, p); + } + else if (luax_islightuserdata(L, vp)) + { + void* p = luax_tolightuserdata(L, vp); + t->send(slot, p); + } return 0; } @@ -123,6 +134,13 @@ namespace jin case thread::Thread::Variant::REAL: luax_pushnumber(L, v.real); break; + + case thread::Thread::Variant::POINTER: + Proxy* p = (Proxy*)v.pointer; + Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + proxy->bind(p->object, p->type); + break; + } return 1; } @@ -149,6 +167,13 @@ namespace jin case thread::Thread::Variant::REAL: luax_pushnumber(L, v.real); break; + + case thread::Thread::Variant::POINTER: + Proxy* p = (Proxy*)v.pointer; + Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + proxy->bind(p->object, p->type); + break; + } return 1; } @@ -190,10 +215,10 @@ namespace jin { "isRunning", l_isRunning }, { 0, 0 } }; - + static int luaopen_Thread(lua_State* L) { - luax_newtype(L, TYPE_THREAD, thread_function); + luax_newtype(L, JIN_THREAD_THREAD, thread_function); return 0; } @@ -203,9 +228,9 @@ namespace jin { const char* name = luax_checkstring(L, 1); const char* code = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_THREAD, sizeof(Proxy)); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); Thread* thread = new Thread(name, code, Thread::threadRunner); - proxy->bind(thread); + proxy->bind(thread, JIN_THREAD_THREAD); return 1; } |