ToolSystem.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/Core/Context.h>
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Resource/ResourceCache.h>
  26. #include "Platform/PlatformWeb.h"
  27. #include "Platform/PlatformMac.h"
  28. #include "Platform/PlatformWindows.h"
  29. #include "Platform/PlatformAndroid.h"
  30. #include "Platform/PlatformIOS.h"
  31. #include "Assets/AssetDatabase.h"
  32. #include "Net/CurlManager.h"
  33. #include "License/LicenseSystem.h"
  34. #include "Build/BuildSystem.h"
  35. #include "Subprocess/SubprocessSystem.h"
  36. #include "ToolSystem.h"
  37. #include "ToolEnvironment.h"
  38. #include "ToolEvents.h"
  39. #include "Project/Project.h"
  40. #include "Project/ProjectEvents.h"
  41. #include "Project/ProjectUserPrefs.h"
  42. namespace ToolCore
  43. {
  44. ToolSystem::ToolSystem(Context* context) : Object(context),
  45. cli_(false),
  46. updateDelta_(0.0f)
  47. {
  48. context_->RegisterSubsystem(new AssetDatabase(context_));
  49. context_->RegisterSubsystem(new CurlManager(context_));
  50. context_->RegisterSubsystem(new LicenseSystem(context_));
  51. context_->RegisterSubsystem(new BuildSystem(context_));
  52. context_->RegisterSubsystem(new SubprocessSystem(context_));
  53. // platform registration
  54. RegisterPlatform(new PlatformMac(context));
  55. RegisterPlatform(new PlatformWeb(context));
  56. RegisterPlatform(new PlatformWindows(context));
  57. RegisterPlatform(new PlatformIOS(context));
  58. RegisterPlatform(new PlatformAndroid(context));
  59. SubscribeToEvent(E_UPDATE, HANDLER(ToolSystem, HandleUpdate));
  60. }
  61. ToolSystem::~ToolSystem()
  62. {
  63. }
  64. void ToolSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  65. {
  66. using namespace Update;
  67. updateDelta_ += eventData[P_TIMESTEP].GetFloat();
  68. if (updateDelta_ >= 0.5f)
  69. {
  70. updateDelta_ = 0.0f;
  71. SendEvent(E_TOOLUPDATE);
  72. }
  73. }
  74. bool ToolSystem::LoadProject(const String& fullpath)
  75. {
  76. String pathName, fileName, ext;
  77. SplitPath(fullpath, pathName, fileName, ext);
  78. String path;
  79. if (ext == ".atomic")
  80. {
  81. path = RemoveTrailingSlash(GetPath(AddTrailingSlash(pathName)));
  82. }
  83. else
  84. {
  85. path = RemoveTrailingSlash(GetPath(AddTrailingSlash(fullpath)));
  86. }
  87. ResourceCache* cache = GetSubsystem<ResourceCache>();
  88. // Do NOT add the root project path as a resource path, otherwise
  89. // it will introduce a situation where there are nested resource paths
  90. // which will confuse the ResourceCache and can cause resources referenced
  91. // by various paths to be seen as unique resources
  92. //cache->AddResourceDir(path, 0);
  93. String resourcePath = path;
  94. resourcePath += "/Resources";
  95. cache->AddResourceDir(resourcePath, 0);
  96. project_ = new Project(context_);
  97. project_->SetResourcePath(resourcePath);
  98. bool result = project_->Load(fullpath);
  99. if (result)
  100. {
  101. // TODO: persistent platform setting
  102. SetCurrentPlatform(project_->GetUserPrefs()->GetDefaultPlatform());
  103. }
  104. return result;
  105. }
  106. void ToolSystem::CloseProject()
  107. {
  108. if (project_.Null())
  109. return;
  110. SendEvent(E_PROJECTUNLOADED);
  111. ResourceCache* cache = GetSubsystem<ResourceCache>();
  112. String projectPath = project_->GetProjectPath();
  113. String resourcePath = project_->GetResourcePath();
  114. project_ = 0;
  115. cache->RemoveResourceDir(resourcePath);
  116. cache->RemoveResourceDir(projectPath);
  117. cache->ReleaseAllResources(true);
  118. }
  119. void ToolSystem::SetCurrentPlatform(PlatformID platform)
  120. {
  121. VariantMap eventData;
  122. if (platform == PLATFORMID_UNDEFINED)
  123. {
  124. currentPlatform_ = NULL;
  125. eventData[PlatformChanged::P_PLATFORM] = (Platform*) 0;
  126. SendEvent(E_PLATFORMCHANGED, eventData);
  127. return;
  128. }
  129. if (!platforms_.Contains((unsigned) platform))
  130. return;
  131. currentPlatform_ = platforms_[(unsigned)platform];
  132. eventData[PlatformChanged::P_PLATFORM] = currentPlatform_;
  133. SendEvent(E_PLATFORMCHANGED, eventData);
  134. }
  135. Platform* ToolSystem::GetPlatformByID(PlatformID platform)
  136. {
  137. if (!platforms_.Contains((unsigned) platform))
  138. return NULL;
  139. return platforms_[(unsigned)platform];
  140. }
  141. Platform* ToolSystem::GetPlatformByName(const String& name)
  142. {
  143. HashMap<unsigned, SharedPtr<Platform> >::Iterator itr = platforms_.Begin();
  144. while (itr != platforms_.End())
  145. {
  146. if ((*itr).second_->GetName().ToLower()== name.ToLower())
  147. return (*itr).second_;
  148. itr++;
  149. }
  150. return NULL;
  151. }
  152. Platform *ToolSystem::GetCurrentPlatform()
  153. {
  154. return currentPlatform_;
  155. }
  156. void ToolSystem::RegisterPlatform(Platform* platform)
  157. {
  158. platforms_.Insert(MakePair((unsigned)platform->GetPlatformID(), SharedPtr<Platform>(platform)));
  159. }
  160. }