NETProjectGen.cpp 62 KB

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