AppBase.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. AppBase::AppBase(Context* context) :
  35. Application(context)
  36. {
  37. // Copy arguments
  38. arguments_ = GetArguments();
  39. }
  40. AppBase::~AppBase()
  41. {
  42. }
  43. void AppBase::ProcessArguments()
  44. {
  45. for (unsigned i = 0; i < arguments_.Size(); ++i)
  46. {
  47. if (arguments_[i].Length() > 1)
  48. {
  49. String argument = arguments_[i].ToLower();
  50. String value = i + 1 < arguments_.Size() ? arguments_[i + 1] : String::EMPTY;
  51. if (argument == "--log-std")
  52. {
  53. SubscribeToEvent(E_LOGMESSAGE, HANDLER(AppBase, HandleLogMessage));
  54. }
  55. }
  56. }
  57. }
  58. void AppBase::Setup()
  59. {
  60. Application::Setup();
  61. #ifdef ATOMIC_3D
  62. // Move me!
  63. RegisterEnvironmentLibrary(context_);
  64. #endif
  65. ProcessArguments();
  66. // Read the engine configuration
  67. ReadEngineConfig();
  68. context_->RegisterSubsystem(new ScriptSystem(context_));
  69. // Instantiate and register the Javascript subsystem
  70. Javascript* javascript = new Javascript(context_);
  71. context_->RegisterSubsystem(javascript);
  72. SubscribeToEvent(E_JSERROR, HANDLER(AppBase, HandleJSError));
  73. }
  74. void AppBase::Start()
  75. {
  76. Application::Start();
  77. Javascript* javascript = GetSubsystem<Javascript>();
  78. vm_ = javascript->InstantiateVM("MainVM");
  79. vm_->InitJSContext();
  80. }
  81. void AppBase::Stop()
  82. {
  83. vm_ = 0;
  84. context_->RemoveSubsystem<Javascript>();
  85. // make sure JSVM is really down and no outstanding refs
  86. // as if not, will hold on engine subsystems, which is bad
  87. assert(!JSVM::GetJSVM(0));
  88. Application::Stop();
  89. }
  90. void AppBase::ReadEngineConfig()
  91. {
  92. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  93. for (unsigned i = 0; i < engineConfigSearchPaths_.Size(); i++)
  94. {
  95. const String& path = engineConfigSearchPaths_[i];
  96. if (!fileSystem->DirExists(path))
  97. continue;
  98. String filename = AddTrailingSlash(path) + "Engine.json";
  99. if (!fileSystem->FileExists(filename))
  100. return;
  101. if (EngineConfig::LoadFromFile(context_, filename))
  102. {
  103. EngineConfig::ApplyConfig(engineParameters_);
  104. }
  105. }
  106. }
  107. // Handlers
  108. void AppBase::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  109. {
  110. using namespace LogMessage;
  111. int level = eventData[P_LEVEL].GetInt();
  112. // The message may be multi-line, so split to rows in that case
  113. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  114. for (unsigned i = 0; i < rows.Size(); ++i)
  115. {
  116. if (level == LOG_ERROR)
  117. {
  118. fprintf(stderr, "%s\n", rows[i].CString());
  119. }
  120. else
  121. {
  122. fprintf(stdout, "%s\n", rows[i].CString());
  123. }
  124. }
  125. }
  126. void AppBase::HandleJSError(StringHash eventType, VariantMap& eventData)
  127. {
  128. using namespace JSError;
  129. String errMessage = eventData[P_ERRORMESSAGE].GetString();
  130. String errFilename = eventData[P_ERRORFILENAME].GetString();
  131. int errLineNumber = eventData[P_ERRORLINENUMBER].GetInt();
  132. String errorString = ToString("%s - %s - Line: %i",
  133. errFilename.CString(), errMessage.CString(), errLineNumber);
  134. LOGERROR(errorString);
  135. }
  136. }