diff --git a/ModuleConfig.lua b/ModuleConfig.lua index 2fd63bc..ff8b882 100644 --- a/ModuleConfig.lua +++ b/ModuleConfig.lua @@ -30,6 +30,7 @@ loadModule('adminDamage') --loadModule('noBattleInjury') --loadModule('summonDemo') loadModule('setupMagicAttr') +loadModule('http') --loadModule('itembox') --黑白宝箱概率调整lua --loadModule('petBPExtend') --useModule('Welcome2') diff --git a/Modules/http.lua b/Modules/http.lua new file mode 100644 index 0000000..8f0ef80 --- /dev/null +++ b/Modules/http.lua @@ -0,0 +1,115 @@ +---http模块 +local Module = ModuleBase:createModule('http') + +---@alias ParamType {string:string} +---@alias HttpApiFn {string:fun(params:ParamType, body:string):string} +---@alias HttpMethods 'get'|'post'|'put'|'delete'|'patch' + +--- 加载模块钩子 +function Module:onLoad() + self:logInfo('load') + local status = Http.GetStatus(); + self:logInfo('Http.GetStatus', status); + + if status == 0 then + Http.Init(); + self:logInfo('Http.Init'); + Http.AddMountPoint("/", "./lua/www/") + self:logInfo('Http.AddMountPoint'); + status = 1; + end + if status == 1 then + Http.Start("0.0.0.0", 10086); + self:logInfo('Http.Start'); + end + self._Apis = {} --[[@type {string: HttpApiFn}]]; + self:regCallback('HttpRequestEvent', Func.bind(self.onHttpRequest, self)); + self:regApi('post', "register", Func.bind(self.ApiRegister, self)); + self:regApi('post', "doLua", Func.bind(self.doLua, self)); + self:regApi('post', "reloadModule", Func.bind(self.reloadModule, self)); +end + +---http://127.0.0.1:10086/api/doLua +---@param params ParamType +---@param body string +---@return string +function Module:doLua(params, body) + self:logInfo("doLua", params['lua']); + local r, ret = pcall(dofile, params['lua']); + self:logDebug('result', r, ret); + return "true" +end + +---http://127.0.0.1:10086/api/reloadModule +---@param params ParamType +---@param body string +---@return string +function Module:reloadModule(params, body) + self:logInfo("reloadModule", params['module']); + reloadModule(params['module']); + return "true" +end + +---注册新用户 http://127.0.0.1:10086/api/register +---@param params ParamType +---@param body string +---@return string +function Module:ApiRegister(params, body) + local b, ret = pcall(JSON.decode, body); + if b ~= true or ret == nil then + return "false"; + end + local account = ret.account; + local password = ret.password; + if (account or '') == '' or (password or '') == '' then + return "false"; + end + self:logInfo("Register", account, password); + local user = SQL.QueryEx('select CdKey from tbl_user where CdKey = ?', account); + if #user.rows == 0 then + local seq = SQL.QueryEx('select max(SequenceNumber) + 1 as Max from tbl_user'); + local sql = 'insert into tbl_user (CdKey, SequenceNumber, AccountID, AccountPassWord, ' + .. ' EnableFlg, UseFlg, BadMsg, TrialFlg, DownFlg, ExpFlg) values (' + .. SQL.sqlValue(account) .. ', ' .. SQL.sqlValue(seq.rows[1].Max) .. ', ' + .. SQL.sqlValue(account) .. ', ' + .. SQL.sqlValue(password) .. ',1,1,0,8,0,0);' + local r = SQL.QueryEx(sql); + if r.effectRows == 1 then + return "true" + end + --print(r, sql); + end + + return "false"; +end + +---http请求回调 +---@param method string +---@param api string API名字 +---@param params ParamType 参数 +---@param body string body内容 +---@return string body 返回内容 +function Module:onHttpRequest(method, api, params, body) + if self._Apis[string.lower(method .. api)] then + self:logInfo(string.lower(method .. api), self._Apis[string.lower(method .. api)]); + return self._Apis[string.lower(method .. api)](params, body); + end + return ""; +end + +---@param method HttpMethods +---@param api string 对应http://127.0.0.1:10086/api/****** +---@param fn HttpApiFn +function Module:regApi(method, api, fn) + self._Apis[string.lower(method .. api)] = fn; +end + +--- 卸载模块钩子 +function Module:onUnload() + self:logInfo('unload') + if Http.GetStatus() == 2 then + Http.Stop(); + end +end + +return Module; diff --git a/docs.lua b/docs.lua index c3c0881..ccbf942 100644 --- a/docs.lua +++ b/docs.lua @@ -716,6 +716,11 @@ function NL.RegItemTribeRateEvent(dofile, callback) end ---@return number 返回新的克制比率 function NL.ItemTribeRateEventCallback(a, b, rate) end +---Http请求事件 +---@param callback string callback回调参数 +---@see Http.HttpRequestEventCallback +function NL.RegHttpRequestEvent(dofile, callback) end + ---@param sql string sql ---@vararg string|number 绑定参数,最多40个 ---@return {status:number, effectRows:number, rows: table} 返回查询内容 @@ -934,3 +939,38 @@ function Map.GetDungeonId(floor) end ---@param dungeonId integer dungeonId ---@return number mapType, number floor, number x, number y function Map.FindDungeonEntry(dungeonId) end + +Http = _G.Http or {} + +---初始化Http服务器 +function Http.Init() end + +---开启Http服务 +---@param addr string 监听IP,例如: "0.0.0.0" +---@param port integer 端口 建议10000以上 +---@return integer ret @1:成功,其他为失败 +function Http.Start(addr, port) end + +---关闭Http服务器,需要注意,在请求中停止会导致请求响应502并且强制关闭所有未处理的请求 +function Http.Stop() end + +---获取Http服务器状态 +---@return 0|1|2 status @0=未初始化 1=未启动 2=运行中 +function Http.GetStatus() end + +---绑定静态资源 +---@param path string url地址 +---@param dir string 本地目录 +function Http.AddMountPoint(path, dir) end + +---移除静态资源 +---@param path string url地址 +function Http.RemoveMountPoint(path) end + +---http请求回调 +---@param method string +---@param api string API名字 +---@param params {string:string} 参数 +---@param body string body内容 +---@return string body 返回内容 +function Http.HttpRequestEventCallback(method, api, params, body) end \ No newline at end of file diff --git a/libs/Gmsv/BATTLE_PVE_PATCH.lua b/libs/Gmsv/BATTLE_PVE_PATCH.lua index 2637bce..8e813ee 100644 --- a/libs/Gmsv/BATTLE_PVE_PATCH.lua +++ b/libs/Gmsv/BATTLE_PVE_PATCH.lua @@ -8,7 +8,7 @@ local Pve = Battle.PVE ---@param DoFunc string ---@param EnemyIdAr number[] ---@param BaseLevelAr number[] ----@param RandLv number[] +---@param RandLv? number[] Battle.PVE = function(CharIndex, CreatePtr, DoFunc, EnemyIdAr, BaseLevelAr, RandLv) if #EnemyIdAr < 1 then return -1; diff --git a/libs/Gmsv/NL.lua b/libs/Gmsv/NL.lua index 2981075..d57ecc2 100644 --- a/libs/Gmsv/NL.lua +++ b/libs/Gmsv/NL.lua @@ -56,6 +56,7 @@ NL.newEvent('CheckDummyDollEvent', 1); -- function PreItemPickUpEventCallBack(CharIndex, ItemIndex) end NL.newEvent('PreItemPickUpEvent', 0); NL.newEvent('ItemBoxGenerateEvent', nil); +NL.newEvent('HttpRequestEvent', ""); --local hookOnCharaDeleted; --hookOnCharaDeleted = ffi.hook.new('int (__cdecl*)(int a1)', function(queueIndex) -- local queuePtr = Addresses.DBQueue + 0x58 * queueIndex diff --git a/types.lua b/types.lua index 8139b0f..d400b4a 100644 --- a/types.lua +++ b/types.lua @@ -54,3 +54,14 @@ NPCPart = NPCPart or {}; ---@return CharIndex function NPCPart:NPC_createNormal(name, image, positionInfo) end +JSON = JSON or {}; + +---@generic T +---@param s string +---@return T +function JSON.decode(s) end + +---@generic T +---@param s T +---@return string +function JSON.encode(s) end \ No newline at end of file diff --git a/www/index.html b/www/index.html new file mode 100644 index 0000000..1080eb4 --- /dev/null +++ b/www/index.html @@ -0,0 +1,49 @@ + + + +
+ + + +