using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Threading; using DjRedo.Controls; using DjRedo.Core.Configuration; using DjRedo.Core.Managers; using DjRedo.Core.Trees; using DjRedo.TplTemplates; using DjRedo.ViewModels; using JD.Serialization; using NLog; namespace DjRedo.Core { public class TplCleaner { private readonly Dispatcher _dispatcher; private static readonly ILogger Logs = LogManager.GetCurrentClassLogger(); public TplCleaner(Dispatcher dispatcher) { _dispatcher = dispatcher; } private object[] BuildPerforcetree(IStepData stepData, params object[] parameters) { _dispatcher.Invoke(() => stepData.IsProcessing = true); PerforceInfos infos = new PerforceInfos(); DjRedoTreeManager.Current.BuildPerforceTree(infos); _dispatcher.Invoke(() => { stepData.Progress = 100; stepData.IsProcessing = false; }); return new object[0]; } private object[] CreateAssetTree(IStepData stepData, params object[] parameters) { _dispatcher.Invoke(() => stepData.IsProcessing = true); int nodeCount = 0; List tplToRefactor = DjRedoTreeManager.Current.Tree.Where(node => { _dispatcher.Invoke(() => { int progressPercent = (int)((++nodeCount / (float)DjRedoTreeManager.Current.Tree.NodeCount) * 100); stepData.Progress = progressPercent; }); return CheckNode(node); }).ToList(); _dispatcher.Invoke(() => stepData.IsProcessing = false); return tplToRefactor.Cast().ToArray(); } /// /// I need to exclude the tpl that are related to a trk file because tempoblaster needs the trk files /// /// /// /// private object[] ExludeTplRelatedToTrk(IStepData stepData, params object[] parameters) { _dispatcher.Invoke(() => stepData.IsProcessing = true); int nodeCount = 0; object[] result = parameters.Cast().Select(node => { _dispatcher.Invoke(() => { int progressPercent = (int)((++nodeCount / (float)parameters.Length) * 100); stepData.Progress = progressPercent; }); try { //LoadTpl var serializer = new LuaSerializer { IgnoreDefaultValues = true }; return new AudioNodeInfos { AudioNode = node, Serializer = serializer, Tpl = (Actor_Template)serializer.LoadObjectFromFile(node.DiskPath.Path, typeof(Actor_Template)) }; } catch (Exception e) { Logs.Error(node.DiskPath.Path); File.AppendAllLines("RefactoReport.log", new[] { "#######################################################################################################", $"{node.DiskPath.Path} couldn't be read", e.Message, "#######################################################################################################" }); return AudioNodeInfos.Empty; } }).Where(a => !a.Equals(AudioNodeInfos.Empty) && !a.Tpl.__AdditionalInstructions.Any(i => i.Instruction.EndsWith(".trk\")\r"))).Cast().ToArray(); _dispatcher.Invoke(() => stepData.IsProcessing = false); return result; } private object[] ProcessTpl(IStepData stepData, params object[] parameters) { _dispatcher.Invoke(() => stepData.IsProcessing = true); int parametersCount = parameters.Length; int currentFile = 0; foreach (var audioNode in parameters.Cast()) { int progressPercent = (int)((++currentFile / (float)parametersCount)* 100); _dispatcher.Invoke(() => { stepData.Progress = progressPercent; }); audioNode.Serializer.InitializeEngineTypes(); //I remove the tablepath from the components foreach (ActorComponent_Template component in audioNode.Tpl.COMPONENTS) { component.__TablePath = null; } //remove all the additionalInstruction audioNode.Tpl.__AdditionalInstructions.Clear(); //Add the sound_base reference audioNode.Tpl.__AdditionalInstructions.Add(new SpecialCustomTypeDescriptor.AdditionalInstruction("includeReference(\"EngineData/Helpers/sound_base.ilu\")", 0)); //Checkout tpl file DjRedoPerforceManager.Current.CheckoutFile(audioNode.AudioNode.DiskPath.Path, "RefactoCL"); //write into the file the virgin content File.WriteAllText(audioNode.AudioNode.DiskPath.Path, new VirginTpl().TransformText(), Encoding.UTF8); //save the tpl into the file audioNode.Serializer.SaveObjectToFile(audioNode.Tpl, audioNode.AudioNode.DiskPath.Path); //check that the first line is the includeReference("EngineData/Helpers/sound_base.ilu") if (File.ReadLines(audioNode.AudioNode.DiskPath.Path).First() != "includeReference(\"EngineData/Helpers/sound_base.ilu\")") { throw new Exception("The first line does not match expectation"); } } _dispatcher.Invoke(() => stepData.IsProcessing = false); return new object[0]; } private static bool CheckNode(AudioNode n) { return n.IsOnDisk && n.NodeType == NodeType.File && new FileInfo(n.DiskPath.Path).Extension == ".tpl"; } public RunnableStepTaskCollection ProvideSteps() { var allSteps = new[] { new StepTask(new DjRedoStepViewModel(1, "Build Perforce Tree", "Build Perforce Tree", infinitespin: true), BuildPerforcetree), new StepTask(new DjRedoStepViewModel(2, "Create asset Tree", "Create the memory tree to be pushed into Wwise"), CreateAssetTree), new StepTask(new DjRedoStepViewModel(3, "Exlude Tpl related to trk", "Exclude TPL related to TRK because tempoblaster needs the trk files"), ExludeTplRelatedToTrk), new StepTask(new DjRedoStepViewModel(4, "Clean Tpl", "Clean Tpl"), ProcessTpl), }; return new RunnableStepTaskCollection(allSteps); } } }