* add server_handle_rehash to Lua

* function in Lua to save GHost state when rehash
This commit is contained in:
HarpyWar 2014-08-02 18:54:17 +04:00
parent 607d798609
commit f4fff62f82
7 changed files with 67 additions and 3 deletions

29
lua/ghost/ghost.lua Normal file
View file

@ -0,0 +1,29 @@
--[[
Copyright (C) 2014 HarpyWar (harpywar@gmail.com)
This file is a part of the PvPGN Project http://pvpgn.pro
Licensed under the same terms as Lua itself.
]]--
function gh_load()
local filename = gh_directory() .. "/users.dmp"
if not file_exists(filename) then
return
end
-- load ghost state
gh_load_userbot(filename)
DEBUG("Loaded GHost state from " .. filename)
end
function gh_unload()
-- save ghost state
local filename = gh_directory() .. "/users.dmp"
gh_save_userbot(filename)
DEBUG("Saved GHost state to " .. filename)
end

View file

@ -29,6 +29,18 @@ end
function gh_del_userbot(username)
user2bot[username] = nil
end
-- save table to file
function gh_save_userbot(filename)
-- do not save empty table
if not next(gh_user2bot) then return end
table.save(gh_user2bot, filename)
end
-- load table from file
function gh_load_userbot(filename)
gh_user2bot = table.load(filename)
end
@ -48,6 +60,11 @@ function gh_read_silentping(username)
end
-- Get path to GHost directory
function gh_directory()
return config.scriptdir .. "/ghost"
end
-- is user owner of bot in current game

View file

@ -15,3 +15,12 @@ function handle_server_mainloop()
-- DEBUG(os.time())
end
-- When restart Lua VM
function handle_server_rehash()
if (config.ghost) then
gh_unload()
end
end

View file

@ -9,9 +9,12 @@
-- Executes after preload all the lua files
function main()
-- start antihack
if (config.ah) then
-- start antihack
ah_init()
end
if (config.ghost) then
gh_load()
end
end

View file

@ -316,7 +316,7 @@ namespace pvpgn
switch (luaevent)
{
case luaevent_command:
func_name = "handle_command_before";
func_name = "handle_command";
break;
case luaevent_command_before:
func_name = "handle_command_before";
@ -584,6 +584,9 @@ namespace pvpgn
case luaevent_server_mainloop:
func_name = "handle_server_mainloop"; // one time per second
break;
case luaevent_server_rehash:
func_name = "handle_server_rehash"; // when restart Lua VM
break;
default:
return;
}

View file

@ -57,6 +57,7 @@ namespace pvpgn
luaevent_user_disconnect,
luaevent_server_start,
luaevent_server_rehash,
luaevent_server_mainloop,
luaevent_client_readmemory

View file

@ -1483,6 +1483,8 @@ namespace pvpgn
#ifdef WITH_LUA
if (do_restart == restart_mode_all || do_restart == restart_mode_lua)
{
lua_handle_server(luaevent_server_rehash);
lua_unload();
lua_load(prefs_get_scriptdir());
}