NETProjectGen.cpp 47 KB

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