using UnityEditor; using UnityEngine; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml.Serialization; using Perforce.P4; using log4net; namespace P4Connect { [System.Serializable] public class ConnectionConfig : IEquatable { private static readonly XmlSerializer ConnectionSerializer = new XmlSerializer(typeof(ConnectionConfig)); // To serialize this object we need mark private fields with SerializeField #region SerializedFields [SerializeField] private bool _tested; [SerializeField] private string _server; [SerializeField] private bool _isCaseSensitive; [SerializeField] private bool _isUnicode; [SerializeField] private int _apiLevel; [SerializeField] private bool _requiresLogin; [SerializeField] private bool _serverValid; [SerializeField] private string _user; [SerializeField] private bool _userValid; [SerializeField] private string _password; [SerializeField] private bool _passwordValid; [SerializeField] private bool _passwordChanged; [SerializeField] private string _workspace; [SerializeField] private bool _workspaceValid; [SerializeField] private string _hostname; [SerializeField] private string _charset; [SerializeField] private bool _metafilesValid; [SerializeField] private bool _projectRootValid; #endregion #region Properties // Each of these static properties is tied to an private instance variable. public bool Tested { get { return _tested; } set { _tested = value; } } public string Server { get { return _server; } set { _server = value; } } // Server contains Protocol:Host:Port public string Protocol { get { string[] pieces = Server.Split(':'); if (pieces.Count() == 3) { return pieces[0]; } else { return ""; } } } public string Host { get { string[] pieces = Server.Split(':'); int count = pieces.Count(); if (count == 1 && pieces[0].All(c => c >= '0' && c <= '9')) return "localhost"; return pieces[count - 2]; } } public string Port { get { string[] pieces = Server.Split(':'); int count = pieces.Count(); if (count == 1) { if (pieces[0].All(c => c >= '0' && c <= '9')) return pieces[0]; return "1666"; } return pieces[count - 1]; } } /// /// Create a Server string "protocol:host:port" from the components /// /// Protocol for connection, can be empty or "ssl" /// Hostname for connection, defaults to "localhost" /// Port number for connection, defaults to "1666" /// public static string ServerFromPieces(string protocol, string host, string port) { string server = ""; if (protocol != null) protocol = protocol.Trim(); if (host != null) host = host.Trim(); if (port != null) port = port.Trim(); if (!string.IsNullOrEmpty(protocol)) { server = protocol + ":"; } if (string.IsNullOrEmpty(host)) { server += "localhost:"; ; } else { server += host + ":"; } if (string.IsNullOrEmpty(port)) { server += "1666"; } else { server += port; } return server; } public bool IsCaseSensitive { get { return _isCaseSensitive; } set { _isCaseSensitive = value; } } public bool IsUnicode { get { return _isUnicode; } set { _isUnicode = value; } } public int ApiLevel { get { return _apiLevel; } set { _apiLevel = value; } } public bool RequiresLogin { get { return _requiresLogin; } set { _requiresLogin = value; } } public bool ServerValid { get { return _serverValid; } set { _serverValid = value; } } public string User { get { return _user; } set { _user = value; } } public bool UserValid { get { return _userValid; } set { _userValid = value; } } public string Password { get { return _password; } set { _password = value; } } public bool PasswordValid { get { return _passwordValid; } set { _passwordValid = value; } } public bool PasswordChanged { get { return _passwordChanged; } set { _passwordChanged = value; } } public string Workspace { get { return _workspace; } set { _workspace = value; } } public bool WorkspaceValid { get { return _workspaceValid; } set { _workspaceValid = value; } } public string Hostname { get { return _hostname; } set { _hostname = value; } } public string Charset { get { return _charset; } set { _charset = value; } } public bool MetafilesValid { get { return _metafilesValid; } set { _metafilesValid = value; } } public bool ProjectRootValid { get { return _projectRootValid; } set { _projectRootValid = value; } } public bool Valid { get { return MetafilesValid && ServerValid && UserValid && WorkspaceValid && PasswordValid && ProjectRootValid; } } #endregion public override string ToString() { string svr = "Server: " + Server + " " + (ServerValid ? "valid" : "invalid") + "\n"; string usr = "User: " + User + " " + (UserValid ? "valid" : "invalid") + "\n"; string pwd = "Password: " + Password + " " + (PasswordValid ? "valid" : "invalid") + " " + (PasswordChanged ? "changed" : "unchanged") + "\n"; string wsp = "Workspace: " + Workspace + " " + (WorkspaceValid ? "valid" : "invalid") + "\n"; string msc = "Hostname: " + Hostname + " " + "Charset: " + Charset + "\n"; return svr + usr + pwd + wsp + msc; } /// /// Default Constructor /// public ConnectionConfig() { Reset(); } /// /// Copy Constructor /// /// public ConnectionConfig(ConnectionConfig other) { Server = other.Server; ServerValid = other.ServerValid; User = other.User; UserValid = other.UserValid; Password = other.Password; PasswordValid = other.PasswordValid; Workspace = other.Workspace; WorkspaceValid = other.WorkspaceValid; Hostname = other.Hostname; Charset = other.Charset; MetafilesValid = other.MetafilesValid; ProjectRootValid = other.ProjectRootValid; IsCaseSensitive = other.IsCaseSensitive; IsUnicode = other.IsUnicode; ApiLevel = other.ApiLevel; RequiresLogin = other.RequiresLogin; Tested = other.Tested; } public ConnectionConfig(bool bConfig) { this.FromConfig(); } public string ToXml() { StringWriter myWriter = new StringWriter(); ConnectionSerializer.Serialize(myWriter, this); return myWriter.ToString(); } public bool Equals(ConnectionConfig other) { if (other == null) return false; if (other == this) return true; string myxml = ToXml(); string otherxml = other.ToXml(); bool rv = string.Equals(myxml, otherxml); if (!rv) { Debug.Log("Equals 1:" + myxml); Debug.Log("Equals 2:" + otherxml); } return rv; } public bool Matches(ConnectionConfig other) { if (!string.Equals(Server, other.Server)) return false; if (!string.Equals(User, other.User)) return false; if (!string.Equals(Password, other.Password)) return false; if (!string.Equals(Workspace, other.Workspace)) return false; if (!string.Equals(Hostname, other.Hostname)) return false; if (!string.Equals(Charset, other.Charset)) return false; return true; } public bool MatchesConfig() { if (!string.Equals(Server, Config.ServerUri)) return false; if (!string.Equals(User, Config.Username)) return false; if (!string.Equals(Password, Config.Password)) return false; if (!string.Equals(Workspace, Config.Workspace)) return false; if (!string.Equals(Hostname, Config.Hostname)) return false; if (!string.Equals(Charset, Config.Charset)) return false; return true; } /// /// Copy ConnectionConfig Information into Config Variables /// public void ToConfig() { Config.ServerUri = Server; Config.Username = User; Config.Password = Password; Config.Workspace = Workspace; Config.Hostname = Hostname; Config.Charset = Charset; } /// /// Copy Config Variables into ConnectionConfig /// public void FromConfig() { Server = Config.ServerUri; User = Config.Username; Password = Config.Password; Workspace = Config.Workspace; Hostname = Config.Hostname; Charset = Config.Charset; Reset(); } public void Reset() { MetafilesValid = ServerValid = UserValid = PasswordValid = WorkspaceValid = ProjectRootValid = false; IsCaseSensitive = IsUnicode = RequiresLogin = PasswordChanged = false; ApiLevel = 0; Tested = false; } public string Summary() { if (!Tested) return "Untested"; if (Valid) return "Connection Valid"; if (!MetafilesValid) return "Metafiles Misconfigured"; if (!ServerValid) return "Server invalid"; if (!UserValid) return "User invalid"; if (!PasswordValid) return "Password invalid"; if (!WorkspaceValid) return "Workspace invalid"; if (!ProjectRootValid) return "Project root invalid"; return ("unknown"); } } }