using UnityEditor;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
namespace P4Connect
{
///
/// This class hooks onto the Asset Save/Delete/Move process and makes sure that
/// Perforce is updated accordingly. It uses the perforce connection class
/// (which internally uses Config to retrieve the connection settings)
/// to open a connection to the server and add the required changes (add/checkout/delete/move).
///
public class AssetPostProcessor
: UnityEditor.AssetPostprocessor
{
private static readonly ILog log = LogManager.GetLogger(typeof(AssetPostProcessor));
public static void OnPostprocessAllAssets (
String [] aImportedAssets,
String [] aDeletedAssets,
String [] aMovedAssets,
String [] aMovedFromAssetPaths)
{
if (!Config.ValidConfiguration) // Should I queue up this stuff until the configuration is valid?
return;
#if DEBUG
log.DebugFormat("import: {0} delete: {1} move: {2} moveTo: {3}",
Logger.StringArrayToString(aImportedAssets),
Logger.StringArrayToString(aDeletedAssets),
Logger.StringArrayToString(aMovedAssets),
Logger.StringArrayToString(aMovedFromAssetPaths));
#endif
HashSet deleted = new HashSet(aDeletedAssets);
HashSet imported = new HashSet(aImportedAssets);
// Find all files which are both deleted and imported. These are "replaced" - They should be Checked Out.
List replaced = deleted.Intersect(aImportedAssets).ToList();
if (replaced.Count > 0)
{
deleted.ExceptWith(replaced); // remove replaced files from the delete list
imported.ExceptWith(replaced); // remove replaced files from the imported list
Engine.CheckoutAssets(replaced.ToArray());
}
if (deleted.Any())
{
Engine.DeleteAssets(deleted.ToArray());
}
if (imported.Any())
{
Engine.CreateAssets(imported.ToArray());
}
if (aMovedAssets.Length > 0)
{
Engine.MoveAssets(aMovedFromAssetPaths, aMovedAssets);
}
#if DEBUG
log.Debug("... Complete");
#endif
}
}
}