NETProjectSystem.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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::OpenSolution()
  54. {
  55. if (!visualStudioPath_.Length())
  56. return;
  57. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  58. if (!fileSystem->FileExists(solutionPath_))
  59. {
  60. if (!GenerateSolution())
  61. return;
  62. }
  63. OpenSourceFile(String::EMPTY);
  64. }
  65. void NETProjectSystem::OpenSourceFile(const String& sourceFilePath)
  66. {
  67. if (!visualStudioPath_.Length())
  68. return;
  69. StringVector args;
  70. if (vsSubprocess_.Expired())
  71. {
  72. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  73. vsSubprocess_ = 0;
  74. args.Push(solutionPath_);
  75. if (sourceFilePath.Length())
  76. args.Push(sourceFilePath);
  77. try
  78. {
  79. vsSubprocess_ = subs->Launch(visualStudioPath_, args);
  80. }
  81. catch (Poco::SystemException)
  82. {
  83. vsSubprocess_ = 0;
  84. }
  85. }
  86. else
  87. {
  88. if (!sourceFilePath.Length())
  89. return;
  90. try
  91. {
  92. std::vector<std::string> args;
  93. args.push_back("/edit");
  94. args.push_back(sourceFilePath.CString());
  95. Poco::Process::launch(visualStudioPath_.CString(), args);
  96. }
  97. catch (Poco::SystemException)
  98. {
  99. }
  100. }
  101. }
  102. void NETProjectSystem::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  103. {
  104. using namespace NETBuildResult;
  105. if (eventData[P_SUCCESS].GetBool())
  106. {
  107. LOGINFOF("NETBuild Success for project");
  108. }
  109. else
  110. {
  111. const String& errorText = eventData[P_ERRORTEXT].GetString();
  112. LOGERRORF("\n%s\n", errorText.CString());
  113. LOGERRORF("NETBuild Error for project");
  114. }
  115. }
  116. void NETProjectSystem::BuildAtomicProject()
  117. {
  118. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  119. if (!fileSystem->FileExists(solutionPath_))
  120. {
  121. if (!GenerateSolution())
  122. {
  123. LOGERRORF("NETProjectSystem::BuildAtomicProject - solutionPath does not exist: %s", solutionPath_.CString());
  124. return;
  125. }
  126. }
  127. #ifdef ATOMIC_PLATFORM_WINDOWS
  128. Project* project = GetSubsystem<ToolSystem>()->GetProject();
  129. NETBuildSystem* buildSystem = GetSubsystem<NETBuildSystem>();
  130. if (buildSystem)
  131. {
  132. NETBuild* build = buildSystem->BuildAtomicProject(project);
  133. if (build)
  134. {
  135. build->SubscribeToEvent(E_NETBUILDRESULT, HANDLER(NETProjectSystem, HandleNETBuildResult));
  136. }
  137. }
  138. #endif
  139. }
  140. bool NETProjectSystem::GenerateSolution()
  141. {
  142. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  143. Project* project = tsystem->GetProject();
  144. if (!project)
  145. {
  146. LOGERRORF("NETProjectSystem::GenerateSolution - No Project Loaded");
  147. return false;
  148. }
  149. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  150. gen->SetScriptPlatform("WINDOWS");
  151. if (!gen->LoadProject(project))
  152. {
  153. LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Load Project");
  154. return false;
  155. }
  156. if (!gen->Generate())
  157. {
  158. LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Generate Project");
  159. return false;
  160. }
  161. return true;
  162. }
  163. void NETProjectSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  164. {
  165. using namespace Update;
  166. float delta = eventData[P_TIMESTEP].GetFloat();
  167. quietPeriod_ -= delta;
  168. if (quietPeriod_ < 0.0f)
  169. quietPeriod_ = 0.0f;
  170. if (quietPeriod_ > 0.0f)
  171. return;
  172. if (solutionDirty_)
  173. {
  174. solutionDirty_ = false;
  175. GenerateSolution();
  176. }
  177. if (projectAssemblyDirty_)
  178. {
  179. BuildAtomicProject();
  180. projectAssemblyDirty_ = false;
  181. }
  182. }
  183. void NETProjectSystem::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  184. {
  185. using namespace ProjectLoaded;
  186. String projectPath = eventData[P_PROJECTPATH].GetString();
  187. if (GetExtension(projectPath) == ".atomic")
  188. projectPath = GetParentPath(projectPath);
  189. solutionPath_ = AddTrailingSlash(projectPath) + "AtomicNET/Solution/AtomicProject.sln";
  190. projectAssemblyPath_ = AddTrailingSlash(projectPath) + "Resource/AtomicProject.dll";
  191. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  192. // if the solution or project assemblies don't exist mark as dirty
  193. if (!fileSystem->FileExists(solutionPath_))
  194. solutionDirty_ = true;
  195. if (!fileSystem->FileExists(projectAssemblyPath_))
  196. projectAssemblyDirty_ = true;
  197. }
  198. void NETProjectSystem::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
  199. {
  200. Clear();
  201. }
  202. void NETProjectSystem::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  203. {
  204. }
  205. void NETProjectSystem::HandleResourceAdded(StringHash eventType, VariantMap& eventData)
  206. {
  207. }
  208. void NETProjectSystem::HandleResourceRemoved(StringHash eventType, VariantMap& eventData)
  209. {
  210. }
  211. void NETProjectSystem::HandleAssetRenamed(StringHash eventType, VariantMap& eventData)
  212. {
  213. }
  214. void NETProjectSystem::HandleAssetMoved(StringHash eventType, VariantMap& eventData)
  215. {
  216. }
  217. void NETProjectSystem::Initialize()
  218. {
  219. Clear();
  220. #ifdef ATOMIC_PLATFORM_WINDOWS
  221. SubscribeToEvent(E_UPDATE, HANDLER(NETProjectSystem, HandleUpdate));
  222. SubscribeToEvent(E_PROJECTLOADED, HANDLER(NETProjectSystem, HandleProjectLoaded));
  223. SubscribeToEvent(E_PROJECTUNLOADED, HANDLER(NETProjectSystem, HandleProjectUnloaded));
  224. SubscribeToEvent(E_FILECHANGED, HANDLER(NETProjectSystem, HandleFileChanged));
  225. SubscribeToEvent(E_RESOURCEADDED, HANDLER(NETProjectSystem, HandleResourceAdded));
  226. SubscribeToEvent(E_RESOURCEREMOVED, HANDLER(NETProjectSystem, HandleResourceRemoved));
  227. SubscribeToEvent(E_ASSETRENAMED, HANDLER(NETProjectSystem, HandleAssetRenamed));
  228. SubscribeToEvent(E_ASSETMOVED, HANDLER(NETProjectSystem, HandleAssetMoved));
  229. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  230. // Query for Visual Studio 2015 path
  231. visualStudioPath_ = Poco::Environment::get("VS140COMNTOOLS").c_str();
  232. if (visualStudioPath_.Length())
  233. {
  234. visualStudioPath_.Replace("Tools\\", "IDE\\devenv.exe");
  235. if (!fileSystem->FileExists(visualStudioPath_))
  236. visualStudioPath_.Clear();
  237. }
  238. #endif
  239. }
  240. }