ToolSystem.cpp 5.7 KB

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