394 lines
No EOL
16 KiB
C#
394 lines
No EOL
16 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Helper methods on Sharpmake objects.
|
|
/// </summary>
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers all projects that inherits from the specified type.
|
|
/// </summary>
|
|
public static void RegisterProjectsOfType<TProject>(this Solution.Configuration conf, ITarget target, Func<Type, bool> where = null)
|
|
{
|
|
foreach (var type in GetInheritors<TProject>(where))
|
|
{
|
|
// Specific UAF Begin
|
|
if (type.GetCustomAttributes(typeof(Generate), false).Any())
|
|
{
|
|
conf.AddProject(type, target);
|
|
}
|
|
// Specific UAF End
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers all projects that inherits from the specified type.
|
|
/// </summary>
|
|
public static void GenerateSolutionsOfType<TSolution>(this Arguments arguments, Func<Type, bool> where = null)
|
|
{
|
|
foreach (var type in GetInheritors<TSolution>(where))
|
|
{
|
|
arguments.Generate(type);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all the classes that inherit from the specified type.
|
|
/// </summary>
|
|
public static IEnumerable<Type> GetInheritors<T>(Func<Type, bool> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all the instances of classes inheriting from a specified type.
|
|
/// </summary>
|
|
public static IEnumerable<T> GetInstances<T>(Func<Type, bool> 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<Engine.Optimization>());
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="manifestFilePath">Manifest File path.</param>
|
|
/// <param name="applicationName">Application to be embeded with the manifest</param>
|
|
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));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a shortcut to a file
|
|
/// </summary>
|
|
/// <param name="shortcutName">the name of the shortcut</param>
|
|
/// <param name="shortcutPath">the path of the shortcut</param>
|
|
/// <param name="targetFileLocation">the path of the target</param>
|
|
/// <param name="description">the description of the shortcut</param>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a nuget package by passing a NugetDependency object
|
|
/// </summary>
|
|
/// <param name="packageReferences"></param>
|
|
/// <param name="dependency"></param>
|
|
public static void Add(this PackageReferences packageReferences, NuGetDependency dependency)
|
|
{
|
|
packageReferences.Add(dependency.Name, dependency.Version);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Classes implementing this interface will be called before the solutions generation.
|
|
/// </summary>
|
|
internal interface IAdditionalFileGenerator
|
|
{
|
|
void Generate();
|
|
}
|
|
|
|
public static class AdditionalFileGenerator
|
|
{
|
|
public static void GenerateAll()
|
|
{
|
|
foreach (var generator in Utilities.GetInstances<IAdditionalFileGenerator>())
|
|
{
|
|
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<string>();
|
|
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);
|
|
}
|
|
}
|
|
} |