using UnityEditor;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using Perforce.P4;
namespace P4Connect
{
[InitializeOnLoad]
public class Main
{
// Path to the Asset folder
public static string DataPath { get; private set; }
// Path the project folder
public static string RootPath { get; private set; }
// The name of the current project
public static string ProjectName { get; private set; }
// Returns whether the system is initialized and we can access perforce
public static bool Initialized { get; private set; }
// Import this method to override the DLLImport search directory, so that P4API.NET can load P4Bridge
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
///
/// Static constructor
///
static Main()
{
Initialized = false;
Initialize();
Initialized = true;
}
///
/// Initialize the different parts of P4Connect
///
static void Initialize()
{
DateTime startTimestamp = DateTime.Now;
SetupRootDirectory();
Config.Initialize();
SetDLLDirectory();
Engine.Initialize();
AssetBridge.Initialize();
UnityVSBridge.Initialize();
AssetStatusCache.Initialize();
Icons.Initialize();
Config.SetProjectRootDirectory();
// First time - always check out sln files
if (Config.IncludeSolutionFiles)
{
AssetBridge.CheckoutProjectFiles(false, true);
}
PendingChanges.UpdateDisplay();
if (Config.DisplayP4Timings)
{
double deltaTime = (DateTime.Now - startTimestamp).TotalMilliseconds;
StringBuilder builder = new StringBuilder();
Debug.Log("P4Connect - Initialization Time: " + deltaTime + " ms");
}
}
///
/// Initializes the root directory, asset directory and project name
///
static void SetupRootDirectory()
{
// Grab all the directories and project names from the asset folder
DataPath = Application.dataPath;
string[] path = DataPath.Split(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar);
RootPath = "";
for (int i = 0; i < path.Length - 1; ++i)
RootPath += path[i] + System.IO.Path.DirectorySeparatorChar;
ProjectName = path[path.Length - 2];
}
///
/// Allow users to override the dll search directory
///
public static void SetDLLDirectory()
{
if (Application.platform == RuntimePlatform.WindowsEditor)
{
// Force the DLL search directory as soon as possible
SetDllDirectory(Utils.GetDLLFullDirectory());
}
else if (Application.platform == RuntimePlatform.OSXEditor)
{
string dylibSourcePath = System.IO.Path.Combine(Utils.GetDLLFullDirectory(), Config.P4BridgeDYLIBName);
string dylibDestPath = System.IO.Path.Combine(RootPath, Config.P4BridgeDYLIBName);
if (!System.IO.File.Exists(dylibDestPath))
{
System.IO.File.Copy(dylibSourcePath, dylibDestPath);
EditorUtility.DisplayDialog("Restart Unity?",
"In order for P4Connect to function on OSX, P4Connect\n" +
"had to copy libP4Bridge.dylib to the root folder of your project\n" +
"You now have to restart Unity for it to properly load the dylib.", "Ok");
}
}
}
}
}