Skip to content

Commit

Permalink
Add exit code to shutdown function (Fixes #2293) (PR #2298)
Browse files Browse the repository at this point in the history
  • Loading branch information
botder committed Aug 9, 2021
1 parent 7963997 commit 4d1ba12
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 259 deletions.
12 changes: 11 additions & 1 deletion Server/core/CModManagerImpl.cpp
Expand Up @@ -127,7 +127,7 @@ void CModManagerImpl::Unload(bool bKeyPressBeforeTerm)
Print("Press Q to shut down the server!\n");
WaitForKey('q');
}
TerminateProcess(GetCurrentProcess(), 0);
TerminateProcess(GetCurrentProcess(), GetExitCode());
}
#endif
// Unload the library
Expand Down Expand Up @@ -179,6 +179,16 @@ bool CModManagerImpl::GetSleepIntervals(int& iSleepBusyMs, int& iSleepIdleMs, in
return false;
}

void CModManagerImpl::SetExitCode(int exitCode)
{
m_pServer->SetExitCode(exitCode);
}

int CModManagerImpl::GetExitCode() const
{
return m_pServer->GetExitCode();
}

void CModManagerImpl::GetTag(char* szInfoTag, int iInfoTag)
{
if (m_pBase)
Expand Down
3 changes: 3 additions & 0 deletions Server/core/CModManagerImpl.h
Expand Up @@ -52,6 +52,9 @@ class CModManagerImpl : public CModManager
bool GetSleepIntervals(int& iSleepBusyMs, int& iSleepIdleMs, int& iLogicFpsLimit);
CDynamicLibrary& GetDynamicLibrary() { return m_Library; };

void SetExitCode(int exitCode) override;
int GetExitCode() const override;

private:
CServerImpl* m_pServer;

Expand Down
1 change: 1 addition & 0 deletions Server/core/CServerImpl.cpp
Expand Up @@ -64,6 +64,7 @@ CServerImpl::CServerImpl()
m_pNetwork = NULL;
m_bRequestedQuit = false;
m_bRequestedReset = false;
m_exitCode = ERROR_NO_ERROR;
memset(&m_szInputBuffer, 0, sizeof(m_szInputBuffer));
memset(&m_szTag, 0, sizeof(m_szTag) * sizeof(char));
m_uiInputCount = 0;
Expand Down
5 changes: 5 additions & 0 deletions Server/core/CServerImpl.h
Expand Up @@ -67,6 +67,9 @@ class CServerImpl : public CServerInterface
bool HasConsole();
#endif

void SetExitCode(int exitCode) { m_exitCode = exitCode; }
int GetExitCode() const { return m_exitCode; }

private:
void MainLoop();

Expand Down Expand Up @@ -104,6 +107,8 @@ class CServerImpl : public CServerInterface
double m_dLastTimeMs;
double m_dPrevOverrun;

int m_exitCode;

std::vector<std::vector<SString>> m_vecCommandHistory = {{"", ""}};
uint m_uiSelectedCommandHistoryEntry = 0;

Expand Down
5 changes: 5 additions & 0 deletions Server/core/Server.cpp
Expand Up @@ -13,6 +13,7 @@
#include "CServerImpl.h"
#define ALLOC_STATS_MODULE_NAME "core"
#include "SharedUtil.hpp"
#include "ErrorCodes.h"
#ifdef WIN_x86
// TODO - 64 bit file hooks
#include "SharedUtil.Win32Utf8FileHooks.hpp"
Expand Down Expand Up @@ -66,6 +67,10 @@ MTAEXPORT int Run(int iArgumentCount, char* szArguments[])
RemoveUtf8FileHooks();
#endif

// Overwrite the exit code, if we are stopping gracefully without errors
if (iReturn == ERROR_NO_ERROR)
iReturn = Server.GetExitCode();

return iReturn;
}

