ConsoleBatchFile.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : Executes an ASCII batch file of console commands...
  9. #include "CrySystem_precompiled.h"
  10. #include "ConsoleBatchFile.h"
  11. #include "IConsole.h"
  12. #include "ISystem.h"
  13. #include "XConsole.h"
  14. #include <CryPath.h>
  15. #include <stdio.h>
  16. #include "System.h"
  17. #include <AzCore/IO/FileIO.h>
  18. IConsole* CConsoleBatchFile::m_pConsole = NULL;
  19. void CConsoleBatchFile::Init()
  20. {
  21. m_pConsole = gEnv->pConsole;
  22. REGISTER_COMMAND("exec", (ConsoleCommandFunc)ExecuteFileCmdFunc, 0, "executes a batch file of console commands");
  23. }
  24. //////////////////////////////////////////////////////////////////////////
  25. void CConsoleBatchFile::ExecuteFileCmdFunc(IConsoleCmdArgs* args)
  26. {
  27. if (!m_pConsole)
  28. {
  29. Init();
  30. }
  31. if (!args->GetArg(1))
  32. {
  33. return;
  34. }
  35. ExecuteConfigFile(args->GetArg(1));
  36. }
  37. //////////////////////////////////////////////////////////////////////////
  38. bool CConsoleBatchFile::ExecuteConfigFile(const char* sFilename)
  39. {
  40. if (!sFilename)
  41. {
  42. return false;
  43. }
  44. if (!m_pConsole)
  45. {
  46. Init();
  47. }
  48. AZStd::string filename;
  49. if (sFilename[0] != '@') // console config files are actually by default in @products@ instead of @products@
  50. {
  51. // However, if we've passed in a relative or absolute path that matches an existing file name,
  52. // don't change it. Only change it to "@products@/filename" and strip off any relative paths
  53. // if the given pattern *didn't* match a file.
  54. if (AZ::IO::FileIOBase::GetDirectInstance()->Exists(sFilename))
  55. {
  56. filename = sFilename;
  57. }
  58. else
  59. {
  60. filename = PathUtil::Make("@products@", PathUtil::GetFile(sFilename));
  61. }
  62. }
  63. else
  64. {
  65. filename = sFilename;
  66. }
  67. if (strlen(PathUtil::GetExt(filename.c_str())) == 0)
  68. {
  69. filename = PathUtil::ReplaceExtension(filename, "cfg");
  70. }
  71. //////////////////////////////////////////////////////////////////////////
  72. CCryFile file;
  73. {
  74. [[maybe_unused]] const char* szLog = "Executing console batch file (try game,config,root):";
  75. AZStd::string filenameLog;
  76. AZStd::string sfn = PathUtil::GetFile(filename);
  77. if (file.Open(filename.c_str(), "rb"))
  78. {
  79. filenameLog = AZStd::string("game/") + sfn;
  80. }
  81. else if (file.Open((AZStd::string("config/") + sfn).c_str(), "rb"))
  82. {
  83. filenameLog = AZStd::string("game/config/") + sfn;
  84. }
  85. else if (file.Open((AZStd::string("./") + sfn).c_str(), "rb"))
  86. {
  87. filenameLog = AZStd::string("./") + sfn;
  88. }
  89. else
  90. {
  91. CryLog("%s \"%s\" not found!", szLog, filename.c_str());
  92. return false;
  93. }
  94. CryLog("%s \"%s\" found in %s ...", szLog, PathUtil::GetFile(filenameLog.c_str()), PathUtil::GetPath(filenameLog).c_str());
  95. }
  96. size_t nLen = file.GetLength();
  97. char* sAllText = new char [nLen + 16];
  98. file.ReadRaw(sAllText, nLen);
  99. sAllText[nLen] = '\0';
  100. sAllText[nLen + 1] = '\0';
  101. /*
  102. This can't work properly as ShowConsole() can be called during the execution of the scripts,
  103. which means bConsoleStatus is outdated and must not be set at the end of the function
  104. bool bConsoleStatus = ((CXConsole*)m_pConsole)->GetStatus();
  105. ((CXConsole*)m_pConsole)->SetStatus(false);
  106. */
  107. char* strLast = sAllText + nLen;
  108. char* str = sAllText;
  109. while (str < strLast)
  110. {
  111. char* s = str;
  112. while (str < strLast && *str != '\n' && *str != '\r')
  113. {
  114. str++;
  115. }
  116. *str = '\0';
  117. str++;
  118. while (str < strLast && (*str == '\n' || *str == '\r'))
  119. {
  120. str++;
  121. }
  122. AZStd::string strLine = s;
  123. //trim all whitespace characters at the beginning and the end of the current line and store its size
  124. AZ::StringFunc::TrimWhiteSpace(strLine, true, true);
  125. size_t strLineSize = strLine.size();
  126. //skip comments, comments start with ";" or "--" but may have preceding whitespace characters
  127. if (strLineSize > 0)
  128. {
  129. if (strLine[0] == ';')
  130. {
  131. continue;
  132. }
  133. else if (strLine.find("--") == 0)
  134. {
  135. continue;
  136. }
  137. }
  138. //skip empty lines
  139. else
  140. {
  141. continue;
  142. }
  143. {
  144. m_pConsole->ExecuteString(strLine.c_str());
  145. }
  146. }
  147. // See above
  148. // ((CXConsole*)m_pConsole)->SetStatus(bConsoleStatus);
  149. delete []sAllText;
  150. return true;
  151. }