JD2022-TU1/main/tools/framework/JD.Timeline/Dance/PictogramSynchronizer.cs

147 lines
6.6 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using JD.Application.Program;
using JD.Kernel.StaticHelpers;
using JD.LogService;
using JD.Perforce;
using JD.Timeline.Model;
using JD.Timeline.Model.Clips;
using TeaBox;
using JD.Serialization;
using Ubisoft.Perforce.Data;
namespace JD.Timeline.Dance
{
public static class PictogramSynchronizer
{
/// <summary>
/// Apply all add/edit/remove operations on perforce on pictograms
/// </summary>
public static void Synchronize(Tape tape, ILogger logger)
{
using (var p4Manager = P4Manager.Instance)
{
string rootPath = Core.DataRoot.Value;
if (tape.Map().IsFromJdCatalog)
{
rootPath = tape.Map().Folder.Parent;
}
p4Manager.Connect(rootPath);
var pictogramsModelsInTape = tape.PictogramModels()
.Where(x => x.InstancesCount > 0).ToList();
if (!p4Manager.Connected)
{
MessageService.MessageService.ShowBalloon(TimeSpan.FromSeconds(6), "Connection to perforce failed", "Connection to perforce failed. Please don't forget to 'reconcile offline work' on perforce.", ToolTipIcon.Error);
foreach (var pictogramModel in pictogramsModelsInTape)
{
if (pictogramModel.BitmapHandler().IsDirty)
{
if (System.IO.File.Exists(pictogramModel.PictoFullPath()))
{
System.IO.File.SetAttributes(pictogramModel.PictoFullPath(), FileAttributes.Normal);
}
pictogramModel.BitmapHandler().Save();
}
}
return;
}
//We create a changelist
string changelistMessage = $"[LD][{tape.Map().Name}] Operation on pictograms with {ProgramIdentity.Instance.Name}";
var changelistInfo = p4Manager.PerforceWrapper.GetCurrentClientPendingChangelists().FirstOrDefault(x => x.Description.TrimEnd('\n') == changelistMessage);
var changelist = changelistInfo != null ? changelistInfo.Change : p4Manager.PerforceWrapper.CreatePendingChangelist(changelistMessage);
var depotFilesInfos = p4Manager.PerforceWrapper.GetFilesInfo(tape.Map().PictosFolder + "\\...");
//We save pictograms that need to be saved
foreach (var pictogramModel in pictogramsModelsInTape)
{
var path = pictogramModel.PictoFullPath();
bool fileExists = System.IO.File.Exists(path);
DepotFileInfo depotFileInfo = depotFilesInfos.Values.FirstOrDefault(x => x.ClientFile.PathEquals(path));
//If the file is not on depot
if (depotFileInfo == null)
{
pictogramModel.BitmapHandler().Save();
p4Manager.PerforceWrapper.OpenForAdd(path, changelist);
}
//If the file exists in perforce but isn't in a pending changelist
else if (depotFileInfo.Action == ActionType.None && pictogramModel.BitmapHandler().IsDirty)
{
if (fileExists)
{
p4Manager.PerforceWrapper.OpenForEdit(path, changelist);
pictogramModel.BitmapHandler().Save();
}
else
{
pictogramModel.BitmapHandler().Save();
p4Manager.PerforceWrapper.OpenForEdit(path, changelist);
}
}
//If the file exists in perforce and is marked for delete in a pending changelist
else if (depotFileInfo.Action == ActionType.Delete)
{
p4Manager.PerforceWrapper.Revert(path);
p4Manager.PerforceWrapper.OpenForEdit(path, changelist);
pictogramModel.BitmapHandler().Save();
}
// Save in any case, when the picto is dirty
else if (pictogramModel.BitmapHandler().IsDirty)
{
pictogramModel.BitmapHandler().Save();
}
}
//We remove all pictograms that are on perforce but not in tape
var pictogramsToDelete = depotFilesInfos.Select(x => x.Value)
.Where(x => x.HaveRev == 1)
.Where(x => x.ClientFile.EndsWith("png"))
.Where(x => !pictogramsModelsInTape.Any(picto => picto.PictoFullPath().PathEquals(x.ClientFile)))
.ToList();
foreach (var path in pictogramsToDelete.Select(x => x.ClientFile))
{
DepotFileInfo depotFileInfo = depotFilesInfos.Values.FirstOrDefault(x => x.ClientFile.PathEquals(path));
//If the file exists on disk but not in perforce
if (depotFileInfo == null)
{
System.IO.File.Delete(path);
}
//If the file exists in perforce and is not in a pending changelist
else if (depotFileInfo.Action == ActionType.None)
{
p4Manager.PerforceWrapper.OpenForDelete(path, changelist);
}
//If the file exists in perforce and is marked for add in a pending changelist
else if (depotFileInfo.Action == ActionType.Add)
{
p4Manager.PerforceWrapper.Revert(path);
System.IO.File.Delete(path);
}
//If the file exists in perforce and is marked for edit in a pending changelist
else if (depotFileInfo.Action == ActionType.Edit)
{
p4Manager.PerforceWrapper.OpenForDelete(path, changelist);
}
}
p4Manager.Disconnect();
}
}
private static bool PathEquals(this string pathA, string pathB)
{
return FileUtility.IsEqualFileName(pathA, pathB);
}
}
}