From cda3575d10ebc68de2b83a69a2c0968378374f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Tue, 9 May 2023 23:18:52 +0200 Subject: [PATCH] Quit client when existing config file cannot be loaded When the configuration file exists but cannot be loaded, the client continues to launch. When closing, the client then saves the default config and overwrites the existing config that could not be loaded. This is prevented by quitting the client with an error message popup when the config exists but cannot be loaded. Closes #3843. --- src/engine/client/client.cpp | 8 +++++++- src/engine/console.h | 2 +- src/engine/shared/console.cpp | 17 ++++++++++------- src/engine/shared/console.h | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 997c19e28..08d3c367e 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -4683,7 +4683,13 @@ int main(int argc, const char **argv) // execute config file if(pStorage->FileExists(CONFIG_FILE, IStorage::TYPE_ALL)) { - pConsole->ExecuteFile(CONFIG_FILE); + if(!pConsole->ExecuteFile(CONFIG_FILE)) + { + const char *pError = "Failed to load config from '" CONFIG_FILE "'."; + dbg_msg("client", "%s", pError); + pClient->ShowMessageBox("Config File Error", pError); + return -1; + } } // execute autoexec file diff --git a/src/engine/console.h b/src/engine/console.h index ca8669740..e0708e835 100644 --- a/src/engine/console.h +++ b/src/engine/console.h @@ -109,7 +109,7 @@ public: virtual void ExecuteLine(const char *pStr, int ClientID = -1, bool InterpretSemicolons = true) = 0; virtual void ExecuteLineFlag(const char *pStr, int FlasgMask, int ClientID = -1, bool InterpretSemicolons = true) = 0; virtual void ExecuteLineStroked(int Stroke, const char *pStr, int ClientID = -1, bool InterpretSemicolons = true) = 0; - virtual void ExecuteFile(const char *pFilename, int ClientID = -1, bool LogFailure = false, int StorageType = IStorage::TYPE_ALL) = 0; + virtual bool ExecuteFile(const char *pFilename, int ClientID = -1, bool LogFailure = false, int StorageType = IStorage::TYPE_ALL) = 0; virtual char *Format(char *pBuf, int Size, const char *pFrom, const char *pStr) = 0; virtual void Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor = gs_ConsoleDefaultColor) const = 0; diff --git a/src/engine/shared/console.cpp b/src/engine/shared/console.cpp index 9533b1fcf..4870027ed 100644 --- a/src/engine/shared/console.cpp +++ b/src/engine/shared/console.cpp @@ -622,15 +622,15 @@ void CConsole::ExecuteLineFlag(const char *pStr, int FlagMask, int ClientID, boo m_FlagMask = Temp; } -void CConsole::ExecuteFile(const char *pFilename, int ClientID, bool LogFailure, int StorageType) +bool CConsole::ExecuteFile(const char *pFilename, int ClientID, bool LogFailure, int StorageType) { // make sure that this isn't being executed already for(CExecFile *pCur = m_pFirstExec; pCur; pCur = pCur->m_pPrev) if(str_comp(pFilename, pCur->m_pFilename) == 0) - return; + return false; if(!m_pStorage) - return; + return false; // push this one to the stack CExecFile ThisFile; @@ -642,20 +642,22 @@ void CConsole::ExecuteFile(const char *pFilename, int ClientID, bool LogFailure, // exec the file IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ | IOFLAG_SKIP_BOM, StorageType); - char aBuf[128]; + bool Success = false; + char aBuf[32 + IO_MAX_PATH_LENGTH]; if(File) { - char *pLine; - CLineReader Reader; - str_format(aBuf, sizeof(aBuf), "executing '%s'", pFilename); Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf); + + CLineReader Reader; Reader.Init(File); + char *pLine; while((pLine = Reader.Get())) ExecuteLine(pLine, ClientID); io_close(File); + Success = true; } else if(LogFailure) { @@ -664,6 +666,7 @@ void CConsole::ExecuteFile(const char *pFilename, int ClientID, bool LogFailure, } m_pFirstExec = pPrev; + return Success; } void CConsole::Con_Echo(IResult *pResult, void *pUserData) diff --git a/src/engine/shared/console.h b/src/engine/shared/console.h index a2beb8573..cd496c948 100644 --- a/src/engine/shared/console.h +++ b/src/engine/shared/console.h @@ -214,7 +214,7 @@ public: bool LineIsValid(const char *pStr) override; void ExecuteLine(const char *pStr, int ClientID = -1, bool InterpretSemicolons = true) override; void ExecuteLineFlag(const char *pStr, int FlagMask, int ClientID = -1, bool InterpretSemicolons = true) override; - void ExecuteFile(const char *pFilename, int ClientID = -1, bool LogFailure = false, int StorageType = IStorage::TYPE_ALL) override; + bool ExecuteFile(const char *pFilename, int ClientID = -1, bool LogFailure = false, int StorageType = IStorage::TYPE_ALL) override; char *Format(char *pBuf, int Size, const char *pFrom, const char *pStr) override; void Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor = gs_ConsoleDefaultColor) const override;