Expand Down
1 change: 0 additions & 1 deletion Server/mods/deathmatch/StdInc.h
Expand Up @@ -157,7 +157,6 @@ struct SAclRequest;
#include "lua/CLuaManager.h"
#include "lua/CLuaTimerManager.h"
#include "lua/CLuaTimer.h"
#include "lua/CLuaFunctionDefs.h"
#include "lua/CLuaModuleManager.h"
#include "lua/CLuaArgument.h"
#include "lua/CLuaCFunctions.h"
Expand Down
1 change: 0 additions & 1 deletion Server/mods/deathmatch/logic/CGame.cpp
Expand Up @@ -800,7 +800,6 @@ bool CGame::Start(int iArgumentCount, char* szArguments[])
m_pZoneNames = new CZoneNames;

CStaticFunctionDefinitions(this);
CLuaFunctionDefs::Initialize(m_pLuaManager, this);
CLuaDefs::Initialize(this);

m_pPlayerManager->SetScriptDebugging(m_pScriptDebugging);
Expand Down
3 changes: 2 additions & 1 deletion Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Expand Up @@ -11,7 +11,8 @@

#include "StdInc.h"

extern CGame* g_pGame;
extern CGame* g_pGame;
extern CTimeUsMarker<20> markerLatentEvent;

static CLuaManager* m_pLuaManager;
static CColManager* m_pColManager;
Expand Down
55 changes: 0 additions & 55 deletions Server/mods/deathmatch/logic/lua/CLuaFunctionDefs.cpp

This file was deleted.

3 changes: 1 addition & 2 deletions Server/mods/deathmatch/logic/lua/CLuaMain.cpp
Expand Up @@ -10,8 +10,7 @@
*****************************************************************************/

#include "StdInc.h"

#include "CLuaFunctionDefs.h"
#include "luadefs/CLuaFunctionDefs.h"
#include <clocale>

static CLuaManager* m_pLuaManager;
Expand Down
2 changes: 0 additions & 2 deletions Server/mods/deathmatch/logic/lua/CLuaMain.h
Expand Up @@ -20,8 +20,6 @@ class CLuaMain;
#include "CLuaModuleManager.h"
#include "../CTextDisplay.h"

#include "CLuaFunctionDefs.h"

#define MAX_SCRIPTNAME_LENGTH 64

class CBlipManager;
Expand Down
125 changes: 2 additions & 123 deletions Server/mods/deathmatch/logic/lua/CLuaManager.cpp
Expand Up @@ -11,6 +11,7 @@

#include "StdInc.h"
#include "../luadefs/CLuaGenericDefs.h"
#include "../luadefs/CLuaFunctionDefs.h"

extern CGame* g_pGame;

Expand Down Expand Up @@ -154,130 +155,8 @@ CResource* CLuaManager::GetVirtualMachineResource(lua_State* luaVM)

