NETProjectSystem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 (!idePath_.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 (!idePath_.Length())
  69. return;
  70. String command = idePath_;
  71. StringVector args;
  72. if (ideSubprocess_.Expired())
  73. {
  74. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  75. ideSubprocess_ = 0;
  76. #ifdef ATOMIC_PLATFORM_OSX
  77. command = "open";
  78. args.Push("-W");
  79. args.Push("-a");
  80. args.Push(idePath_);
  81. #endif
  82. args.Push(solutionPath_);
  83. if (sourceFilePath.Length())
  84. args.Push(sourceFilePath);
  85. try
  86. {
  87. ideSubprocess_ = subs->Launch(command, args);
  88. }
  89. catch (Poco::SystemException)
  90. {
  91. ideSubprocess_ = 0;
  92. }
  93. }
  94. else
  95. {
  96. if (!sourceFilePath.Length())
  97. return;
  98. try
  99. {
  100. std::vector<std::string> args;
  101. #ifdef ATOMIC_PLATFORM_WINDOWS
  102. args.push_back("/edit");
  103. #elif defined ATOMIC_PLATFORM_OSX
  104. command = "open";
  105. args.push_back("-a");
  106. args.push_back(idePath_.CString());
  107. #endif
  108. args.push_back(sourceFilePath.CString());
  109. Poco::Process::launch(command.CString(), args);
  110. }
  111. catch (Poco::SystemException)
  112. {
  113. }
  114. }
  115. }
  116. void NETProjectSystem::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  117. {
  118. using namespace NETBuildResult;
  119. if (eventData[P_SUCCESS].GetBool())
  120. {
  121. ATOMIC_LOGINFOF("NETBuild Success for project");
  122. }
  123. else
  124. {
  125. const String& errorText = eventData[P_ERRORTEXT].GetString();
  126. ATOMIC_LOGERRORF("\n%s\n", errorText.CString());
  127. ATOMIC_LOGERRORF("NETBuild Error for project");
  128. }
  129. }
  130. void NETProjectSystem::BuildAtomicProject()
  131. {
  132. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  133. if (!fileSystem->FileExists(solutionPath_))
  134. {
  135. if (!GenerateSolution())
  136. {
  137. ATOMIC_LOGERRORF("NETProjectSystem::BuildAtomicProject - solutionPath does not exist: %s", solutionPath_.CString());
  138. return;
  139. }
  140. }
  141. Project* project = GetSubsystem<ToolSystem>()->GetProject();
  142. NETBuildSystem* buildSystem = GetSubsystem<NETBuildSystem>();
  143. if (buildSystem)
  144. {
  145. NETBuild* build = buildSystem->BuildAtomicProject(project);
  146. if (build)
  147. {
  148. build->SubscribeToEvent(E_NETBUILDRESULT, ATOMIC_HANDLER(NETProjectSystem, HandleNETBuildResult));
  149. }
  150. }
  151. }
  152. bool NETProjectSystem::GenerateSolution()
  153. {
  154. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  155. Project* project = tsystem->GetProject();
  156. if (!project)
  157. {
  158. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - No Project Loaded");
  159. return false;
  160. }
  161. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  162. gen->SetScriptPlatform("WINDOWS");
  163. if (!gen->LoadProject(project))
  164. {
  165. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Load Project");
  166. return false;
  167. }
  168. if (!gen->Generate())
  169. {
  170. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Generate Project");
  171. return false;
  172. }
  173. return true;
  174. }
  175. void NETProjectSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  176. {
  177. using namespace Update;
  178. float delta = eventData[P_TIMESTEP].GetFloat();
  179. quietPeriod_ -= delta;
  180. if (quietPeriod_ < 0.0f)
  181. quietPeriod_ = 0.0f;
  182. if (quietPeriod_ > 0.0f)
  183. return;
  184. if (solutionDirty_)
  185. {
  186. // set to false in case of error, we don't want to keep trying to
  187. // rebuild, TODO: better error handling
  188. solutionDirty_ = false;
  189. GenerateSolution();
  190. }
  191. if (projectAssemblyDirty_)
  192. {
  193. BuildAtomicProject();
  194. projectAssemblyDirty_ = false;
  195. }
  196. }
  197. void NETProjectSystem::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  198. {
  199. using namespace ProjectLoaded;
  200. String projectPath = eventData[P_PROJECTPATH].GetString();
  201. if (GetExtension(projectPath) == ".atomic")
  202. projectPath = GetParentPath(projectPath);
  203. solutionPath_ = AddTrailingSlash(projectPath) + "AtomicNET/Solution/AtomicProject.sln";
  204. projectAssemblyPath_ = AddTrailingSlash(projectPath) + "Resources/AtomicProject.dll";
  205. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  206. // TODO: We need a better way of marking C# projects
  207. StringVector results;
  208. fileSystem->ScanDir(results, AddTrailingSlash(projectPath) + "Resources", "*.cs", SCAN_FILES, true);
  209. if (!results.Size())
  210. {
  211. fileSystem->ScanDir(results, AddTrailingSlash(projectPath) + "Resources", "*.dll", SCAN_FILES, true);
  212. if (!results.Size())
  213. return;
  214. }
  215. // if the solution or project assemblies don't exist mark as dirty
  216. if (!fileSystem->FileExists(solutionPath_))
  217. solutionDirty_ = true;
  218. if (!fileSystem->FileExists(projectAssemblyPath_))
  219. projectAssemblyDirty_ = true;
  220. }
  221. void NETProjectSystem::HandleAssetScanBegin(StringHash eventType, VariantMap& eventData)
  222. {
  223. }
  224. void NETProjectSystem::HandleAssetScanEnd(StringHash eventType, VariantMap& eventData)
  225. {
  226. if (solutionDirty_)
  227. {
  228. // set to false in case of error, we don't want to keep trying to
  229. // rebuild, TODO: better error handling
  230. solutionDirty_ = false;
  231. GenerateSolution();
  232. }
  233. }
  234. void NETProjectSystem::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
  235. {
  236. Clear();
  237. }
  238. void NETProjectSystem::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  239. {
  240. }
  241. void NETProjectSystem::HandleAssetNew(StringHash eventType, VariantMap& eventData)
  242. {
  243. using namespace ResourceAdded;
  244. const String& guid = eventData[P_GUID].ToString();
  245. Asset* asset = GetSubsystem<AssetDatabase>()->GetAssetByGUID(guid);
  246. if (asset->GetExtension() == ".cs")
  247. solutionDirty_ = true;
  248. }
  249. void NETProjectSystem::HandleResourceAdded(StringHash eventType, VariantMap& eventData)
  250. {
  251. }
  252. void NETProjectSystem::HandleResourceRemoved(StringHash eventType, VariantMap& eventData)
  253. {
  254. }
  255. void NETProjectSystem::HandleAssetRenamed(StringHash eventType, VariantMap& eventData)
  256. {
  257. }
  258. void NETProjectSystem::HandleAssetMoved(StringHash eventType, VariantMap& eventData)
  259. {
  260. }
  261. void NETProjectSystem::Initialize()
  262. {
  263. Clear();
  264. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(NETProjectSystem, HandleUpdate));
  265. SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectLoaded));
  266. SubscribeToEvent(E_PROJECTUNLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectUnloaded));
  267. SubscribeToEvent(E_ASSETSCANBEGIN, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanBegin));
  268. SubscribeToEvent(E_ASSETSCANEND, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanEnd));
  269. SubscribeToEvent(E_FILECHANGED, ATOMIC_HANDLER(NETProjectSystem, HandleFileChanged));
  270. SubscribeToEvent(E_RESOURCEADDED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceAdded));
  271. SubscribeToEvent(E_RESOURCEREMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceRemoved));
  272. SubscribeToEvent(E_ASSETNEW, ATOMIC_HANDLER(NETProjectSystem, HandleAssetNew));
  273. SubscribeToEvent(E_ASSETRENAMED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetRenamed));
  274. SubscribeToEvent(E_ASSETMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetMoved));
  275. #ifdef ATOMIC_PLATFORM_WINDOWS
  276. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  277. // Query for Visual Studio 2015 path
  278. idePath_ = Poco::Environment::get("VS140COMNTOOLS").c_str();
  279. if (idePath_.Length())
  280. {
  281. idePath_.Replace("Tools\\", "IDE\\devenv.exe");
  282. if (!fileSystem->FileExists(idePath_))
  283. idePath_.Clear();
  284. }
  285. #elif defined ATOMIC_PLATFORM_OSX
  286. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  287. if (fileSystem->DirExists("/Applications/Xamarin Studio.app"))
  288. {
  289. idePath_ = "/Applications/Xamarin Studio.app/Contents/MacOS/XamarinStudio";
  290. }
  291. #endif
  292. }
  293. }