AppBase.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/IO/IOEvents.h>
  24. #include <Atomic/Engine/Engine.h>
  25. #include <Atomic/Engine/EngineConfig.h>
  26. #include <Atomic/Script/ScriptSystem.h>
  27. #include <AtomicJS/Javascript/Javascript.h>
  28. // Move me!
  29. #include <Atomic/Environment/Environment.h>
  30. #include "AppBase.h"
  31. namespace Atomic
  32. {
  33. Vector<String> AppBase::engineConfigSearchPaths_;
  34. Vector<String> AppBase::arguments_;
  35. AppBase::AppBase(Context* context) :
  36. Application(context)
  37. {
  38. // Copy arguments
  39. if (!arguments_.Size())
  40. {
  41. arguments_ = GetArguments();
  42. }
  43. else
  44. {
  45. // Add quotes to any arguments that need them, and construct command line
  46. Vector<String> args = arguments_;
  47. QuoteArguments(args);
  48. String commandline = String::Joined(args, " ");
  49. ParseArguments(commandline, false);
  50. arguments_ = GetArguments();
  51. }
  52. }
  53. AppBase::~AppBase()
  54. {
  55. }
  56. void AppBase::AddArgument(const String& argument)
  57. {
  58. if (argument == "--autometrics")
  59. {
  60. Application::autoMetrics_ = true;
  61. }
  62. arguments_.Push(argument);
  63. }
  64. unsigned AppBase::GetNumArguments()
  65. {
  66. return arguments_.Size();
  67. }
  68. String AppBase::GetArgument(unsigned index)
  69. {
  70. return arguments_[index];
  71. }
  72. void AppBase::ProcessArguments()
  73. {
  74. for (unsigned i = 0; i < arguments_.Size(); ++i)
  75. {
  76. if (arguments_[i].Length() > 1)
  77. {
  78. String argument = arguments_[i].ToLower();
  79. String value = i + 1 < arguments_.Size() ? arguments_[i + 1] : String::EMPTY;
  80. if (argument == "--log-std")
  81. {
  82. SubscribeToEvent(E_LOGMESSAGE, ATOMIC_HANDLER(AppBase, HandleLogMessage));
  83. }
  84. }
  85. }
  86. }
  87. void AppBase::Setup()
  88. {
  89. Application::Setup();
  90. #ifdef ATOMIC_3D
  91. // Move me!
  92. RegisterEnvironmentLibrary(context_);
  93. #endif
  94. ProcessArguments();
  95. // Read the engine configuration
  96. ReadEngineConfig();
  97. context_->RegisterSubsystem(new ScriptSystem(context_));
  98. // Instantiate and register the Javascript subsystem
  99. Javascript* javascript = new Javascript(context_);
  100. context_->RegisterSubsystem(javascript);
  101. vm_ = javascript->InstantiateVM("MainVM");
  102. SubscribeToEvent(E_JSERROR, ATOMIC_HANDLER(AppBase, HandleJSError));
  103. }
  104. void AppBase::Start()
  105. {
  106. Application::Start();
  107. vm_->InitJSContext();
  108. }
  109. bool AppBase::RunFrame()
  110. {
  111. engine_->RunFrame();
  112. if (engine_->IsExiting())
  113. {
  114. return false;
  115. }
  116. return true;
  117. }
  118. void AppBase::Stop()
  119. {
  120. vm_ = 0;
  121. context_->RemoveSubsystem<Javascript>();
  122. // make sure JSVM is really down and no outstanding refs
  123. // as if not, will hold on engine subsystems, which is bad
  124. assert(!JSVM::GetJSVM(0));
  125. Application::Stop();
  126. }
  127. void AppBase::ReadEngineConfig()
  128. {
  129. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  130. // if we haven't defined any search paths, insert default
  131. if (!engineConfigSearchPaths_.Size())
  132. {
  133. #ifdef ATOMIC_PLATFORM_OSX
  134. AddEngineConfigSearchPath(fileSystem->GetProgramDir() + "../Resources/Settings/");
  135. #else
  136. AddEngineConfigSearchPath(fileSystem->GetProgramDir() + "Settings/");
  137. #endif
  138. }
  139. for (unsigned i = 0; i < engineConfigSearchPaths_.Size(); i++)
  140. {
  141. const String& path = engineConfigSearchPaths_[i];
  142. if (!fileSystem->DirExists(path))
  143. continue;
  144. String filename = AddTrailingSlash(path) + "Engine.json";
  145. // take the 1st Engine.json found in the paths
  146. if (fileSystem->FileExists(filename))
  147. {
  148. if (EngineConfig::LoadFromFile(context_, filename))
  149. {
  150. EngineConfig::ApplyConfig(engineParameters_);
  151. }
  152. return;
  153. }
  154. }
  155. }
  156. // Handlers
  157. void AppBase::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  158. {
  159. using namespace LogMessage;
  160. int level = eventData[P_LEVEL].GetInt();
  161. // The message may be multi-line, so split to rows in that case
  162. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  163. for (unsigned i = 0; i < rows.Size(); ++i)
  164. {
  165. if (level == LOG_ERROR)
  166. {
  167. fprintf(stderr, "%s\n", rows[i].CString());
  168. }
  169. else
  170. {
  171. fprintf(stdout, "%s\n", rows[i].CString());
  172. }
  173. }
  174. }
  175. void AppBase::HandleJSError(StringHash eventType, VariantMap& eventData)
  176. {
  177. using namespace JSError;
  178. String errMessage = eventData[P_ERRORMESSAGE].GetString();
  179. String errFilename = eventData[P_ERRORFILENAME].GetString();
  180. int errLineNumber = eventData[P_ERRORLINENUMBER].GetInt();
  181. String errorString = ToString("%s - %s - Line: %i",
  182. errFilename.CString(), errMessage.CString(), errLineNumber);
  183. ATOMIC_LOGERROR(errorString);
  184. }
  185. bool AppBase::GetDebuggerAttached()
  186. {
  187. #ifdef ATOMIC_PLATFORM_WINDOWS
  188. return IsDebuggerPresent() ? true : false;
  189. #else
  190. return false;
  191. #endif
  192. }
  193. }