JD2022-TU1/main/tools/apps/TeaBox/Script/Data/MapValidation/MapUpdateTemplate.cs

173 lines
8 KiB
C#

using Cottle;
using JD.Asset.Template.Map;
using JD.Asset.Template.References;
using JD.Asset.Template.Utils;
using JD.Asset.Types.Map;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using TeaBox.Macros;
using TeaBox.SourceControl;
namespace TeaBox.MapValidation
{
[Module(Icon = "verify.png"), Category("Data"), DisplayName("Validations")]
class MapUpdateTemplate
{
/// <summary>
/// Get a list of Map to check and find their template type
/// </summary>
/// <param name="context">The context</param>
/// <param name="maps">The list of maps' name to check</param>
[Macro(Icon = "verify.png")]
[DisplayName("Reconcile maps with templates")]
[Description("Works for ALT and CLASSIC templates. Check if a map contains all its template. Add them if some templates are missing")]
public static void MapsUpdateTemplates(IExecutionContext context, MapList maps)
{
if (maps.Count == 0)
{
context.LogError($"Error : at least one map is required");
return;
}
foreach (Map map in maps)
{
MapUpdateTemplates(map, context, GetTemplateFromMap(map));
}
}
/// <summary>
/// Get a Map to check if some templates files are missing
/// and put them in a perforce changelist
/// </summary>
/// <param name="currentMap">The map we want to check</param>
/// <param name="context">the context</param>
/// <param name="template">the template type of the map</param>
public static void MapUpdateTemplates(Map currentMap, IExecutionContext context, MapCreationStore.Template template)
{
Source_ControlMacros.GetLatestData(context, syncEngine: true, maps: new MapList { currentMap }, folder: currentMap.Folder);
var templatePath = string.Empty;
if (Directory.Exists(System.IO.Path.Combine(currentMap.DataRoot, ("EngineData/MapTemplates/"), template.ToString())))
templatePath = System.IO.Path.GetFullPath(System.IO.Path.Combine(currentMap.DataRoot, ("EngineData/MapTemplates/"), template.ToString()));
if (string.IsNullOrEmpty(templatePath) == false)
{
Source_ControlMacros.GetLatestData(context, syncEngine: false, maps: null, folder: templatePath);
}
List<string> createdFiles = CheckingMissingTemplatesFiles(currentMap, templatePath, template);
if (createdFiles.Count > 0)
{
//reconcile file with perforce and create a changelist
Changelist reconcileChangelist = null;
context.AddStep(
$"Creating Changelist in P4 (Data) {currentMap.Name}",
step =>
{
var filesPathEntry = new List<PathEntry>();
createdFiles.ForEach(file => filesPathEntry.Add(PathEntry.FromFile(file)));
if (!SourceControlService.Instance.Data().Reconcile(context, $"[Teabox][{currentMap.Name}] Update map missing files",
out reconcileChangelist, filesPathEntry.ToArray()))
{
context.LogError("Something went wrong while reconciling your map related data");
}
});
context.LogMessage($"{createdFiles.Count} file(s) have been created");
createdFiles.ForEach(x => context.LogMessage((x)));
}
context.LogMessage("Map is valid , no files to add");
}
/// <summary>
/// Get a map and search if some templates files are missing
/// Create them if missing
/// </summary>
/// <param name="map">the map to check</param>
/// <param name="templatePath">the path to the templates files</param>
/// <param name="mapTemplate">the template type of the map</param>
/// <returns>the list of created files. if none created return an empty List</returns>
private static List<string> CheckingMissingTemplatesFiles(Map map, string templatePath, MapCreationStore.Template mapTemplate)
{
List<string> createdFiles = new List<string>();
//get all files in the map
List<string> mapFilesList = System.IO.Directory.GetFiles(map.Folder, "*", System.IO.SearchOption.AllDirectories)
.Select(x => x.Replace('\\', '/'))
.ToList();
templatePath = templatePath.Replace('\\', '/');
var creationScope = new MapCreationStore()
{
MapName = map.Name,
MapTemplate = mapTemplate.ToString(),
};
if (map.SongDesc != null)
{
creationScope.Artist = map.SongDesc.Artist;
creationScope.Title = map.SongDesc.Title;
creationScope.NumberCoach = map.SongDesc.NumCoach;
creationScope.OriginalJDVersion = map.SongDesc.OriginalJDVersion.ToString();
}
IReplaceStrategy replaceStrategy = new CottleReplaceStrategy(creationScope);
Dictionary<string, string> templateDictionary = new Dictionary<string, string>();
//get all templates
List<string> templateFiles = System.IO.Directory.GetFiles(templatePath, "*", System.IO.SearchOption.AllDirectories).Select(x => x.Replace('\\', '/')).ToList();
CottleIO.IgnoreScriptEvaluator ignoreScriptEvaluator = new CottleIO.IgnoreScriptEvaluator(replaceStrategy, (IEnumerable<string>)templateFiles);
templateFiles.RemoveAll(x => ignoreScriptEvaluator.ShouldIgnore(x));
foreach (string templateFile in templateFiles)
{
//ex : Key = 7rings\Timeline\7Rings_TML_Dance.dtape ; Value = {DATA_REPO}\data\EngineData\MapTemplates\Classic\Timeline\${MapName}$_TML_Dance.dtape
templateDictionary.Add(replaceStrategy.Replace(templateFile).Replace(templatePath, map.Name), templateFile);
}
foreach (string currentTemplate in templateDictionary.Keys)
{
string mapFile = mapFilesList.FirstOrDefault(x => x.Contains(currentTemplate));
if (mapFile == null)
{
string fileToCreate = $"{map.Folder.Parent}/{currentTemplate}";
if (File.Exists(fileToCreate) == false)
{
var cottleFileCopies = CottleIO.CopyFiles(new List<string> { templateDictionary[currentTemplate] }, templateDictionary[currentTemplate], fileToCreate,
(IReplaceStrategy)new CottleReplaceStrategy((IStore)creationScope), (Plume.Core.Logging.ILogger)null, null);
if (cottleFileCopies.Count > 0)
createdFiles.Add(cottleFileCopies.First());
}
}
}
return createdFiles;
}
/// <summary>
/// Find the template type of the map
/// </summary>
/// <param name="map">the map</param>
/// <returns>the template type of the map</returns>
private static MapCreationStore.Template GetTemplateFromMap(Map map)
{
if (map.IsAlternate)
return MapCreationStore.Template.ALT;
if (map.IsAlternateCommunityMashUp)
return MapCreationStore.Template.CMU;
if (map.IsCommunityMashUp)
return MapCreationStore.Template.CMU;
if (map.IsJDMachine)
return MapCreationStore.Template.JDM;
if (map.IsMashUp)
return MapCreationStore.Template.MU;
if (map.IsPartyMaster)
return MapCreationStore.Template.PM;
return MapCreationStore.Template.Classic;
}
}
}