P4Connect.VerifySettings.cs #1

  • //
  • guest/
  • anis_sg/
  • perforce_software/
  • p4connect/
  • src/
  • P4Connect/
  • P4Connect/
  • P4Connect.VerifySettings.cs
  • View
  • Commits
  • Open Download .zip Download (8 KB)
using UnityEditor;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perforce.P4;
using log4net;

namespace P4Connect
{
	/// <summary>
	/// A collection of static methods that allow us to check the perforce connection settings
	/// </summary>
	public class VerifySettings
	{
        private static readonly ILog log = LogManager.GetLogger(typeof(VerifySettings));

		public static string LastWorkspaceMapping;
        public static bool WorkspaceChecked;
        public static bool ConnectionChecked;

		/// <summary>
		/// Checks whether the source control setting is correct
		/// </summary>
		/// <returns></returns>
		public static bool CheckMetaFiles()
		{
			return EditorSettings.externalVersionControl.Contains("Meta Files");
		}

		/// <summary>
		/// Checks that the server is reachable
		/// </summary>
		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;
		}

		/// <summary>
		/// Checks that the user password if valid
		/// </summary>
		/// <returns></returns>
		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;
		}

		/// <summary>
		/// Check that the workspace is valid
		/// </summary>
		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;
		}

		/// <summary>
		/// Verifies that the project root is valid
		/// </summary>
		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;
		}
	}
}
# Change User Description Committed
#1 12954 anis_sg Populate -o //guest/perforce_software/p4connect/...
//guest/anis_sg/perforce_software/p4connect/....
//guest/perforce_software/p4connect/src/P4Connect/P4Connect/P4Connect.VerifySettings.cs
#7 12568 Norman Morse Fixed some error handling during Perforce Configuration
#6 12565 Norman Morse Integrated from Dev Branch
Made ChangeManager into Static Class.
Improved close window behavior for when connection is invalid
Fixed localOpenFiles not updating on submit
#5 12553 Norman Morse integrate from internal main
Build fixes for EC.
Major changes to Configuration and re-initialization code.
Bug fixes
#4 12512 Norman Morse Integrate from Dev branch, preparing for Beta3 release
#3 12251 Norman Morse Fixes for Beta 2 release
Mostly Configuration dialog bug fixes
#2 12135 Norman Morse Integrate dev branch changes into main.

This code is the basiis of the 2.7 BETA release which provides Unity 5 compatibility
#1 10940 Norman Morse Inital Workshop release of P4Connect.
Released under BSD-2 license