From 8c417384e29635c680745425abd630d5208cbaf2 Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 18 Mar 2013 15:27:26 +0100 Subject: replace mod-param with generic "-config key=val"-param --- CCommandLine.cpp | 38 +++++++++++++++++++++----- CCommandLine.h | 4 ++- main.cpp | 81 ++++++++++++++++++++++++++++++++------------------------ 3 files changed, 82 insertions(+), 41 deletions(-) diff --git a/CCommandLine.cpp b/CCommandLine.cpp index 3df07b1..afb115d 100644 --- a/CCommandLine.cpp +++ b/CCommandLine.cpp @@ -31,18 +31,28 @@ void CCommandLine::AddParm(const char *psz) m_argc++; } -const char* CCommandLine::ParmValue(const char *psz, const char *pDefaultVal) const +const char* CCommandLine::ParmValues(const char *psz, unsigned int ix) const { - int nIndex = FindParm(psz); + int nIndex = FindParm(psz, ix); if((nIndex == 0) || (nIndex == m_argc - 1)) - return pDefaultVal; + return NULL; if(m_argv[nIndex + 1][0] == '-' || m_argv[nIndex + 1][0] == '+') - return pDefaultVal; + return NULL; return m_argv[nIndex + 1]; } +const char* CCommandLine::ParmValue(const char *psz, const char *pDefaultVal) const +{ + const char* cszValue = ParmValues(psz, 0); + + if(!cszValue) + return pDefaultVal; + + return cszValue; +} + int CCommandLine::ParmValue(const char *psz, int nDefaultVal) const { const char* cszValue = ParmValue(psz); @@ -58,12 +68,28 @@ unsigned int CCommandLine::ParmCount() const return m_argc; } -unsigned int CCommandLine::FindParm(const char *psz) const +unsigned int CCommandLine::FindParmCount(const char *psz) const +{ + unsigned cnt = 0; + for(int i = 1; i < m_argc; i++) + { + if(strcmp(m_argv[i], psz) == 0) + cnt++; + } + + return cnt; +} + +unsigned int CCommandLine::FindParm(const char *psz, unsigned int ix) const { for(int i = 1; i < m_argc; i++) { if(strcmp(m_argv[i], psz) == 0) - return i; + { + if (ix == 0) + return i; + ix--; + } } return 0; diff --git a/CCommandLine.h b/CCommandLine.h index 899a875..0d4817d 100644 --- a/CCommandLine.h +++ b/CCommandLine.h @@ -13,11 +13,13 @@ public: CCommandLine(int argc, char **argv); ~CCommandLine(); + const char* ParmValues(const char *psz, unsigned int ix) const; const char* ParmValue(const char *psz, const char *pDefaultVal = 0) const; int ParmValue(const char *psz, int nDefaultVal) const; unsigned int ParmCount() const; - unsigned int FindParm(const char *psz) const; + unsigned int FindParmCount(const char *psz) const; + unsigned int FindParm(const char *psz, unsigned int ix = 0) const; const char* GetParm(unsigned int nIndex) const; void AddParm(const char *psz); diff --git a/main.cpp b/main.cpp index 394f5b8..d126fdf 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "CCommandLine.h" @@ -22,6 +23,12 @@ #define strcasecmp _stricmp #endif +#ifdef _WIN32 + #define _TOOL_EXT "" +#else + #define _TOOL_EXT ".sh" +#endif + #define EXIT_GAME_UPDATED (1 << 2) #define EXIT_UPDATE_RELEASED EXIT_GAME_UPDATED @@ -61,7 +68,7 @@ private: EUpdateResult InstallOrUpdateApp(AppId_t uAppId, bool bVerifyAll = false, const char* cszBetaKey = NULL, const char* cszBetaPassword = NULL); bool UninstallApp(AppId_t uAppId); void ShowAvailableApps(); - void ParseAppConfig(AppId_t uAppId); + bool ParseAppConfig(AppId_t uAppId); void Msg(const char* cszFormat, ...); void Error(const char* cszFormat, ...); @@ -161,7 +168,9 @@ void CApplication::Exit(int iCode) bool CApplication::UninstallApp(AppId_t uAppId) { - ParseAppConfig(uAppId); + if (!ParseAppConfig(uAppId)) + return false; + if(g_pClientAppManager->GetAppInstallState(uAppId) & k_EAppStateUninstalled) { Error("This app (%u:%s) isn't installed\n", uAppId, GetAppName(uAppId)); @@ -257,11 +266,23 @@ void CApplication::OnLicensesUpdated(LicensesUpdated_t* pParam) } } -void CApplication::ParseAppConfig(AppId_t uAppId) +bool CApplication::ParseAppConfig(AppId_t uAppId) { - const char* mod = commandLine.ParmValue("-mod"); - if (mod) - g_pClientAppManager->SetAppConfigValue(uAppId, "mod", mod); + bool ret = true; + for(unsigned int i = 0; i < commandLine.FindParmCount("-config"); i++) + { + std::string keyval = commandLine.ParmValues("-config", i); + size_t pos = keyval.find_first_of('='); + if (pos == std::string::npos) + { + Error("Invalid syntax for param \"-config %s\"\n", keyval.c_str()); + ret = false; + continue; + } + g_pClientAppManager->SetAppConfigValue(uAppId, keyval.substr(0, pos).c_str(), + keyval.substr(pos + 1).c_str()); + } + return ret; } void CApplication::OnAppEventStateChange(AppEventStateChange_t* pParam) @@ -348,7 +369,9 @@ CApplication::EUpdateResult CApplication::InstallOrUpdateApp(AppId_t uAppId, boo } } - ParseAppConfig(uAppId); + if (!ParseAppConfig(uAppId)) + return k_EUpdateResultFailed; + EAppState eState = g_pClientAppManager->GetAppInstallState(uAppId); if(eState == k_EAppStateInvalid || eState & k_EAppStateUninstalled) { @@ -434,7 +457,12 @@ void CApplication::OnAppInfoUpdateComplete(AppInfoUpdateComplete_t* pParam) { AppId_t uAppId = commandLine.ParmValue("-game", (int)k_uAppIdInvalid); - ParseAppConfig(uAppId); + if (!ParseAppConfig(uAppId)) + { + this->Exit(EXIT_FAILURE); + return; + } + if(g_pClientAppManager->GetAppInstallState(uAppId) & k_EAppStateUninstalled) { Error("This app (%u:%s) isn't installed\n", uAppId, GetAppName(uAppId)); @@ -751,10 +779,12 @@ bool CApplication::ParseScript(const char* szFilename) } else if(strcasecmp(argv[0], "app_set_config") == 0) { - if(argc >= 4 && strcasecmp(argv[2], "mod") == 0) + if(argc == 4) { - commandLine.AddParm("-mod"); - commandLine.AddParm(argv[3]); + commandLine.AddParm("-config"); + std::ostringstream oss; + oss << argv[2] << "=" << argv[3]; + commandLine.AddParm(oss.str().c_str()); } } } @@ -770,8 +800,8 @@ bool CApplication::CheckCommandline() { printf ( - "Use: %s%s -command [parameters] [flags]\n" - "Or: %s%s +runscript \n" + "Use: %s"_TOOL_EXT" -command [parameters] [flags]\n" + "Or: %s"_TOOL_EXT" +runscript \n" "\n" "Commands:\n" " update: Install or update a game\n" @@ -782,7 +812,7 @@ bool CApplication::CheckCommandline() "Parameters:\n" " -game - Game AppID to install / update / uninstall\n" " (use '-command list' to see available games)\n" - " -mod - Set game mod to install / update / uninstall (e.g. czero)\n" + " -config = - Set app specific settings (e.g. -config mod=czero,dod)\n" " -dir - Game install dir\n" " (if dir not specified, will use %s/steamapps/common//)\n" " -username - Steam account username\n" @@ -795,32 +825,15 @@ bool CApplication::CheckCommandline() " -verify_all - Verify all game files are up to date\n" " -remember_password - Remember password\n" "\n" - "For example: %s%s -command update -game 740 -dir %s -username foo -password bar\n" - , commandLine.GetParm(0), -#ifdef _WIN32 - "" -#else - ".sh" -#endif - , commandLine.GetParm(0), -#ifdef _WIN32 - "" -#else - ".sh" -#endif - , + "For example: %s"_TOOL_EXT" -command update -game 740 -dir %s -username foo -password bar\n", + commandLine.GetParm(0), + commandLine.GetParm(0), #ifdef _WIN32 ".", #else "~/Steam", #endif commandLine.GetParm(0), -#ifdef _WIN32 - "" -#else - ".sh" -#endif - , #ifdef _WIN32 "C:\\csgo" #else -- cgit v1.2.3