JD2022-TU1/main/tools/apps/Packager/Tasks/ExtractSkuMapsTask.cs

120 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using JD.Core.ProjectManaging;
using Packager.Parameters;
using JD.Serialization;
using Path = System.IO.Path;
namespace Packager.Tasks
{
[DisplayName("Extract Sku Maps")]
[Description("Extract the maps specified in the .isc file for the present running profile")]
[Category("Map List Generation")]
public class ExtractSkuMapsTask: Task
{
[Input("SkuScene", IOCardinality.Single, IOType.File)]
public SingleReference SkuScene { get; set; }
[Output(nameof(MapIscPaths), IOType.File)]
public ReferenceList MapIscPaths = new ReferenceList();
[Output(nameof(MapNames), IOType.String)]
public ReferenceList MapNames = new ReferenceList();
[Output(nameof(BlockFlows), IOType.File)]
public ReferenceList BlockFlows = new ReferenceList();
public ExtractSkuMapsTask()
{
SkuScene = new SingleReference();
}
protected override void StartInternal(BuildConfiguration profile, cookerWrapper cooker)
{
var error = 0;
try
{
var skuScenePath = DataPath.GetAbsolutePath(SkuScene.Value);
LogMessage(false, skuScenePath);
// first find excluded modes
var excludedModes = new Dictionary<string, List<string>>(); // map -> mode
XDocument.Load(skuScenePath)
.Descendants("exclude_modes")
.ToList().ForEach(exclude => AppendExclude(ref excludedModes, exclude.Parent?.Attribute("name").Value.ToLower(), exclude.Attribute("VAL").Value.ToLower()));
// then list maps and blockflows
XDocument.Load(skuScenePath)
.Descendants("Actor")
.Where(actor => actor.Element("COMPONENTS").Attribute("NAME").Value == "JD_SongDescComponent")
.ToList().ForEach(actor => AppendMap(actor.Attribute("INSTANCEDATAFILE").Value, ref error));
}
catch (System.Exception ex)
{
LogMessage(true, ex.Message);
var inner = ex.InnerException;
while (inner != null)
{
LogMessage(true, inner.Message);
inner = inner.InnerException;
}
LogMessage(true, ex.StackTrace);
return;
}
ErrorCode = error;
}
private void AppendMap(string songDescPath, ref int error)
{
var songDescFullPath = Path.Combine(DataPath.Value, songDescPath);
if (!File.Exists(songDescFullPath))
{
error++;
LogMessage(true, "SongDesc not found: '" + songDescPath + "'");
return;
}
// extract map main scene
var mapFolder = Path.GetDirectoryName(songDescPath);
var mapName = Path.GetFileName(mapFolder);
var mapPath = Path.Combine(mapFolder, mapName + "_main_scene.isc");
MapIscPaths.Add(mapPath);
MapNames.Add(mapName);
// read song desc and deduce blockflows to include
var actor = (Actor_Template)Serializers.LuaSerializer.LoadObjectFromFile(Path.ChangeExtension(songDescFullPath, ".tpl"));
var songDesc = (JD_SongDescTemplate)actor.COMPONENTS.First();
mapName = mapName.ToLower();
var mode = songDesc.Mode;
var blockFlowPath = Path.Combine(mapFolder, "timeline\\", mapName + ".tpl");
if (File.Exists(DataPath.GetAbsolutePath(blockFlowPath)) && mapName.EndsWith("pm"))
{
BlockFlows.Add(blockFlowPath);
}
}
private void AppendExclude(ref Dictionary<string, List<string>> excludedModes, string map, string mode)
{
if (excludedModes.ContainsKey(map))
{
excludedModes[map].Add(mode);
}
else
{
var modes = new List<string>();
modes.Add(mode);
excludedModes[map] = modes;
}
}
}
}