NETProjectSystem.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <Poco/Environment.h>
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include <Atomic/Resource/ResourceEvents.h>
  27. #include "../ToolSystem.h"
  28. #include "../Assets/AssetEvents.h"
  29. #include "../Project/Project.h"
  30. #include "../Project/ProjectEvents.h"
  31. #include "../Subprocess/SubprocessSystem.h"
  32. #include "NETProjectGen.h"
  33. #include "NETBuildSystem.h"
  34. #include "NETProjectSystem.h"
  35. namespace ToolCore
  36. {
  37. NETProjectSystem::NETProjectSystem(Context* context) :
  38. Object(context)
  39. {
  40. Initialize();
  41. }
  42. NETProjectSystem::~NETProjectSystem()
  43. {
  44. }
  45. void NETProjectSystem::Clear()
  46. {
  47. quietPeriod_ = 0.0f;
  48. solutionPath_.Clear();
  49. projectAssemblyPath_.Clear();
  50. solutionDirty_ = false;
  51. projectAssemblyDirty_ = false;
  52. }
  53. void NETProjectSystem::OpenSourceFile(const String& sourceFilePath)
  54. {
  55. if (!visualStudioPath_.Length())
  56. return;
  57. StringVector args;
  58. if (vsSubprocess_.Expired())
  59. {
  60. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  61. vsSubprocess_ = 0;
  62. args.Push(solutionPath_);
  63. args.Push(sourceFilePath);
  64. try
  65. {
  66. vsSubprocess_ = subs->Launch(visualStudioPath_, args);
  67. }
  68. catch (Poco::SystemException)
  69. {
  70. vsSubprocess_ = 0;
  71. }
  72. }
  73. else
  74. {
  75. try
  76. {
  77. std::vector<std::string> args;
  78. args.push_back("/edit");
  79. args.push_back(sourceFilePath.CString());
  80. Poco::Process::launch(visualStudioPath_.CString(), args);
  81. }
  82. catch (Poco::SystemException)
  83. {
  84. }
  85. }
  86. }
  87. void NETProjectSystem::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  88. {
  89. using namespace NETBuildResult;
  90. if (eventData[P_SUCCESS].GetBool())
  91. {
  92. LOGINFOF("NETBuild Success for project");
  93. }
  94. else
  95. {
  96. const String& errorText = eventData[P_ERRORTEXT].GetString();
  97. LOGERRORF("\n%s\n", errorText.CString());
  98. LOGERRORF("NETBuild Error for project");
  99. }
  100. }
  101. void NETProjectSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  102. {
  103. using namespace Update;
  104. float delta = eventData[P_TIMESTEP].GetFloat();
  105. quietPeriod_ -= delta;
  106. if (quietPeriod_ < 0.0f)
  107. quietPeriod_ = 0.0f;
  108. if (quietPeriod_ > 0.0f)
  109. return;
  110. if (solutionDirty_)
  111. {
  112. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  113. Project* project = tsystem->GetProject();
  114. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  115. gen->SetScriptPlatform("WINDOWS");
  116. if (!gen->LoadProject(project))
  117. {
  118. return;
  119. }
  120. if (!gen->Generate())
  121. {
  122. return;
  123. }
  124. solutionDirty_ = false;
  125. }
  126. if (projectAssemblyDirty_)
  127. {
  128. Project* project = GetSubsystem<ToolSystem>()->GetProject();
  129. NETBuild* build = GetSubsystem<NETBuildSystem>()->BuildAtomicProject(project);
  130. if (build)
  131. {
  132. build->SubscribeToEvent(E_NETBUILDRESULT, HANDLER(NETProjectSystem, HandleNETBuildResult));
  133. }
  134. projectAssemblyDirty_ = false;
  135. }
  136. }
  137. void NETProjectSystem::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  138. {
  139. using namespace ProjectLoaded;
  140. String projectPath = eventData[P_PROJECTPATH].GetString();
  141. if (GetExtension(projectPath) == ".atomic")
  142. projectPath = GetParentPath(projectPath);
  143. solutionPath_ = AddTrailingSlash(projectPath) + "AtomicNET/Solution/AtomicProject.sln";
  144. projectAssemblyPath_ = AddTrailingSlash(projectPath) + "Resource/AtomicProject.dll";
  145. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  146. // if the solution or project assemblies don't exist mark as dirty
  147. if (!fileSystem->FileExists(solutionPath_))
  148. solutionDirty_ = true;
  149. if (!fileSystem->FileExists(projectAssemblyPath_))
  150. projectAssemblyDirty_ = true;
  151. }
  152. void NETProjectSystem::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
  153. {
  154. Clear();
  155. }
  156. void NETProjectSystem::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  157. {
  158. }
  159. void NETProjectSystem::HandleResourceAdded(StringHash eventType, VariantMap& eventData)
  160. {
  161. }
  162. void NETProjectSystem::HandleResourceRemoved(StringHash eventType, VariantMap& eventData)
  163. {
  164. }
  165. void NETProjectSystem::HandleAssetRenamed(StringHash eventType, VariantMap& eventData)
  166. {
  167. }
  168. void NETProjectSystem::HandleAssetMoved(StringHash eventType, VariantMap& eventData)
  169. {
  170. }
  171. void NETProjectSystem::Initialize()
  172. {
  173. Clear();
  174. #ifdef ATOMIC_PLATFORM_WINDOWS
  175. SubscribeToEvent(E_UPDATE, HANDLER(NETProjectSystem, HandleUpdate));
  176. SubscribeToEvent(E_PROJECTLOADED, HANDLER(NETProjectSystem, HandleProjectLoaded));
  177. SubscribeToEvent(E_PROJECTUNLOADED, HANDLER(NETProjectSystem, HandleProjectUnloaded));
  178. SubscribeToEvent(E_FILECHANGED, HANDLER(NETProjectSystem, HandleFileChanged));
  179. SubscribeToEvent(E_RESOURCEADDED, HANDLER(NETProjectSystem, HandleResourceAdded));
  180. SubscribeToEvent(E_RESOURCEREMOVED, HANDLER(NETProjectSystem, HandleResourceRemoved));
  181. SubscribeToEvent(E_ASSETRENAMED, HANDLER(NETProjectSystem, HandleAssetRenamed));
  182. SubscribeToEvent(E_ASSETMOVED, HANDLER(NETProjectSystem, HandleAssetMoved));
  183. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  184. // Query for Visual Studio 2015 path
  185. visualStudioPath_ = Poco::Environment::get("VS140COMNTOOLS").c_str();
  186. if (visualStudioPath_.Length())
  187. {
  188. visualStudioPath_.Replace("Tools\\", "IDE\\devenv.exe");
  189. if (!fileSystem->FileExists(visualStudioPath_))
  190. visualStudioPath_.Clear();
  191. }
  192. #endif
  193. }
  194. }