NETProjectSystem.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. void 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;
  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. }
  170. }
  171. bool NETProjectSystem::GenerateResourcePak()
  172. {
  173. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  174. Project* project = tsystem->GetProject();
  175. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  176. // TODO: We just use WINDOWS platform for PAK generation for now
  177. Platform* platform = tsystem->GetPlatformByName("WINDOWS");
  178. buildSystem->SetBuildPath(project->GetProjectPath() + "AtomicNET/Resources/");
  179. SharedPtr<BuildBase> buildBase(platform->NewBuild(project));
  180. buildBase->SetResourcesOnly(true);
  181. buildBase->SetVerbose(true);
  182. buildSystem->QueueBuild(buildBase);
  183. buildSystem->StartNextBuild();
  184. if (buildBase->GetBuildFailed())
  185. {
  186. const StringVector& errors = buildBase->GetBuildErrors();
  187. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Build Resources.pak: %s", errors.Size() ? errors[0].CString() : "Unknown Error");
  188. return false;
  189. }
  190. return true;
  191. }
  192. bool NETProjectSystem::GenerateSolution()
  193. {
  194. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  195. Project* project = tsystem->GetProject();
  196. if (!project)
  197. {
  198. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - No Project Loaded");
  199. return false;
  200. }
  201. // TODO: Generalize and move me
  202. if (project->GetSupportsPlatform("android") || project->GetSupportsPlatform("ios"))
  203. {
  204. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  205. if (!fileSystem->FileExists(project->GetProjectPath() + "AtomicNET/Resources/AtomicResources.pak"))
  206. {
  207. if (!GenerateResourcePak())
  208. return false;
  209. }
  210. }
  211. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  212. if (!gen->LoadAtomicProject(project->GetProjectPath()))
  213. {
  214. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Load Project");
  215. return false;
  216. }
  217. if (!gen->Generate())
  218. {
  219. ATOMIC_LOGERRORF("NETProjectSystem::GenerateSolution - Unable to Generate Project");
  220. return false;
  221. }
  222. return true;
  223. }
  224. void NETProjectSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  225. {
  226. using namespace Update;
  227. float delta = eventData[P_TIMESTEP].GetFloat();
  228. quietPeriod_ -= delta;
  229. if (quietPeriod_ < 0.0f)
  230. quietPeriod_ = 0.0f;
  231. if (quietPeriod_ > 0.0f)
  232. return;
  233. if (solutionDirty_)
  234. {
  235. // set to false in case of error, we don't want to keep trying to
  236. // rebuild, TODO: better error handling
  237. solutionDirty_ = false;
  238. GenerateSolution();
  239. }
  240. if (projectAssemblyDirty_)
  241. {
  242. BuildAtomicProject();
  243. projectAssemblyDirty_ = false;
  244. }
  245. }
  246. void NETProjectSystem::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  247. {
  248. using namespace ProjectLoaded;
  249. projectPath_ = eventData[P_PROJECTPATH].GetString();
  250. Project* project = static_cast<Project*>(eventData[P_PROJECT].GetPtr());
  251. if (GetExtension(projectPath_) == ".atomic")
  252. projectPath_ = GetParentPath(projectPath_);
  253. String projectName = project->GetProjectSettings()->GetName();
  254. solutionPath_ = AddTrailingSlash(projectPath_) + "AtomicNET/Solution/" + projectName + ".sln";
  255. projectAssemblyPath_ = AddTrailingSlash(projectPath_) + "Resources/" + projectName + ".dll";
  256. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  257. // TODO: We need a better way of marking C# projects
  258. StringVector results;
  259. fileSystem->ScanDir(results, AddTrailingSlash(projectPath_) + "Resources", "*.cs", SCAN_FILES, true);
  260. if (!results.Size())
  261. {
  262. fileSystem->ScanDir(results, AddTrailingSlash(projectPath_) + "Resources", "*.dll", SCAN_FILES, true);
  263. if (!results.Size())
  264. {
  265. solutionPath_.Clear();
  266. return;
  267. }
  268. }
  269. // if the solution or project assemblies don't exist mark as dirty
  270. if (!fileSystem->FileExists(solutionPath_))
  271. solutionDirty_ = true;
  272. if (!fileSystem->FileExists(projectAssemblyPath_))
  273. projectAssemblyDirty_ = true;
  274. }
  275. void NETProjectSystem::HandleAssetScanBegin(StringHash eventType, VariantMap& eventData)
  276. {
  277. }
  278. void NETProjectSystem::HandleAssetScanEnd(StringHash eventType, VariantMap& eventData)
  279. {
  280. if (solutionDirty_)
  281. {
  282. // set to false in case of error, we don't want to keep trying to
  283. // rebuild, TODO: better error handling
  284. solutionDirty_ = false;
  285. GenerateSolution();
  286. }
  287. }
  288. void NETProjectSystem::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
  289. {
  290. Clear();
  291. }
  292. void NETProjectSystem::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  293. {
  294. }
  295. void NETProjectSystem::HandleAssetNew(StringHash eventType, VariantMap& eventData)
  296. {
  297. using namespace ResourceAdded;
  298. const String& guid = eventData[P_GUID].ToString();
  299. Asset* asset = GetSubsystem<AssetDatabase>()->GetAssetByGUID(guid);
  300. if (asset->GetExtension() == ".cs")
  301. solutionDirty_ = true;
  302. }
  303. void NETProjectSystem::HandleResourceAdded(StringHash eventType, VariantMap& eventData)
  304. {
  305. }
  306. void NETProjectSystem::HandleResourceRemoved(StringHash eventType, VariantMap& eventData)
  307. {
  308. }
  309. void NETProjectSystem::HandleAssetRenamed(StringHash eventType, VariantMap& eventData)
  310. {
  311. }
  312. void NETProjectSystem::HandleAssetMoved(StringHash eventType, VariantMap& eventData)
  313. {
  314. }
  315. void NETProjectSystem::Initialize()
  316. {
  317. Clear();
  318. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(NETProjectSystem, HandleUpdate));
  319. SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectLoaded));
  320. SubscribeToEvent(E_PROJECTUNLOADED, ATOMIC_HANDLER(NETProjectSystem, HandleProjectUnloaded));
  321. SubscribeToEvent(E_ASSETSCANBEGIN, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanBegin));
  322. SubscribeToEvent(E_ASSETSCANEND, ATOMIC_HANDLER(NETProjectSystem, HandleAssetScanEnd));
  323. SubscribeToEvent(E_FILECHANGED, ATOMIC_HANDLER(NETProjectSystem, HandleFileChanged));
  324. SubscribeToEvent(E_RESOURCEADDED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceAdded));
  325. SubscribeToEvent(E_RESOURCEREMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleResourceRemoved));
  326. SubscribeToEvent(E_ASSETNEW, ATOMIC_HANDLER(NETProjectSystem, HandleAssetNew));
  327. SubscribeToEvent(E_ASSETRENAMED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetRenamed));
  328. SubscribeToEvent(E_ASSETMOVED, ATOMIC_HANDLER(NETProjectSystem, HandleAssetMoved));
  329. #ifdef ATOMIC_PLATFORM_WINDOWS
  330. // On Windows, we first check for VS2015, then VS2017 which
  331. // at the time of this comment is in RC, refactor once
  332. // in general release
  333. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  334. // Query for Visual Studio 2015 path
  335. idePath_ = Poco::Environment::get("VS140COMNTOOLS", "").c_str();
  336. if (idePath_.Length())
  337. {
  338. idePath_.Replace("Tools\\", "IDE\\devenv.exe");
  339. if (!fileSystem->FileExists(idePath_))
  340. idePath_.Clear();
  341. }
  342. else
  343. {
  344. // check for VS2017
  345. Poco::WinRegistryKey regKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\VisualStudio\\SxS\\VS7", true);
  346. if (regKey.exists() && regKey.exists("15.0"))
  347. idePath_ = regKey.getString("15.0").c_str();
  348. if (idePath_.Length())
  349. {
  350. // We have VS2017
  351. idePath_ += "Common7\\IDE\\devenv.exe";
  352. }
  353. }
  354. #elif defined ATOMIC_PLATFORM_OSX
  355. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  356. // first look for Visual Studio Mac
  357. idePath_ = "/Applications/Visual Studio.app/Contents/MacOS/VisualStudio";
  358. if (!fileSystem->FileExists(idePath_))
  359. {
  360. // not found, look for Xamarin Studio
  361. idePath_ = "/Applications/Xamarin Studio.app/Contents/MacOS/XamarinStudio";
  362. if (!fileSystem->FileExists(idePath_))
  363. {
  364. idePath_.Clear();
  365. }
  366. }
  367. #elif defined ATOMIC_PLATFORM_LINUX
  368. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  369. if (fileSystem->FileExists("/usr/bin/monodevelop"))
  370. {
  371. idePath_ = "/usr/bin/monodevelop";
  372. }
  373. #endif
  374. }
  375. bool AtomicNETCopyAssemblies(Context* context, const String& dstFolder)
  376. {
  377. FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
  378. ToolEnvironment* tenv = context->GetSubsystem<ToolEnvironment>();
  379. StringVector results;
  380. fileSystem->ScanDir(results, tenv->GetAtomicNETCoreAssemblyDir(), "*", SCAN_FILES, true);
  381. for (unsigned i = 0; i < results.Size(); i++)
  382. {
  383. String srcFile = tenv->GetAtomicNETCoreAssemblyDir() + results[i];
  384. String dstFile = dstFolder + results[i];
  385. unsigned srcModifiedTime = 0;
  386. if (fileSystem->FileExists(srcFile))
  387. {
  388. srcModifiedTime = fileSystem->GetLastModifiedTime(srcFile);
  389. }
  390. // skip if same modified time
  391. if (srcModifiedTime && fileSystem->FileExists(dstFile))
  392. {
  393. if (srcModifiedTime == fileSystem->GetLastModifiedTime(dstFile))
  394. {
  395. ATOMIC_LOGDEBUGF("NETProjectSystem::CopyAtomicAssemblies - Skipping AtomicNET %s as %s exists and has same modified time", srcFile.CString(), dstFile.CString());
  396. continue;
  397. }
  398. ATOMIC_LOGDEBUGF("NETProjectSystem::CopyAtomicAssemblies - %u %u", srcModifiedTime, fileSystem->GetLastModifiedTime(dstFile));
  399. }
  400. String dstPath = GetPath(dstFile);
  401. if (!fileSystem->CreateDirsRecursive(dstPath))
  402. {
  403. ATOMIC_LOGERRORF("NETProjectGen::CopyAtomicAssemblies - Unable to create folder: %s", dstPath.CString());
  404. continue;
  405. }
  406. if (!fileSystem->Copy(srcFile, dstFile))
  407. {
  408. ATOMIC_LOGERRORF("NETProjectGen::CopyAtomicAssemblies - Unable to copy file from: %s to %s", srcFile.CString(), dstPath.CString());
  409. continue;
  410. }
  411. // Update time so we don't needlessly copy
  412. if (srcModifiedTime)
  413. fileSystem->SetLastModifiedTime(dstFile, srcModifiedTime);
  414. ATOMIC_LOGDEBUGF(" NETProjectSystem::CopyAtomicAssemblies - Copied AtomicNET %s to %s", srcFile.CString(), dstPath.CString());
  415. }
  416. return true;
  417. }
  418. }