void CLuaManager::LoadCFunctions()
{
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
{"addEvent", CLuaFunctionDefs::AddEvent},
{"addEventHandler", CLuaFunctionDefs::AddEventHandler},
{"removeEventHandler", CLuaFunctionDefs::RemoveEventHandler},
{"getEventHandlers", CLuaFunctionDefs::GetEventHandlers},
{"triggerEvent", CLuaFunctionDefs::TriggerEvent},
{"triggerClientEvent", CLuaFunctionDefs::TriggerClientEvent},
{"cancelEvent", CLuaFunctionDefs::CancelEvent},
{"wasEventCancelled", CLuaFunctionDefs::WasEventCancelled},
{"getCancelReason", CLuaFunctionDefs::GetCancelReason},
{"triggerLatentClientEvent", CLuaFunctionDefs::TriggerLatentClientEvent},
{"getLatentEventHandles", CLuaFunctionDefs::GetLatentEventHandles},
{"getLatentEventStatus", CLuaFunctionDefs::GetLatentEventStatus},
{"cancelLatentEvent", CLuaFunctionDefs::CancelLatentEvent},
{"addDebugHook", CLuaFunctionDefs::AddDebugHook},
{"removeDebugHook", CLuaFunctionDefs::RemoveDebugHook},

// Explosion create funcs
{"createExplosion", CLuaFunctionDefs::CreateExplosion},

// Fire create funcs
// CLuaCFunctions::AddFunction ( "createFire", CLuaFunctionDefinitions::CreateFire );

// Path(node) funcs
// CLuaCFunctions::AddFunction ( "createNode", CLuaFunctionDefinitions::CreateNode );

// Ped body funcs?
{"getBodyPartName", CLuaFunctionDefs::GetBodyPartName},
{"getClothesByTypeIndex", CLuaFunctionDefs::GetClothesByTypeIndex},
{"getTypeIndexFromClothes", CLuaFunctionDefs::GetTypeIndexFromClothes},
{"getClothesTypeName", CLuaFunctionDefs::GetClothesTypeName},

// Weapon funcs
{"getWeaponNameFromID", CLuaFunctionDefs::GetWeaponNameFromID},
{"getWeaponIDFromName", CLuaFunctionDefs::GetWeaponIDFromName},
{"getWeaponProperty", CLuaFunctionDefs::GetWeaponProperty},
{"getOriginalWeaponProperty", CLuaFunctionDefs::GetOriginalWeaponProperty},
{"setWeaponProperty", CLuaFunctionDefs::SetWeaponProperty},
{"setWeaponAmmo", CLuaFunctionDefs::SetWeaponAmmo},
{"getSlotFromWeapon", CLuaFunctionDefs::GetSlotFromWeapon},

#if MTASA_VERSION_TYPE < VERSION_TYPE_RELEASE
{"createWeapon", CLuaFunctionDefs::CreateWeapon},
{"fireWeapon", CLuaFunctionDefs::FireWeapon},
{"setWeaponState", CLuaFunctionDefs::SetWeaponState},
{"getWeaponState", CLuaFunctionDefs::GetWeaponState},
{"setWeaponTarget", CLuaFunctionDefs::SetWeaponTarget},
{"getWeaponTarget", CLuaFunctionDefs::GetWeaponTarget},
{"setWeaponOwner", CLuaFunctionDefs::SetWeaponOwner},
{"getWeaponOwner", CLuaFunctionDefs::GetWeaponOwner},
{"setWeaponFlags", CLuaFunctionDefs::SetWeaponFlags},
{"getWeaponFlags", CLuaFunctionDefs::GetWeaponFlags},
{"setWeaponFiringRate", CLuaFunctionDefs::SetWeaponFiringRate},
{"getWeaponFiringRate", CLuaFunctionDefs::GetWeaponFiringRate},
{"resetWeaponFiringRate", CLuaFunctionDefs::ResetWeaponFiringRate},
{"getWeaponAmmo", CLuaFunctionDefs::GetWeaponAmmo},
{"getWeaponClipAmmo", CLuaFunctionDefs::GetWeaponClipAmmo},
{"setWeaponClipAmmo", CLuaFunctionDefs::SetWeaponClipAmmo},
#endif

// Console funcs
{"addCommandHandler", CLuaFunctionDefs::AddCommandHandler},
{"removeCommandHandler", CLuaFunctionDefs::RemoveCommandHandler},
{"executeCommandHandler", CLuaFunctionDefs::ExecuteCommandHandler},
{"getCommandHandlers", CLuaFunctionDefs::GetCommandHandlers},

// Loaded map funcs
{"getRootElement", CLuaFunctionDefs::GetRootElement},
{"loadMapData", CLuaFunctionDefs::LoadMapData},
{"saveMapData", CLuaFunctionDefs::SaveMapData},

// All-Seeing Eye Functions
{"getGameType", CLuaFunctionDefs::GetGameType},
{"getMapName", CLuaFunctionDefs::GetMapName},
{"setGameType", CLuaFunctionDefs::SetGameType},
{"setMapName", CLuaFunctionDefs::SetMapName},
{"getRuleValue", CLuaFunctionDefs::GetRuleValue},
{"setRuleValue", CLuaFunctionDefs::SetRuleValue},
{"removeRuleValue", CLuaFunctionDefs::RemoveRuleValue},

// Registry functions
{"getPerformanceStats", CLuaFunctionDefs::GetPerformanceStats},

// Admin functions
/*
CLuaCFunctions::AddFunction ( "aexec", CLuaFunctionDefinitions::Aexec },
CLuaCFunctions::AddFunction ( "kickPlayer", CLuaFunctionDefinitions::KickPlayer },
CLuaCFunctions::AddFunction ( "banPlayer", CLuaFunctionDefinitions::BanPlayer },
CLuaCFunctions::AddFunction ( "banPlayerIP", CLuaFunctionDefinitions::BanPlayerIP },
CLuaCFunctions::AddFunction ( "setPlayerMuted", CLuaFunctionDefinitions::SetPlayerMuted },
CLuaCFunctions::AddFunction ( "addAccount", CLuaFunctionDefinitions::AddAccount },
CLuaCFunctions::AddFunction ( "delAccount", CLuaFunctionDefinitions::DelAccount },
CLuaCFunctions::AddFunction ( "setAccountPassword", CLuaFunctionDefinitions::SetAccountPassword },
*/

// Misc funcs
{"resetMapInfo", CLuaFunctionDefs::ResetMapInfo},
{"getServerPort", CLuaFunctionDefs::GetServerPort},

// Settings registry funcs
{"get", CLuaFunctionDefs::Get},
{"set", CLuaFunctionDefs::Set},

// Utility
{"getVersion", CLuaFunctionDefs::GetVersion},
{"getNetworkUsageData", CLuaFunctionDefs::GetNetworkUsageData},
{"getNetworkStats", CLuaFunctionDefs::GetNetworkStats},
{"getLoadedModules", CLuaFunctionDefs::GetModules},
{"getModuleInfo", CLuaFunctionDefs::GetModuleInfo},

{"setDevelopmentMode", CLuaFunctionDefs::SetDevelopmentMode},
{"getDevelopmentMode", CLuaFunctionDefs::GetDevelopmentMode},
};

