JD2022-TU1/main/tools/apps/TeaBox/Script/Build/Version/MakePackageCommand.cs

262 lines
No EOL
10 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BetaVersionLauncher;
using TeaBox.Code;
using TeaBox.Macros;
using Ubisoft;
namespace TeaBox.BuildVersion
{
public abstract class MakePackageCommand : ITask
{
internal GameType GameType { get; set; }
internal string PkgTool { get; set; }
protected string PkgFormat { get; set; }
protected string LayoutPath { get; set; }
public virtual string WorkingDirectory { get; set; }
public IExecutionContext Context { get; set; }
public Platform Platform { get; set; }
public string Branch { get; set; }
public Region Region { get; set; }
public string Sku { get; set; }
public DevEnvBuildVariant Variant { get; set; }
public virtual string OutputDirectory { get; set; }
public EngineExecutable EngineExecutable { get; set; }
public GameOnlineEnvironment GameOnlineEnvironment { get; set; }
/// <summary>
/// Disc purpose. Development or submission
/// </summary>
public OutputFormat OutputFormat { get; set; }
public string DiscLayout { get; set; }
public string EnvironmentFile { get; set; }
protected virtual string CodeWorkingDirectory => WorkingDirectory;
protected virtual string DataWorkingDirectory => WorkingDirectory;
protected virtual string ResourcesDirectory => Path.Combine(WorkingDirectory, "Resources");
public virtual string DefaultDiscLayout => string.Empty;
public string DataChangelist { get; set; }
protected abstract IEnumerable<string> GetFilesList();
/// <summary>
/// The list of folders to copy in the working directory of the version.
/// </summary>
protected virtual IEnumerable<string> GetFoldersList()
{
yield break;
}
/// <summary>
/// Execute the command
/// </summary>
/// <param name="context"></param>
public void Execute(IExecutionContext context)
{
CleanDiscWorkingDirectory();
PackageDataAssembly();
SetSpecificPackageSkuFiles();
PrePackage();
SetPackageOnlineEnvironment();
GeneratePackage();
}
/// <summary>
/// Clean the folders and files found into the temporary working directory
/// </summary>
protected virtual void CleanDiscWorkingDirectory()
{
// Clean previous packaged data if it exists
Context.AddStep("Cleaning existing data...", stepcontext =>
{
try
{
stepcontext.LogMessage("== Clean step ==");
stepcontext.LogMessage($"Starting to clean folder {WorkingDirectory}");
if (Directory.Exists(WorkingDirectory))
{
Directory.ForceDelete(WorkingDirectory);
}
}
catch
{
throw new MacroException();
}
stepcontext.LogMessage("== Create folder hierarchy ==");
stepcontext.LogMessage($"Starting to create folder {WorkingDirectory}");
Directory.CreateDirectory(WorkingDirectory);
});
}
/// <summary>
/// Retrieve engine and data files in a unique place
/// </summary>
private void PackageDataAssembly()
{
Context.AddStep("Copying new files...", PackageDataAssemblyImpl);
}
protected virtual void CopyDataFromOutput(IStep stepcontext)
{
stepcontext.LogMessage("== Data copy step ==");
stepcontext.LogMessage($"Starting to copy data from {GameConstants.BundleOutputPath} to {DataWorkingDirectory}");
RoboCopy.ReplicateDirectory(GameConstants.BundleOutputPath, DataWorkingDirectory, stepcontext, deleteMissingFiles: false);
}
protected virtual void CopyExecutableFromOutput(IStep stepcontext)
{
stepcontext.LogMessage("== Executables copy step ==");
stepcontext.LogMessage($"Starting to copy engine from {EngineExecutable.DeliveredDirectory} to {CodeWorkingDirectory}");
RoboCopy.ReplicateDirectory(EngineExecutable.DeliveredDirectory, CodeWorkingDirectory, stepcontext, deleteMissingFiles: false);
}
protected virtual void CopyDataMasterToDiscWorkingDirectory(IStep stepcontext)
{
stepcontext.LogMessage("== Master data copy step ==");
stepcontext.LogMessage($"Starting to copy data from {GameConstants.GetMasterBundle(Platform, Region)} to {DataWorkingDirectory}");
RoboCopy.ReplicateDirectory(GameConstants.GetMasterBundle(Platform, Region), DataWorkingDirectory, stepcontext, deleteMissingFiles: false);
}
protected virtual void CopyPackageMasterToDiscWorkingDirectory(IStep stepcontext)
{
stepcontext.LogMessage("== Master data copy step ==");
stepcontext.LogMessage($"Starting to copy data from {GameConstants.GetMasterPackage(Platform, Region, Variant)} to {DataWorkingDirectory}");
RoboCopy.ReplicateDirectory(GameConstants.GetMasterPackage(Platform, Region, Variant), WorkingDirectory, stepcontext, deleteMissingFiles: false);
}
protected virtual void CopyPatchDataToDiscWorkingDirectory(IStep stepcontext)
{
stepcontext.LogMessage("== ArgumentsForPatch Data copy step ==");
stepcontext.LogMessage($"Starting to copy data from {GameConstants.PatchOutputPath} to {DataWorkingDirectory}");
Func<string, bool> keepFunc = file =>
{
var patterns = new[] { ".csv", ".txt" };
return !patterns.Any(file.Replace('\\', '/').Contains);
};
RoboCopy.ReplicateDirectory(GameConstants.PatchOutputPath, DataWorkingDirectory, stepcontext, deleteMissingFiles: false, @where: keepFunc);
}
/// <summary>
/// Copy or rename specifics sku files in the package directory
/// </summary>
private void SetSpecificPackageSkuFiles()
{
Context.AddStep("Setting specific sku files...", stepcontext =>
{
Context.LogMessage("== Specific sku files step ==");
Context.LogMessage("Start to set specific sku files for this package");
SetSpecificPackageSkuFilesImpl();
});
}
/// <summary>
/// Change the default (UAT) environment of the package
/// </summary>
private void SetPackageOnlineEnvironment()
{
Context.AddStep("Setting environment...", stepcontext =>
{
SetOnlineEnvironmentImpl();
});
}
/// <summary>
/// Process any necessary pre package operation
/// </summary>
protected virtual void PrePackage()
{
Context.AddStep("Prepackaging process...", stepcontext =>
{
Context.LogMessage("== Prepackage step ==");
Context.LogMessage("Start prepackage package");
PrePackageImpl();
});
}
/// <summary>
/// Generate the package for the given platform
/// </summary>
protected virtual void GeneratePackage()
{
Context.AddStep("Generating package...", stepcontext =>
{
Context.LogMessage("== Generate package step ==");
Context.LogMessage("Start to generate package");
GeneratePackageImpl();
});
}
#region Implementation
protected virtual void PackageDataAssemblyImpl(IStep stepcontext)
{
CopyExecutableFromOutput(stepcontext);
CopyDataFromOutput(stepcontext);
}
protected virtual void SetSpecificPackageSkuFilesImpl()
{
foreach (var item in GetFilesList())
{
var filename = Path.Combine(EngineExecutable.DeliveredDirectory, item);
var newFilename = Path.Combine(WorkingDirectory, item.Replace($"_{Sku}", "").Replace($"_{Variant}", "").Remove("_patch"));
Context.LogMessage($"Rename {filename} into {newFilename}");
File.Copy(filename, newFilename, true);
}
// Set layout
LayoutPath = Path.Combine(WorkingDirectory, DiscLayout.NonEmpty(DefaultDiscLayout));
}
protected virtual void SetOnlineEnvironmentImpl()
{
EnvironmentFile = Path.Combine(DataWorkingDirectory, "onlineConfig.ini");
Context.LogMessage("== Set environment step ==");
Context.LogMessage("Start to setting environment");
System.IO.File.WriteAllText(EnvironmentFile, GameOnlineEnvironment.ToString().ToLower());
}
protected virtual void PrePackageImpl()
{
Context.AddStep("Defining layout...", stepcontext =>
{
stepcontext.LogMessage($"Packaging version using layout: {LayoutPath}");
});
Context.AddStep("Defining package tool...", stepcontext =>
{
// Set package tool
// PkgTool = GameConstants.GetPkgTool(Platform);
PkgFormat = GameType.PackageFormat(Platform, Variant, OutputFormat);
stepcontext.LogMessage($"Packaging version using tool: {PkgTool}");
});
}
protected virtual void GeneratePackageImpl()
{
Context.AddStep("Creation output folder...", stepcontext =>
{
if (!System.IO.Directory.Exists(OutputDirectory))
{
System.IO.Directory.CreateDirectory(OutputDirectory);
}
});
}
#endregion
protected void UpdateDiscLayout(string pattern, string environmentString)
{
var layoutContent = System.IO.File.ReadAllLines(LayoutPath).ToList();
// Instert new environment file in the layout just after the line containing the pattern
var patternIndex = layoutContent.IndexOf(line => line.Contains(pattern));
layoutContent.Insert(patternIndex + 1, environmentString);
// Update layout file
System.IO.File.WriteAllLines(LayoutPath, layoutContent);
}
}
}