NETProjectGen.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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/UUID.h>
  23. #include <Poco/UUIDGenerator.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/File.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include "../ToolEnvironment.h"
  28. #include "../ToolSystem.h"
  29. #include "../Project/ProjectSettings.h"
  30. #include "../Project/Project.h"
  31. #include "NETProjectGen.h"
  32. namespace ToolCore
  33. {
  34. NETProjectBase::NETProjectBase(Context* context, NETProjectGen* projectGen) :
  35. Object(context), xmlFile_(new XMLFile(context)), projectGen_(projectGen)
  36. {
  37. }
  38. NETProjectBase::~NETProjectBase()
  39. {
  40. }
  41. void NETProjectBase::ReplacePathStrings(String& path)
  42. {
  43. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  44. String atomicRoot = tenv->GetRootSourceDir();
  45. atomicRoot = RemoveTrailingSlash(atomicRoot);
  46. path.Replace("$ATOMIC_ROOT$", atomicRoot, false);
  47. String atomicProjectPath = projectGen_->GetAtomicProjectPath();
  48. atomicProjectPath.Replace("/", "\\");
  49. if (atomicProjectPath.Length())
  50. {
  51. path.Replace("$ATOMIC_PROJECT_ROOT$", atomicProjectPath, false);
  52. }
  53. }
  54. NETCSProject::NETCSProject(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen),
  55. androidApplication_(false),
  56. playerApplication_(false)
  57. {
  58. }
  59. NETCSProject::~NETCSProject()
  60. {
  61. }
  62. bool NETCSProject::SupportsDesktop() const
  63. {
  64. if (!platforms_.Size())
  65. return true;
  66. if (platforms_.Contains("desktop") || platforms_.Contains("windows") || platforms_.Contains("macosx") || platforms_.Contains("linux"))
  67. return true;
  68. return false;
  69. }
  70. bool NETCSProject::SupportsPlatform(const String& platform, bool explicitCheck) const
  71. {
  72. if (!explicitCheck && !platforms_.Size())
  73. return true;
  74. if (platforms_.Contains(platform.ToLower()))
  75. return true;
  76. return false;
  77. }
  78. bool NETCSProject::CreateProjectFolder(const String& path)
  79. {
  80. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  81. if (fileSystem->DirExists(path))
  82. return true;
  83. fileSystem->CreateDirsRecursive(path);
  84. if (!fileSystem->DirExists(path))
  85. {
  86. ATOMIC_LOGERRORF("Unable to create dir: %s", path.CString());
  87. return false;
  88. }
  89. return true;
  90. }
  91. void NETCSProject::CreateCompileItemGroup(XMLElement &projectRoot)
  92. {
  93. FileSystem* fs = GetSubsystem<FileSystem>();
  94. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  95. // Compile AssemblyInfo.cs
  96. if (!GetIsPCL() && !sharedReferences_.Size() && outputType_ != "Shared")
  97. igroup.CreateChild("Compile").SetAttribute("Include", "Properties\\AssemblyInfo.cs");
  98. for (unsigned i = 0; i < sourceFolders_.Size(); i++)
  99. {
  100. const String& sourceFolder = sourceFolders_[i];
  101. Vector<String> result;
  102. fs->ScanDir(result, sourceFolder, "*.cs", SCAN_FILES, true);
  103. for (unsigned j = 0; j < result.Size(); j++)
  104. {
  105. XMLElement compile = igroup.CreateChild("Compile");
  106. String path = sourceFolder + result[j];
  107. String relativePath;
  108. if (GetRelativePath(projectPath_, GetPath(path), relativePath))
  109. {
  110. path = relativePath + GetFileName(path) + GetExtension(path);
  111. }
  112. // IMPORTANT: / Slash direction breaks intellisense :/
  113. path.Replace('/', '\\');
  114. compile.SetAttribute("Include", path.CString());
  115. // put generated files into generated folder
  116. if (sourceFolder.Contains("Generated") && sourceFolder.Contains("CSharp") && sourceFolder.Contains("Packages"))
  117. {
  118. compile.CreateChild("Link").SetValue("Generated\\" + result[j]);
  119. }
  120. else
  121. {
  122. compile.CreateChild("Link").SetValue(result[j]);
  123. }
  124. }
  125. }
  126. }
  127. void NETProjectBase::CopyXMLElementRecursive(XMLElement source, XMLElement dest)
  128. {
  129. Vector<String> attrNames = source.GetAttributeNames();
  130. for (unsigned i = 0; i < attrNames.Size(); i++)
  131. {
  132. String value = source.GetAttribute(attrNames[i]);
  133. dest.SetAttribute(attrNames[i], value);
  134. }
  135. dest.SetValue(source.GetValue());
  136. XMLElement child = source.GetChild();
  137. while (child.NotNull() && child.GetName().Length())
  138. {
  139. XMLElement childDest = dest.CreateChild(child.GetName());
  140. CopyXMLElementRecursive(child, childDest);
  141. child = child.GetNext();
  142. }
  143. }
  144. void NETCSProject::CreateReferencesItemGroup(XMLElement &projectRoot)
  145. {
  146. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  147. XMLElement xref;
  148. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  149. for (unsigned i = 0; i < references_.Size(); i++)
  150. {
  151. String ref = references_[i];
  152. // project reference
  153. if (projectGen_->GetCSProjectByName(ref))
  154. continue;
  155. String platform;
  156. if (projectGen_->GetAtomicProjectPath().Length() && ref.StartsWith("AtomicNET"))
  157. {
  158. if (GetIsPCL())
  159. {
  160. ref = "AtomicNET";
  161. platform = "Portable";
  162. }
  163. else if (SupportsDesktop())
  164. {
  165. ref = "AtomicNET";
  166. platform = "Desktop";
  167. }
  168. else if (SupportsPlatform("android"))
  169. {
  170. if (ref != "AtomicNET.Android.SDL")
  171. ref = "AtomicNET";
  172. platform = "Android";
  173. }
  174. if (platform.Length())
  175. {
  176. String atomicNETAssembly = tenv->GetAtomicNETCoreAssemblyDir() + ToString("%s/%s.dll", platform.CString(), ref.CString());
  177. xref = igroup.CreateChild("Reference");
  178. xref.SetAttribute("Include", atomicNETAssembly);
  179. }
  180. continue;
  181. }
  182. // NuGet project
  183. if (ref.StartsWith("<"))
  184. {
  185. XMLFile xmlFile(context_);
  186. if (!xmlFile.FromString(ref))
  187. {
  188. ATOMIC_LOGERROR("NETCSProject::CreateReferencesItemGroup - Unable to parse reference XML");
  189. }
  190. xref = igroup.CreateChild("Reference");
  191. CopyXMLElementRecursive(xmlFile.GetRoot(), xref);
  192. continue;
  193. }
  194. xref = igroup.CreateChild("Reference");
  195. xref.SetAttribute("Include", ref);
  196. }
  197. const String atomicProjectPath = projectGen_->GetAtomicProjectPath();
  198. if (atomicProjectPath.Length())
  199. {
  200. String resourceDir = AddTrailingSlash(atomicProjectPath) + "Resources/";
  201. Vector<String> result;
  202. GetSubsystem<FileSystem>()->ScanDir(result, resourceDir , "*.dll", SCAN_FILES, true);
  203. for (unsigned j = 0; j < result.Size(); j++)
  204. {
  205. String path = resourceDir + result[j];
  206. String relativePath;
  207. if (GetRelativePath(projectPath_, GetPath(path), relativePath))
  208. {
  209. if (projectGen_->GetCSProjectByName(GetFileName(path)))
  210. continue;
  211. path = relativePath + GetFileName(path) + GetExtension(path);
  212. }
  213. xref = igroup.CreateChild("Reference");
  214. xref.SetAttribute("Include", path);
  215. }
  216. }
  217. }
  218. void NETCSProject::CreateProjectReferencesItemGroup(XMLElement &projectRoot)
  219. {
  220. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  221. for (unsigned i = 0; i < references_.Size(); i++)
  222. {
  223. const String& ref = references_[i];
  224. NETCSProject* project = projectGen_->GetCSProjectByName(ref);
  225. if (!project)
  226. continue;
  227. XMLElement projectRef = igroup.CreateChild("ProjectReference");
  228. projectRef.SetAttribute("Include", ToString("..\\%s\\%s.csproj", ref.CString(), ref.CString()));
  229. XMLElement xproject = projectRef.CreateChild("Project");
  230. xproject.SetValue(ToString("{%s}", project->GetProjectGUID().ToLower().CString()));
  231. XMLElement xname = projectRef.CreateChild("Name");
  232. xname.SetValue(project->GetName());
  233. }
  234. }
  235. void NETCSProject::CreatePackagesItemGroup(XMLElement &projectRoot)
  236. {
  237. if (!packages_.Size())
  238. return;
  239. XMLElement xref;
  240. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  241. xref = igroup.CreateChild("None");
  242. xref.SetAttribute("Include", "packages.config");
  243. XMLFile packageConfig(context_);
  244. XMLElement packageRoot = packageConfig.CreateRoot("packages");
  245. for (unsigned i = 0; i < packages_.Size(); i++)
  246. {
  247. XMLFile xmlFile(context_);
  248. if (!xmlFile.FromString(packages_[i]))
  249. {
  250. ATOMIC_LOGERROR("NETCSProject::CreatePackagesItemGroup - Unable to parse package xml");
  251. }
  252. xref = packageRoot.CreateChild("package");
  253. CopyXMLElementRecursive(xmlFile.GetRoot(), xref);
  254. }
  255. SharedPtr<File> output(new File(context_, GetSanitizedPath(projectPath_ + "packages.config"), FILE_WRITE));
  256. String source = packageConfig.ToString();
  257. output->Write(source.CString(), source.Length());
  258. }
  259. void NETCSProject::GetAssemblySearchPaths(String& paths)
  260. {
  261. paths.Clear();
  262. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  263. Vector<String> searchPaths;
  264. if (assemblySearchPaths_.Length())
  265. searchPaths.Push(assemblySearchPaths_);
  266. paths.Join(searchPaths, ";");
  267. }
  268. void NETCSProject::CreateReleasePropertyGroup(XMLElement &projectRoot)
  269. {
  270. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  271. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ");
  272. pgroup.CreateChild("Optimize").SetValue("true");
  273. String outputPath = assemblyOutputPath_;
  274. outputPath.Replace("$ATOMIC_CONFIG$", "Release");
  275. pgroup.CreateChild("OutputPath").SetValue(outputPath);
  276. Vector<String> constants;
  277. constants.Push("TRACE");
  278. constants += defineConstants_;
  279. const Vector<String>& globalConstants = projectGen_->GetGlobalDefineConstants();
  280. constants += globalConstants;
  281. pgroup.CreateChild("DefineConstants").SetValue(String::Joined(constants, ";").CString());
  282. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  283. pgroup.CreateChild("WarningLevel").SetValue("4");
  284. pgroup.CreateChild("ConsolePause").SetValue("false");
  285. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  286. if (SupportsDesktop())
  287. {
  288. pgroup.CreateChild("DebugType").SetValue(GetIsPCL() ? "pdbonly": "full");
  289. if (!GetIsPCL())
  290. pgroup.CreateChild("PlatformTarget").SetValue("x64");
  291. #ifndef ATOMIC_PLATFORM_WINDOWS
  292. String startArguments;
  293. #ifndef ATOMIC_DEV_BUILD
  294. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  295. #ifdef ATOMIC_PLATFORM_OSX
  296. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "../Resources/").CString());
  297. #else
  298. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "Resources/").CString());
  299. #endif
  300. #endif
  301. startArguments += ToString("--project \"%s\"", projectGen_->GetAtomicProjectPath().CString());
  302. pgroup.CreateChild("Commandlineparameters").SetValue(startArguments);
  303. #endif
  304. }
  305. else
  306. {
  307. pgroup.CreateChild("DebugType").SetValue("pdbonly");
  308. if (SupportsPlatform("android"))
  309. {
  310. if (outputType_.ToLower() != "library")
  311. {
  312. pgroup.CreateChild("AndroidUseSharedRuntime").SetValue("False");
  313. pgroup.CreateChild("AndroidLinkMode").SetValue("SdkOnly");
  314. }
  315. }
  316. }
  317. }
  318. void NETCSProject::CreateDebugPropertyGroup(XMLElement &projectRoot)
  319. {
  320. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  321. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
  322. pgroup.CreateChild("Optimize").SetValue("false");
  323. String outputPath = assemblyOutputPath_;
  324. outputPath.Replace("$ATOMIC_CONFIG$", "Debug");
  325. pgroup.CreateChild("OutputPath").SetValue(outputPath);
  326. Vector<String> constants;
  327. constants.Push("DEBUG");
  328. constants.Push("TRACE");
  329. constants += defineConstants_;
  330. const Vector<String>& globalConstants = projectGen_->GetGlobalDefineConstants();
  331. constants += globalConstants;
  332. pgroup.CreateChild("DefineConstants").SetValue(String::Joined(constants, ";").CString());
  333. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  334. pgroup.CreateChild("WarningLevel").SetValue("4");
  335. pgroup.CreateChild("ConsolePause").SetValue("false");
  336. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  337. pgroup.CreateChild("DebugSymbols").SetValue("true");
  338. if (SupportsDesktop())
  339. {
  340. pgroup.CreateChild("DebugType").SetValue(GetIsPCL() ? "pdbonly": "full");
  341. if (!GetIsPCL())
  342. pgroup.CreateChild("PlatformTarget").SetValue("x64");
  343. #ifndef ATOMIC_PLATFORM_WINDOWS
  344. String startArguments;
  345. #ifndef ATOMIC_DEV_BUILD
  346. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  347. #ifdef ATOMIC_PLATFORM_OSX
  348. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "../Resources/").CString());
  349. #else
  350. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "Resources/").CString());
  351. #endif
  352. #endif
  353. startArguments += ToString("--project \"%s\"", projectGen_->GetAtomicProjectPath().CString());
  354. pgroup.CreateChild("Commandlineparameters").SetValue(startArguments);
  355. #endif
  356. }
  357. else
  358. {
  359. pgroup.CreateChild("DebugType").SetValue(GetIsPCL() ? "pdbonly": "full");
  360. if (androidApplication_)
  361. {
  362. pgroup.CreateChild("AndroidUseSharedRuntime").SetValue("False");
  363. pgroup.CreateChild("AndroidLinkMode").SetValue("None");
  364. pgroup.CreateChild("EmbedAssembliesIntoApk").SetValue("True");
  365. pgroup.CreateChild("BundleAssemblies").SetValue("False");
  366. pgroup.CreateChild("AndroidCreatePackagePerAbi").SetValue("False");
  367. pgroup.CreateChild("Debugger").SetValue("Xamarin");
  368. pgroup.CreateChild("AndroidEnableMultiDex").SetValue("False");
  369. pgroup.CreateChild("AndroidSupportedAbis").SetValue("armeabi-v7a");
  370. }
  371. }
  372. }
  373. void NETCSProject::CreateApplicationItems(XMLElement &projectRoot)
  374. {
  375. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  376. XMLElement itemGroup = projectRoot.CreateChild("ItemGroup");
  377. if (SupportsDesktop())
  378. {
  379. // AtomicNETNative
  380. XMLElement atomicNETNativeDLL = itemGroup.CreateChild("None");
  381. #ifdef ATOMIC_DEBUG
  382. String config = "Debug";
  383. #else
  384. String config = "Release";
  385. #endif
  386. #ifdef ATOMIC_PLATFORM_WINDOWS
  387. String platform = "Windows";
  388. String filename = "AtomicNETNative.dll";
  389. #elif defined(ATOMIC_PLATFORM_OSX)
  390. String platform = "Mac";
  391. String filename = "libAtomicNETNative.dylib";
  392. #elif defined(ATOMIC_PLATFORM_LINUX)
  393. String platform = "Linux";
  394. String filename = "libAtomicNETNative.so";
  395. #endif
  396. #ifdef ATOMIC_DEV_BUILD
  397. String nativePath = AddTrailingSlash(tenv->GetAtomicNETRootDir()) + config + "/Native/" + platform + "/" + filename;
  398. #else
  399. String nativePath = AddTrailingSlash(tenv->GetAtomicNETRootDir()) + config + "/Native/" + platform + "/" + filename;
  400. #endif
  401. atomicNETNativeDLL.SetAttribute("Include", nativePath);
  402. atomicNETNativeDLL.CreateChild("Link").SetValue(filename);
  403. atomicNETNativeDLL.CreateChild("CopyToOutputDirectory").SetValue("PreserveNewest");
  404. #ifdef ATOMIC_PLATFORM_WINDOWS
  405. XMLElement d3dCompilerDLL = itemGroup.CreateChild("None");
  406. String d3dCompilerPath = AddTrailingSlash(tenv->GetAtomicNETRootDir()) + config + "/Native/" + platform + "/D3DCompiler_47.dll";
  407. d3dCompilerDLL.SetAttribute("Include", d3dCompilerPath);
  408. d3dCompilerDLL.CreateChild("Link").SetValue("D3DCompiler_47.dll");
  409. d3dCompilerDLL.CreateChild("CopyToOutputDirectory").SetValue("PreserveNewest");
  410. #endif
  411. }
  412. else
  413. {
  414. const String& projectPath = projectGen_->GetAtomicProjectPath();
  415. if (!playerApplication_ || !projectPath.Length())
  416. return;
  417. XMLElement androidAsset = itemGroup.CreateChild("AndroidAsset");
  418. androidAsset.SetAttribute("Include", projectPath + "AtomicNET/Resources/AtomicResources.pak");
  419. androidAsset.CreateChild("Link").SetValue("Assets\\AtomicResources.pak");
  420. }
  421. }
  422. void NETCSProject::CreateAndroidItems(XMLElement &projectRoot)
  423. {
  424. if (!libraryProjectZips_.Size())
  425. {
  426. }
  427. if (libraryProjectZips_.Size())
  428. {
  429. XMLElement libraryGroup = projectRoot.CreateChild("ItemGroup");
  430. for (unsigned i = 0; i < libraryProjectZips_.Size(); i++)
  431. {
  432. libraryGroup.CreateChild("LibraryProjectZip").SetAttribute("Include", libraryProjectZips_[i].CString());
  433. }
  434. }
  435. if (transformFiles_.Size())
  436. {
  437. XMLElement transformGroup = projectRoot.CreateChild("ItemGroup");
  438. for (unsigned i = 0; i < transformFiles_.Size(); i++)
  439. {
  440. transformGroup.CreateChild("TransformFile").SetAttribute("Include", transformFiles_[i].CString());
  441. }
  442. }
  443. if (!importProjects_.Size())
  444. {
  445. projectRoot.CreateChild("Import").SetAttribute("Project", "$(MSBuildExtensionsPath)\\Xamarin\\Android\\Xamarin.Android.CSharp.targets");
  446. }
  447. if (androidApplication_)
  448. {
  449. // TODO: other abi
  450. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  451. #ifdef ATOMIC_DEBUG
  452. String config = "Debug";
  453. #else
  454. String config = "Release";
  455. #endif
  456. // TODO: more than armeabi-v7a (which this is)
  457. String nativePath = AddTrailingSlash(tenv->GetAtomicNETRootDir()) + config + "/Native/Android/libAtomicNETNative.so";
  458. XMLElement nativeLibrary = projectRoot.CreateChild("ItemGroup").CreateChild("AndroidNativeLibrary");
  459. nativeLibrary.SetAttribute("Include", nativePath);
  460. nativeLibrary.CreateChild("Link").SetValue("Libs\\armeabi-v7a\\libAtomicNETNative.so");
  461. XMLElement resourceGroup = projectRoot.CreateChild("ItemGroup");
  462. String relativePath;
  463. if (GetRelativeProjectPath("$ATOMIC_PROJECT_ROOT$/Project/AtomicNET/Platforms/Android/Resources/values/Strings.xml", projectPath_, relativePath))
  464. {
  465. relativePath.Replace("/", "\\");
  466. XMLElement strings = resourceGroup.CreateChild("AndroidResource");
  467. strings.SetAttribute("Include", relativePath);
  468. // Link has to exist for manifest to find resource!
  469. strings.CreateChild("Link").SetValue("Resources\\values\\Strings.xml");
  470. }
  471. else
  472. {
  473. ATOMIC_LOGERROR("Unabled to get relative path for Strings.xml");
  474. }
  475. if (GetRelativeProjectPath("$ATOMIC_PROJECT_ROOT$/Project/AtomicNET/Platforms/Android/Resources/drawable/icon.png", projectPath_, relativePath))
  476. {
  477. relativePath.Replace("/", "\\");
  478. XMLElement icon = resourceGroup.CreateChild("AndroidResource");
  479. icon.SetAttribute("Include", relativePath);
  480. // Link has to exist for manifest to find resource!
  481. icon.CreateChild("Link").SetValue("Resources\\drawable\\icon.png");
  482. }
  483. else
  484. {
  485. ATOMIC_LOGERROR("Unabled to get relative path for Icon.png");
  486. }
  487. }
  488. }
  489. void NETCSProject::CreateAssemblyInfo()
  490. {
  491. String info = "using System.Reflection;\nusing System.Runtime.CompilerServices;\nusing System.Runtime.InteropServices;\n\n\n";
  492. info += ToString("[assembly:AssemblyTitle(\"%s\")]\n", name_.CString());
  493. info += "[assembly:AssemblyDescription(\"\")]\n";
  494. info += "[assembly:AssemblyConfiguration(\"\")]\n";
  495. info += "[assembly:AssemblyCompany(\"\")]\n";
  496. info += ToString("[assembly:AssemblyProduct(\"%s\")]\n", name_.CString());
  497. info += "\n\n\n";
  498. info += "[assembly:ComVisible(false)]\n";
  499. info += "\n\n";
  500. info += ToString("[assembly:Guid(\"%s\")]\n", projectGuid_.CString());
  501. info += "\n\n";
  502. info += "[assembly:AssemblyVersion(\"1.0.0.0\")]\n";
  503. info += "[assembly:AssemblyFileVersion(\"1.0.0.0\")]\n";
  504. SharedPtr<File> output(new File(context_, GetSanitizedPath(projectPath_ + "Properties/AssemblyInfo.cs"), FILE_WRITE));
  505. output->Write(info.CString(), info.Length());
  506. }
  507. bool NETCSProject::GetRelativeProjectPath(const String& fromPath, const String& toPath, String& output)
  508. {
  509. String path = fromPath;
  510. ReplacePathStrings(path);
  511. String relativePath;
  512. if (GetRelativePath(projectPath_, GetPath(path), relativePath))
  513. {
  514. path = relativePath + GetFileName(path) + GetExtension(path);
  515. output = path;
  516. return true;
  517. }
  518. output = fromPath;
  519. return false;
  520. }
  521. void NETCSProject::CreateMainPropertyGroup(XMLElement& projectRoot)
  522. {
  523. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  524. // Configuration
  525. XMLElement config = pgroup.CreateChild("Configuration");
  526. config.SetAttribute("Condition", " '$(Configuration)' == '' ");
  527. config.SetValue("Debug");
  528. // Platform
  529. XMLElement platform = pgroup.CreateChild("Platform");
  530. platform.SetAttribute("Condition", " '$(Platform)' == '' ");
  531. platform.SetValue("AnyCPU");
  532. // ProjectGuid
  533. XMLElement guid = pgroup.CreateChild("ProjectGuid");
  534. guid.SetValue("{" + projectGuid_ + "}");
  535. // OutputType
  536. XMLElement outputType = pgroup.CreateChild("OutputType");
  537. outputType.SetValue(outputType_);
  538. pgroup.CreateChild("AppDesignerFolder").SetValue("Properties");
  539. // RootNamespace
  540. XMLElement rootNamespace = pgroup.CreateChild("RootNamespace");
  541. rootNamespace.SetValue(rootNamespace_);
  542. // AssemblyName
  543. XMLElement assemblyName = pgroup.CreateChild("AssemblyName");
  544. assemblyName.SetValue(assemblyName_);
  545. pgroup.CreateChild("FileAlignment").SetValue("512");
  546. if (projectTypeGuids_.Size())
  547. {
  548. pgroup.CreateChild("ProjectTypeGuids").SetValue(String::Joined(projectTypeGuids_, ";"));
  549. }
  550. if (SupportsDesktop())
  551. {
  552. pgroup.CreateChild("TargetFrameworkVersion").SetValue("v4.5");
  553. }
  554. else
  555. {
  556. pgroup.CreateChild("ProductVersion").SetValue("8.0.30703");
  557. pgroup.CreateChild("SchemaVersion").SetValue("2.0");
  558. pgroup.CreateChild("TargetFrameworkVersion").SetValue("v6.0");
  559. if (SupportsPlatform("android"))
  560. {
  561. if (!projectTypeGuids_.Size())
  562. {
  563. pgroup.CreateChild("ProjectTypeGuids").SetValue("{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}");
  564. }
  565. pgroup.CreateChild("AndroidUseLatestPlatformSdk").SetValue("True");
  566. if (!androidApplication_)
  567. {
  568. // 10368E6C-D01B-4462-8E8B-01FC667A7035 is a binding library
  569. if (!projectTypeGuids_.Contains("{10368E6C-D01B-4462-8E8B-01FC667A7035}"))
  570. pgroup.CreateChild("GenerateSerializationAssemblies").SetValue("Off");
  571. }
  572. else
  573. {
  574. // Android Application
  575. pgroup.CreateChild("AndroidApplication").SetValue("true");
  576. // AndroidManifest.xml must reside in Properties/AndroidManifest.xml, which introduces sync issues :/
  577. pgroup.CreateChild("AndroidManifest").SetValue("Properties\\AndroidManifest.xml");
  578. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  579. String manifestSourceFile = "$ATOMIC_PROJECT_ROOT$/Project/AtomicNET/Platforms/Android/Properties/AndroidManifest.xml";
  580. ReplacePathStrings(manifestSourceFile);
  581. if (fileSystem->FileExists(manifestSourceFile))
  582. {
  583. String manifestDest = projectPath_ + "Properties/";
  584. if (!fileSystem->DirExists(manifestDest))
  585. {
  586. fileSystem->CreateDirs(projectGen_->GetAtomicProjectPath(), ToString("AtomicNET/Solution/%s/Properties/", name_.CString()));
  587. }
  588. if (fileSystem->DirExists(manifestDest))
  589. {
  590. if (!fileSystem->Copy(manifestSourceFile, manifestDest + "AndroidManifest.xml"))
  591. {
  592. ATOMIC_LOGERRORF("Unable to copy AndroidManifest from %s to %s", manifestSourceFile.CString(), manifestDest.CString());
  593. }
  594. }
  595. else
  596. {
  597. ATOMIC_LOGERRORF("Unable to create folder %s for AndroidManifest.xml", manifestDest.CString());
  598. }
  599. }
  600. else
  601. {
  602. ATOMIC_LOGERROR("No AndroidManifest.xml, project will not deploy");
  603. }
  604. String relativePath;
  605. if (GetRelativeProjectPath("$ATOMIC_PROJECT_ROOT$/Project/AtomicNET/Platforms/Android/Resources/Resource.Designer.cs", projectPath_, relativePath))
  606. {
  607. relativePath.Replace("/", "\\");
  608. pgroup.CreateChild("AndroidResgenFile").SetValue(relativePath);
  609. }
  610. else
  611. {
  612. ATOMIC_LOGERROR("Unabled to get relative path for AndroidResgenFile");
  613. }
  614. pgroup.CreateChild("GenerateSerializationAssemblies").SetValue("Off");
  615. }
  616. }
  617. }
  618. if (targetFrameworkProfile_.Length())
  619. {
  620. pgroup.CreateChild("TargetFrameworkProfile").SetValue(targetFrameworkProfile_);
  621. }
  622. }
  623. bool NETCSProject::GenerateShared()
  624. {
  625. // .shproj
  626. XMLElement project = xmlFile_->CreateRoot("Project");
  627. project.SetAttribute("DefaultTargets", "Build");
  628. project.SetAttribute("ToolsVersion", "14.0");
  629. project.SetAttribute("DefaultTargets", "Build");
  630. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  631. // Project Group
  632. XMLElement projectGroup = project.CreateChild("PropertyGroup");
  633. projectGroup.SetAttribute("Label", "Globals");
  634. projectGroup.CreateChild("ProjectGuid").SetValue(projectGuid_);
  635. projectGroup.CreateChild("MinimumVisualStudioVersion").SetValue("14.0");
  636. XMLElement import = project.CreateChild("Import");
  637. import.SetAttribute("Project", "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props");
  638. import.SetAttribute("Condition", "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')");
  639. import = project.CreateChild("Import");
  640. import.SetAttribute("Project", "$(MSBuildExtensionsPath32)\\Microsoft\\VisualStudio\\v$(VisualStudioVersion)\\CodeSharing\\Microsoft.CodeSharing.Common.Default.props");
  641. import = project.CreateChild("Import");
  642. import.SetAttribute("Project", "$(MSBuildExtensionsPath32)\\Microsoft\\VisualStudio\\v$(VisualStudioVersion)\\CodeSharing\\Microsoft.CodeSharing.Common.props");
  643. import = project.CreateChild("Import");
  644. import.SetAttribute("Project", ToString("%s.projitems", name_.CString()));
  645. import.SetAttribute("Label", "Shared");
  646. import = project.CreateChild("Import");
  647. import.SetAttribute("Project", "$(MSBuildExtensionsPath32)\\Microsoft\\VisualStudio\\v$(VisualStudioVersion)\\CodeSharing\\Microsoft.CodeSharing.CSharp.targets");
  648. String projectSource = xmlFile_->ToString();
  649. SharedPtr<File> output(new File(context_, GetSanitizedPath(projectPath_ + name_ + ".shproj"), FILE_WRITE));
  650. output->Write(projectSource.CString(), projectSource.Length());
  651. // projitems
  652. SharedPtr<XMLFile> itemsXMLFile(new XMLFile(context_));
  653. XMLElement itemsProject = itemsXMLFile->CreateRoot("Project");
  654. itemsProject.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  655. XMLElement propertyGroup = itemsProject.CreateChild("PropertyGroup");
  656. propertyGroup.CreateChild("MSBuildAllProjects").SetValue("$(MSBuildAllProjects);$(MSBuildThisFileFullPath)");
  657. propertyGroup.CreateChild("HasSharedItems").SetValue("true");
  658. propertyGroup.CreateChild("SharedGUID").SetValue(projectGuid_);
  659. propertyGroup = itemsProject.CreateChild("PropertyGroup");
  660. propertyGroup.SetAttribute("Label", "Configuration");
  661. propertyGroup.CreateChild("Import_RootNamespace").SetValue("AtomicEngine");
  662. CreateCompileItemGroup(itemsProject);
  663. String itemSource = itemsXMLFile->ToString();
  664. SharedPtr<File> itemsOutput(new File(context_, GetSanitizedPath(projectPath_ + name_ + ".projitems"), FILE_WRITE));
  665. itemsOutput->Write(itemSource.CString(), itemSource.Length());
  666. return true;
  667. }
  668. bool NETCSProject::GenerateStandard()
  669. {
  670. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  671. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  672. NETSolution* solution = projectGen_->GetSolution();
  673. XMLElement project = xmlFile_->CreateRoot("Project");
  674. project.SetAttribute("DefaultTargets", "Build");
  675. project.SetAttribute("ToolsVersion", "14.0");
  676. project.SetAttribute("DefaultTargets", "Build");
  677. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  678. XMLElement import = project.CreateChild("Import");
  679. import.SetAttribute("Project", "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props");
  680. import.SetAttribute("Condition", "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')");
  681. CreateMainPropertyGroup(project);
  682. CreateDebugPropertyGroup(project);
  683. CreateReleasePropertyGroup(project);
  684. CreateReferencesItemGroup(project);
  685. CreateProjectReferencesItemGroup(project);
  686. CreateCompileItemGroup(project);
  687. CreatePackagesItemGroup(project);
  688. if (SupportsPlatform("android"))
  689. {
  690. CreateAndroidItems(project);
  691. }
  692. if (SupportsDesktop() && !GetIsPCL())
  693. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildToolsPath)\\Microsoft.CSharp.targets");
  694. if (outputType_.ToLower() == "exe" || androidApplication_)
  695. {
  696. CreateApplicationItems(project);
  697. }
  698. if (!GetIsPCL() && !sharedReferences_.Size() && outputType_ != "Shared")
  699. CreateAssemblyInfo();
  700. const String& atomicProjectPath = projectGen_->GetAtomicProjectPath();
  701. if (atomicProjectPath.Length())
  702. {
  703. // Create the AtomicProject.csproj.user file if it doesn't exist
  704. String userSettingsFilename = projectPath_ + name_ + ".csproj.user";
  705. if (!fileSystem->FileExists(userSettingsFilename))
  706. {
  707. SharedPtr<XMLFile> userSettings(new XMLFile(context_));
  708. XMLElement project = userSettings->CreateRoot("Project");
  709. project.SetAttribute("ToolsVersion", "14.0");
  710. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  711. StringVector configs;
  712. configs.Push("Debug");
  713. configs.Push("Release");
  714. for (unsigned i = 0; i < configs.Size(); i++)
  715. {
  716. String cfg = configs[i];
  717. XMLElement propertyGroup = project.CreateChild("PropertyGroup");
  718. propertyGroup.SetAttribute("Condition", ToString("'$(Configuration)|$(Platform)' == '%s|AnyCPU'", cfg.CString()));
  719. String startArguments;
  720. #ifndef ATOMIC_DEV_BUILD
  721. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "Resources/").CString());
  722. #endif
  723. propertyGroup.CreateChild("StartAction").SetValue("Project");
  724. startArguments += ToString("--project \"%s\"", atomicProjectPath.CString());
  725. propertyGroup.CreateChild("StartArguments").SetValue(startArguments);
  726. }
  727. String userSettingsSource = userSettings->ToString();
  728. SharedPtr<File> output(new File(context_, GetSanitizedPath(userSettingsFilename), FILE_WRITE));
  729. output->Write(userSettingsSource.CString(), userSettingsSource.Length());
  730. output->Close();
  731. }
  732. }
  733. for (unsigned i = 0; i < sharedReferences_.Size(); i++)
  734. {
  735. NETCSProject* sharedProject = projectGen_->GetCSProjectByName(sharedReferences_[i]);
  736. if (!sharedProject)
  737. {
  738. ATOMIC_LOGERRORF("Unable to get shared project %s", sharedReferences_[i].CString());
  739. continue;
  740. }
  741. String path = sharedProject->projectPath_ + sharedReferences_[i] + ".projitems";
  742. String relativePath;
  743. if (GetRelativePath(projectPath_, GetPath(path), relativePath))
  744. {
  745. path = relativePath + GetFileName(path) + GetExtension(path);
  746. }
  747. XMLElement shared = project.CreateChild("Import");
  748. shared.SetAttribute("Project", path);
  749. shared.SetAttribute("Label", "Shared");
  750. }
  751. for (unsigned i = 0; i < importProjects_.Size(); i++)
  752. {
  753. project.CreateChild("Import").SetAttribute("Project", importProjects_[i].CString());
  754. }
  755. // Have to come after the imports, so AfterBuild exists
  756. String projectName = "AtomicProject";
  757. if (projectGen_->GetProjectSettings())
  758. projectName = projectGen_->GetProjectSettings()->GetName();
  759. if (name_ == projectName)
  760. {
  761. XMLElement afterBuild = project.CreateChild("Target");
  762. afterBuild.SetAttribute("Name", "AfterBuild");
  763. XMLElement copy = afterBuild.CreateChild("Copy");
  764. copy.SetAttribute("SourceFiles", "$(TargetPath)");
  765. String destPath = projectPath_ + "../../../Resources/";
  766. String relativePath;
  767. String resourceDir = AddTrailingSlash(atomicProjectPath) + "Resources/";
  768. if (GetRelativePath(projectPath_, resourceDir, relativePath))
  769. {
  770. destPath = AddTrailingSlash(relativePath);
  771. }
  772. copy.SetAttribute("DestinationFolder", destPath);
  773. }
  774. String projectSource = xmlFile_->ToString();
  775. SharedPtr<File> output(new File(context_, GetSanitizedPath(projectPath_ + name_ + ".csproj"), FILE_WRITE));
  776. output->Write(projectSource.CString(), projectSource.Length());
  777. return true;
  778. }
  779. bool NETCSProject::Generate()
  780. {
  781. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  782. NETSolution* solution = projectGen_->GetSolution();
  783. projectPath_ = solution->GetOutputPath() + name_ + "/";
  784. if (!CreateProjectFolder(projectPath_))
  785. return false;
  786. if (!CreateProjectFolder(projectPath_ + "Properties"))
  787. return false;
  788. if (outputType_ == "Shared")
  789. {
  790. return GenerateShared();
  791. }
  792. return GenerateStandard();
  793. }
  794. bool NETCSProject::Load(const JSONValue& root)
  795. {
  796. name_ = root["name"].GetString();
  797. projectGuid_ = root["projectGuid"].GetString();
  798. if (!projectGuid_.Length())
  799. {
  800. ATOMIC_LOGINFOF("GUID not provided for project %s, generating one", name_.CString());
  801. projectGuid_ = projectGen_->GenerateUUID();
  802. }
  803. outputType_ = root["outputType"].GetString();
  804. androidApplication_ = root["androidApplication"].GetBool();
  805. playerApplication_ = root["playerApplication"].GetBool();
  806. rootNamespace_ = root["rootNamespace"].GetString();
  807. assemblyName_ = root["assemblyName"].GetString();
  808. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  809. ReplacePathStrings(assemblyOutputPath_);
  810. assemblySearchPaths_ = root["assemblySearchPaths"].GetString();
  811. ReplacePathStrings(assemblySearchPaths_);
  812. const JSONArray& platforms = root["platforms"].GetArray();
  813. for (unsigned i = 0; i < platforms.Size(); i++)
  814. {
  815. String platform = platforms[i].GetString();
  816. platforms_.Push(platform.ToLower());
  817. }
  818. const JSONArray& references = root["references"].GetArray();
  819. for (unsigned i = 0; i < references.Size(); i++)
  820. {
  821. String reference = references[i].GetString();
  822. ReplacePathStrings(reference);
  823. references_.Push(reference);
  824. }
  825. const JSONArray& packages = root["packages"].GetArray();
  826. for (unsigned i = 0; i < packages.Size(); i++)
  827. {
  828. String package = packages[i].GetString();
  829. if (packages_.Find(package) != packages_.End())
  830. {
  831. ATOMIC_LOGERRORF("Duplicate package found %s", package.CString());
  832. continue;
  833. }
  834. projectGen_->GetSolution()->RegisterPackage(package);
  835. packages_.Push(package);
  836. }
  837. const JSONArray& sources = root["sources"].GetArray();
  838. for (unsigned i = 0; i < sources.Size(); i++)
  839. {
  840. String source = sources[i].GetString();
  841. ReplacePathStrings(source);
  842. sourceFolders_.Push(AddTrailingSlash(source));
  843. }
  844. const JSONArray& defineConstants = root["defineConstants"].GetArray();
  845. for (unsigned i = 0; i < defineConstants.Size(); i++)
  846. {
  847. defineConstants_.Push(defineConstants[i].GetString());
  848. }
  849. const JSONArray& projectTypeGuids = root["projectTypeGuids"].GetArray();
  850. for (unsigned i = 0; i < projectTypeGuids.Size(); i++)
  851. {
  852. String guid = projectTypeGuids[i].GetString();
  853. projectTypeGuids_.Push(ToString("{%s}", guid.CString()));
  854. }
  855. const JSONArray& importProjects = root["importProjects"].GetArray();
  856. for (unsigned i = 0; i < importProjects.Size(); i++)
  857. {
  858. importProjects_.Push(importProjects[i].GetString());
  859. }
  860. const JSONArray& libraryProjectZips = root["libraryProjectZips"].GetArray();
  861. for (unsigned i = 0; i < libraryProjectZips.Size(); i++)
  862. {
  863. String zipPath = libraryProjectZips[i].GetString();
  864. ReplacePathStrings(zipPath);
  865. libraryProjectZips_.Push(zipPath);
  866. }
  867. const JSONArray& transformFiles = root["transformFiles"].GetArray();
  868. for (unsigned i = 0; i < transformFiles.Size(); i++)
  869. {
  870. String transformFile = transformFiles[i].GetString();
  871. ReplacePathStrings(transformFile);
  872. transformFiles_.Push(transformFile);
  873. }
  874. const JSONArray& sharedReferences = root["sharedReferences"].GetArray();
  875. for (unsigned i = 0; i < sharedReferences.Size(); i++)
  876. {
  877. sharedReferences_.Push(sharedReferences[i].GetString());
  878. }
  879. targetFrameworkProfile_ = root["targetFrameworkProfile"].GetString();
  880. return true;
  881. }
  882. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen, bool rewrite) : NETProjectBase(context, projectGen),
  883. rewriteSolution_(rewrite)
  884. {
  885. }
  886. NETSolution::~NETSolution()
  887. {
  888. }
  889. bool NETSolution::Generate()
  890. {
  891. String slnPath = outputPath_ + name_ + ".sln";
  892. GenerateSolution(slnPath);
  893. return true;
  894. }
  895. void NETSolution::GenerateSolution(const String &slnPath)
  896. {
  897. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  898. source += "# Visual Studio 14\n";
  899. source += "VisualStudioVersion = 14.0.25420.1\n";
  900. source += "MinimumVisualStudioVersion = 10.0.40219.1\n";
  901. solutionGUID_ = projectGen_->GenerateUUID();
  902. PODVector<NETCSProject*> depends;
  903. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  904. for (unsigned i = 0; i < projects.Size(); i++)
  905. {
  906. NETCSProject* p = projects.At(i);
  907. const String& projectName = p->GetName();
  908. const String& projectGUID = p->GetProjectGUID();
  909. String CSharpProjectGUID = "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  910. String ext = "csproj";
  911. if (p->outputType_ == "Shared")
  912. {
  913. CSharpProjectGUID = "D954291E-2A0B-460D-934E-DC6B0785DB48";
  914. ext = "shproj";
  915. }
  916. source += ToString("Project(\"{%s}\") = \"%s\", \"%s\\%s.%s\", \"{%s}\"\n",
  917. CSharpProjectGUID.CString(), projectName.CString(), projectName.CString(),
  918. projectName.CString(), ext.CString(), projectGUID.CString());
  919. projectGen_->GetCSProjectDependencies(p, depends);
  920. if (depends.Size())
  921. {
  922. source += "\tProjectSection(ProjectDependencies) = postProject\n";
  923. for (unsigned j = 0; j < depends.Size(); j++)
  924. {
  925. source += ToString("\t{%s} = {%s}\n",
  926. depends[j]->GetProjectGUID().CString(), depends[j]->GetProjectGUID().CString());
  927. }
  928. source += "\tEndProjectSection\n";
  929. }
  930. source += "EndProject\n";
  931. }
  932. source += "Global\n";
  933. // SharedMSBuildProjectFiles
  934. source += " GlobalSection(SharedMSBuildProjectFiles) = preSolution\n";
  935. for (unsigned i = 0; i < projects.Size(); i++)
  936. {
  937. NETCSProject* p = projects.At(i);
  938. if (p->outputType_ == "Shared")
  939. {
  940. for (unsigned j = 0; j < projects.Size(); j++)
  941. {
  942. NETCSProject* p2 = projects.At(j);
  943. if (p == p2)
  944. {
  945. source += ToString(" %s\\%s.projitems*{%s}*SharedItemsImports = 13\n", p->name_.CString(), p->name_.CString(), p->projectGuid_.CString());
  946. }
  947. else
  948. {
  949. if (p2->sharedReferences_.Contains(p->name_))
  950. {
  951. source += ToString(" %s\\%s.projitems*{%s}*SharedItemsImports = 4\n", p->name_.CString(), p->name_.CString(), p2->projectGuid_.CString());
  952. }
  953. }
  954. }
  955. }
  956. }
  957. source += " EndGlobalSection\n";
  958. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  959. source += " Debug|Any CPU = Debug|Any CPU\n";
  960. source += " Release|Any CPU = Release|Any CPU\n";
  961. source += " EndGlobalSection\n";
  962. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  963. for (unsigned i = 0; i < projects.Size(); i++)
  964. {
  965. NETCSProject* p = projects.At(i);
  966. if (p->outputType_ == "Shared")
  967. continue;
  968. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  969. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  970. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  971. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  972. }
  973. source += " EndGlobalSection\n";
  974. source += "EndGlobal\n";
  975. if (!rewriteSolution_)
  976. {
  977. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  978. if (fileSystem->Exists(slnPath))
  979. return;
  980. }
  981. SharedPtr<File> output(new File(context_, GetSanitizedPath(slnPath), FILE_WRITE));
  982. output->Write(source.CString(), source.Length());
  983. output->Close();
  984. }
  985. bool NETSolution::Load(const JSONValue& root)
  986. {
  987. FileSystem* fs = GetSubsystem<FileSystem>();
  988. name_ = root["name"].GetString();
  989. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  990. ReplacePathStrings(outputPath_);
  991. // TODO: use poco mkdirs
  992. if (!fs->DirExists(outputPath_))
  993. fs->CreateDirsRecursive(outputPath_);
  994. return true;
  995. }
  996. bool NETSolution::RegisterPackage(const String& package)
  997. {
  998. if (packages_.Find(package) != packages_.End())
  999. return false;
  1000. packages_.Push(package);
  1001. return true;
  1002. }
  1003. NETProjectGen::NETProjectGen(Context* context) : Object(context),
  1004. rewriteSolution_(false)
  1005. {
  1006. }
  1007. NETProjectGen::~NETProjectGen()
  1008. {
  1009. }
  1010. NETCSProject* NETProjectGen::GetCSProjectByName(const String & name)
  1011. {
  1012. for (unsigned i = 0; i < projects_.Size(); i++)
  1013. {
  1014. if (projects_[i]->GetName() == name)
  1015. return projects_[i];
  1016. }
  1017. return nullptr;
  1018. }
  1019. bool NETProjectGen::GetCSProjectDependencies(NETCSProject* source, PODVector<NETCSProject*>& depends) const
  1020. {
  1021. depends.Clear();
  1022. const Vector<String>& references = source->GetReferences();
  1023. for (unsigned i = 0; i < projects_.Size(); i++)
  1024. {
  1025. NETCSProject* pdepend = projects_.At(i);
  1026. if (source == pdepend)
  1027. continue;
  1028. for (unsigned j = 0; j < references.Size(); j++)
  1029. {
  1030. if (pdepend->GetName() == references[j])
  1031. {
  1032. depends.Push(pdepend);
  1033. }
  1034. }
  1035. }
  1036. return depends.Size() != 0;
  1037. }
  1038. bool NETProjectGen::Generate()
  1039. {
  1040. solution_->Generate();
  1041. for (unsigned i = 0; i < projects_.Size(); i++)
  1042. {
  1043. if (!projects_[i]->Generate())
  1044. return false;
  1045. }
  1046. return true;
  1047. }
  1048. void NETProjectGen::SetRewriteSolution(bool rewrite)
  1049. {
  1050. rewriteSolution_ = rewrite;
  1051. if (solution_.NotNull())
  1052. solution_->SetRewriteSolution(rewrite);
  1053. }
  1054. bool NETProjectGen::IncludeProjectOnPlatform(const JSONValue& projectRoot, const String& platform)
  1055. {
  1056. const JSONArray& platforms = projectRoot["platforms"].GetArray();
  1057. if (!platforms.Size())
  1058. return true; // all platforms
  1059. String scriptPlatform = platform.ToLower();
  1060. for (unsigned i = 0; i < platforms.Size(); i++)
  1061. {
  1062. String platform = platforms[i].GetString().ToLower();
  1063. if (platform == "desktop")
  1064. {
  1065. if (scriptPlatform == "windows" || scriptPlatform == "macosx" || scriptPlatform == "linux")
  1066. return true;
  1067. return false;
  1068. }
  1069. if (platform == "android" && scriptPlatform != "android")
  1070. return false;
  1071. }
  1072. return true;
  1073. }
  1074. bool NETProjectGen::LoadProject(const JSONValue &root)
  1075. {
  1076. solution_ = new NETSolution(context_, this, rewriteSolution_);
  1077. solution_->Load(root["solution"]);
  1078. const JSONValue& jprojects = root["projects"];
  1079. if (!jprojects.IsArray() || !jprojects.Size())
  1080. return false;
  1081. for (unsigned i = 0; i < jprojects.Size(); i++)
  1082. {
  1083. const JSONValue& jproject = jprojects[i];
  1084. if (!jproject.IsObject())
  1085. return false;
  1086. JSONArray platforms = jproject["platforms"].GetArray();
  1087. if (platforms.Size())
  1088. {
  1089. bool found = false;
  1090. for (unsigned j = 0; j < platforms.Size(); j++)
  1091. {
  1092. if (GetSupportsPlatform(platforms[j].GetString()))
  1093. {
  1094. found = true;
  1095. break;
  1096. }
  1097. }
  1098. if (!found)
  1099. {
  1100. continue;
  1101. }
  1102. }
  1103. // HACK! Do not generate AtomicNETService in the AtomicProject solution
  1104. if (jproject["name"].GetString() == "AtomicNETService" && atomicProjectPath_.Length())
  1105. {
  1106. continue;
  1107. }
  1108. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  1109. if (!csProject->Load(jproject))
  1110. return false;
  1111. projects_.Push(csProject);
  1112. }
  1113. return true;
  1114. }
  1115. bool NETProjectGen::LoadJSONProject(const String& jsonProjectPath)
  1116. {
  1117. SharedPtr<File> file(new File(context_));
  1118. if (!file->Open(GetSanitizedPath(jsonProjectPath)))
  1119. return false;
  1120. String json;
  1121. file->ReadText(json);
  1122. JSONValue jvalue;
  1123. if (!JSONFile::ParseJSON(json, jvalue))
  1124. return false;
  1125. return LoadProject(jvalue);
  1126. }
  1127. bool NETProjectGen::LoadAtomicProject(const String& atomicProjectPath)
  1128. {
  1129. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  1130. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  1131. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  1132. String pathname, filename, ext;
  1133. SplitPath(atomicProjectPath, pathname, filename, ext);
  1134. if (ext == ".atomic")
  1135. {
  1136. atomicProjectPath_ = AddTrailingSlash(pathname);
  1137. }
  1138. else
  1139. {
  1140. atomicProjectPath_ = AddTrailingSlash(atomicProjectPath);
  1141. }
  1142. // Do we have a loaded project?
  1143. if (Project* project = tsystem->GetProject())
  1144. {
  1145. // If so, use loaded project settings
  1146. projectSettings_ = project->GetProjectSettings();
  1147. }
  1148. else
  1149. {
  1150. // Nope, load them up
  1151. projectSettings_ = SharedPtr<ProjectSettings>(new ProjectSettings(context_));
  1152. projectSettings_->Load(atomicProjectPath_ + "Settings/Platforms.json");
  1153. }
  1154. #ifdef ATOMIC_DEV_BUILD
  1155. JSONValue netJSON;
  1156. SharedPtr<File> netJSONFile(new File(context_));
  1157. String atomicNETProject = tenv->GetRootSourceDir() + "Script/AtomicNET/AtomicNETProject.json";
  1158. if (!netJSONFile->Open(GetSanitizedPath(atomicNETProject)))
  1159. return false;
  1160. String netJSONString;
  1161. netJSONFile->ReadText(netJSONString);
  1162. if (!JSONFile::ParseJSON(netJSONString, netJSON))
  1163. return false;
  1164. #endif
  1165. #ifdef ATOMIC_DEV_BUILD
  1166. String projectPath = tenv->GetRootSourceDir() + "Script/AtomicNET/AtomicProject.json";
  1167. #else
  1168. String projectPath = tenv->GetAtomicNETRootDir() + "Build/Projects/AtomicProject.json";
  1169. #endif
  1170. SharedPtr<File> file(new File(context_));
  1171. if (!file->Open(GetSanitizedPath(projectPath)))
  1172. return false;
  1173. String json;
  1174. file->ReadText(json);
  1175. json.Replace("$ATOMIC_PROJECT_NAME$", projectSettings_->GetName());
  1176. JSONValue jvalue;
  1177. if (!JSONFile::ParseJSON(json, jvalue))
  1178. return false;
  1179. #ifdef ATOMIC_DEV_BUILD
  1180. // patch projects
  1181. JSONArray netProjects = netJSON["projects"].GetArray();
  1182. JSONArray projects = jvalue["projects"].GetArray();
  1183. for (unsigned i = 0; i < projects.Size(); i++)
  1184. {
  1185. netProjects.Push(JSONValue(projects[i].GetObject()));
  1186. }
  1187. jvalue["projects"] = netProjects;
  1188. return LoadProject(jvalue);
  1189. #else
  1190. return LoadProject(jvalue);
  1191. #endif
  1192. }
  1193. void NETProjectGen::SetSupportedPlatforms(const StringVector& platforms)
  1194. {
  1195. projectSettings_ = SharedPtr<ProjectSettings>(new ProjectSettings(context_));
  1196. for (unsigned i = 0; i < platforms.Size(); i++)
  1197. {
  1198. projectSettings_->AddSupportedPlatform(platforms[i]);
  1199. }
  1200. }
  1201. bool NETProjectGen::GetSupportsPlatform(const String& platform) const
  1202. {
  1203. // If no project platform settings are loaded, always supports
  1204. if (projectSettings_.Null())
  1205. {
  1206. return true;
  1207. }
  1208. return projectSettings_->GetSupportsPlatform(platform);
  1209. }
  1210. bool NETProjectGen::GetRequiresNuGet()
  1211. {
  1212. if (solution_.Null())
  1213. {
  1214. ATOMIC_LOGERROR("NETProjectGen::GetRequiresNuGet() - called without a solution loaded");
  1215. return false;
  1216. }
  1217. return solution_->GetPackages().Size() != 0;
  1218. }
  1219. String NETProjectGen::GenerateUUID()
  1220. {
  1221. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  1222. Poco::UUID uuid(generator.create()); // time based
  1223. return String(uuid.toString().c_str()).ToUpper();
  1224. }
  1225. }