JD2022-TU1/main/tools/apps/SongEditor/Actions/OpenSongDescAction.cs

76 lines
2.9 KiB
C#

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
{
/// <inheritdoc />
public OpenSongDescAction() :
base("Open Songdesc", "Open the songdesc file", StandardIcon.OpenFolder, null, null)
{
}
/// <inheritdoc />
public override void TryExecute()
{
if (App.Current.SelectedMapEntity == null)
{
throw new Exception("Please select a map first");
}
var perforceService = App.Current.ServiceManager.Get<IPerforceService>();
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<string>{ depotSongDescPath };
var errors = new List<string>();
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);
}
/// <inheritdoc />
public override bool CanExecute()
{
return true;
}
}
}