AEPlayerApp.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/Atomic.h>
  23. #include <Atomic/Engine/Engine.h>
  24. #include <Atomic/Engine/EngineConfig.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include <Atomic/IO/Log.h>
  27. #include <Atomic/IO/IOEvents.h>
  28. #include <Atomic/Input/InputEvents.h>
  29. #include <Atomic/Core/Main.h>
  30. #include <Atomic/Core/ProcessUtils.h>
  31. #include <Atomic/Resource/ResourceCache.h>
  32. #include <Atomic/Resource/ResourceEvents.h>
  33. #include <Atomic/UI/UI.h>
  34. // Move me
  35. #include <Atomic/Environment/Environment.h>
  36. #include <AtomicJS/Javascript/Javascript.h>
  37. #ifdef ATOMIC_DOTNET
  38. #include <AtomicNET/NETCore/NETCore.h>
  39. #include <AtomicNET/NETScript/NETScript.h>
  40. #endif
  41. #include "../PlayerMode/AEPlayerMode.h"
  42. #include <AtomicPlayer/Player.h>
  43. #include "../PlayerMode/AEPlayerEvents.h"
  44. #include "AEPlayerApp.h"
  45. #include <Atomic/DebugNew.h>
  46. #ifdef __APPLE__
  47. #include <unistd.h>
  48. #endif
  49. namespace AtomicPlayer
  50. {
  51. extern void jsapi_init_atomicplayer(JSVM* vm);
  52. }
  53. namespace AtomicEditor
  54. {
  55. AEPlayerApplication::AEPlayerApplication(Context* context) :
  56. AEEditorCommon(context),
  57. debugPlayer_(false),
  58. launchedByEditor_(false)
  59. {
  60. }
  61. void AEPlayerApplication::Setup()
  62. {
  63. AEEditorCommon::Setup();
  64. // Read the project engine configuration
  65. ReadEngineConfig();
  66. engine_->SetAutoExit(false);
  67. engineParameters_.InsertNew("WindowTitle", "AtomicPlayer");
  68. // Set defaults not already set from config
  69. #if (ATOMIC_PLATFORM_ANDROID)
  70. engineParameters_.InsertNew("FullScreen", true);
  71. engineParameters_.InsertNew("ResourcePaths", "CoreData;AtomicResources");
  72. #elif ATOMIC_PLATFORM_WEB
  73. engineParameters_.InsertNew("FullScreen", false);
  74. engineParameters_.InsertNew("ResourcePaths", "AtomicResources");
  75. // engineParameters_.InsertNew("WindowWidth", 1280);
  76. // engineParameters_.InsertNew("WindowHeight", 720);
  77. #elif ATOMIC_PLATFORM_IOS
  78. engineParameters_.InsertNew("FullScreen", false);
  79. engineParameters_.InsertNew("ResourcePaths", "AtomicResources)";
  80. #else
  81. engineParameters_.InsertNew("FullScreen", false);
  82. engineParameters_.InsertNew("WindowWidth", 1280);
  83. engineParameters_.InsertNew("WindowHeight", 720);
  84. #endif
  85. engineParameters_.InsertNew("LogLevel", LOG_DEBUG);
  86. #if ATOMIC_PLATFORM_WINDOWS || ATOMIC_PLATFORM_LINUX
  87. engineParameters_.InsertNew("WindowIcon", "Images/AtomicLogo32.png");
  88. engineParameters_.InsertNew("ResourcePrefixPath", "AtomicPlayer_Resources");
  89. #elif ATOMIC_PLATFORM_ANDROID
  90. //engineParameters_.InsertNew("ResourcePrefixPath", "assets");
  91. #elif ATOMIC_PLATFORM_OSX
  92. engineParameters_.InsertNew("ResourcePrefixPath", "../Resources");
  93. #endif
  94. // Read command line arguments, potentially overwriting project settings
  95. ReadCommandLineArguments();
  96. // Re-apply project settings if running from editor play button
  97. if (launchedByEditor_)
  98. {
  99. EngineConfig::ApplyConfig(engineParameters_, true);
  100. }
  101. // Use the script file name as the base name for the log file
  102. engineParameters_["LogName"] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("AtomicPlayer", "Logs") + "AtomicPlayer.log";
  103. }
  104. void AEPlayerApplication::ReadEngineConfig()
  105. {
  106. // find the project path from the command line args
  107. String projectPath;
  108. const Vector<String>& arguments = GetArguments();
  109. for (unsigned i = 0; i < arguments.Size(); ++i)
  110. {
  111. if (arguments[i].Length() > 1)
  112. {
  113. String argument = arguments[i].ToLower();
  114. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  115. if (argument == "--project" && value.Length())
  116. {
  117. projectPath = AddTrailingSlash(value);
  118. break;
  119. }
  120. }
  121. }
  122. if (!projectPath.Length())
  123. return;
  124. String filename = projectPath + "Settings/Engine.json";
  125. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  126. if (!fileSystem->FileExists(filename))
  127. return;
  128. if (EngineConfig::LoadFromFile(context_, filename))
  129. {
  130. EngineConfig::ApplyConfig(engineParameters_);
  131. }
  132. }
  133. void AEPlayerApplication::ReadCommandLineArguments()
  134. {
  135. const Vector<String>& arguments = GetArguments();
  136. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  137. for (unsigned i = 0; i < arguments.Size(); ++i)
  138. {
  139. if (arguments[i].Length() > 1)
  140. {
  141. String argument = arguments[i].ToLower();
  142. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  143. if (argument == "--log-std")
  144. {
  145. SubscribeToEvent(E_LOGMESSAGE, HANDLER(AEPlayerApplication, HandleLogMessage));
  146. }
  147. else if (argument.StartsWith("--ipc-server=") || argument.StartsWith("--ipc-client="))
  148. {
  149. // If we have IPC server/client we're being launched from the Editor
  150. // TODO: Unify this with AEPlayerMode handling
  151. launchedByEditor_ = true;
  152. }
  153. else if (argument == "--debug")
  154. {
  155. debugPlayer_ = true;
  156. }
  157. else if (argument == "--project" && value.Length())
  158. {
  159. engineParameters_["ResourcePrefixPath"] = "";
  160. value = AddTrailingSlash(value);
  161. // check that cache exists
  162. if (!fileSystem->DirExists(value + "Cache"))
  163. {
  164. ErrorExit("Project cache folder does not exist, projects must be loaded into the Atomic Editor at least once before using the --player command line mode");
  165. return;
  166. }
  167. #ifdef ATOMIC_DEV_BUILD
  168. String resourcePaths = ToString("%s/Resources/CoreData;%s/Resources/PlayerData;%sResources;%s;%sCache",
  169. ATOMIC_ROOT_SOURCE_DIR, ATOMIC_ROOT_SOURCE_DIR, value.CString(), value.CString(), value.CString());
  170. #else
  171. #ifdef __APPLE__
  172. engineParameters_["ResourcePrefixPath"] = "../Resources";
  173. #else
  174. engineParameters_["ResourcePrefixPath"] = fileSystem->GetProgramDir() + "Resources";
  175. #endif
  176. String resourcePaths = ToString("CoreData;PlayerData;%s/;%s/Resources;%s;%sCache",
  177. value.CString(), value.CString(), value.CString(), value.CString());
  178. #endif
  179. LOGINFOF("Adding ResourcePaths: %s", resourcePaths.CString());
  180. engineParameters_["ResourcePaths"] = resourcePaths;
  181. #ifdef ATOMIC_DOTNET
  182. NETCore* netCore = GetSubsystem<NETCore>();
  183. String assemblyLoadPath = GetNativePath(ToString("%sResources/Assemblies/", value.CString()));
  184. netCore->AddAssemblyLoadPath(assemblyLoadPath);
  185. #endif
  186. }
  187. else if (argument == "--windowposx" && value.Length())
  188. {
  189. engineParameters_["WindowPositionX"] = atoi(value.CString());
  190. }
  191. else if (argument == "--windowposy" && value.Length())
  192. {
  193. engineParameters_["WindowPositionY"] = atoi(value.CString());
  194. }
  195. else if (argument == "--windowwidth" && value.Length())
  196. {
  197. engineParameters_["WindowWidth"] = atoi(value.CString());
  198. }
  199. else if (argument == "--windowheight" && value.Length())
  200. {
  201. engineParameters_["WindowHeight"] = atoi(value.CString());
  202. }
  203. else if (argument == "--resizable")
  204. {
  205. engineParameters_["WindowResizable"] = true;
  206. }
  207. else if (argument == "--maximize")
  208. {
  209. engineParameters_["WindowMaximized"] = true;
  210. }
  211. }
  212. }
  213. }
  214. void AEPlayerApplication::Start()
  215. {
  216. AEEditorCommon::Start();
  217. UI* ui = GetSubsystem<UI>();
  218. ui->Initialize("DefaultUI/language/lng_en.tb.txt");
  219. ui->LoadDefaultPlayerSkin();
  220. context_->RegisterSubsystem(new PlayerMode(context_));
  221. PlayerMode* playerMode = GetSubsystem<PlayerMode>();
  222. playerMode->ProcessArguments();
  223. SubscribeToEvent(E_JSERROR, HANDLER(AEPlayerApplication, HandleJSError));
  224. #ifdef ATOMIC_DOTNET
  225. if (debugPlayer_)
  226. {
  227. GetSubsystem<NETCore>()->WaitForDebuggerConnect();
  228. }
  229. #endif
  230. vm_->SetModuleSearchPaths("Modules");
  231. // Instantiate and register the Player subsystem
  232. context_->RegisterSubsystem(new AtomicPlayer::Player(context_));
  233. AtomicPlayer::jsapi_init_atomicplayer(vm_);
  234. #ifdef ATOMIC_DOTNET
  235. // Initialize Scripting Subsystem
  236. NETScript* netScript = new NETScript(context_);
  237. context_->RegisterSubsystem(netScript);
  238. netScript->Initialize();
  239. netScript->ExecMainAssembly();
  240. #endif
  241. if (!playerMode->launchedByEditor())
  242. {
  243. JSVM* vm = JSVM::GetJSVM(0);
  244. if (!vm->ExecuteMain())
  245. {
  246. SendEvent(E_EXITREQUESTED);
  247. }
  248. }
  249. SubscribeToEvent(E_PLAYERQUIT, HANDLER(AEPlayerApplication, HandleQuit));
  250. return;
  251. }
  252. void AEPlayerApplication::HandleQuit(StringHash eventType, VariantMap& eventData)
  253. {
  254. engine_->Exit();
  255. }
  256. void AEPlayerApplication::Stop()
  257. {
  258. AEEditorCommon::Stop();
  259. }
  260. void AEPlayerApplication::HandleScriptReloadStarted(StringHash eventType, VariantMap& eventData)
  261. {
  262. }
  263. void AEPlayerApplication::HandleScriptReloadFinished(StringHash eventType, VariantMap& eventData)
  264. {
  265. }
  266. void AEPlayerApplication::HandleScriptReloadFailed(StringHash eventType, VariantMap& eventData)
  267. {
  268. ErrorExit();
  269. }
  270. void AEPlayerApplication::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  271. {
  272. using namespace LogMessage;
  273. int level = eventData[P_LEVEL].GetInt();
  274. // The message may be multi-line, so split to rows in that case
  275. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  276. for (unsigned i = 0; i < rows.Size(); ++i)
  277. {
  278. if (level == LOG_ERROR)
  279. {
  280. fprintf(stderr, "%s\n", rows[i].CString());
  281. }
  282. else
  283. {
  284. fprintf(stdout, "%s\n", rows[i].CString());
  285. }
  286. }
  287. }
  288. void AEPlayerApplication::HandleJSError(StringHash eventType, VariantMap& eventData)
  289. {
  290. using namespace JSError;
  291. //String errName = eventData[P_ERRORNAME].GetString();
  292. //String errStack = eventData[P_ERRORSTACK].GetString();
  293. String errMessage = eventData[P_ERRORMESSAGE].GetString();
  294. String errFilename = eventData[P_ERRORFILENAME].GetString();
  295. int errLineNumber = eventData[P_ERRORLINENUMBER].GetInt();
  296. String errorString = ToString("%s - %s - Line: %i",
  297. errFilename.CString(), errMessage.CString(), errLineNumber);
  298. }
  299. }