NETProjectSystem.cpp 16 KB

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