JD2022-TU1/main/tools/apps/TeaBox/Script/Build/Data/BuildDataMacros.cs

264 lines
12 KiB
C#

using System;
using System.ComponentModel;
using System.IO;
using System.Text.RegularExpressions;
using JD.Core.ProjectManaging;
using TeaBox.Data;
using TeaBox.Gui.PropertyGrid;
using TeaBox.Macros;
using TeaBox.UAF;
namespace TeaBox.Scripts.BuildData
{
[Module(Icon = "build")]
[DisplayName(@"Build (Data)")]
public class BuildDataMacros
{
[Macro(ThreadAffinity.ExecuteAsync, Icon = "build.png")]
[Description(@"Generates the Game Release (region Specific) data bundles")]
[DisplayName(@"Build Game Bundles")]
public static void GenerateGameBundle(IExecutionContext context,
[SingleEnumEditor]
[DefaultValue(Platform.win64)]
Platform platform,
Region region,
[Description(@"Default sku scene path is the platform + region specific sku scene file in the directory World\SkuScenes")]
[BrowseForPath(InitialDirectory = @"World\SkuScenes", FileFilter = "SkuScene files (*.isc)|*.isc|All files (*.*)|*.*")]
DataRelativeFileName skuScenePath,
[Description(@"Represents the output directory for the data bundle creation, default output directory is \data\output")]
[BrowseForPath(InitialDirectory = @"output")]
DataRelativeFolder outputDirectory,
[Category(@"Advanced")]int dataVersion,
[Category(@"Advanced")]bool stopOnErrors,
[Category(@"Advanced")]bool binaryCooking,
[Category(@"Advanced")]bool clearCacheDirectory,
[Category(@"Advanced"), Browsable(false)]string skuSceneName)
{
var packagerCommand = new PackagerCommand
{
Platform = platform,
Region = region,
SkuScenePath = skuScenePath.Value,
BundleType = "GameBundle",
TaskListType = PackagerTaskList.Local,
DataVersion = dataVersion,
StopOnErrors = stopOnErrors,
BinaryCooking = binaryCooking,
OutputPath = !outputDirectory.Value.IsEmpty() ? outputDirectory.FullPath : "",
ClearCacheDirectory = clearCacheDirectory
};
if (!string.IsNullOrEmpty(skuSceneName))
{
packagerCommand.SkuSceneName = skuSceneName;
}
packagerCommand.Execute(context);
}
[Macro(ThreadAffinity.ExecuteAsync, Icon = "build")]
[Description(@"Generates the data Bundles for DLC Builds")]
[DisplayName(@"Build DLC Bundles")]
public static void GenreateDlcBundles(IExecutionContext context,
[SingleEnumEditor]
[DefaultValue(Platform.win64)]
Platform platform,
DataRelativeFileName mapListFilePath,
[Description(@"Represents the output directory for the data bundle creation, default output directory is \data\output")]
[BrowseForPath(InitialDirectory = @"output")]
DataRelativeFolder outputDirectory,
[Category(@"Advanced")]int dataVersion,
[Category(@"Advanced")]bool stopOnErrors,
[Category(@"Advanced")]bool binaryCooking,
[Category(@"Advanced")]bool clearCacheDirectory)
{
PackagerCommand packagerCommand = new PackagerCommand
{
Platform = platform,
BundleType = "DLC",
TaskListType = PackagerTaskList.Dlc,
MapListFilePath = mapListFilePath.FullPath,
OutputPath = !outputDirectory.Value.IsEmpty() ? outputDirectory.FullPath : "",
DataVersion = dataVersion,
StopOnErrors = stopOnErrors,
BinaryCooking = binaryCooking,
ClearCacheDirectory = clearCacheDirectory
};
packagerCommand.Execute(context);
}
[Macro(ThreadAffinity.ExecuteAsync, Icon = "build.png")]
[Description(@"Generates the Patch data bundles")]
[DisplayName(@"Build Patch")]
public static void Patch(IExecutionContext context,
[SingleEnumEditor]
[DefaultValue(Platform.win64)]
Platform platform,
Region region,
[Description("To be specified only when the selected platform is Ps4 or Durango")]
[BrowseForPath(InitialDirectory = @"World\SkuScenes", FileFilter = "SkuScene files (*.isc)|*.isc|All files (*.*)|*.*")]
DataRelativeFileName skuScenePath,
[Description(@"The bundle directory, containing the bundles against which the patch has to be created")]
DataRelativeFolder dataBundlePath,
[Description(@"Represents the output directory for the data bundle creation, default output directory is \data\output")]
[BrowseForPath(InitialDirectory = @"output")]
DataRelativeFolder outputDirectory,
[Category(@"Advanced")]int rebaseEngineVersion,
[Category(@"Advanced")]int dataVersion,
[Category(@"Advanced")]bool stopOnErrors,
[Category(@"Advanced")]bool binaryCooking,
[Category(@"Advanced")]bool clearCacheDirectory)
{
PackagerCommand packagerCommand = new PackagerCommand
{
Platform = platform,
BundleType = "Patch",
TaskListType = PackagerTaskList.Patch,
Region = region,
SkuScenePath = skuScenePath.Value,
OutputPath = !outputDirectory.Value.IsEmpty() ? outputDirectory.FullPath : "",
RebaseEngineVersion = rebaseEngineVersion,
DataVersion = dataVersion,
StopOnErrors = stopOnErrors,
BinaryCooking = binaryCooking,
ClearCacheDirectory = clearCacheDirectory,
BundleDirectory = dataBundlePath.FullPath
};
packagerCommand.Execute(context);
}
[Macro(ThreadAffinity.ExecuteAsync, Icon = "build.png")]
[Description(@"Generates the Online data bundles")]
[DisplayName(@"Build Online Bundles")]
[AvailableInBranch(TeaBoxBranch.Main)]
public static void GenerateOnlineBundles(IExecutionContext context,
OnlineDataBundleType onlineBundleType,
[BrowseForPath(InitialDirectory = @"World\MAPS", FileFilter = "SkuScene files (*.isc)|*.isc|All files (*.*)|*.*")]
DataRelativeFileName skuScenePath,
[Description(@"Represents the output directory for the data bundle creation, default output directory is \data\output")]
[SingleEnumEditor]
[DefaultValue(Platform.win64)]
Platform platform,
DataRelativeFolder outputDirectory,
[Category(@"Advanced")]int dataVersion,
[Category(@"Advanced")]bool stopOnErrors,
[Category(@"Advanced")]bool binaryCooking,
[Category(@"Advanced")]bool clearCacheDirectory)
{
PackagerCommand packagerCommand = new PackagerCommand
{
BundleType = onlineBundleType.ToString(),
TaskListType = PackagerTaskList.OnlinePackage,
Platform = platform,
OutputPath = !outputDirectory.Value.IsEmpty() ? outputDirectory.FullPath : "",
DataVersion = dataVersion,
StopOnErrors = stopOnErrors,
BinaryCooking = binaryCooking,
ClearCacheDirectory = clearCacheDirectory,
SkuScenePath = skuScenePath.Value,
};
packagerCommand.Execute(context);
}
[Macro(ThreadAffinity.ExecuteAsync, Icon = "build.png")]
[Description(@"Generates the CheckSum File for the data bundle")]
[DisplayName(@"Generate Bundle Checksum")]
[Category(@"Build (Data)\BundleValidation")]
public static string GenerateCheckSumFile(IExecutionContext context,
DataRelativeFolder dataBundlePath,
[Description(@"Represents the output directory for the data bundle creation, default output directory is \data\output\Checksums")]
DataRelativeFolder outputDirectory)
{
PackagerCommand packagerCommand = new PackagerCommand
{
BundleDirectory = dataBundlePath.FullPath,
OutputChecksumFileName = Path.GetFileNameWithoutExtension(dataBundlePath.Value),
OutputPath = !outputDirectory.Value.IsEmpty() ? outputDirectory.FullPath : "",
TaskListType = PackagerTaskList.BundleChecksum,
};
packagerCommand.Execute(context);
var resultFilePath = Path.Combine(packagerCommand.OutputPath, packagerCommand.OutputChecksumFileName) + ".txt";
return resultFilePath;
}
[Macro(ThreadAffinity.ExecuteAsync, Icon = "build.png")]
[Description(@"Performs the bundle comparison and presents the result as a HTML report ")]
[DisplayName(@"Compare Data Bundles")]
[Category(@"Build (Data)\BundleValidation")]
public static void CompareBundles(IExecutionContext context,
[BrowseForPath(InitialDirectory = @"output")]
DataRelativeFolder newDataBundlePath,
[BrowseForPath(InitialDirectory = @"output")]
DataRelativeFolder oldDataBundlePath)
{
var checksumOutputPath = Path.Combine(DataPath.GetAbsolutePath("output"), "Checksums");
var bundleComparisionReportGenerator = new BundleComparisionReportGenerator
{
NewBundlleChecksumFilePath = GenerateCheckSumFile(context, newDataBundlePath, new DataRelativeFolder(checksumOutputPath)),
OldBundlleChecksumFilePath = GenerateCheckSumFile(context, oldDataBundlePath, new DataRelativeFolder(checksumOutputPath)),
NewBundlePath = newDataBundlePath.FullPath,
OldBundlePath = oldDataBundlePath.FullPath
};
bundleComparisionReportGenerator.WorkingDirectory = Path.Combine(DataPath.GetAbsolutePath("output"), "BundleComparision");
bundleComparisionReportGenerator.Run(context);
}
[Macro(ThreadAffinity.ExecuteAsync, Icon = "build.png")]
[Description(@"Replace a set of selected files in the already existing bundle")]
[DisplayName(@"Replace Files in Bundles")]
[Category(@"Build (Data)")]
public static void InjectFileInBundles(IExecutionContext context,
[Description(@"specify the file paths for the all the files to be injected, each path mentioned on a new line")]
string files,
[Description(@"specify the Bundle Diretory where the files are to be replaced")]
[BrowseForPath(InitialDirectory = @"output")]
DataRelativeFolder replacementBundleDirectory)
{
var platformName = GetPlatformFromBundleName(replacementBundleDirectory);
Platform platform;
if (platformName == null)
{
context.LogError(@"The platform name cannot be determined from the files in the bundle directory");
return;
}
if (platformName.Equals("wiiu", StringComparison.OrdinalIgnoreCase))
{
platform = Platform.Cafe;
}
else if(!Enum.TryParse(platformName, true, out platform))
{
context.LogError(@"The platform name extracted from the file in directory is {0}, which is not present in the Platform Enum", platformName);
return;
}
var packagerCommand = new PackagerCommand
{
Platform = platform,
BundleDirectory = replacementBundleDirectory.FullPath,
FileList = files.Replace('\n', ','),
TaskListType = PackagerTaskList.ReplaceFilesInBundles,
OutputPath = Path.Combine(DataPath.GetAbsolutePath("output"), "ReplacedBundles_" + platform),
};
packagerCommand.Execute(context);
}
private static string GetPlatformFromBundleName(RelativePath injectionBundleDirectory)
{
var directory = new System.IO.DirectoryInfo(injectionBundleDirectory.FullPath);
var pattern = new Regex(@"[0-9a-zA-Z]+_(?<platform>[0-9a-zA-Z]+)\.ipk");
var filesInDirectory = directory.GetFiles();
foreach (var file in filesInDirectory)
{
var match = pattern.Match(file.Name);
if (match.Success)
{
return match.Groups["platform"].ToString();
}
}
return null;
}
}
}