using UnityEditor; using UnityEngine; using System; using System.Collections.Generic; using System.Linq; using System.Text; using Perforce.P4; namespace P4Connect { /// /// A collection of static methods that allow us to check the perforce connection settings /// public class VerifySettings { public static string LastWorkspaceMapping; /// /// Checks whether the source control setting is correct /// /// public static bool CheckMetaFiles() { return EditorSettings.externalVersionControl.Contains("Meta Files"); } /// /// Checks that the DLL file to talk to P4API.NET exists /// //public static bool CheckDLLLocation() //{ // Just check that the file exists at that location // return System.IO.File.Exists(System.IO.Path.Combine(System.IO.Path.Combine(Application.dataPath, Config.DLLLocation), Config.P4BridgeDLLName)); //} /// /// Checks that the server is reachable /// public static bool CheckServerURI() { // Build a server object and try to connect to it bool serverValid = false; Server testServer = new Server(new ServerAddress(Config.ServerURI)); Repository depot = new Repository(testServer); Connection con = depot.Connection; if (Config.OverrideHostname) { System.Environment.SetEnvironmentVariable("P4HOST", Config.Hostname); } try { // Attempt to connect con.Connect(null); 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) { serverValid = false; } System.Environment.SetEnvironmentVariable("P4HOST", ""); return serverValid; } /// /// Checks that the user password if valid /// /// public static bool CheckUsernamePassword() { // Connect to the server with the username Server testServer = new Server(new ServerAddress(Config.ServerURI)); Repository depot = new Repository(testServer); Connection con = depot.Connection; if (Config.OverrideHostname) { System.Environment.SetEnvironmentVariable("P4HOST", Config.Hostname); } if (Config.OverrideCharset) { System.Environment.SetEnvironmentVariable("P4CHARSET", Config.Charset); } con.UserName = Config.Username; bool passwordValid = false; try { // Attempt to open a secure connection with the given password con.Connect(null); con.Trust(new TrustCmdOptions(TrustCmdFlags.AutoAccept), ""); Credential credentials = con.Login(Config.Password); passwordValid = (credentials != null); con.Disconnect(); } catch (P4Exception) { passwordValid = false; } System.Environment.SetEnvironmentVariable("P4HOST", ""); System.Environment.SetEnvironmentVariable("P4CHARSET", ""); return passwordValid; } /// /// Check that the workspace is valid /// public static bool CheckWorkspace() { if (String.IsNullOrEmpty(Config.Workspace)) return false; // Open a connection, assume a secure one, won't hurt if it's not needed Server testServer = new Server(new ServerAddress(Config.ServerURI)); Repository depot = new Repository(testServer); Connection con = depot.Connection; if (Config.OverrideHostname) { System.Environment.SetEnvironmentVariable("P4HOST", Config.Hostname); } if (Config.OverrideCharset) { System.Environment.SetEnvironmentVariable("P4CHARSET", Config.Charset); } con.UserName = Config.Username; bool workspaceValid = false; try { // 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(null); 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(); // made it all the way workspaceValid = true; } catch (P4Exception) { workspaceValid = false; } System.Environment.SetEnvironmentVariable("P4HOST", ""); System.Environment.SetEnvironmentVariable("P4CHARSET", ""); return workspaceValid; } /// /// Verifies that the project root is valid /// public static bool CheckProjectRoot() { // Open a connection, assume a secure one, won't hurt if it's not needed Server testServer = new Server(new ServerAddress(Config.ServerURI)); Repository depot = new Repository(testServer); Connection con = depot.Connection; if (Config.OverrideHostname) { System.Environment.SetEnvironmentVariable("P4HOST", Config.Hostname); } if (Config.OverrideCharset) { System.Environment.SetEnvironmentVariable("P4CHARSET", Config.Charset); } con.UserName = Config.Username; bool rootValid = false; try { // 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(null); con.Trust(new TrustCmdOptions(TrustCmdFlags.AutoAccept), ""); Credential credentials = con.Login(Config.Password); con.Credential = credentials; ClientMetadata metaData = depot.GetClientMetadata(); if (metaData != null) { LastWorkspaceMapping = metaData.Root.Replace('/', System.IO.Path.DirectorySeparatorChar); rootValid = Utils.IsDirOrValidSubDirectoryOf(Main.RootPath, LastWorkspaceMapping); } con.Disconnect(); } catch (P4Exception ex) { Debug.LogException(ex); rootValid = false; } System.Environment.SetEnvironmentVariable("P4HOST", ""); System.Environment.SetEnvironmentVariable("P4CHARSET", ""); return rootValid; } } }