using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using JD.Serialization; using TeaBox.Macros; namespace JDSongConverter.Tasks { public partial class Jd2016ToJd5Downgrader { internal class ConvertFilesForJd5 : Task { private readonly XmlSerializer _xmlSerializer = new XmlSerializer(); private readonly LuaSerializer _luaSerializer = new LuaSerializer(); public Path AudioSceneFilePath { get; set; } public Path TimelineSceneFilePath { get; set; } public Path CinematicTplFilePath { get; set; } public Path SequenceTapeFilePath { get; set; } private enum ActorPosition { DTape = 0, KTape = 1 } public override string Name { get { return "Convert Scene and TPL files"; } set { } } public override string ProgressText { get { return "Converting Scene and TPL files"; } } public override TaskResult Process(IExecutionContext executionContext) { ConvertAudioSceneFile(AudioSceneFilePath.getAbsolute()); ConvertTmlSceneFile(TimelineSceneFilePath.getAbsolute()); ConvertCinematicsTplFile(CinematicTplFilePath.getAbsolute()); ConvertCinematicSequenceTapeFile(SequenceTapeFilePath.getAbsolute()); return TaskResult.Ok; } public void ConvertAudioSceneFile(string audioSceneFilePath) { Scene audioScene = _xmlSerializer.LoadObjectFromFile(audioSceneFilePath) as Scene; List actorsToRemove = new List(); foreach (var actor in audioScene.ACTORS) { var actorComponents = actor.COMPONENTS; if (actorComponents.Any(x => x.GetType().Name == "TapeCase_Component")) { actorsToRemove.Add(actor); } } foreach (var actor in actorsToRemove) { audioScene.ACTORS.Remove(actor); } _xmlSerializer.SaveObjectToFile(audioScene, audioSceneFilePath); } public void ConvertTmlSceneFile(string timelineFilePath) { Scene timelineScene = _xmlSerializer.LoadObjectFromFile(timelineFilePath) as Scene; timelineScene.ACTORS.RemoveAt((int) ActorPosition.KTape); Actor actorTomodify = timelineScene.ACTORS[(int) ActorPosition.DTape]; timelineScene.ACTORS.RemoveAt((int) ActorPosition.DTape); actorTomodify.INSTANCEDATAFILE = Path.Empty; //var tapeComponent = actorTomodify.COMPONENTS.Where(x => x.GetType().Name == "TapeCase_Component") as ActorComponent; //actorTomodify.COMPONENTS.Remove(tapeComponent); //actorTomodify.COMPONENTS.Add(new JD_Timeline_Component()); ChangeLuaFilePath(actorTomodify); ChangeUserFriendlyName(actorTomodify); timelineScene.ACTORS.Add(actorTomodify); _xmlSerializer.SaveObjectToFile(timelineScene, timelineFilePath); } private static void ChangeUserFriendlyName(Actor actorTomodify) { var userFirendlyName = actorTomodify.USERFRIENDLY; const string patternUserFriendlyName = "_tml_dance"; const string replacementName = ""; actorTomodify.USERFRIENDLY = Regex.Replace(userFirendlyName, patternUserFriendlyName, replacementName, RegexOptions.IgnoreCase); } private static void ChangeLuaFilePath(Actor actorTomodify) { var luaFilePath = actorTomodify.LUA.ToString(); const string patternPath = @"Timeline.*"; const string replacementPath = @"Timeline/timeline.tpl"; actorTomodify.LUA = new Path(Regex.Replace(luaFilePath, patternPath, replacementPath, RegexOptions.IgnoreCase)); } public void ConvertCinematicsTplFile(string actorTemplateFilePath) { Regex path = new Regex(@"Path = ""(\w+|\/|\.)+"""); Regex removeTapeTrack = new Regex(@"TapesRack(.*\s*){18}"); string fileData = System.IO.File.ReadAllText(actorTemplateFilePath); if (path.IsMatch(fileData) && removeTapeTrack.IsMatch(fileData)) { string tapePath = path.Match(fileData).ToString(); fileData = removeTapeTrack.Replace(fileData, "TapePath"); fileData = Regex.Replace(fileData, "TapePath", "Tape" + tapePath, RegexOptions.IgnoreCase); } System.IO.File.WriteAllText(actorTemplateFilePath, fileData); } public void ConvertCinematicSequenceTapeFile(string tapeFilePath) { Tape sequenceTape = _luaSerializer.LoadObjectFromFile(tapeFilePath) as Tape; var clipsToRemove = sequenceTape.Clips.OfType().ToList(); if (clipsToRemove.Any()) { foreach (var clip in clipsToRemove) { sequenceTape.Clips.Remove(clip); } } _luaSerializer.SaveObjectToFile(sequenceTape, tapeFilePath); } } } }