using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text.RegularExpressions; using JustDance.Engine; using JustDance.Tools; using Sharpmake; namespace JustDance { /// /// Helper methods on Sharpmake objects. /// public static class Utilities { public static string GetDevEnvString(this DevEnv env) { switch (env) { case DevEnv.vs2015: return "2015"; case DevEnv.vs2017: return "2017"; case DevEnv.vs2019: return "2019"; case DevEnv.vs2022: return "2022"; default: return ""; } } public static ITarget[] GetDefaultTargets() { return new ITarget[] { new Target( Platform.win64 | Platform.anycpu, DevEnv.vs2017, Sharpmake.Optimization.Debug | Sharpmake.Optimization.Release, OutputType.Dll, framework: DotNetFramework.v4_7) }; } public static void RegisterWpf(this Project.Configuration configuration) { configuration.ReferencesByName.Add("System.Xaml"); configuration.ReferencesByName.Add("WindowsBase"); configuration.ReferencesByName.Add("PresentationCore"); configuration.ReferencesByName.Add("PresentationFramework"); } public static void RegisterPerforce(this Project.Configuration configuration) { configuration.ReferencesByPath.Add(Externs.P4api); } public static void RegisterWinForms(this Project.Configuration configuration) { configuration.ReferencesByName.Add("System.Drawing"); configuration.ReferencesByName.Add("System.Windows.Forms"); } public static void RegisterWindowsFormsIntegration(this Project.Configuration configuration) { configuration.ReferencesByName.Add("WindowsFormsIntegration"); } public static void RegisterEngineWrapper(this Project.Configuration conf, Target target) { conf.ReferencesByPath.Add(UAF_EngineWrapper.DllPath); var csharpProject = conf.Project as CSharpProject; if (csharpProject != null) { string plainArch = conf.Platform == Platform.win32 ? "32" : "64"; string kinectArch = conf.Platform == Platform.win32 ? "x86" : "amd64"; string nvttSubFolder = conf.Platform == Platform.win32 ? "" : "x64/"; csharpProject.AdditionalContentCopyIfNewer.Add(BaseCppProject.ExternPath + @"NVIDIA GPU Computing Toolkit/cudart" + plainArch + "_80.dll"); csharpProject.AdditionalContentCopyIfNewer.Add(BaseCppProject.ExternPath + @"NVIDIA GPU Computing Toolkit/cudart" + plainArch + "_101.dll"); csharpProject.AdditionalContentCopyIfNewer.Add(BaseCppProject.ExternPath + @"NVIDIA Texture Tools 2/lib/Release2015/" + nvttSubFolder + "nvtt" + plainArch + "_vs15.dll"); csharpProject.AdditionalContentCopyIfNewer.Add(BaseCppProject.ExternPath + @"NVIDIA Texture Tools 2/lib/Release2017/" + nvttSubFolder + "nvtt" + plainArch + "_vs17.dll"); csharpProject.AdditionalContentCopyIfNewer.Add(BaseCppProject.ExternPath + @"KinectPC_Sdk/v1.8/lib/" + kinectArch + "/Kinect10.dll"); } } /// /// Registers all projects that inherits from the specified type. /// public static void RegisterProjectsOfType(this Solution.Configuration conf, ITarget target, Func where = null) { foreach (var type in GetInheritors(where)) { // Specific UAF Begin if (type.GetCustomAttributes(typeof(Generate), false).Any()) { conf.AddProject(type, target); } // Specific UAF End } } /// /// Registers all projects that inherits from the specified type. /// public static void GenerateSolutionsOfType(this Arguments arguments, Func where = null) { foreach (var type in GetInheritors(where)) { arguments.Generate(type); } } /// /// Returns all the classes that inherit from the specified type. /// public static IEnumerable GetInheritors(Func where = null) { Type baseType = typeof(T); var result = baseType.Assembly.GetTypes().Where(assemblyType => baseType.IsAssignableFrom(assemblyType) && assemblyType != baseType && !assemblyType.IsAbstract); if (where != null) { result = result.Where(where); } return result; } /// /// Returns all the instances of classes inheriting from a specified type. /// public static IEnumerable GetInstances(Func where = null) { Type baseType = typeof(T); var result = baseType.Assembly.GetTypes().Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract && type.IsClass); if (where != null) { result = result.Where(where); } return result.Select(type => (T)Activator.CreateInstance(type)); } public static bool IsIn(this Platform val, params Platform[] values) { return values.Any(x => x.HasFlag(val)); } public static bool Contains(this string source, string toCheck, StringComparison comp) { return source.IndexOf(toCheck, comp) >= 0; } public static bool Is(this Project.Configuration configuration, Platform platform) { return platform.HasFlag(configuration.Platform); } public static bool Is(this Project.Configuration configuration, Engine.Optimization optimization) { return optimization.HasFlag(configuration.Target.GetFragment()); } public static bool Is(this Project.Configuration configuration, Platform platform, Engine.Optimization optimization) { return configuration.Is(platform) && configuration.Is(optimization); } public static bool Is(this Project.Configuration configuration, Engine.Optimization optimization, Platform platform) { return configuration.Is(platform) && configuration.Is(optimization); } public static Project.Configuration If(this Project.Configuration configuration, bool condition) { return condition ? configuration : new Project.Configuration(); } public static Project.Configuration If(this Project.Configuration configuration, Platform platform) { return configuration.Is(platform) ? configuration : new Project.Configuration(); } public static Project.Configuration IfNot(this Project.Configuration configuration, Platform platform) { return !configuration.Is(platform) ? configuration : new Project.Configuration(); } public static Project.Configuration If(this Project.Configuration configuration, Engine.Optimization optimization) { return configuration.Is(optimization) ? configuration : new Project.Configuration(); } public static Project.Configuration IfNot(this Project.Configuration configuration, Engine.Optimization optimization) { return !configuration.Is(optimization) ? configuration : new Project.Configuration(); } public static Project.Configuration If(this Project.Configuration configuration, Engine.Optimization optimization, Platform platform) { return configuration.Is(optimization, platform) ? configuration : new Project.Configuration(); } public static Project.Configuration IfNot(this Project.Configuration configuration, Engine.Optimization optimization, Platform platform) { return !configuration.Is(optimization, platform) ? configuration : new Project.Configuration(); } public static Project.Configuration If(this Project.Configuration configuration, Platform platform, Engine.Optimization optimization) { return configuration.Is(platform, optimization) ? configuration : new Project.Configuration(); } public static Project.Configuration IfNot(this Project.Configuration configuration, Platform platform, Engine.Optimization optimization) { return !configuration.Is(platform, optimization) ? configuration : new Project.Configuration(); } public static string ReplaceFirst(this string text, string search, string replace) { int pos = text.IndexOf(search, StringComparison.Ordinal); if (pos < 0) { return text; } return text.Substring(0, pos) + replace + text.Substring(pos + search.Length); } public static bool IsWindows(this Platform platform) { return platform == Platform.win32 || platform == Platform.win64; } public static void AddPostBuildCopy(this Project.Configuration configuration, string source) { configuration.EventPostBuild.Add(String.Format(@"xcopy /f /Y /C /S ""{0}"" ""{1}""", source, "$(TargetDir)")); } public static void AddPostBuildCopy(this Project.Configuration configuration, string source, string destination) { configuration.EventPostBuild.Add(String.Format(@"xcopy /f /Y /C /S ""{0}"" ""{1}""", source, destination)); } public static void AddPostBuildCopy(this Project.Configuration configuration, XCopyCommand command) { configuration.EventPostBuild.Add(command.ToString()); } public static void AddPostBuildCopy(this Project.Configuration configuration, string source, string destination, string[] arguments = null) { if (arguments == null) { arguments = new[] { "/f", "/Y", "/C", "/S", "/D" }; } configuration.EventPostBuild.Add(String.Format(@"xcopy {0} ""{1}"" ""{2}""", string.Join(" ", arguments), source, destination)); } public static void AddPostBuildDirectoryCopy(this Project.Configuration configuration, string sourceDirectory, string destinationDirectory) { // we just check if its English that is set as UI language. var folderOption = Regex.Match(CultureInfo.CurrentUICulture.Name, @"\s*(?:(en)\s*-)", RegexOptions.IgnoreCase).Groups[1].Value.Equals("en", StringComparison.InvariantCultureIgnoreCase) ? "D" : "R"; configuration.EventPostBuild.Add(String.Format(@"echo {0} | xcopy /f /Y /C /S ""{1}"" ""{2}""", folderOption, sourceDirectory, destinationDirectory)); } /// /// /// /// /// Manifest File path. /// Application to be embeded with the manifest public static void AddApplicationManifest(this Project.Configuration configuration, string manifestFilePath, string applicationName) { configuration.EventPostBuild.Add(String.Format(@"xcopy /f /Y /C /S ""{0}"" ""{1}""", manifestFilePath, "$(TargetDir)")); // make call to mt.exe for embeding the manifest with the application string mt_kits_8_1_path = @"C:\Program Files (x86)\Windows Kits\8.1\bin\x64"; string manifestFileName = System.IO.Path.GetFileName(manifestFilePath); string applicationFilePath = @"$(TargetDir)" + "\\" + applicationName; configuration.EventPostBuild.Add(String.Format(@" ""{0}""\mt.exe -manifest ""{1}"" -outputresource:""{2}"";#1", mt_kits_8_1_path, manifestFileName, applicationFilePath)); } /// /// Create a shortcut to a file /// /// the name of the shortcut /// the path of the shortcut /// the path of the target /// the description of the shortcut public static void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation, string description = "") { string shortcutLocation = Path.Combine(Path.GetFullPath(shortcutPath), shortcutName + ".lnk"); Type shellType = Type.GetTypeFromProgID("WScript.Shell"); dynamic shell = Activator.CreateInstance(shellType); dynamic shortcut = shell.CreateShortcut(shortcutLocation); shortcut.Description = description; shortcut.IconLocation = Path.GetFullPath(targetFileLocation); shortcut.TargetPath = Path.GetFullPath(targetFileLocation); shortcut.Save(); } /// /// Add a nuget package by passing a NugetDependency object /// /// /// public static void Add(this PackageReferences packageReferences, NuGetDependency dependency) { packageReferences.Add(dependency.Name, dependency.Version); } } /// /// Classes implementing this interface will be called before the solutions generation. /// internal interface IAdditionalFileGenerator { void Generate(); } public static class AdditionalFileGenerator { public static void GenerateAll() { foreach (var generator in Utilities.GetInstances()) { generator.Generate(); } } } public class XCopyCommand { public string Source { get; set; } public string Destination { get; set; } public bool CopyIfNewer { get; set; } public bool IgnoreErrors { get; set; } public bool SuppressPrompting { get; set; } public bool CopySubdirectories { get; set; } public XCopyCommand(string source, string destination = "$(TargetDir)") { Source = source; Destination = destination; CopyIfNewer = true; IgnoreErrors = true; SuppressPrompting = true; CopySubdirectories = true; } public static explicit operator XCopyCommand(string source) { return new XCopyCommand(source); } public override string ToString() { var arguments = new List(); arguments.Add("/f"); if (SuppressPrompting) { arguments.Add("/Y"); } if (IgnoreErrors) { arguments.Add("/C"); } if (CopySubdirectories) { arguments.Add("/S"); } if (CopyIfNewer) { arguments.Add("/D"); } return String.Format(@"xcopy {0} ""{1}"" ""{2}""", string.Join(" ", arguments), Source, Destination); } } }