NETProjectSystem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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/ProjectSettings.h"
  32. #include "../Project/ProjectEvents.h"
  33. #include "../Build/BuildSystem.h"
  34. #include "../Subprocess/SubprocessSystem.h"
  35. #include "NETProjectGen.h"
  36. #include "NETBuildSystem.h"
  37. #include "NETProjectSystem.h"
  38. namespace ToolCore
  39. {
  40. NETProjectSystem::NETProjectSystem(Context* context) :
  41. Object(context)
  42. {
  43. Initialize();
  44. }
  45. NETProjectSystem::~NETProjectSystem()
  46. {
  47. }
  48. void NETProjectSystem::Clear()
  49. {
  50. quietPeriod_ = 0.0f;
  51. solutionPath_.Clear();
  52. projectAssemblyPath_.Clear();
  53. solutionDirty_ = false;
  54. projectAssemblyDirty_ = false;
  55. }
  56. void NETProjectSystem::OpenSolution()
  57. {
  58. if (!idePath_.Length())
  59. return;
  60. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  61. if (!fileSystem->FileExists(solutionPath_))
  62. {
  63. if (!GenerateSolution())
  64. return;
  65. }
  66. OpenSourceFile(String::EMPTY);
  67. }
  68. void NETProjectSystem::OpenSourceFile(const String& sourceFilePath)
  69. {
  70. if (!idePath_.Length())
  71. return;
  72. String command = idePath_;
  73. StringVector args;
  74. if (ideSubprocess_.Expired())
  75. {
  76. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  77. ideSubprocess_ = 0;
  78. #ifdef ATOMIC_PLATFORM_OSX
  79. command = "open";
  80. args.Push("-W");
  81. args.Push("-a");
  82. args.Push(idePath_);
  83. #endif
  84. args.Push(solutionPath_);
  85. if (sourceFilePath.Length())
  86. args.Push(sourceFilePath);
  87. try
  88. {
  89. ideSubprocess_ = subs->Launch(command, args);
  90. }
  91. catch (Poco::SystemException)
  92. {
  93. ideSubprocess_ = 0;
  94. }
  95. }
  96. else
  97. {
  98. if (!sourceFilePath.Length())
  99. return;
  100. try
  101. {
  102. std::vector<std::string> args;
  103. #ifdef ATOMIC_PLATFORM_WINDOWS
  104. args.push_back("/edit");
  105. #elif defined ATOMIC_PLATFORM_OSX
  106. command = "open";
  107. args.push_back("-a");
  108. args.push_back(idePath_.CString());
  109. #elif defined ATOMIC_PLATFORM_LINUX
  110. args.push_back(idePath_.CString());
  111. #endif
  112. args.push_back(sourceFilePath.CString());
  113. Poco::Process::launch(command.CString(), args);
  114. }
  115. catch (Poco::SystemException)
  116. {
  117. }
  118. }
  119. }
  120. void NETProjectSystem::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  121. {
  122. using namespace NETBuildResult;
  123. if (eventData[P_SUCCESS].GetBool())
  124. {
  125. ATOMIC_LOGINFOF("NETBuild Success for project");
  126. }
  127. else
  128. {
  129. const String& errorText = eventData[P_ERRORTEXT].GetString();
  130. ATOMIC_LOGERRORF("\n%s\n", errorText.CString());
  131. ATOMIC_LOGERRORF("NETBuild Error for project");
  132. }
  133. }
  134. void NETProjectSystem::BuildAtomicProject()
  135. {
  136. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  137. if (!fileSystem->FileExists(solutionPath_))
  138. {
  139. if (!GenerateSolution())
  140. {
  141. ATOMIC_LOGERRORF("NETProjectSystem::BuildAtomicProject - solutionPath does not exist: %s", solutionPath_.CString());
  142. return;
  143. }
  144. }
  145. Project* project = GetSubsystem<ToolSystem>()->GetProject();
  146. NETBuildSystem* buildSystem = GetSubsystem<NETBuildSystem>();
  147. if (buildSystem)
  148. {
  149. NETBuild* build = buildSystem->BuildAtomicProject(project);
  150. if (build)
  151. {
  152. build->SubscribeToEvent(E_NETBUILDRESULT, ATOMIC_HANDLER(NETProjectSystem, HandleNETBuildResult));
  153. }
  154. }
  155. }
  156. bool NETProjectSystem::GenerateResourcePak()
  157. {
  158. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  159. Project* project = tsystem->GetProject();
  160. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  161. // TODO: We just use WINDOWS platform for PAK generation for now
  162. Platform* platform = tsystem->GetPlatformByName("WINDOWS");
  163. buildSystem->SetBuildPath(project->GetProjectPath() + "AtomicNET/Resources/");
  164. SharedPtr<BuildBase> buildBase(platform->NewBuild(project));
  165. buildBase->SetResourcesOnly(true);
  166. buildBase->SetVerbose(true);
  167. buildSystem->QueueBuild(buildBase);
  168. buildSystem->StartNextBuild();
  169. if (buildBase->GetBuildFailed())
  170. {
  171. const StringVector& errors = buildBase->GetBuildErrors();
  172. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Build Resources.pak: %s", errors.Size() ? errors[0].CString() : "Unknown Error");
  173. return false;
  174. }
  175. return true;
  176. }
  177. bool NETProjectSystem::GenerateSolution()
  178. {
  179. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  180. Project* project = tsystem->GetProject();
  181. if (!project)
  182. {
  183. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - No Project Loaded");
  184. return false;
  185. }
  186. // TODO: Generalize and move me
  187. if (project->GetSupportsPlatform("android"))
  188. {
  189. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  190. if (!fileSystem->FileExists(project->GetProjectPath() + "AtomicNET/Resources/AtomicResources.pak"))
  191. {
  192. if (!GenerateResourcePak())
  193. return false;
  194. }
  195. }
  196. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  197. if (!gen->LoadAtomicProject(project->GetProjectPath()))
  198. {
  199. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Load Project");
  200. return false;
  201. }
  202. if (!gen->Generate())
  203. {
  204. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Generate Project");
  205. return false;
  206. }
  207. return true;
  208. }
  209. void NETProjectSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  210. {
  211. using namespace Update;
  212. float delta = eventData[P_TIMESTEP].GetFloat();
  213. quietPeriod_ -= delta;
  214. if (quietPeriod_ < 0.0f)
  215. quietPeriod_ = 0.0f;
  216. if (quietPeriod_ > 0.0f)
  217. return;
  218. if (solutionDirty_)
  219. {
  220. // set to false in case of error, we don't want to keep trying to
  221. // rebuild, TODO: better error handling
  222. solutionDirty_ = false;
  223. GenerateSolution();
  224. }
  225. if (projectAssemblyDirty_)
  226. {
  227. BuildAtomicProject();
  228. projectAssemblyDirty_ = false;
  229. }
  230. }
  231. void NETProjectSystem::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  232. {
  233. using namespace ProjectLoaded;
  234. String projectPath = eventData[P_PROJECTPATH].GetString();
  235. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  236. if (GetExtension(projectPath) == ".atomic")
  237. projectPath = GetParentPath(projectPath);
  238. String projectName = project->GetProjectSettings()->GetName();
  239. solutionPath_ = AddTrailingSlash(projectPath) + "AtomicNET/Solution/" + projectName + ".sln";
  240. projectAssemblyPath_ = AddTrailingSlash(projectPath) + "Resources/" + projectName + ".dll";
  241. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  242. // TODO: We need a better way of marking C# projects
  243. StringVector results;
  244. fileSystem->ScanDir(results, AddTrailingSlash(projectPath) + "Resources", "*.cs", SCAN_FILES, true);
  245. if (!results.Size())
  246. {
  247. fileSystem->ScanDir(results, AddTrailingSlash(projectPath) + "Resources", "*.dll", SCAN_FILES, true);
  248. if (!results.Size())
  249. return;
  250. }
  251. // if the solution or project assemblies don't exist mark as dirty
  252. if (!fileSystem->FileExists(solutionPath_))
  253. solutionDirty_ = true;
  254. if (!fileSystem->FileExists(projectAssemblyPath_))
  255. projectAssemblyDirty_ = true;
  256. }
  257. void NETProjectSystem::HandleAssetScanBegin(StringHash eventType, VariantMap& eventData)
  258. {
  259. }
  260. void NETProjectSystem::HandleAssetScanEnd(StringHash eventType, VariantMap& eventData)
  261. {
  262. if (solutionDirty_)
  263. {
  264. // set to false in case of error, we don't want to keep trying to
  265. // rebuild, TODO: better error handling
  266. solutionDirty_ = false;
  267. GenerateSolution();
  268. }
  269. }
  270. void NETProjectSystem::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
  271. {
  272. Clear();
  273. }
  274. void NETProjectSystem::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  275. {
  276. }
  277. void NETProjectSystem::HandleAssetNew(StringHash eventType, VariantMap& eventData)
  278. {
  279. using namespace ResourceAdded;
  280. const String& guid = eventData[P_GUID].ToString();
  281. Asset* asset = GetSubsystem<AssetDatabase>()->GetAssetByGUID(guid);
  282. if (asset->GetExtension() == ".cs")
  283. solutionDirty_ = true;
  284. }
  285. void NETProjectSystem::HandleResourceAdded(StringHash eventType, VariantMap& eventData)
  286. {
  287. }
  288. void NETProjectSystem::HandleResourceRemoved(StringHash eventType, VariantMap& eventData)
  289. {
  290. }
  291. void NETProjectSystem::HandleAssetRenamed(StringHash eventType, VariantMap& eventData)
  292. {
  293. }
  294. void NETProjectSystem::HandleAssetMoved(StringHash eventType, VariantMap& eventData)
  295. {
  296. }
  297. void NETProjectSystem::Initialize()
  298. {
  299. Clear();
  300. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(NETProjectSystem, HandleUpdate));
  301. SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectLoaded));
  302. SubscribeToEvent(E_PROJECTUNLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectUnloaded));
  303. SubscribeToEvent(E_ASSETSCANBEGIN, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanBegin));
  304. SubscribeToEvent(E_ASSETSCANEND, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanEnd));
  305. SubscribeToEvent(E_FILECHANGED, ATOMIC_HANDLER(NETProjectSystem, HandleFileChanged));
  306. SubscribeToEvent(E_RESOURCEADDED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceAdded));
  307. SubscribeToEvent(E_RESOURCEREMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceRemoved));
  308. SubscribeToEvent(E_ASSETNEW, ATOMIC_HANDLER(NETProjectSystem, HandleAssetNew));
  309. SubscribeToEvent(E_ASSETRENAMED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetRenamed));
  310. SubscribeToEvent(E_ASSETMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetMoved));
  311. #ifdef ATOMIC_PLATFORM_WINDOWS
  312. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  313. // Query for Visual Studio 2015 path
  314. idePath_ = Poco::Environment::get("VS140COMNTOOLS").c_str();
  315. if (idePath_.Length())
  316. {
  317. idePath_.Replace("Tools\\", "IDE\\devenv.exe");
  318. if (!fileSystem->FileExists(idePath_))
  319. idePath_.Clear();
  320. }
  321. #elif defined ATOMIC_PLATFORM_OSX
  322. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  323. if (fileSystem->DirExists("/Applications/Xamarin Studio.app"))
  324. {
  325. idePath_ = "/Applications/Xamarin Studio.app/Contents/MacOS/XamarinStudio";
  326. }
  327. #elif defined ATOMIC_PLATFORM_LINUX
  328. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  329. if (fileSystem->FileExists("/usr/bin/monodevelop"))
  330. {
  331. idePath_ = "/usr/bin/monodevelop";
  332. }
  333. #endif
  334. }
  335. }