using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using JD.Asset.Types.Map; using Plume.Icons; using Plume.Interfaces.WPF.Actions; using Plume.Perforce.Service.Api; using Ubisoft.Perforce; using Ubisoft.Perforce.Helper; using Action = Plume.Interfaces.WPF.Actions.Action; namespace SongEditor.Actions { public class OpenSongDescAction : Action { /// public OpenSongDescAction() : base("Open Songdesc", "Open the songdesc file", StandardIcon.OpenFolder, null, null) { } /// public override void TryExecute() { if (App.Current.SelectedMapEntity == null) { throw new Exception("Please select a map first"); } var perforceService = App.Current.ServiceManager.Get(); if (perforceService == null) { throw new Exception("Perforce service is not initialized."); } var mapName = App.Current.SelectedMapEntity.Name; var mapDepotPath = $"//JD_DATA/main/data/World/MAPS/{mapName}/"; if (!perforceService.CurrentClient.IsDepotFileInView(mapDepotPath)) { throw new Exception($"Path '{mapDepotPath}' is not mapped in your Perforce workspace. Please switch workspace or modify the current workspace."); } var depotSongDescPath = Path.Combine(mapDepotPath, "SongDesc.tpl"); var localSongDescPath = Path.Combine(perforceService.CurrentClient.Root, "data", "World", "Maps", mapName, "SongDesc.tpl"); var filesToSync = new List{ depotSongDescPath }; var errors = new List(); var result = perforceService.Connection.Sync(filesToSync, "head", SyncOptions.None, PerforceWrapper.SyncType.Revision, errors); if (errors.Any(x => !x.Contains("up-to-date"))) { Context.Logger.Error(string.Join("\n", errors)); throw new Exception($"Unable to sync map '{mapName}' : {result}"); } if (!File.Exists(localSongDescPath)) { throw new Exception($"Unable to find {localSongDescPath}."); } var documentBinding = Context.DocumentManager.GetBindingForPath(localSongDescPath); var document = Context.DocumentManager.CreateFromFile(localSongDescPath); var view = Context.WindowManager.CreateDocumentView(document, documentBinding); Context.WindowManager.AddDocumentWindow("DocumentWindow" + Guid.NewGuid().ToString("N"), $"{document.TitleName} [{mapName}]", null, view); } /// public override bool CanExecute() { return true; } } }