AtomicPlayer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Atomic.h>
  24. #include <Atomic/Engine/Engine.h>
  25. #include <Atomic/Engine/EngineConfig.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include <Atomic/IO/Log.h>
  28. #include <Atomic/IO/IOEvents.h>
  29. #include <Atomic/Input/InputEvents.h>
  30. #include <Atomic/Core/Main.h>
  31. #include <Atomic/Core/ProcessUtils.h>
  32. #include <Atomic/Resource/ResourceCache.h>
  33. #include <Atomic/Resource/ResourceEvents.h>
  34. #include <Atomic/Script/ScriptSystem.h>
  35. #include <Atomic/UI/UI.h>
  36. // Move me
  37. #include <Atomic/Environment/Environment.h>
  38. #include <AtomicJS/Javascript/Javascript.h>
  39. #ifdef ATOMIC_DOTNET
  40. #include <AtomicNET/NETCore/NETCore.h>
  41. #include <AtomicNET/NETCore/NETHost.h>
  42. #endif
  43. #include <AtomicPlayer/Player.h>
  44. #include "AtomicPlayer.h"
  45. #include <Atomic/DebugNew.h>
  46. #ifdef __APPLE__
  47. #include <unistd.h>
  48. #endif
  49. DEFINE_APPLICATION_MAIN(AtomicPlayer::AtomicPlayerApp)
  50. namespace AtomicPlayer
  51. {
  52. extern void jsapi_init_atomicplayer(JSVM* vm);
  53. AtomicPlayerApp::AtomicPlayerApp(Context* context) :
  54. Application(context)
  55. {
  56. }
  57. void AtomicPlayerApp::Setup()
  58. {
  59. #ifdef ATOMIC_3D
  60. RegisterEnvironmentLibrary(context_);
  61. #endif
  62. FileSystem* filesystem = GetSubsystem<FileSystem>();
  63. context_->RegisterSubsystem(new ScriptSystem(context_));
  64. // Read the engine configuration
  65. ReadEngineConfig();
  66. engineParameters_.InsertNew("WindowTitle", "AtomicPlayer");
  67. #if (ATOMIC_PLATFORM_ANDROID)
  68. engineParameters_.InsertNew("FullScreen", true);
  69. engineParameters_.InsertNew("ResourcePaths", "CoreData;PlayerData;Cache;AtomicResources");
  70. #elif ATOMIC_PLATFORM_WEB
  71. engineParameters_.InsertNew("FullScreen", false);
  72. engineParameters_.InsertNew("ResourcePaths", "AtomicResources");
  73. // engineParameters_.InsertNew("WindowWidth", 1280);
  74. // engineParameters_.InsertNew("WindowHeight", 720);
  75. #elif ATOMIC_PLATFORM_IOS
  76. engineParameters_.InsertNew("FullScreen", false);
  77. engineParameters_.InsertNew("ResourcePaths", "AtomicResources");
  78. #else
  79. engineParameters_.InsertNew("FullScreen", false);
  80. engineParameters_.InsertNew("WindowWidth", 1280);
  81. engineParameters_.InsertNew("WindowHeight", 720);
  82. engineParameters_.InsertNew("ResourcePaths", "AtomicResources");
  83. #endif
  84. #if ATOMIC_PLATFORM_WINDOWS || ATOMIC_PLATFORM_LINUX
  85. engineParameters_.InsertNew("WindowIcon", "Images/AtomicLogo32.png");
  86. engineParameters_.InsertNew("ResourcePrefixPath", "AtomicPlayer_Resources");
  87. #elif ATOMIC_PLATFORM_ANDROID
  88. //engineParameters_.InsertNew("ResourcePrefixPath"], "assets");
  89. #elif ATOMIC_PLATFORM_OSX
  90. engineParameters_.InsertNew("ResourcePrefixPath", "../Resources");
  91. #endif
  92. const Vector<String>& arguments = GetArguments();
  93. for (unsigned i = 0; i < arguments.Size(); ++i)
  94. {
  95. if (arguments[i].Length() > 1)
  96. {
  97. String argument = arguments[i].ToLower();
  98. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  99. if (argument == "--log-std")
  100. {
  101. SubscribeToEvent(E_LOGMESSAGE, HANDLER(AtomicPlayerApp, HandleLogMessage));
  102. }
  103. }
  104. }
  105. // Use the script file name as the base name for the log file
  106. engineParameters_.InsertNew("LogName", filesystem->GetAppPreferencesDir("AtomicPlayer", "Logs") + "AtomicPlayer.log");
  107. }
  108. void AtomicPlayerApp::Start()
  109. {
  110. Application::Start();
  111. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  112. // Instantiate and register the Javascript subsystem
  113. Javascript* javascript = new Javascript(context_);
  114. context_->RegisterSubsystem(javascript);
  115. vm_ = javascript->InstantiateVM("MainVM");
  116. vm_->InitJSContext();
  117. UI* ui = GetSubsystem<UI>();
  118. ui->Initialize("DefaultUI/language/lng_en.tb.txt");
  119. ui->LoadDefaultPlayerSkin();
  120. SubscribeToEvent(E_JSERROR, HANDLER(AtomicPlayerApp, HandleJSError));
  121. vm_->SetModuleSearchPaths("Modules");
  122. // Instantiate and register the Player subsystem
  123. context_->RegisterSubsystem(new AtomicPlayer::Player(context_));
  124. AtomicPlayer::jsapi_init_atomicplayer(vm_);
  125. JSVM* vm = JSVM::GetJSVM(0);
  126. #ifdef ATOMIC_DOTNET
  127. // Instantiate and register the AtomicNET subsystem
  128. SharedPtr<NETCore> netCore (new NETCore(context_));
  129. context_->RegisterSubsystem(netCore);
  130. String netCoreErrorMsg;
  131. #ifdef ATOMIC_PLATFORM_WINDOWS
  132. String rootNETDir = fileSystem->GetProgramDir() + "AtomicPlayer_Resources/AtomicNET/";
  133. #else
  134. String rootNETDir = fileSystem->GetProgramDir() + "AtomicPlayer_Resources/AtomicNET/";
  135. #endif
  136. NETHost::SetCoreCLRFilesAbsPath(GetNativePath(rootNETDir + "CoreCLR/"));
  137. NETHost::SetCoreCLRTPAPaths(GetNativePath(rootNETDir + "Atomic/TPA/"));
  138. NETHost::SetCoreCLRAssemblyLoadPaths(GetNativePath(rootNETDir + "Atomic/Assemblies/"));
  139. if (!netCore->Initialize(netCoreErrorMsg))
  140. {
  141. LOGERRORF("NetCore: Unable to initialize! %s", netCoreErrorMsg.CString());
  142. context_->RemoveSubsystem(NETCore::GetTypeStatic());
  143. }
  144. else
  145. {
  146. }
  147. #endif
  148. if (!vm->ExecuteMain())
  149. {
  150. SendEvent(E_EXITREQUESTED);
  151. }
  152. return;
  153. }
  154. void AtomicPlayerApp::Stop()
  155. {
  156. vm_ = 0;
  157. context_->RemoveSubsystem<Javascript>();
  158. // make sure JSVM is really down and no outstanding refs
  159. // as if not, will hold on engine subsystems, which is bad
  160. assert(!JSVM::GetJSVM(0));
  161. Application::Stop();
  162. }
  163. void AtomicPlayerApp::HandleScriptReloadStarted(StringHash eventType, VariantMap& eventData)
  164. {
  165. }
  166. void AtomicPlayerApp::HandleScriptReloadFinished(StringHash eventType, VariantMap& eventData)
  167. {
  168. }
  169. void AtomicPlayerApp::HandleScriptReloadFailed(StringHash eventType, VariantMap& eventData)
  170. {
  171. ErrorExit();
  172. }
  173. void AtomicPlayerApp::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  174. {
  175. using namespace LogMessage;
  176. int level = eventData[P_LEVEL].GetInt();
  177. // The message may be multi-line, so split to rows in that case
  178. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  179. for (unsigned i = 0; i < rows.Size(); ++i)
  180. {
  181. if (level == LOG_ERROR)
  182. {
  183. fprintf(stderr, "%s\n", rows[i].CString());
  184. }
  185. else
  186. {
  187. fprintf(stdout, "%s\n", rows[i].CString());
  188. }
  189. }
  190. }
  191. void AtomicPlayerApp::HandleJSError(StringHash eventType, VariantMap& eventData)
  192. {
  193. using namespace JSError;
  194. //String errName = eventData[P_ERRORNAME].GetString();
  195. //String errStack = eventData[P_ERRORSTACK].GetString();
  196. String errMessage = eventData[P_ERRORMESSAGE].GetString();
  197. String errFilename = eventData[P_ERRORFILENAME].GetString();
  198. int errLineNumber = eventData[P_ERRORLINENUMBER].GetInt();
  199. String errorString = ToString("%s - %s - Line: %i",
  200. errFilename.CString(), errMessage.CString(), errLineNumber);
  201. }
  202. void AtomicPlayerApp::ReadEngineConfig()
  203. {
  204. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  205. String filename = fileSystem->GetProgramDir() + "Settings/Engine.json";
  206. if (!fileSystem->FileExists(filename))
  207. return;
  208. if (EngineConfig::LoadFromFile(context_, filename))
  209. {
  210. EngineConfig::ApplyConfig(engineParameters_);
  211. }
  212. }
  213. }