using UnityEditor; using UnityEngine; using System; using System.Collections.Generic; using System.Linq; using System.Text; using Perforce.P4; using log4net; namespace P4Connect { /// /// A collection of static methods that allow us to check the perforce connection settings /// public class VerifySettings { private static readonly ILog log = LogManager.GetLogger(typeof(VerifySettings)); public static string LastWorkspaceMapping; public static bool WorkspaceChecked; public static bool ConnectionChecked; /// /// Checks whether the source control setting is correct /// /// public static bool CheckMetaFiles() { return EditorSettings.externalVersionControl.Contains("Meta Files"); } /// /// Checks that the server is reachable /// public static bool CheckServerURI() { if (Config.ServerURI.Length == 0) return false; // Build a server object and try to connect to it bool serverValid = false; Repository depot = null; // IDisposable try { Server testServer = new Server(new ServerAddress(Config.ServerURI)); depot = new Repository(testServer); Connection con = depot.Connection; if (!String.IsNullOrEmpty(Config.Hostname)) { System.Environment.SetEnvironmentVariable("P4HOST", Config.Hostname); } // Attempt to connect con.Connect(Version.ConnectOptions); con.Trust(new TrustCmdOptions(TrustCmdFlags.AutoAccept), ""); // If that didn't throw an exception, make sure the server is reported as online serverValid = (testServer.State == ServerState.Online); con.Disconnect(); } catch (P4Exception ex) { log.Debug("exception", ex); serverValid = false; } finally { depot.Dispose(); } System.Environment.SetEnvironmentVariable("P4HOST", ""); return serverValid; } /// /// Checks that the user password if valid /// /// public static bool CheckUsernamePassword() { // Connect to the server with the username bool passwordValid = false; Repository depot = null; // IDisposable try { Server testServer = new Server(new ServerAddress(Config.ServerURI)); depot = new Repository(testServer); Connection con = depot.Connection; if (!String.IsNullOrEmpty(Config.Hostname)) { System.Environment.SetEnvironmentVariable("P4HOST", Config.Hostname); } if (!String.IsNullOrEmpty(Config.Charset)) { System.Environment.SetEnvironmentVariable("P4CHARSET", Config.Charset); } con.UserName = Config.Username; // Attempt to open a secure connection with the given password con.Connect(Version.ConnectOptions); con.Trust(new TrustCmdOptions(TrustCmdFlags.AutoAccept), ""); Credential credentials = con.Login(Config.Password); passwordValid = (credentials != null); con.Disconnect(); } catch (P4Exception ex) { log.Debug("exception", ex); passwordValid = false; } finally { depot.Dispose(); } System.Environment.SetEnvironmentVariable("P4HOST", ""); System.Environment.SetEnvironmentVariable("P4CHARSET", ""); return passwordValid; } /// /// Check that the workspace is valid /// public static bool CheckWorkspace() { // System.Console.WriteLine("CheckWorkspace()"); if (String.IsNullOrEmpty(Config.Workspace)) return false; bool workspaceValid = false; Repository depot = null; // IDisposable try { // Open a connection, assume a secure one, won't hurt if it's not needed Server testServer = new Server(new ServerAddress(Config.ServerURI)); depot = new Repository(testServer); Connection con = depot.Connection; if (!String.IsNullOrEmpty(Config.Hostname)) { System.Environment.SetEnvironmentVariable("P4HOST", Config.Hostname); } if (!String.IsNullOrEmpty(Config.Charset)) { System.Environment.SetEnvironmentVariable("P4CHARSET", Config.Charset); } con.UserName = Config.Username; // Open the connection and try to get the list of opened files on the workspace con.Client = new Client(); con.Client.Name = Config.Workspace; con.Connect(Version.ConnectOptions); con.Trust(new TrustCmdOptions(TrustCmdFlags.AutoAccept), ""); Credential credentials = con.Login(Config.Password); con.Credential = credentials; // Try to get the list of opened files, it'll throw an exception if workspace is invalid depot.GetOpenedFiles(null, null); con.Disconnect(); workspaceValid = true; } catch (P4Exception ex) { log.Debug("exception", ex); workspaceValid = false; } finally { depot.Dispose(); } System.Environment.SetEnvironmentVariable("P4HOST", ""); System.Environment.SetEnvironmentVariable("P4CHARSET", ""); return workspaceValid; } /// /// Verifies that the project root is valid /// public static bool CheckProjectRoot() { Repository depot = null; // IDisposable bool rootValid = false; ConnectionChecked = false; WorkspaceChecked = false; if (Config.ServerURI.Length == 0 || Config.Username.Length == 0 || Config.Workspace.Length == 0) return false; try { // Open a connection, assume a secure one, won't hurt if it's not needed Server testServer = new Server(new ServerAddress(Config.ServerURI)); depot = new Repository(testServer); Connection con = depot.Connection; if (! String.IsNullOrEmpty(Config.Hostname)) { System.Environment.SetEnvironmentVariable("P4HOST", Config.Hostname); } if (! String.IsNullOrEmpty(Config.Charset)) { System.Environment.SetEnvironmentVariable("P4CHARSET", Config.Charset); } con.UserName = Config.Username; Client myclient = new Client(); myclient.Name = Config.Workspace; con.Client = myclient; if (con.Connect(Version.ConnectOptions)) { ConnectionChecked = true; con.Trust(new TrustCmdOptions(TrustCmdFlags.AutoAccept), ""); Credential credentials = con.Login(Config.Password); con.Credential = credentials; ClientMetadata metaData = depot.GetClientMetadata(); if (metaData != null && metaData.Root != null) { WorkspaceChecked = true; LastWorkspaceMapping = metaData.Root.Replace('/', System.IO.Path.DirectorySeparatorChar); rootValid = Utils.IsDirOrValidSubDirectoryOf(Main.RootPath, LastWorkspaceMapping); } con.Disconnect(); } } catch (Exception ex) { Debug.LogException(ex); rootValid = false; } finally { depot.Dispose(); } System.Environment.SetEnvironmentVariable("P4HOST", ""); System.Environment.SetEnvironmentVariable("P4CHARSET", ""); return rootValid; } } }