NETProjectSystem.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. 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, ATOMIC_HANDLER(NETProjectSystem, HandleNETBuildResult));
  136. }
  137. }
  138. }
  139. bool NETProjectSystem::GenerateSolution()
  140. {
  141. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  142. Project* project = tsystem->GetProject();
  143. if (!project)
  144. {
  145. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - No Project Loaded");
  146. return false;
  147. }
  148. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  149. gen->SetScriptPlatform("WINDOWS");
  150. if (!gen->LoadProject(project))
  151. {
  152. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Load Project");
  153. return false;
  154. }
  155. if (!gen->Generate())
  156. {
  157. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Generate Project");
  158. return false;
  159. }
  160. return true;
  161. }
  162. void NETProjectSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  163. {
  164. using namespace Update;
  165. float delta = eventData[P_TIMESTEP].GetFloat();
  166. quietPeriod_ -= delta;
  167. if (quietPeriod_ < 0.0f)
  168. quietPeriod_ = 0.0f;
  169. if (quietPeriod_ > 0.0f)
  170. return;
  171. if (solutionDirty_)
  172. {
  173. // set to false in case of error, we don't want to keep trying to
  174. // rebuild, TODO: better error handling
  175. solutionDirty_ = false;
  176. GenerateSolution();
  177. }
  178. if (projectAssemblyDirty_)
  179. {
  180. BuildAtomicProject();
  181. projectAssemblyDirty_ = false;
  182. }
  183. }
  184. void NETProjectSystem::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  185. {
  186. using namespace ProjectLoaded;
  187. String projectPath = eventData[P_PROJECTPATH].GetString();
  188. if (GetExtension(projectPath) == ".atomic")
  189. projectPath = GetParentPath(projectPath);
  190. solutionPath_ = AddTrailingSlash(projectPath) + "AtomicNET/Solution/AtomicProject.sln";
  191. projectAssemblyPath_ = AddTrailingSlash(projectPath) + "Resources/AtomicProject.dll";
  192. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  193. // TODO: We need a better way of marking C# projects
  194. StringVector results;
  195. fileSystem->ScanDir(results, AddTrailingSlash(projectPath) + "Resources", "*.cs", SCAN_FILES, true);
  196. if (!results.Size())
  197. {
  198. fileSystem->ScanDir(results, AddTrailingSlash(projectPath) + "Resources", "*.dll", SCAN_FILES, true);
  199. if (!results.Size())
  200. return;
  201. }
  202. // if the solution or project assemblies don't exist mark as dirty
  203. if (!fileSystem->FileExists(solutionPath_))
  204. solutionDirty_ = true;
  205. if (!fileSystem->FileExists(projectAssemblyPath_))
  206. projectAssemblyDirty_ = true;
  207. }
  208. void NETProjectSystem::HandleAssetScanBegin(StringHash eventType, VariantMap& eventData)
  209. {
  210. }
  211. void NETProjectSystem::HandleAssetScanEnd(StringHash eventType, VariantMap& eventData)
  212. {
  213. if (solutionDirty_)
  214. {
  215. // set to false in case of error, we don't want to keep trying to
  216. // rebuild, TODO: better error handling
  217. solutionDirty_ = false;
  218. GenerateSolution();
  219. }
  220. }
  221. void NETProjectSystem::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
  222. {
  223. Clear();
  224. }
  225. void NETProjectSystem::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  226. {
  227. }
  228. void NETProjectSystem::HandleAssetNew(StringHash eventType, VariantMap& eventData)
  229. {
  230. using namespace ResourceAdded;
  231. const String& guid = eventData[P_GUID].ToString();
  232. Asset* asset = GetSubsystem<AssetDatabase>()->GetAssetByGUID(guid);
  233. if (asset->GetExtension() == ".cs")
  234. solutionDirty_ = true;
  235. }
  236. void NETProjectSystem::HandleResourceAdded(StringHash eventType, VariantMap& eventData)
  237. {
  238. }
  239. void NETProjectSystem::HandleResourceRemoved(StringHash eventType, VariantMap& eventData)
  240. {
  241. }
  242. void NETProjectSystem::HandleAssetRenamed(StringHash eventType, VariantMap& eventData)
  243. {
  244. }
  245. void NETProjectSystem::HandleAssetMoved(StringHash eventType, VariantMap& eventData)
  246. {
  247. }
  248. void NETProjectSystem::Initialize()
  249. {
  250. Clear();
  251. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(NETProjectSystem, HandleUpdate));
  252. SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectLoaded));
  253. SubscribeToEvent(E_PROJECTUNLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectUnloaded));
  254. SubscribeToEvent(E_ASSETSCANBEGIN, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanBegin));
  255. SubscribeToEvent(E_ASSETSCANEND, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanEnd));
  256. SubscribeToEvent(E_FILECHANGED, ATOMIC_HANDLER(NETProjectSystem, HandleFileChanged));
  257. SubscribeToEvent(E_RESOURCEADDED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceAdded));
  258. SubscribeToEvent(E_RESOURCEREMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceRemoved));
  259. SubscribeToEvent(E_ASSETNEW, ATOMIC_HANDLER(NETProjectSystem, HandleAssetNew));
  260. SubscribeToEvent(E_ASSETRENAMED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetRenamed));
  261. SubscribeToEvent(E_ASSETMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetMoved));
  262. #ifdef ATOMIC_PLATFORM_WINDOWS
  263. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  264. // Query for Visual Studio 2015 path
  265. visualStudioPath_ = Poco::Environment::get("VS140COMNTOOLS").c_str();
  266. if (visualStudioPath_.Length())
  267. {
  268. visualStudioPath_.Replace("Tools\\", "IDE\\devenv.exe");
  269. if (!fileSystem->FileExists(visualStudioPath_))
  270. visualStudioPath_.Clear();
  271. }
  272. #endif
  273. }
  274. }