NETProjectSystem.cpp 13 KB

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