NETProjectSystem.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 "../ToolEnvironment.h"
  30. #include "../Assets/AssetEvents.h"
  31. #include "../Assets/AssetDatabase.h"
  32. #include "../Project/Project.h"
  33. #include "../Project/ProjectSettings.h"
  34. #include "../Project/ProjectEvents.h"
  35. #include "../Build/BuildSystem.h"
  36. #include "../Subprocess/SubprocessSystem.h"
  37. #include "NETProjectGen.h"
  38. #include "NETBuildSystem.h"
  39. #include "NETProjectSystem.h"
  40. #ifdef ATOMIC_PLATFORM_WINDOWS
  41. #include <Poco/WinRegistryKey.h>
  42. #endif
  43. namespace ToolCore
  44. {
  45. NETProjectSystem::NETProjectSystem(Context* context) :
  46. Object(context)
  47. {
  48. Initialize();
  49. }
  50. NETProjectSystem::~NETProjectSystem()
  51. {
  52. }
  53. void NETProjectSystem::Clear()
  54. {
  55. quietPeriod_ = 0.0f;
  56. solutionPath_.Clear();
  57. projectAssemblyPath_.Clear();
  58. solutionDirty_ = false;
  59. projectAssemblyDirty_ = false;
  60. }
  61. void NETProjectSystem::OpenSolution()
  62. {
  63. if (!idePath_.Length())
  64. return;
  65. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  66. if (!fileSystem->FileExists(solutionPath_))
  67. {
  68. if (!GenerateSolution())
  69. return;
  70. }
  71. OpenSourceFile(String::EMPTY);
  72. }
  73. void NETProjectSystem::OpenSourceFile(const String& sourceFilePath)
  74. {
  75. if (!idePath_.Length())
  76. return;
  77. String command = idePath_;
  78. StringVector args;
  79. if (ideSubprocess_.Expired())
  80. {
  81. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  82. ideSubprocess_ = 0;
  83. #ifdef ATOMIC_PLATFORM_OSX
  84. command = "open";
  85. args.Push("-W");
  86. args.Push("-a");
  87. args.Push(idePath_);
  88. #endif
  89. args.Push(solutionPath_);
  90. if (sourceFilePath.Length())
  91. args.Push(sourceFilePath);
  92. #ifndef ATOMIC_PLATFORM_OSX
  93. QuoteArguments(args);
  94. #endif
  95. try
  96. {
  97. ideSubprocess_ = subs->Launch(command, args);
  98. }
  99. catch (Poco::SystemException)
  100. {
  101. ideSubprocess_ = 0;
  102. }
  103. }
  104. else
  105. {
  106. if (!sourceFilePath.Length())
  107. return;
  108. try
  109. {
  110. std::vector<std::string> args;
  111. #ifdef ATOMIC_PLATFORM_WINDOWS
  112. args.push_back("/edit");
  113. #elif defined ATOMIC_PLATFORM_OSX
  114. command = "open";
  115. args.push_back("-a");
  116. args.push_back(idePath_.CString());
  117. #elif defined ATOMIC_PLATFORM_LINUX
  118. args.push_back(idePath_.CString());
  119. #endif
  120. #ifdef ATOMIC_PLATFORM_OSX
  121. args.push_back(sourceFilePath.CString());
  122. #else
  123. if (sourceFilePath.Contains(" ") && !sourceFilePath.Contains("\""))
  124. args.push_back(("\"" + sourceFilePath + "\"").CString());
  125. else
  126. args.push_back(sourceFilePath.CString());
  127. #endif
  128. Poco::Process::launch(command.CString(), args);
  129. }
  130. catch (Poco::SystemException)
  131. {
  132. }
  133. }
  134. }
  135. void NETProjectSystem::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  136. {
  137. using namespace NETBuildResult;
  138. if (eventData[P_SUCCESS].GetBool())
  139. {
  140. ATOMIC_LOGINFOF("NETBuild Success for project");
  141. }
  142. else
  143. {
  144. const String& errorText = eventData[P_ERRORTEXT].GetString();
  145. ATOMIC_LOGERRORF("\n%s\n", errorText.CString());
  146. ATOMIC_LOGERRORF("NETBuild Error for project");
  147. }
  148. }
  149. NETBuild* NETProjectSystem::BuildAtomicProject()
  150. {
  151. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  152. if (!fileSystem->FileExists(solutionPath_))
  153. {
  154. if (!GenerateSolution())
  155. {
  156. ATOMIC_LOGERRORF("NETProjectSystem::BuildAtomicProject - solutionPath does not exist: %s", solutionPath_.CString());
  157. return nullptr;
  158. }
  159. }
  160. Project* project = GetSubsystem<ToolSystem>()->GetProject();
  161. NETBuildSystem* buildSystem = GetSubsystem<NETBuildSystem>();
  162. if (buildSystem)
  163. {
  164. NETBuild* build = buildSystem->BuildAtomicProject(project);
  165. if (build)
  166. {
  167. build->SubscribeToEvent(E_NETBUILDRESULT, ATOMIC_HANDLER(NETProjectSystem, HandleNETBuildResult));
  168. }
  169. return build;
  170. }
  171. return nullptr;
  172. }
  173. bool NETProjectSystem::GenerateResourcePak()
  174. {
  175. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  176. Project* project = tsystem->GetProject();
  177. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  178. // TODO: We just use WINDOWS platform for PAK generation for now
  179. Platform* platform = tsystem->GetPlatformByName("WINDOWS");
  180. buildSystem->SetBuildPath(project->GetProjectPath() + "AtomicNET/Resources/");
  181. SharedPtr<BuildBase> buildBase(platform->NewBuild(project));
  182. buildBase->SetResourcesOnly(true);
  183. buildBase->SetVerbose(true);
  184. buildSystem->QueueBuild(buildBase);
  185. buildSystem->StartNextBuild();
  186. if (buildBase->GetBuildFailed())
  187. {
  188. const StringVector& errors = buildBase->GetBuildErrors();
  189. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Build Resources.pak: %s", errors.Size() ? errors[0].CString() : "Unknown Error");
  190. return false;
  191. }
  192. return true;
  193. }
  194. bool NETProjectSystem::GenerateSolution()
  195. {
  196. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  197. Project* project = tsystem->GetProject();
  198. if (!project)
  199. {
  200. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - No Project Loaded");
  201. return false;
  202. }
  203. // TODO: Generalize and move me
  204. if (project->GetSupportsPlatform("android") || project->GetSupportsPlatform("ios"))
  205. {
  206. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  207. if (!fileSystem->FileExists(project->GetProjectPath() + "AtomicNET/Resources/AtomicResources.pak"))
  208. {
  209. if (!GenerateResourcePak())
  210. return false;
  211. }
  212. }
  213. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  214. if (!gen->LoadAtomicProject(project->GetProjectPath()))
  215. {
  216. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Load Project");
  217. return false;
  218. }
  219. if (!gen->Generate())
  220. {
  221. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Generate Project");
  222. return false;
  223. }
  224. return true;
  225. }
  226. void NETProjectSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  227. {
  228. using namespace Update;
  229. float delta = eventData[P_TIMESTEP].GetFloat();
  230. quietPeriod_ -= delta;
  231. if (quietPeriod_ < 0.0f)
  232. quietPeriod_ = 0.0f;
  233. if (quietPeriod_ > 0.0f)
  234. return;
  235. if (solutionDirty_)
  236. {
  237. // set to false in case of error, we don't want to keep trying to
  238. // rebuild, TODO: better error handling
  239. solutionDirty_ = false;
  240. GenerateSolution();
  241. }
  242. if (projectAssemblyDirty_)
  243. {
  244. BuildAtomicProject();
  245. projectAssemblyDirty_ = false;
  246. }
  247. }
  248. void NETProjectSystem::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  249. {
  250. using namespace ProjectLoaded;
  251. projectPath_ = eventData[P_PROJECTPATH].GetString();
  252. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  253. if (GetExtension(projectPath_) == ".atomic")
  254. projectPath_ = GetParentPath(projectPath_);
  255. String projectName = project->GetProjectSettings()->GetName();
  256. solutionPath_ = AddTrailingSlash(projectPath_) + "AtomicNET/Solution/" + projectName + ".sln";
  257. projectAssemblyPath_ = AddTrailingSlash(projectPath_) + "Resources/" + projectName + ".dll";
  258. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  259. // TODO: We need a better way of marking C# projects
  260. StringVector results;
  261. fileSystem->ScanDir(results, AddTrailingSlash(projectPath_) + "Resources", "*.cs", SCAN_FILES, true);
  262. if (!results.Size())
  263. {
  264. fileSystem->ScanDir(results, AddTrailingSlash(projectPath_) + "Resources", "*.dll", SCAN_FILES, true);
  265. if (!results.Size())
  266. {
  267. solutionPath_.Clear();
  268. return;
  269. }
  270. }
  271. // if the solution or project assemblies don't exist mark as dirty
  272. if (!fileSystem->FileExists(solutionPath_))
  273. solutionDirty_ = true;
  274. CheckProjectAssembly();
  275. }
  276. void NETProjectSystem::HandleAssetScanBegin(StringHash eventType, VariantMap& eventData)
  277. {
  278. }
  279. void NETProjectSystem::HandleAssetScanEnd(StringHash eventType, VariantMap& eventData)
  280. {
  281. if (solutionDirty_)
  282. {
  283. // set to false in case of error, we don't want to keep trying to
  284. // rebuild, TODO: better error handling
  285. solutionDirty_ = false;
  286. GenerateSolution();
  287. }
  288. }
  289. void NETProjectSystem::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
  290. {
  291. Clear();
  292. }
  293. void NETProjectSystem::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  294. {
  295. }
  296. void NETProjectSystem::HandleAssetNew(StringHash eventType, VariantMap& eventData)
  297. {
  298. using namespace ResourceAdded;
  299. const String& guid = eventData[P_GUID].ToString();
  300. Asset* asset = GetSubsystem<AssetDatabase>()->GetAssetByGUID(guid);
  301. if (asset->GetExtension() == ".cs")
  302. solutionDirty_ = true;
  303. }
  304. void NETProjectSystem::HandleResourceAdded(StringHash eventType, VariantMap& eventData)
  305. {
  306. }
  307. void NETProjectSystem::HandleResourceRemoved(StringHash eventType, VariantMap& eventData)
  308. {
  309. }
  310. void NETProjectSystem::HandleAssetRenamed(StringHash eventType, VariantMap& eventData)
  311. {
  312. }
  313. void NETProjectSystem::HandleAssetMoved(StringHash eventType, VariantMap& eventData)
  314. {
  315. }
  316. bool NETProjectSystem::GetProjectAssemblyDirty()
  317. {
  318. if (projectAssemblyDirty_)
  319. return true;
  320. CheckProjectAssembly();
  321. return projectAssemblyDirty_;
  322. }
  323. void NETProjectSystem::CheckProjectAssembly()
  324. {
  325. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  326. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  327. Project* project = tsystem->GetProject();
  328. if (!project || !projectAssemblyPath_.Length())
  329. {
  330. ATOMIC_LOGERROR("NETProjectSystem::CheckProjectAssembly() - Called with no project loaded or an empty project assembly path");
  331. projectAssemblyDirty_ = false;
  332. return;
  333. }
  334. if (projectAssemblyDirty_)
  335. return;
  336. // If we don't have a project or the project assembly is missing, we must rebuild
  337. if (!fileSystem->FileExists(projectAssemblyPath_))
  338. {
  339. projectAssemblyDirty_ = true;
  340. return;
  341. }
  342. StringVector results;
  343. // timestamps for present assemblies, use the filesystem and not asset db as the later is cached
  344. PODVector<unsigned> assemblyTimestamps;
  345. fileSystem->ScanDir(results, project->GetResourcePath(), "*.dll", SCAN_FILES, true);
  346. for (unsigned i = 0; i < results.Size(); i++)
  347. {
  348. assemblyTimestamps.Push(fileSystem->GetLastModifiedTime(project->GetResourcePath() + results[i]));
  349. }
  350. fileSystem->ScanDir(results, project->GetResourcePath(), "*.cs", SCAN_FILES, true);
  351. for (unsigned i = 0; i < results.Size(); i++)
  352. {
  353. unsigned timestamp = fileSystem->GetLastModifiedTime(project->GetResourcePath() + results[i]);
  354. for (unsigned j = 0; j < assemblyTimestamps.Size(); j++)
  355. {
  356. if (timestamp > assemblyTimestamps[j])
  357. {
  358. projectAssemblyDirty_ = true;
  359. return;
  360. }
  361. }
  362. }
  363. }
  364. void NETProjectSystem::Initialize()
  365. {
  366. Clear();
  367. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(NETProjectSystem, HandleUpdate));
  368. SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectLoaded));
  369. SubscribeToEvent(E_PROJECTUNLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectUnloaded));
  370. SubscribeToEvent(E_ASSETSCANBEGIN, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanBegin));
  371. SubscribeToEvent(E_ASSETSCANEND, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanEnd));
  372. SubscribeToEvent(E_FILECHANGED, ATOMIC_HANDLER(NETProjectSystem, HandleFileChanged));
  373. SubscribeToEvent(E_RESOURCEADDED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceAdded));
  374. SubscribeToEvent(E_RESOURCEREMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceRemoved));
  375. SubscribeToEvent(E_ASSETNEW, ATOMIC_HANDLER(NETProjectSystem, HandleAssetNew));
  376. SubscribeToEvent(E_ASSETRENAMED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetRenamed));
  377. SubscribeToEvent(E_ASSETMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetMoved));
  378. #ifdef ATOMIC_PLATFORM_WINDOWS
  379. // On Windows, we first check for VS2015, then VS2017 which
  380. // at the time of this comment is in RC, refactor once
  381. // in general release
  382. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  383. // Query for Visual Studio 2015 path
  384. idePath_ = Poco::Environment::get("VS140COMNTOOLS", "").c_str();
  385. if (idePath_.Length())
  386. {
  387. idePath_.Replace("Tools\\", "IDE\\devenv.exe");
  388. if (!fileSystem->FileExists(idePath_))
  389. idePath_.Clear();
  390. }
  391. // If we didn't find VS2015, look for VS2017
  392. if (!idePath_.Length())
  393. {
  394. // check for VS2017
  395. Poco::WinRegistryKey regKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\VisualStudio\\SxS\\VS7", true);
  396. if (regKey.exists() && regKey.exists("15.0"))
  397. idePath_ = regKey.getString("15.0").c_str();
  398. if (idePath_.Length())
  399. {
  400. // We have VS2017
  401. idePath_ += "Common7\\IDE\\devenv.exe";
  402. }
  403. }
  404. #elif defined ATOMIC_PLATFORM_OSX
  405. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  406. // first look for Visual Studio Mac
  407. idePath_ = "/Applications/Visual Studio.app/Contents/MacOS/VisualStudio";
  408. if (!fileSystem->FileExists(idePath_))
  409. {
  410. // not found, look for Xamarin Studio
  411. idePath_ = "/Applications/Xamarin Studio.app/Contents/MacOS/XamarinStudio";
  412. if (!fileSystem->FileExists(idePath_))
  413. {
  414. idePath_.Clear();
  415. }
  416. }
  417. #elif defined ATOMIC_PLATFORM_LINUX
  418. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  419. if (fileSystem->FileExists("/usr/bin/monodevelop"))
  420. {
  421. idePath_ = "/usr/bin/monodevelop";
  422. }
  423. #endif
  424. }
  425. bool AtomicNETCopyAssemblies(Context* context, const String& dstFolder)
  426. {
  427. FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
  428. ToolEnvironment* tenv = context->GetSubsystem<ToolEnvironment>();
  429. StringVector results;
  430. fileSystem->ScanDir(results, tenv->GetAtomicNETCoreAssemblyDir(), "*", SCAN_FILES, true);
  431. for (unsigned i = 0; i < results.Size(); i++)
  432. {
  433. String srcFile = tenv->GetAtomicNETCoreAssemblyDir() + results[i];
  434. String dstFile = dstFolder + results[i];
  435. unsigned srcModifiedTime = 0;
  436. if (fileSystem->FileExists(srcFile))
  437. {
  438. srcModifiedTime = fileSystem->GetLastModifiedTime(srcFile);
  439. }
  440. // skip if same modified time
  441. if (srcModifiedTime && fileSystem->FileExists(dstFile))
  442. {
  443. if (srcModifiedTime == fileSystem->GetLastModifiedTime(dstFile))
  444. {
  445. continue;
  446. }
  447. }
  448. String dstPath = GetPath(dstFile);
  449. if (!fileSystem->CreateDirsRecursive(dstPath))
  450. {
  451. ATOMIC_LOGERRORF("NETProjectGen::CopyAtomicAssemblies - Unable to create folder: %s", dstPath.CString());
  452. continue;
  453. }
  454. if (!fileSystem->Copy(srcFile, dstFile))
  455. {
  456. ATOMIC_LOGERRORF("NETProjectGen::CopyAtomicAssemblies - Unable to copy file from: %s to %s", srcFile.CString(), dstPath.CString());
  457. continue;
  458. }
  459. // Update time so we don't needlessly copy
  460. if (srcModifiedTime)
  461. fileSystem->SetLastModifiedTime(dstFile, srcModifiedTime);
  462. }
  463. return true;
  464. }
  465. }