// Add all functions
for (const auto& [name, func] : functions)
CLuaCFunctions::AddFunction(name, func);

// Restricted functions
CLuaCFunctions::AddFunction("setServerConfigSetting", CLuaFunctionDefs::SetServerConfigSetting, true);
CLuaCFunctions::AddFunction("shutdown", CLuaFunctionDefs::shutdown, true);

// Load the functions from our classes
CLuaFunctionDefs::LoadFunctions();
CLuaACLDefs::LoadFunctions();
CLuaAccountDefs::LoadFunctions();
CLuaBanDefs::LoadFunctions();
Expand Down
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/luadefs/CLuaDefs.cpp
Expand Up @@ -72,6 +72,7 @@ void CLuaDefs::Initialize(CGame* pGame)
m_pResourceManager = pGame->GetResourceManager();
m_pACLManager = pGame->GetACLManager();
m_pMainConfig = pGame->GetConfig();
m_pLuaModuleManager = m_pLuaManager->GetLuaModuleManager();
}

bool CLuaDefs::CanUseFunction(const char* szFunction, lua_State* luaVM, bool bRestricted)
Expand Down
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/luadefs/CLuaDefs.h
Expand Up @@ -85,6 +85,7 @@ class CLuaDefs
static CResourceManager* m_pResourceManager;
static CAccessControlListManager* m_pACLManager;
static CMainConfig* m_pMainConfig;
static inline CLuaModuleManager* m_pLuaModuleManager = nullptr;

protected:
// Old style: Only warn on failure. This should
Expand Down
Expand Up @@ -10,6 +10,7 @@
*****************************************************************************/

#include "StdInc.h"
#include "CLuaFunctionDefs.h"

int CLuaFunctionDefs::GetBodyPartName(lua_State* luaVM)
{
Expand Down Expand Up @@ -107,4 +108,4 @@ int CLuaFunctionDefs::GetClothesTypeName(lua_State* luaVM)

lua_pushboolean(luaVM, false);
return 1;
}
}
Expand Up @@ -10,8 +10,12 @@
*****************************************************************************/

#include "StdInc.h"
#include "CLuaFunctionDefs.h"

#define MIN_SERVER_REQ_TRIGGERCLIENTEVENT_SENDLIST "1.3.0-9.04570"

extern CTimeUsMarker<20> markerLatentEvent;

int CLuaFunctionDefs::AddEvent(lua_State* luaVM)
{
// bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] )
Expand Down
Expand Up @@ -10,6 +10,7 @@
*****************************************************************************/

#include "StdInc.h"
#include "CLuaFunctionDefs.h"

int CLuaFunctionDefs::CreateExplosion(lua_State* luaVM)
{
Expand Down

0 comments on commit 4d1ba12

Please sign in to comment.