NETProjectGen.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  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. outputType.SetValue(outputType_);
  624. pgroup.CreateChild("AppDesignerFolder").SetValue("Properties");
  625. // RootNamespace
  626. XMLElement rootNamespace = pgroup.CreateChild("RootNamespace");
  627. rootNamespace.SetValue(rootNamespace_);
  628. // AssemblyName
  629. XMLElement assemblyName = pgroup.CreateChild("AssemblyName");
  630. assemblyName.SetValue(assemblyName_);
  631. pgroup.CreateChild("FileAlignment").SetValue("512");
  632. if (projectTypeGuids_.Size())
  633. {
  634. pgroup.CreateChild("ProjectTypeGuids").SetValue(String::Joined(projectTypeGuids_, ";"));
  635. }
  636. if (SupportsDesktop())
  637. {
  638. pgroup.CreateChild("TargetFrameworkVersion").SetValue("v4.5");
  639. }
  640. else
  641. {
  642. pgroup.CreateChild("ProductVersion").SetValue("8.0.30703");
  643. pgroup.CreateChild("SchemaVersion").SetValue("2.0");
  644. if (SupportsPlatform("ios"))
  645. {
  646. pgroup.CreateChild("IPhoneResourcePrefix").SetValue("Resources");
  647. }
  648. else
  649. {
  650. pgroup.CreateChild("TargetFrameworkVersion").SetValue("v6.0");
  651. }
  652. if (SupportsPlatform("android"))
  653. {
  654. if (!projectTypeGuids_.Size())
  655. {
  656. pgroup.CreateChild("ProjectTypeGuids").SetValue("{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}");
  657. }
  658. pgroup.CreateChild("AndroidUseLatestPlatformSdk").SetValue("True");
  659. if (!androidApplication_)
  660. {
  661. // 10368E6C-D01B-4462-8E8B-01FC667A7035 is a binding library
  662. if (!projectTypeGuids_.Contains("{10368E6C-D01B-4462-8E8B-01FC667A7035}"))
  663. pgroup.CreateChild("GenerateSerializationAssemblies").SetValue("Off");
  664. }
  665. else
  666. {
  667. // Android Application
  668. pgroup.CreateChild("AndroidApplication").SetValue("true");
  669. // AndroidManifest.xml must reside in Properties/AndroidManifest.xml, which introduces sync issues :/
  670. pgroup.CreateChild("AndroidManifest").SetValue("Properties\\AndroidManifest.xml");
  671. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  672. String manifestSourceFile = "$ATOMIC_PROJECT_ROOT$/Project/AtomicNET/Platforms/Android/Properties/AndroidManifest.xml";
  673. ReplacePathStrings(manifestSourceFile);
  674. manifestSourceFile = GetSanitizedPath(manifestSourceFile);
  675. if (fileSystem->FileExists(manifestSourceFile))
  676. {
  677. String manifestDest = GetSanitizedPath(projectPath_ + "Properties/");
  678. if (!fileSystem->DirExists(manifestDest))
  679. {
  680. fileSystem->CreateDirs(GetSanitizedPath(projectGen_->GetAtomicProjectPath()), ToString("/AtomicNET/Solution/%s/Properties/", name_.CString()));
  681. }
  682. if (fileSystem->DirExists(manifestDest))
  683. {
  684. if (!fileSystem->Copy(manifestSourceFile, manifestDest + "AndroidManifest.xml"))
  685. {
  686. ATOMIC_LOGERRORF("Unable to copy AndroidManifest from %s to %s", manifestSourceFile.CString(), manifestDest.CString());
  687. }
  688. }
  689. else
  690. {
  691. ATOMIC_LOGERRORF("Unable to create folder %s for AndroidManifest.xml", manifestDest.CString());
  692. }
  693. }
  694. else
  695. {
  696. ATOMIC_LOGERRORF("No AndroidManifest.xml, project will not deploy (%s)", manifestSourceFile.CString());
  697. }
  698. String relativePath;
  699. if (GetRelativeProjectPath("$ATOMIC_PROJECT_ROOT$/Project/AtomicNET/Platforms/Android/Resources/Resource.Designer.cs", projectPath_, relativePath))
  700. {
  701. relativePath.Replace("/", "\\");
  702. pgroup.CreateChild("AndroidResgenFile").SetValue(relativePath);
  703. }
  704. else
  705. {
  706. ATOMIC_LOGERROR("Unabled to get relative path for AndroidResgenFile");
  707. }
  708. pgroup.CreateChild("GenerateSerializationAssemblies").SetValue("Off");
  709. }
  710. }
  711. }
  712. if (targetFrameworkProfile_.Length())
  713. {
  714. pgroup.CreateChild("TargetFrameworkProfile").SetValue(targetFrameworkProfile_);
  715. }
  716. }
  717. bool NETCSProject::GenerateShared()
  718. {
  719. // .shproj
  720. XMLElement project = xmlFile_->CreateRoot("Project");
  721. project.SetAttribute("DefaultTargets", "Build");
  722. project.SetAttribute("ToolsVersion", "14.0");
  723. project.SetAttribute("DefaultTargets", "Build");
  724. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  725. // Project Group
  726. XMLElement projectGroup = project.CreateChild("PropertyGroup");
  727. projectGroup.SetAttribute("Label", "Globals");
  728. projectGroup.CreateChild("ProjectGuid").SetValue(projectGuid_);
  729. projectGroup.CreateChild("MinimumVisualStudioVersion").SetValue("14.0");
  730. XMLElement import = project.CreateChild("Import");
  731. import.SetAttribute("Project", "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props");
  732. import.SetAttribute("Condition", "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')");
  733. import = project.CreateChild("Import");
  734. import.SetAttribute("Project", "$(MSBuildExtensionsPath32)\\Microsoft\\VisualStudio\\v$(VisualStudioVersion)\\CodeSharing\\Microsoft.CodeSharing.Common.Default.props");
  735. import = project.CreateChild("Import");
  736. import.SetAttribute("Project", "$(MSBuildExtensionsPath32)\\Microsoft\\VisualStudio\\v$(VisualStudioVersion)\\CodeSharing\\Microsoft.CodeSharing.Common.props");
  737. import = project.CreateChild("Import");
  738. import.SetAttribute("Project", ToString("%s.projitems", name_.CString()));
  739. import.SetAttribute("Label", "Shared");
  740. import = project.CreateChild("Import");
  741. import.SetAttribute("Project", "$(MSBuildExtensionsPath32)\\Microsoft\\VisualStudio\\v$(VisualStudioVersion)\\CodeSharing\\Microsoft.CodeSharing.CSharp.targets");
  742. String projectSource = xmlFile_->ToString();
  743. SharedPtr<File> output(new File(context_, GetSanitizedPath(projectPath_ + name_ + ".shproj"), FILE_WRITE));
  744. output->Write(projectSource.CString(), projectSource.Length());
  745. // projitems
  746. SharedPtr<XMLFile> itemsXMLFile(new XMLFile(context_));
  747. XMLElement itemsProject = itemsXMLFile->CreateRoot("Project");
  748. itemsProject.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  749. XMLElement propertyGroup = itemsProject.CreateChild("PropertyGroup");
  750. propertyGroup.CreateChild("MSBuildAllProjects").SetValue("$(MSBuildAllProjects);$(MSBuildThisFileFullPath)");
  751. propertyGroup.CreateChild("HasSharedItems").SetValue("true");
  752. propertyGroup.CreateChild("SharedGUID").SetValue(projectGuid_);
  753. propertyGroup = itemsProject.CreateChild("PropertyGroup");
  754. propertyGroup.SetAttribute("Label", "Configuration");
  755. propertyGroup.CreateChild("Import_RootNamespace").SetValue("AtomicEngine");
  756. CreateCompileItemGroup(itemsProject);
  757. String itemSource = itemsXMLFile->ToString();
  758. SharedPtr<File> itemsOutput(new File(context_, GetSanitizedPath(projectPath_ + name_ + ".projitems"), FILE_WRITE));
  759. itemsOutput->Write(itemSource.CString(), itemSource.Length());
  760. return true;
  761. }
  762. bool NETCSProject::GenerateStandard()
  763. {
  764. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  765. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  766. NETSolution* solution = projectGen_->GetSolution();
  767. XMLElement project = xmlFile_->CreateRoot("Project");
  768. project.SetAttribute("DefaultTargets", "Build");
  769. project.SetAttribute("ToolsVersion", "14.0");
  770. project.SetAttribute("DefaultTargets", "Build");
  771. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  772. XMLElement import = project.CreateChild("Import");
  773. import.SetAttribute("Project", "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props");
  774. import.SetAttribute("Condition", "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')");
  775. CreateMainPropertyGroup(project);
  776. CreateDebugPropertyGroup(project);
  777. CreateReleasePropertyGroup(project);
  778. CreateReferencesItemGroup(project);
  779. CreateProjectReferencesItemGroup(project);
  780. CreateCompileItemGroup(project);
  781. CreatePackagesItemGroup(project);
  782. if (SupportsPlatform("android"))
  783. {
  784. CreateAndroidItems(project);
  785. }
  786. if (SupportsPlatform("ios"))
  787. {
  788. CreateIOSItems(project);
  789. }
  790. if (SupportsDesktop() && !GetIsPCL())
  791. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildToolsPath)\\Microsoft.CSharp.targets");
  792. if (outputType_.ToLower() == "exe" || androidApplication_)
  793. {
  794. CreateApplicationItems(project);
  795. }
  796. if (!GetIsPCL() && !sharedReferences_.Size() && outputType_ != "Shared")
  797. CreateAssemblyInfo();
  798. const String& atomicProjectPath = projectGen_->GetAtomicProjectPath();
  799. if (atomicProjectPath.Length())
  800. {
  801. // Create the AtomicProject.csproj.user file if it doesn't exist
  802. String userSettingsFilename = projectPath_ + name_ + ".csproj.user";
  803. if (!fileSystem->FileExists(userSettingsFilename))
  804. {
  805. SharedPtr<XMLFile> userSettings(new XMLFile(context_));
  806. XMLElement project = userSettings->CreateRoot("Project");
  807. project.SetAttribute("ToolsVersion", "14.0");
  808. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  809. StringVector configs;
  810. configs.Push("Debug");
  811. configs.Push("Release");
  812. for (unsigned i = 0; i < configs.Size(); i++)
  813. {
  814. String cfg = configs[i];
  815. XMLElement propertyGroup = project.CreateChild("PropertyGroup");
  816. propertyGroup.SetAttribute("Condition", ToString("'$(Configuration)|$(Platform)' == '%s|AnyCPU'", cfg.CString()));
  817. String startArguments;
  818. #ifndef ATOMIC_DEV_BUILD
  819. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "Resources/").CString());
  820. #endif
  821. propertyGroup.CreateChild("StartAction").SetValue("Project");
  822. startArguments += ToString("--project \"%s\"", atomicProjectPath.CString());
  823. propertyGroup.CreateChild("StartArguments").SetValue(startArguments);
  824. }
  825. String userSettingsSource = userSettings->ToString();
  826. SharedPtr<File> output(new File(context_, GetSanitizedPath(userSettingsFilename), FILE_WRITE));
  827. output->Write(userSettingsSource.CString(), userSettingsSource.Length());
  828. output->Close();
  829. }
  830. }
  831. for (unsigned i = 0; i < sharedReferences_.Size(); i++)
  832. {
  833. NETCSProject* sharedProject = projectGen_->GetCSProjectByName(sharedReferences_[i]);
  834. if (!sharedProject)
  835. {
  836. ATOMIC_LOGERRORF("Unable to get shared project %s", sharedReferences_[i].CString());
  837. continue;
  838. }
  839. String path = sharedProject->projectPath_ + sharedReferences_[i] + ".projitems";
  840. String relativePath;
  841. if (GetRelativePath(projectPath_, GetPath(path), relativePath))
  842. {
  843. path = relativePath + GetFileName(path) + GetExtension(path);
  844. }
  845. XMLElement shared = project.CreateChild("Import");
  846. shared.SetAttribute("Project", path);
  847. shared.SetAttribute("Label", "Shared");
  848. }
  849. for (unsigned i = 0; i < importProjects_.Size(); i++)
  850. {
  851. project.CreateChild("Import").SetAttribute("Project", importProjects_[i].CString());
  852. }
  853. // Have to come after the imports, so AfterBuild exists
  854. String projectName = "AtomicProject";
  855. if (projectGen_->GetProjectSettings())
  856. projectName = projectGen_->GetProjectSettings()->GetName();
  857. if (name_ == projectName)
  858. {
  859. XMLElement afterBuild = project.CreateChild("Target");
  860. afterBuild.SetAttribute("Name", "AfterBuild");
  861. XMLElement copy = afterBuild.CreateChild("Copy");
  862. copy.SetAttribute("SourceFiles", "$(TargetPath)");
  863. String destPath = projectPath_ + "../../../Resources/";
  864. String relativePath;
  865. String resourceDir = AddTrailingSlash(atomicProjectPath) + "Resources/";
  866. if (GetRelativePath(projectPath_, resourceDir, relativePath))
  867. {
  868. destPath = AddTrailingSlash(relativePath);
  869. }
  870. copy.SetAttribute("DestinationFolder", destPath);
  871. }
  872. String projectSource = xmlFile_->ToString();
  873. SharedPtr<File> output(new File(context_, GetSanitizedPath(projectPath_ + name_ + ".csproj"), FILE_WRITE));
  874. output->Write(projectSource.CString(), projectSource.Length());
  875. return true;
  876. }
  877. bool NETCSProject::Generate()
  878. {
  879. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  880. NETSolution* solution = projectGen_->GetSolution();
  881. projectPath_ = solution->GetOutputPath() + name_ + "/";
  882. if (!CreateProjectFolder(projectPath_))
  883. return false;
  884. if (!CreateProjectFolder(projectPath_ + "Properties"))
  885. return false;
  886. if (outputType_ == "Shared")
  887. {
  888. return GenerateShared();
  889. }
  890. return GenerateStandard();
  891. }
  892. bool NETCSProject::Load(const JSONValue& root)
  893. {
  894. name_ = root["name"].GetString();
  895. projectGuid_ = root["projectGuid"].GetString();
  896. if (!projectGuid_.Length())
  897. {
  898. ATOMIC_LOGINFOF("GUID not provided for project %s, generating one", name_.CString());
  899. projectGuid_ = projectGen_->GenerateUUID();
  900. }
  901. outputType_ = root["outputType"].GetString();
  902. androidApplication_ = root["androidApplication"].GetBool();
  903. playerApplication_ = root["playerApplication"].GetBool();
  904. rootNamespace_ = root["rootNamespace"].GetString();
  905. assemblyName_ = root["assemblyName"].GetString();
  906. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  907. ReplacePathStrings(assemblyOutputPath_);
  908. assemblyOutputPath_ = GetSanitizedPath(assemblyOutputPath_);
  909. assemblySearchPaths_ = root["assemblySearchPaths"].GetString();
  910. ReplacePathStrings(assemblySearchPaths_);
  911. assemblySearchPaths_ = GetSanitizedPath(assemblySearchPaths_);
  912. const JSONArray& platforms = root["platforms"].GetArray();
  913. for (unsigned i = 0; i < platforms.Size(); i++)
  914. {
  915. String platform = platforms[i].GetString();
  916. platforms_.Push(platform.ToLower());
  917. }
  918. const JSONArray& references = root["references"].GetArray();
  919. for (unsigned i = 0; i < references.Size(); i++)
  920. {
  921. String reference = references[i].GetString();
  922. ReplacePathStrings(reference);
  923. references_.Push(reference);
  924. }
  925. const JSONArray& packages = root["packages"].GetArray();
  926. for (unsigned i = 0; i < packages.Size(); i++)
  927. {
  928. String package = packages[i].GetString();
  929. if (packages_.Find(package) != packages_.End())
  930. {
  931. ATOMIC_LOGERRORF("Duplicate package found %s", package.CString());
  932. continue;
  933. }
  934. projectGen_->GetSolution()->RegisterPackage(package);
  935. packages_.Push(package);
  936. }
  937. const JSONArray& sources = root["sources"].GetArray();
  938. for (unsigned i = 0; i < sources.Size(); i++)
  939. {
  940. String source = sources[i].GetString();
  941. ReplacePathStrings(source);
  942. sourceFolders_.Push(AddTrailingSlash(source));
  943. }
  944. const JSONArray& defineConstants = root["defineConstants"].GetArray();
  945. for (unsigned i = 0; i < defineConstants.Size(); i++)
  946. {
  947. defineConstants_.Push(defineConstants[i].GetString());
  948. }
  949. const JSONArray& projectTypeGuids = root["projectTypeGuids"].GetArray();
  950. for (unsigned i = 0; i < projectTypeGuids.Size(); i++)
  951. {
  952. String guid = projectTypeGuids[i].GetString();
  953. projectTypeGuids_.Push(ToString("{%s}", guid.CString()));
  954. }
  955. const JSONArray& importProjects = root["importProjects"].GetArray();
  956. for (unsigned i = 0; i < importProjects.Size(); i++)
  957. {
  958. importProjects_.Push(importProjects[i].GetString());
  959. }
  960. const JSONArray& libraryProjectZips = root["libraryProjectZips"].GetArray();
  961. for (unsigned i = 0; i < libraryProjectZips.Size(); i++)
  962. {
  963. String zipPath = libraryProjectZips[i].GetString();
  964. ReplacePathStrings(zipPath);
  965. libraryProjectZips_.Push(zipPath);
  966. }
  967. const JSONArray& transformFiles = root["transformFiles"].GetArray();
  968. for (unsigned i = 0; i < transformFiles.Size(); i++)
  969. {
  970. String transformFile = transformFiles[i].GetString();
  971. ReplacePathStrings(transformFile);
  972. transformFiles_.Push(transformFile);
  973. }
  974. const JSONArray& sharedReferences = root["sharedReferences"].GetArray();
  975. for (unsigned i = 0; i < sharedReferences.Size(); i++)
  976. {
  977. sharedReferences_.Push(sharedReferences[i].GetString());
  978. }
  979. targetFrameworkProfile_ = root["targetFrameworkProfile"].GetString();
  980. // iOS
  981. objcBindingApiDefinition_ = root["objcBindingApiDefinition"].GetString();
  982. ReplacePathStrings(objcBindingApiDefinition_);
  983. codesignEntitlements_ = root["codesignEntitlements"].GetString();
  984. ReplacePathStrings(codesignEntitlements_);
  985. infoPList_ = root["infoPList"].GetString();
  986. ReplacePathStrings(infoPList_);
  987. return true;
  988. }
  989. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen, bool rewrite) : NETProjectBase(context, projectGen),
  990. rewriteSolution_(rewrite)
  991. {
  992. }
  993. NETSolution::~NETSolution()
  994. {
  995. }
  996. bool NETSolution::Generate()
  997. {
  998. String slnPath = outputPath_ + name_ + ".sln";
  999. GenerateSolution(slnPath);
  1000. return true;
  1001. }
  1002. void NETSolution::GenerateSolution(const String &slnPath)
  1003. {
  1004. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  1005. source += "# Visual Studio 14\n";
  1006. source += "VisualStudioVersion = 14.0.25420.1\n";
  1007. source += "MinimumVisualStudioVersion = 10.0.40219.1\n";
  1008. solutionGUID_ = projectGen_->GenerateUUID();
  1009. PODVector<NETCSProject*> depends;
  1010. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  1011. for (unsigned i = 0; i < projects.Size(); i++)
  1012. {
  1013. NETCSProject* p = projects.At(i);
  1014. const String& projectName = p->GetName();
  1015. const String& projectGUID = p->GetProjectGUID();
  1016. String CSharpProjectGUID = "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  1017. String ext = "csproj";
  1018. if (p->outputType_ == "Shared")
  1019. {
  1020. CSharpProjectGUID = "D954291E-2A0B-460D-934E-DC6B0785DB48";
  1021. ext = "shproj";
  1022. }
  1023. source += ToString("Project(\"{%s}\") = \"%s\", \"%s\\%s.%s\", \"{%s}\"\n",
  1024. CSharpProjectGUID.CString(), projectName.CString(), projectName.CString(),
  1025. projectName.CString(), ext.CString(), projectGUID.CString());
  1026. projectGen_->GetCSProjectDependencies(p, depends);
  1027. if (depends.Size())
  1028. {
  1029. source += "\tProjectSection(ProjectDependencies) = postProject\n";
  1030. for (unsigned j = 0; j < depends.Size(); j++)
  1031. {
  1032. source += ToString("\t{%s} = {%s}\n",
  1033. depends[j]->GetProjectGUID().CString(), depends[j]->GetProjectGUID().CString());
  1034. }
  1035. source += "\tEndProjectSection\n";
  1036. }
  1037. source += "EndProject\n";
  1038. }
  1039. source += "Global\n";
  1040. // SharedMSBuildProjectFiles
  1041. source += " GlobalSection(SharedMSBuildProjectFiles) = preSolution\n";
  1042. for (unsigned i = 0; i < projects.Size(); i++)
  1043. {
  1044. NETCSProject* p = projects.At(i);
  1045. if (p->outputType_ == "Shared")
  1046. {
  1047. for (unsigned j = 0; j < projects.Size(); j++)
  1048. {
  1049. NETCSProject* p2 = projects.At(j);
  1050. if (p == p2)
  1051. {
  1052. source += ToString(" %s\\%s.projitems*{%s}*SharedItemsImports = 13\n", p->name_.CString(), p->name_.CString(), p->projectGuid_.CString());
  1053. }
  1054. else
  1055. {
  1056. if (p2->sharedReferences_.Contains(p->name_))
  1057. {
  1058. source += ToString(" %s\\%s.projitems*{%s}*SharedItemsImports = 4\n", p->name_.CString(), p->name_.CString(), p2->projectGuid_.CString());
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. source += " EndGlobalSection\n";
  1065. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  1066. source += " Debug|Any CPU = Debug|Any CPU\n";
  1067. source += " Release|Any CPU = Release|Any CPU\n";
  1068. source += " Debug|iPhone = Debug|iPhone\n";
  1069. source += " Release|iPhone = Release|iPhone\n";
  1070. source += " EndGlobalSection\n";
  1071. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  1072. for (unsigned i = 0; i < projects.Size(); i++)
  1073. {
  1074. NETCSProject* p = projects.At(i);
  1075. if (p->outputType_ == "Shared")
  1076. continue;
  1077. String cpu = "Any CPU";
  1078. if (p->GetIsPlayerApp() && p->SupportsPlatform("ios"))
  1079. cpu = "iPhone";
  1080. source += ToString(" {%s}.Debug|%s.ActiveCfg = Debug|%s\n", p->GetProjectGUID().CString(), cpu.CString(), cpu.CString());
  1081. source += ToString(" {%s}.Debug|%s.Build.0 = Debug|%s\n", p->GetProjectGUID().CString(), cpu.CString(), cpu.CString());
  1082. source += ToString(" {%s}.Release|%s.ActiveCfg = Release|%s\n", p->GetProjectGUID().CString(), cpu.CString(), cpu.CString());
  1083. source += ToString(" {%s}.Release|%s.Build.0 = Release|%s\n", p->GetProjectGUID().CString(), cpu.CString(), cpu.CString());
  1084. if (cpu != "iPhone" && (p->SupportsPlatform("ios", false)))
  1085. {
  1086. source += ToString(" {%s}.Debug|iPhone.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  1087. source += ToString(" {%s}.Debug|iPhone.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  1088. source += ToString(" {%s}.Release|iPhone.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  1089. source += ToString(" {%s}.Release|iPhone.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  1090. }
  1091. }
  1092. source += " EndGlobalSection\n";
  1093. source += "EndGlobal\n";
  1094. if (!rewriteSolution_)
  1095. {
  1096. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  1097. if (fileSystem->Exists(slnPath))
  1098. return;
  1099. }
  1100. SharedPtr<File> output(new File(context_, GetSanitizedPath(slnPath), FILE_WRITE));
  1101. output->Write(source.CString(), source.Length());
  1102. output->Close();
  1103. }
  1104. bool NETSolution::Load(const JSONValue& root)
  1105. {
  1106. FileSystem* fs = GetSubsystem<FileSystem>();
  1107. name_ = root["name"].GetString();
  1108. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  1109. ReplacePathStrings(outputPath_);
  1110. // TODO: use poco mkdirs
  1111. if (!fs->DirExists(outputPath_))
  1112. fs->CreateDirsRecursive(outputPath_);
  1113. return true;
  1114. }
  1115. bool NETSolution::RegisterPackage(const String& package)
  1116. {
  1117. if (packages_.Find(package) != packages_.End())
  1118. return false;
  1119. packages_.Push(package);
  1120. return true;
  1121. }
  1122. NETProjectGen::NETProjectGen(Context* context) : Object(context),
  1123. rewriteSolution_(false)
  1124. {
  1125. }
  1126. NETProjectGen::~NETProjectGen()
  1127. {
  1128. }
  1129. NETCSProject* NETProjectGen::GetCSProjectByName(const String & name)
  1130. {
  1131. for (unsigned i = 0; i < projects_.Size(); i++)
  1132. {
  1133. if (projects_[i]->GetName() == name)
  1134. return projects_[i];
  1135. }
  1136. return nullptr;
  1137. }
  1138. bool NETProjectGen::GetCSProjectDependencies(NETCSProject* source, PODVector<NETCSProject*>& depends) const
  1139. {
  1140. depends.Clear();
  1141. const Vector<String>& references = source->GetReferences();
  1142. for (unsigned i = 0; i < projects_.Size(); i++)
  1143. {
  1144. NETCSProject* pdepend = projects_.At(i);
  1145. if (source == pdepend)
  1146. continue;
  1147. for (unsigned j = 0; j < references.Size(); j++)
  1148. {
  1149. if (pdepend->GetName() == references[j])
  1150. {
  1151. depends.Push(pdepend);
  1152. }
  1153. }
  1154. }
  1155. return depends.Size() != 0;
  1156. }
  1157. bool NETProjectGen::Generate()
  1158. {
  1159. solution_->Generate();
  1160. for (unsigned i = 0; i < projects_.Size(); i++)
  1161. {
  1162. if (!projects_[i]->Generate())
  1163. return false;
  1164. }
  1165. return true;
  1166. }
  1167. void NETProjectGen::SetRewriteSolution(bool rewrite)
  1168. {
  1169. rewriteSolution_ = rewrite;
  1170. if (solution_.NotNull())
  1171. solution_->SetRewriteSolution(rewrite);
  1172. }
  1173. bool NETProjectGen::IncludeProjectOnPlatform(const JSONValue& projectRoot, const String& platform)
  1174. {
  1175. const JSONArray& platforms = projectRoot["platforms"].GetArray();
  1176. if (!platforms.Size())
  1177. return true; // all platforms
  1178. String scriptPlatform = platform.ToLower();
  1179. for (unsigned i = 0; i < platforms.Size(); i++)
  1180. {
  1181. String platform = platforms[i].GetString().ToLower();
  1182. if (platform == "desktop")
  1183. {
  1184. if (scriptPlatform == "windows" || scriptPlatform == "macosx" || scriptPlatform == "linux")
  1185. return true;
  1186. return false;
  1187. }
  1188. if (platform == "android" && scriptPlatform != "android")
  1189. return false;
  1190. }
  1191. return true;
  1192. }
  1193. bool NETProjectGen::LoadProject(const JSONValue &root)
  1194. {
  1195. solution_ = new NETSolution(context_, this, rewriteSolution_);
  1196. solution_->Load(root["solution"]);
  1197. const JSONValue& jprojects = root["projects"];
  1198. if (!jprojects.IsArray() || !jprojects.Size())
  1199. return false;
  1200. for (unsigned i = 0; i < jprojects.Size(); i++)
  1201. {
  1202. const JSONValue& jproject = jprojects[i];
  1203. if (!jproject.IsObject())
  1204. return false;
  1205. JSONArray platforms = jproject["platforms"].GetArray();
  1206. if (platforms.Size())
  1207. {
  1208. bool found = false;
  1209. for (unsigned j = 0; j < platforms.Size(); j++)
  1210. {
  1211. if (GetSupportsPlatform(platforms[j].GetString()))
  1212. {
  1213. found = true;
  1214. break;
  1215. }
  1216. }
  1217. if (!found)
  1218. {
  1219. continue;
  1220. }
  1221. }
  1222. // HACK! Do not generate AtomicNETService in the AtomicProject solution
  1223. if (jproject["name"].GetString() == "AtomicNETService" && atomicProjectPath_.Length())
  1224. {
  1225. continue;
  1226. }
  1227. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  1228. if (!csProject->Load(jproject))
  1229. return false;
  1230. projects_.Push(csProject);
  1231. }
  1232. return true;
  1233. }
  1234. bool NETProjectGen::LoadJSONProject(const String& jsonProjectPath)
  1235. {
  1236. SharedPtr<File> file(new File(context_));
  1237. if (!file->Open(GetSanitizedPath(jsonProjectPath)))
  1238. return false;
  1239. String json;
  1240. file->ReadText(json);
  1241. JSONValue jvalue;
  1242. if (!JSONFile::ParseJSON(json, jvalue))
  1243. return false;
  1244. return LoadProject(jvalue);
  1245. }
  1246. bool NETProjectGen::LoadAtomicProject(const String& atomicProjectPath)
  1247. {
  1248. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  1249. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  1250. String pathname, filename, ext;
  1251. SplitPath(atomicProjectPath, pathname, filename, ext);
  1252. if (ext == ".atomic")
  1253. {
  1254. atomicProjectPath_ = AddTrailingSlash(pathname);
  1255. }
  1256. else
  1257. {
  1258. atomicProjectPath_ = AddTrailingSlash(atomicProjectPath);
  1259. }
  1260. // Do we have a loaded project?
  1261. if (Project* project = tsystem->GetProject())
  1262. {
  1263. // If so, use loaded project settings
  1264. projectSettings_ = project->GetProjectSettings();
  1265. }
  1266. else
  1267. {
  1268. // Nope, load them up
  1269. projectSettings_ = SharedPtr<ProjectSettings>(new ProjectSettings(context_));
  1270. projectSettings_->Load(atomicProjectPath_ + "Settings/Project.json");
  1271. }
  1272. #ifdef ATOMIC_DEV_BUILD
  1273. JSONValue netJSON;
  1274. SharedPtr<File> netJSONFile(new File(context_));
  1275. String atomicNETProject = tenv->GetRootSourceDir() + "Script/AtomicNET/AtomicNETProject.json";
  1276. if (!netJSONFile->Open(GetSanitizedPath(atomicNETProject)))
  1277. return false;
  1278. String netJSONString;
  1279. netJSONFile->ReadText(netJSONString);
  1280. if (!JSONFile::ParseJSON(netJSONString, netJSON))
  1281. return false;
  1282. #endif
  1283. #ifdef ATOMIC_DEV_BUILD
  1284. String projectPath = tenv->GetRootSourceDir() + "Script/AtomicNET/AtomicProject.json";
  1285. #else
  1286. String projectPath = tenv->GetAtomicNETRootDir() + "Build/Projects/AtomicProject.json";
  1287. #endif
  1288. SharedPtr<File> file(new File(context_));
  1289. if (!file->Open(GetSanitizedPath(projectPath)))
  1290. return false;
  1291. String json;
  1292. file->ReadText(json);
  1293. json.Replace("$ATOMIC_PROJECT_NAME$", projectSettings_->GetName());
  1294. JSONValue jvalue;
  1295. if (!JSONFile::ParseJSON(json, jvalue))
  1296. return false;
  1297. #ifdef ATOMIC_DEV_BUILD
  1298. // patch projects
  1299. JSONArray netProjects = netJSON["projects"].GetArray();
  1300. JSONArray projects = jvalue["projects"].GetArray();
  1301. for (unsigned i = 0; i < projects.Size(); i++)
  1302. {
  1303. netProjects.Push(JSONValue(projects[i].GetObject()));
  1304. }
  1305. jvalue["projects"] = netProjects;
  1306. return LoadProject(jvalue);
  1307. #else
  1308. return LoadProject(jvalue);
  1309. #endif
  1310. }
  1311. void NETProjectGen::SetSupportedPlatforms(const StringVector& platforms)
  1312. {
  1313. projectSettings_ = SharedPtr<ProjectSettings>(new ProjectSettings(context_));
  1314. for (unsigned i = 0; i < platforms.Size(); i++)
  1315. {
  1316. projectSettings_->AddSupportedPlatform(platforms[i]);
  1317. }
  1318. }
  1319. bool NETProjectGen::GetSupportsPlatform(const String& platform) const
  1320. {
  1321. // If no project platform settings are loaded, always supports
  1322. if (projectSettings_.Null())
  1323. {
  1324. return true;
  1325. }
  1326. return projectSettings_->GetSupportsPlatform(platform);
  1327. }
  1328. bool NETProjectGen::GetRequiresNuGet()
  1329. {
  1330. if (solution_.Null())
  1331. {
  1332. ATOMIC_LOGERROR("NETProjectGen::GetRequiresNuGet() - called without a solution loaded");
  1333. return false;
  1334. }
  1335. return solution_->GetPackages().Size() != 0;
  1336. }
  1337. String NETProjectGen::GenerateUUID()
  1338. {
  1339. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  1340. Poco::UUID uuid(generator.create()); // time based
  1341. return String(uuid.toString().c_str()).ToUpper();
  1342. }
  1343. }