JD2022-TU1/main/tools/apps/JDSongConverter/Macros/MapOperationsMacros.cs

154 lines
8 KiB
C#

using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using JD.Asset.Template.References;
using JD.Asset.Template.Utils;
using JD.Asset.Types.Map;
using TeaBox.Macros;
using JD.Core;
using JD.Core.Logging;
using JD.LogService;
using JD.Perforce;
using JD.Serialization;
using File = System.IO.File;
using Path = System.IO.Path;
namespace JDSongConverter.Macros
{
[Module]
class MapOperationsMacros
{
public class MapMoveReplaceStrategy : MapNameReplaceStrategy
{
public MapMoveReplaceStrategy(string srcMapName, string srcDirectoryRelativeRelative, string destMapName, string destDirectoryRelativeRelative) : base(srcMapName, srcDirectoryRelativeRelative, destMapName, destDirectoryRelativeRelative)
{
}
public override string Replace(string stringToReplace)
{
string replacedValue = base.Replace(stringToReplace);
replacedValue = replacedValue.Replace("world/jd2015/_ui/materials_com/lobby_coach.msh", "world/ui/materials/alpha.msh");
replacedValue = replacedValue.Replace("world/jd2015/_ui/materials_com/cover_coach.msh", "world/ui/materials/alpha.msh");
replacedValue = replacedValue.Replace("world/jd2015/_ui/materials_com/cover_generic.msh", "world/ui/materials/alpha_mul_b.msh");
replacedValue = replacedValue.Replace("jd2015/_ui/textures", "ui/textures");
return replacedValue;
}
}
[Macro(ThreadAffinity.ExecuteAsync)]
public static void MoveMapToMapsDirectory(IExecutionContext context, MapList maps)
{
using (var steps = context.ExecuteSteps())
{
foreach (var map in maps)
{
steps.AddStep(string.Format("Moving map {0}", map.Name), stepcontext =>
{
var destMap = new Map(map.Name, Path.Combine(Map.MapFolder, map.Name), "MAPS");
var replaceStrategy = new MapMoveReplaceStrategy(map.Name, map.Folder.Relative, destMap.Name,
destMap.Folder.Relative);
if (!Directory.Exists(destMap.Folder))
{
Directory.CreateDirectory(destMap.Folder);
}
var allFiles = Directory.GetFiles(map.Folder, "*", SearchOption.AllDirectories).ToList();
CottleIO.CopyFiles(allFiles, map.Folder, destMap.Folder, replaceStrategy);
string ActorsFolder = System.IO.Path.Combine(destMap.MenuArtFolder, "Actors");
string coverOnlineActFile = System.IO.Path.Combine(ActorsFolder, destMap.Name + "_Cover_Online.act");
if (!File.Exists(coverOnlineActFile))
{
context.LogWarning($"The cover_online actor file {coverOnlineActFile} doesn't exist");
}
else
{
var serializer = new LuaSerializer {IgnoreDefaultValues = true};
var actor = (Actor)serializer.LoadObjectFromFile(coverOnlineActFile);
actor.ParentBind = null;
var materialGraphicComponent = actor.COMPONENTS.OfType<MaterialGraphicComponent>().FirstOrDefault();
if (materialGraphicComponent != null)
{
string coverOnlineMshPath = materialGraphicComponent.Material.ShaderPath.ToString();
if (coverOnlineMshPath == "world/jd2015/_ui/materials_com/cover_online.msh")
{
materialGraphicComponent.Material.ShaderPath = new JD.Serialization.Path("world/ui/materials/alpha_mul_b.msh");
serializer.SaveObjectToFile(actor, coverOnlineActFile);
}
}
}
RemoveActor(context, System.IO.Path.Combine(ActorsFolder,
destMap.Name + "_Prelobby_PartyMaster.act"), System.IO.Path.Combine(destMap.MenuArtFolder,
destMap.Name + "_MENUART.isc"), destMap.Name + "_prelobby_partymaster");
string timelineSceneFiles = System.IO.Path.Combine(destMap.TimelineFolder, destMap.Name + "_TML.isc");
RemoveActor(context, System.IO.Path.Combine(destMap.TimelineFolder, destMap.Name + "MU.act"), timelineSceneFiles, destMap.Name + "mu");
RemoveActor(context, System.IO.Path.Combine(destMap.TimelineFolder, destMap.Name + "MU.tpl"), timelineSceneFiles, destMap.Name + "mu");
RemoveActor(context, System.IO.Path.Combine(destMap.TimelineFolder, destMap.Name + "PM.act"), timelineSceneFiles, destMap.Name + "pm");
RemoveActor(context, System.IO.Path.Combine(destMap.TimelineFolder, destMap.Name + "PM.tpl"), timelineSceneFiles, destMap.Name + "pm");
//TODO
//var mapCreationScope = new MapCreationScope();
//mapCreationScope.Map = destMap;
//var cottleReplaceStrategy = new CottleReplaceStrategy(mapCreationScope);
//CottleIO.CopyFiles(
// new[] {"EngineData/MapTemplates/Classic/VideosCoach/video_player_main.act","EngineData/MapTemplates/Classic/VideosCoach/${MapName}$_VIDEO.isc"},
// DataRoot.ToAbsolutePath("EngineData/MapTemplates/Classic/VideosCoach"),
// destMap.VideoCoachFolder, cottleReplaceStrategy);
UpdateSongDesc(destMap);
string changelistMessage = string.Format("[DT][{0}] Integrate map from {1} to {2}", destMap.Name,
map.Folder.Relative, destMap.Folder.Relative);
var changelistInfo =
P4Manager.Instance.PerforceWrapper.GetCurrentClientPendingChangelists()
.FirstOrDefault(x => x.Description.TrimEnd('\n') == changelistMessage);
var changelist = changelistInfo != null
? changelistInfo.Change
: P4Manager.Instance.PerforceWrapper.CreatePendingChangelist(changelistMessage);
foreach (var destFile in Directory.GetFiles(destMap.Folder, "*", SearchOption.AllDirectories))
{
P4Manager.Instance.PerforceWrapper.OpenForAdd(destFile, changelist);
}
});
}
}
}
private static void RemoveActor(IExecutionContext context, string filePath, string fileScenePath, string valueNodeAttribute)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
if (!File.Exists(fileScenePath))
{
context.LogWarning("The scene file \"{0}\" doesn't exist", fileScenePath);
}
else
{
XDocument doc = XDocument.Load(fileScenePath);
doc.Descendants("ACTORS")
.Where(
x =>
(string)x.Element("Actor").Attribute("USERFRIENDLY") ==
valueNodeAttribute).Remove();
doc.Save(fileScenePath);
}
}
private static void UpdateSongDesc(Map map)
{
File.SetAttributes(map.SongDescPath, FileAttributes.Normal);
var songDescContent = File.ReadAllText(map.SongDescPath);
songDescContent = Regex.Replace(songDescContent, @"JDVersion[ \t]+=[ \t]+\d+", "JDVersion = 2016");
File.WriteAllText(map.SongDescPath, songDescContent);
}
}
}