AEPlayerApp.cpp 11 KB

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