NETProjectSystem.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 "../Assets/AssetDatabase.h"
  30. #include "../Project/Project.h"
  31. #include "../Project/ProjectEvents.h"
  32. #include "../Subprocess/SubprocessSystem.h"
  33. #include "NETProjectGen.h"
  34. #include "NETBuildSystem.h"
  35. #include "NETProjectSystem.h"
  36. namespace ToolCore
  37. {
  38. NETProjectSystem::NETProjectSystem(Context* context) :
  39. Object(context)
  40. {
  41. Initialize();
  42. }
  43. NETProjectSystem::~NETProjectSystem()
  44. {
  45. }
  46. void NETProjectSystem::Clear()
  47. {
  48. quietPeriod_ = 0.0f;
  49. solutionPath_.Clear();
  50. projectAssemblyPath_.Clear();
  51. solutionDirty_ = false;
  52. projectAssemblyDirty_ = false;
  53. }
  54. void NETProjectSystem::OpenSolution()
  55. {
  56. if (!visualStudioPath_.Length())
  57. return;
  58. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  59. if (!fileSystem->FileExists(solutionPath_))
  60. {
  61. if (!GenerateSolution())
  62. return;
  63. }
  64. OpenSourceFile(String::EMPTY);
  65. }
  66. void NETProjectSystem::OpenSourceFile(const String& sourceFilePath)
  67. {
  68. if (!visualStudioPath_.Length())
  69. return;
  70. StringVector args;
  71. if (vsSubprocess_.Expired())
  72. {
  73. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  74. vsSubprocess_ = 0;
  75. args.Push(solutionPath_);
  76. if (sourceFilePath.Length())
  77. args.Push(sourceFilePath);
  78. try
  79. {
  80. vsSubprocess_ = subs->Launch(visualStudioPath_, args);
  81. }
  82. catch (Poco::SystemException)
  83. {
  84. vsSubprocess_ = 0;
  85. }
  86. }
  87. else
  88. {
  89. if (!sourceFilePath.Length())
  90. return;
  91. try
  92. {
  93. std::vector<std::string> args;
  94. args.push_back("/edit");
  95. args.push_back(sourceFilePath.CString());
  96. Poco::Process::launch(visualStudioPath_.CString(), args);
  97. }
  98. catch (Poco::SystemException)
  99. {
  100. }
  101. }
  102. }
  103. void NETProjectSystem::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  104. {
  105. using namespace NETBuildResult;
  106. if (eventData[P_SUCCESS].GetBool())
  107. {
  108. ATOMIC_LOGINFOF("NETBuild Success for project");
  109. }
  110. else
  111. {
  112. const String& errorText = eventData[P_ERRORTEXT].GetString();
  113. ATOMIC_LOGERRORF("\n%s\n", errorText.CString());
  114. ATOMIC_LOGERRORF("NETBuild Error for project");
  115. }
  116. }
  117. void NETProjectSystem::BuildAtomicProject()
  118. {
  119. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  120. if (!fileSystem->FileExists(solutionPath_))
  121. {
  122. if (!GenerateSolution())
  123. {
  124. ATOMIC_LOGERRORF("NETProjectSystem::BuildAtomicProject - solutionPath does not exist: %s", solutionPath_.CString());
  125. return;
  126. }
  127. }
  128. #ifdef ATOMIC_PLATFORM_WINDOWS
  129. Project* project = GetSubsystem<ToolSystem>()->GetProject();
  130. NETBuildSystem* buildSystem = GetSubsystem<NETBuildSystem>();
  131. if (buildSystem)
  132. {
  133. NETBuild* build = buildSystem->BuildAtomicProject(project);
  134. if (build)
  135. {
  136. build->SubscribeToEvent(E_NETBUILDRESULT, ATOMIC_HANDLER(NETProjectSystem, HandleNETBuildResult));
  137. }
  138. }
  139. #endif
  140. }
  141. bool NETProjectSystem::GenerateSolution()
  142. {
  143. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  144. Project* project = tsystem->GetProject();
  145. if (!project)
  146. {
  147. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - No Project Loaded");
  148. return false;
  149. }
  150. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  151. gen->SetScriptPlatform("WINDOWS");
  152. if (!gen->LoadProject(project))
  153. {
  154. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Load Project");
  155. return false;
  156. }
  157. if (!gen->Generate())
  158. {
  159. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Generate Project");
  160. return false;
  161. }
  162. return true;
  163. }
  164. void NETProjectSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  165. {
  166. using namespace Update;
  167. float delta = eventData[P_TIMESTEP].GetFloat();
  168. quietPeriod_ -= delta;
  169. if (quietPeriod_ < 0.0f)
  170. quietPeriod_ = 0.0f;
  171. if (quietPeriod_ > 0.0f)
  172. return;
  173. if (solutionDirty_)
  174. {
  175. // set to false in case of error, we don't want to keep trying to
  176. // rebuild, TODO: better error handling
  177. solutionDirty_ = false;
  178. GenerateSolution();
  179. }
  180. if (projectAssemblyDirty_)
  181. {
  182. BuildAtomicProject();
  183. projectAssemblyDirty_ = false;
  184. }
  185. }
  186. void NETProjectSystem::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  187. {
  188. using namespace ProjectLoaded;
  189. String projectPath = eventData[P_PROJECTPATH].GetString();
  190. if (GetExtension(projectPath) == ".atomic")
  191. projectPath = GetParentPath(projectPath);
  192. solutionPath_ = AddTrailingSlash(projectPath) + "AtomicNET/Solution/AtomicProject.sln";
  193. projectAssemblyPath_ = AddTrailingSlash(projectPath) + "Resource/AtomicProject.dll";
  194. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  195. // if the solution or project assemblies don't exist mark as dirty
  196. if (!fileSystem->FileExists(solutionPath_))
  197. solutionDirty_ = true;
  198. if (!fileSystem->FileExists(projectAssemblyPath_))
  199. projectAssemblyDirty_ = true;
  200. }
  201. void NETProjectSystem::HandleAssetScanBegin(StringHash eventType, VariantMap& eventData)
  202. {
  203. }
  204. void NETProjectSystem::HandleAssetScanEnd(StringHash eventType, VariantMap& eventData)
  205. {
  206. if (solutionDirty_)
  207. {
  208. // set to false in case of error, we don't want to keep trying to
  209. // rebuild, TODO: better error handling
  210. solutionDirty_ = false;
  211. GenerateSolution();
  212. }
  213. }
  214. void NETProjectSystem::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
  215. {
  216. Clear();
  217. }
  218. void NETProjectSystem::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  219. {
  220. }
  221. void NETProjectSystem::HandleAssetNew(StringHash eventType, VariantMap& eventData)
  222. {
  223. using namespace ResourceAdded;
  224. const String& guid = eventData[P_GUID].ToString();
  225. Asset* asset = GetSubsystem<AssetDatabase>()->GetAssetByGUID(guid);
  226. if (asset->GetExtension() == ".cs")
  227. solutionDirty_ = true;
  228. }
  229. void NETProjectSystem::HandleResourceAdded(StringHash eventType, VariantMap& eventData)
  230. {
  231. }
  232. void NETProjectSystem::HandleResourceRemoved(StringHash eventType, VariantMap& eventData)
  233. {
  234. }
  235. void NETProjectSystem::HandleAssetRenamed(StringHash eventType, VariantMap& eventData)
  236. {
  237. }
  238. void NETProjectSystem::HandleAssetMoved(StringHash eventType, VariantMap& eventData)
  239. {
  240. }
  241. void NETProjectSystem::Initialize()
  242. {
  243. Clear();
  244. #ifdef ATOMIC_PLATFORM_WINDOWS
  245. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(NETProjectSystem, HandleUpdate));
  246. SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectLoaded));
  247. SubscribeToEvent(E_PROJECTUNLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectUnloaded));
  248. SubscribeToEvent(E_ASSETSCANBEGIN, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanBegin));
  249. SubscribeToEvent(E_ASSETSCANEND, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanEnd));
  250. SubscribeToEvent(E_FILECHANGED, ATOMIC_HANDLER(NETProjectSystem, HandleFileChanged));
  251. SubscribeToEvent(E_RESOURCEADDED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceAdded));
  252. SubscribeToEvent(E_RESOURCEREMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceRemoved));
  253. SubscribeToEvent(E_ASSETNEW, ATOMIC_HANDLER(NETProjectSystem, HandleAssetNew));
  254. SubscribeToEvent(E_ASSETRENAMED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetRenamed));
  255. SubscribeToEvent(E_ASSETMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetMoved));
  256. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  257. // Query for Visual Studio 2015 path
  258. visualStudioPath_ = Poco::Environment::get("VS140COMNTOOLS").c_str();
  259. if (visualStudioPath_.Length())
  260. {
  261. visualStudioPath_.Replace("Tools\\", "IDE\\devenv.exe");
  262. if (!fileSystem->FileExists(visualStudioPath_))
  263. visualStudioPath_.Clear();
  264. }
  265. #endif
  266. }
  267. }