ToolSystem.cpp 5.4 KB

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