NETProjectGen.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  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. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "Resources/").CString());
  831. #endif
  832. propertyGroup.CreateChild("StartAction").SetValue("Project");
  833. startArguments += ToString("--project \"%s\"", atomicProjectPath.CString());
  834. propertyGroup.CreateChild("StartArguments").SetValue(startArguments);
  835. }
  836. String userSettingsSource = userSettings->ToString();
  837. SharedPtr<File> output(new File(context_, GetSanitizedPath(userSettingsFilename), FILE_WRITE));
  838. output->Write(userSettingsSource.CString(), userSettingsSource.Length());
  839. output->Close();
  840. }
  841. }
  842. for (unsigned i = 0; i < sharedReferences_.Size(); i++)
  843. {
  844. NETCSProject* sharedProject = projectGen_->GetCSProjectByName(sharedReferences_[i]);
  845. if (!sharedProject)
  846. {
  847. ATOMIC_LOGERRORF("Unable to get shared project %s", sharedReferences_[i].CString());
  848. continue;
  849. }
  850. String path = sharedProject->projectPath_ + sharedReferences_[i] + ".projitems";
  851. String relativePath;
  852. if (GetRelativePath(projectPath_, GetPath(path), relativePath))
  853. {
  854. path = relativePath + GetFileName(path) + GetExtension(path);
  855. }
  856. XMLElement shared = project.CreateChild("Import");
  857. shared.SetAttribute("Project", path);
  858. shared.SetAttribute("Label", "Shared");
  859. }
  860. for (unsigned i = 0; i < importProjects_.Size(); i++)
  861. {
  862. project.CreateChild("Import").SetAttribute("Project", importProjects_[i].CString());
  863. }
  864. // Have to come after the imports, so AfterBuild exists
  865. String projectName = "AtomicProject";
  866. if (projectGen_->GetProjectSettings())
  867. projectName = projectGen_->GetProjectSettings()->GetName();
  868. if (name_ == projectName)
  869. {
  870. XMLElement afterBuild = project.CreateChild("Target");
  871. afterBuild.SetAttribute("Name", "AfterBuild");
  872. XMLElement copy = afterBuild.CreateChild("Copy");
  873. copy.SetAttribute("SourceFiles", "$(TargetPath)");
  874. String destPath = projectPath_ + "../../../Resources/";
  875. String relativePath;
  876. String resourceDir = AddTrailingSlash(atomicProjectPath) + "Resources/";
  877. if (GetRelativePath(projectPath_, resourceDir, relativePath))
  878. {
  879. destPath = AddTrailingSlash(relativePath);
  880. }
  881. copy.SetAttribute("DestinationFolder", destPath);
  882. }
  883. String projectSource = xmlFile_->ToString();
  884. SharedPtr<File> output(new File(context_, GetSanitizedPath(projectPath_ + name_ + ".csproj"), FILE_WRITE));
  885. output->Write(projectSource.CString(), projectSource.Length());
  886. return true;
  887. }
  888. bool NETCSProject::Generate()
  889. {
  890. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  891. NETSolution* solution = projectGen_->GetSolution();
  892. projectPath_ = solution->GetOutputPath() + name_ + "/";
  893. if (!CreateProjectFolder(projectPath_))
  894. return false;
  895. if (!CreateProjectFolder(projectPath_ + "Properties"))
  896. return false;
  897. if (outputType_ == "Shared")
  898. {
  899. return GenerateShared();
  900. }
  901. return GenerateStandard();
  902. }
  903. bool NETCSProject::Load(const JSONValue& root)
  904. {
  905. name_ = root["name"].GetString();
  906. projectGuid_ = root["projectGuid"].GetString();
  907. if (!projectGuid_.Length())
  908. {
  909. ATOMIC_LOGINFOF("GUID not provided for project %s, generating one", name_.CString());
  910. projectGuid_ = projectGen_->GenerateUUID();
  911. }
  912. outputType_ = root["outputType"].GetString();
  913. androidApplication_ = root["androidApplication"].GetBool();
  914. playerApplication_ = root["playerApplication"].GetBool();
  915. rootNamespace_ = root["rootNamespace"].GetString();
  916. assemblyName_ = root["assemblyName"].GetString();
  917. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  918. ReplacePathStrings(assemblyOutputPath_);
  919. assemblyOutputPath_ = GetSanitizedPath(assemblyOutputPath_);
  920. assemblySearchPaths_ = root["assemblySearchPaths"].GetString();
  921. ReplacePathStrings(assemblySearchPaths_);
  922. assemblySearchPaths_ = GetSanitizedPath(assemblySearchPaths_);
  923. const JSONArray& platforms = root["platforms"].GetArray();
  924. for (unsigned i = 0; i < platforms.Size(); i++)
  925. {
  926. String platform = platforms[i].GetString();
  927. platforms_.Push(platform.ToLower());
  928. }
  929. const JSONArray& references = root["references"].GetArray();
  930. for (unsigned i = 0; i < references.Size(); i++)
  931. {
  932. String reference = references[i].GetString();
  933. ReplacePathStrings(reference);
  934. references_.Push(reference);
  935. }
  936. const JSONArray& packages = root["packages"].GetArray();
  937. for (unsigned i = 0; i < packages.Size(); i++)
  938. {
  939. String package = packages[i].GetString();
  940. if (packages_.Find(package) != packages_.End())
  941. {
  942. ATOMIC_LOGERRORF("Duplicate package found %s", package.CString());
  943. continue;
  944. }
  945. projectGen_->GetSolution()->RegisterPackage(package);
  946. packages_.Push(package);
  947. }
  948. const JSONArray& sources = root["sources"].GetArray();
  949. for (unsigned i = 0; i < sources.Size(); i++)
  950. {
  951. String source = sources[i].GetString();
  952. ReplacePathStrings(source);
  953. sourceFolders_.Push(AddTrailingSlash(source));
  954. }
  955. const JSONArray& defineConstants = root["defineConstants"].GetArray();
  956. for (unsigned i = 0; i < defineConstants.Size(); i++)
  957. {
  958. defineConstants_.Push(defineConstants[i].GetString());
  959. }
  960. const JSONArray& projectTypeGuids = root["projectTypeGuids"].GetArray();
  961. for (unsigned i = 0; i < projectTypeGuids.Size(); i++)
  962. {
  963. String guid = projectTypeGuids[i].GetString();
  964. projectTypeGuids_.Push(ToString("{%s}", guid.CString()));
  965. }
  966. const JSONArray& importProjects = root["importProjects"].GetArray();
  967. for (unsigned i = 0; i < importProjects.Size(); i++)
  968. {
  969. importProjects_.Push(importProjects[i].GetString());
  970. }
  971. const JSONArray& libraryProjectZips = root["libraryProjectZips"].GetArray();
  972. for (unsigned i = 0; i < libraryProjectZips.Size(); i++)
  973. {
  974. String zipPath = libraryProjectZips[i].GetString();
  975. ReplacePathStrings(zipPath);
  976. libraryProjectZips_.Push(zipPath);
  977. }
  978. const JSONArray& transformFiles = root["transformFiles"].GetArray();
  979. for (unsigned i = 0; i < transformFiles.Size(); i++)
  980. {
  981. String transformFile = transformFiles[i].GetString();
  982. ReplacePathStrings(transformFile);
  983. transformFiles_.Push(transformFile);
  984. }
  985. const JSONArray& sharedReferences = root["sharedReferences"].GetArray();
  986. for (unsigned i = 0; i < sharedReferences.Size(); i++)
  987. {
  988. sharedReferences_.Push(sharedReferences[i].GetString());
  989. }
  990. targetFrameworkProfile_ = root["targetFrameworkProfile"].GetString();
  991. // iOS
  992. objcBindingApiDefinition_ = root["objcBindingApiDefinition"].GetString();
  993. ReplacePathStrings(objcBindingApiDefinition_);
  994. codesignEntitlements_ = root["codesignEntitlements"].GetString();
  995. ReplacePathStrings(codesignEntitlements_);
  996. infoPList_ = root["infoPList"].GetString();
  997. ReplacePathStrings(infoPList_);
  998. return true;
  999. }
  1000. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen, bool rewrite) : NETProjectBase(context, projectGen),
  1001. rewriteSolution_(rewrite)
  1002. {
  1003. }
  1004. NETSolution::~NETSolution()
  1005. {
  1006. }
  1007. bool NETSolution::Generate()
  1008. {
  1009. String slnPath = outputPath_ + name_ + ".sln";
  1010. GenerateSolution(slnPath);
  1011. return true;
  1012. }
  1013. void NETSolution::GenerateSolution(const String &slnPath)
  1014. {
  1015. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  1016. source += "# Visual Studio 14\n";
  1017. source += "VisualStudioVersion = 14.0.25420.1\n";
  1018. source += "MinimumVisualStudioVersion = 10.0.40219.1\n";
  1019. solutionGUID_ = projectGen_->GenerateUUID();
  1020. PODVector<NETCSProject*> depends;
  1021. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  1022. for (unsigned i = 0; i < projects.Size(); i++)
  1023. {
  1024. NETCSProject* p = projects.At(i);
  1025. const String& projectName = p->GetName();
  1026. const String& projectGUID = p->GetProjectGUID();
  1027. String CSharpProjectGUID = "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  1028. String ext = "csproj";
  1029. if (p->outputType_ == "Shared")
  1030. {
  1031. CSharpProjectGUID = "D954291E-2A0B-460D-934E-DC6B0785DB48";
  1032. ext = "shproj";
  1033. }
  1034. source += ToString("Project(\"{%s}\") = \"%s\", \"%s\\%s.%s\", \"{%s}\"\n",
  1035. CSharpProjectGUID.CString(), projectName.CString(), projectName.CString(),
  1036. projectName.CString(), ext.CString(), projectGUID.CString());
  1037. projectGen_->GetCSProjectDependencies(p, depends);
  1038. if (depends.Size())
  1039. {
  1040. source += "\tProjectSection(ProjectDependencies) = postProject\n";
  1041. for (unsigned j = 0; j < depends.Size(); j++)
  1042. {
  1043. source += ToString("\t{%s} = {%s}\n",
  1044. depends[j]->GetProjectGUID().CString(), depends[j]->GetProjectGUID().CString());
  1045. }
  1046. source += "\tEndProjectSection\n";
  1047. }
  1048. source += "EndProject\n";
  1049. }
  1050. source += "Global\n";
  1051. // SharedMSBuildProjectFiles
  1052. source += " GlobalSection(SharedMSBuildProjectFiles) = preSolution\n";
  1053. for (unsigned i = 0; i < projects.Size(); i++)
  1054. {
  1055. NETCSProject* p = projects.At(i);
  1056. if (p->outputType_ == "Shared")
  1057. {
  1058. for (unsigned j = 0; j < projects.Size(); j++)
  1059. {
  1060. NETCSProject* p2 = projects.At(j);
  1061. if (p == p2)
  1062. {
  1063. source += ToString(" %s\\%s.projitems*{%s}*SharedItemsImports = 13\n", p->name_.CString(), p->name_.CString(), p->projectGuid_.CString());
  1064. }
  1065. else
  1066. {
  1067. if (p2->sharedReferences_.Contains(p->name_))
  1068. {
  1069. source += ToString(" %s\\%s.projitems*{%s}*SharedItemsImports = 4\n", p->name_.CString(), p->name_.CString(), p2->projectGuid_.CString());
  1070. }
  1071. }
  1072. }
  1073. }
  1074. }
  1075. source += " EndGlobalSection\n";
  1076. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  1077. source += " Debug|Any CPU = Debug|Any CPU\n";
  1078. source += " Release|Any CPU = Release|Any CPU\n";
  1079. source += " Debug|iPhone = Debug|iPhone\n";
  1080. source += " Release|iPhone = Release|iPhone\n";
  1081. source += " EndGlobalSection\n";
  1082. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  1083. for (unsigned i = 0; i < projects.Size(); i++)
  1084. {
  1085. NETCSProject* p = projects.At(i);
  1086. if (p->outputType_ == "Shared")
  1087. continue;
  1088. String cpu = "Any CPU";
  1089. if (p->GetIsPlayerApp() && p->SupportsPlatform("ios"))
  1090. cpu = "iPhone";
  1091. source += ToString(" {%s}.Debug|%s.ActiveCfg = Debug|%s\n", p->GetProjectGUID().CString(), cpu.CString(), cpu.CString());
  1092. source += ToString(" {%s}.Debug|%s.Build.0 = Debug|%s\n", p->GetProjectGUID().CString(), cpu.CString(), cpu.CString());
  1093. source += ToString(" {%s}.Release|%s.ActiveCfg = Release|%s\n", p->GetProjectGUID().CString(), cpu.CString(), cpu.CString());
  1094. source += ToString(" {%s}.Release|%s.Build.0 = Release|%s\n", p->GetProjectGUID().CString(), cpu.CString(), cpu.CString());
  1095. if (cpu != "iPhone" && (p->SupportsPlatform("ios", false)))
  1096. {
  1097. source += ToString(" {%s}.Debug|iPhone.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  1098. source += ToString(" {%s}.Debug|iPhone.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  1099. source += ToString(" {%s}.Release|iPhone.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  1100. source += ToString(" {%s}.Release|iPhone.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  1101. }
  1102. }
  1103. source += " EndGlobalSection\n";
  1104. source += "EndGlobal\n";
  1105. if (!rewriteSolution_)
  1106. {
  1107. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  1108. if (fileSystem->Exists(slnPath))
  1109. return;
  1110. }
  1111. SharedPtr<File> output(new File(context_, GetSanitizedPath(slnPath), FILE_WRITE));
  1112. output->Write(source.CString(), source.Length());
  1113. output->Close();
  1114. }
  1115. bool NETSolution::Load(const JSONValue& root)
  1116. {
  1117. FileSystem* fs = GetSubsystem<FileSystem>();
  1118. name_ = root["name"].GetString();
  1119. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  1120. ReplacePathStrings(outputPath_);
  1121. // TODO: use poco mkdirs
  1122. if (!fs->DirExists(outputPath_))
  1123. fs->CreateDirsRecursive(outputPath_);
  1124. return true;
  1125. }
  1126. bool NETSolution::RegisterPackage(const String& package)
  1127. {
  1128. if (packages_.Find(package) != packages_.End())
  1129. return false;
  1130. packages_.Push(package);
  1131. return true;
  1132. }
  1133. NETProjectGen::NETProjectGen(Context* context) : Object(context),
  1134. rewriteSolution_(false)
  1135. {
  1136. }
  1137. NETProjectGen::~NETProjectGen()
  1138. {
  1139. }
  1140. NETCSProject* NETProjectGen::GetCSProjectByName(const String & name)
  1141. {
  1142. for (unsigned i = 0; i < projects_.Size(); i++)
  1143. {
  1144. if (projects_[i]->GetName() == name)
  1145. return projects_[i];
  1146. }
  1147. return nullptr;
  1148. }
  1149. bool NETProjectGen::GetCSProjectDependencies(NETCSProject* source, PODVector<NETCSProject*>& depends) const
  1150. {
  1151. depends.Clear();
  1152. const Vector<String>& references = source->GetReferences();
  1153. for (unsigned i = 0; i < projects_.Size(); i++)
  1154. {
  1155. NETCSProject* pdepend = projects_.At(i);
  1156. if (source == pdepend)
  1157. continue;
  1158. for (unsigned j = 0; j < references.Size(); j++)
  1159. {
  1160. if (pdepend->GetName() == references[j])
  1161. {
  1162. depends.Push(pdepend);
  1163. }
  1164. }
  1165. }
  1166. return depends.Size() != 0;
  1167. }
  1168. bool NETProjectGen::Generate()
  1169. {
  1170. solution_->Generate();
  1171. for (unsigned i = 0; i < projects_.Size(); i++)
  1172. {
  1173. if (!projects_[i]->Generate())
  1174. return false;
  1175. }
  1176. return true;
  1177. }
  1178. void NETProjectGen::SetRewriteSolution(bool rewrite)
  1179. {
  1180. rewriteSolution_ = rewrite;
  1181. if (solution_.NotNull())
  1182. solution_->SetRewriteSolution(rewrite);
  1183. }
  1184. bool NETProjectGen::IncludeProjectOnPlatform(const JSONValue& projectRoot, const String& platform)
  1185. {
  1186. const JSONArray& platforms = projectRoot["platforms"].GetArray();
  1187. if (!platforms.Size())
  1188. return true; // all platforms
  1189. String scriptPlatform = platform.ToLower();
  1190. for (unsigned i = 0; i < platforms.Size(); i++)
  1191. {
  1192. String platform = platforms[i].GetString().ToLower();
  1193. if (platform == "desktop")
  1194. {
  1195. if (scriptPlatform == "windows" || scriptPlatform == "macosx" || scriptPlatform == "linux")
  1196. return true;
  1197. return false;
  1198. }
  1199. if (platform == "android" && scriptPlatform != "android")
  1200. return false;
  1201. }
  1202. return true;
  1203. }
  1204. bool NETProjectGen::LoadProject(const JSONValue &root)
  1205. {
  1206. solution_ = new NETSolution(context_, this, rewriteSolution_);
  1207. solution_->Load(root["solution"]);
  1208. const JSONValue& jprojects = root["projects"];
  1209. if (!jprojects.IsArray() || !jprojects.Size())
  1210. return false;
  1211. for (unsigned i = 0; i < jprojects.Size(); i++)
  1212. {
  1213. const JSONValue& jproject = jprojects[i];
  1214. if (!jproject.IsObject())
  1215. return false;
  1216. JSONArray platforms = jproject["platforms"].GetArray();
  1217. if (platforms.Size())
  1218. {
  1219. bool found = false;
  1220. for (unsigned j = 0; j < platforms.Size(); j++)
  1221. {
  1222. if (GetSupportsPlatform(platforms[j].GetString()))
  1223. {
  1224. found = true;
  1225. break;
  1226. }
  1227. }
  1228. if (!found)
  1229. {
  1230. continue;
  1231. }
  1232. }
  1233. // HACK! Do not generate AtomicNETService in the AtomicProject solution
  1234. if (jproject["name"].GetString() == "AtomicNETService" && atomicProjectPath_.Length())
  1235. {
  1236. continue;
  1237. }
  1238. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  1239. if (!csProject->Load(jproject))
  1240. return false;
  1241. projects_.Push(csProject);
  1242. }
  1243. return true;
  1244. }
  1245. bool NETProjectGen::LoadJSONProject(const String& jsonProjectPath)
  1246. {
  1247. SharedPtr<File> file(new File(context_));
  1248. if (!file->Open(GetSanitizedPath(jsonProjectPath)))
  1249. return false;
  1250. String json;
  1251. file->ReadText(json);
  1252. JSONValue jvalue;
  1253. if (!JSONFile::ParseJSON(json, jvalue))
  1254. return false;
  1255. return LoadProject(jvalue);
  1256. }
  1257. bool NETProjectGen::LoadAtomicProject(const String& atomicProjectPath)
  1258. {
  1259. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  1260. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  1261. String pathname, filename, ext;
  1262. SplitPath(atomicProjectPath, pathname, filename, ext);
  1263. if (ext == ".atomic")
  1264. {
  1265. atomicProjectPath_ = AddTrailingSlash(pathname);
  1266. }
  1267. else
  1268. {
  1269. atomicProjectPath_ = AddTrailingSlash(atomicProjectPath);
  1270. }
  1271. // Do we have a loaded project?
  1272. if (Project* project = tsystem->GetProject())
  1273. {
  1274. // If so, use loaded project settings
  1275. projectSettings_ = project->GetProjectSettings();
  1276. }
  1277. else
  1278. {
  1279. // Nope, load them up
  1280. projectSettings_ = SharedPtr<ProjectSettings>(new ProjectSettings(context_));
  1281. projectSettings_->Load(atomicProjectPath_ + "Settings/Project.json");
  1282. }
  1283. #ifdef ATOMIC_DEV_BUILD
  1284. JSONValue netJSON;
  1285. SharedPtr<File> netJSONFile(new File(context_));
  1286. String atomicNETProject = tenv->GetRootSourceDir() + "Script/AtomicNET/AtomicNETProject.json";
  1287. if (!netJSONFile->Open(GetSanitizedPath(atomicNETProject)))
  1288. return false;
  1289. String netJSONString;
  1290. netJSONFile->ReadText(netJSONString);
  1291. if (!JSONFile::ParseJSON(netJSONString, netJSON))
  1292. return false;
  1293. #endif
  1294. #ifdef ATOMIC_DEV_BUILD
  1295. String projectPath = tenv->GetRootSourceDir() + "Script/AtomicNET/AtomicProject.json";
  1296. #else
  1297. String projectPath = tenv->GetAtomicNETRootDir() + "Build/Projects/AtomicProject.json";
  1298. #endif
  1299. SharedPtr<File> file(new File(context_));
  1300. if (!file->Open(GetSanitizedPath(projectPath)))
  1301. return false;
  1302. String json;
  1303. file->ReadText(json);
  1304. json.Replace("$ATOMIC_PROJECT_NAME$", projectSettings_->GetName());
  1305. JSONValue jvalue;
  1306. if (!JSONFile::ParseJSON(json, jvalue))
  1307. return false;
  1308. #ifdef ATOMIC_DEV_BUILD
  1309. // patch projects
  1310. JSONArray netProjects = netJSON["projects"].GetArray();
  1311. JSONArray projects = jvalue["projects"].GetArray();
  1312. for (unsigned i = 0; i < projects.Size(); i++)
  1313. {
  1314. netProjects.Push(JSONValue(projects[i].GetObject()));
  1315. }
  1316. jvalue["projects"] = netProjects;
  1317. return LoadProject(jvalue);
  1318. #else
  1319. return LoadProject(jvalue);
  1320. #endif
  1321. }
  1322. void NETProjectGen::SetSupportedPlatforms(const StringVector& platforms)
  1323. {
  1324. projectSettings_ = SharedPtr<ProjectSettings>(new ProjectSettings(context_));
  1325. for (unsigned i = 0; i < platforms.Size(); i++)
  1326. {
  1327. projectSettings_->AddSupportedPlatform(platforms[i]);
  1328. }
  1329. }
  1330. bool NETProjectGen::GetSupportsPlatform(const String& platform) const
  1331. {
  1332. // If no project platform settings are loaded, always supports
  1333. if (projectSettings_.Null())
  1334. {
  1335. return true;
  1336. }
  1337. return projectSettings_->GetSupportsPlatform(platform);
  1338. }
  1339. bool NETProjectGen::GetRequiresNuGet()
  1340. {
  1341. if (solution_.Null())
  1342. {
  1343. ATOMIC_LOGERROR("NETProjectGen::GetRequiresNuGet() - called without a solution loaded");
  1344. return false;
  1345. }
  1346. return solution_->GetPackages().Size() != 0;
  1347. }
  1348. String NETProjectGen::GenerateUUID()
  1349. {
  1350. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  1351. Poco::UUID uuid(generator.create()); // time based
  1352. return String(uuid.toString().c_str()).ToUpper();
  1353. }
  1354. }