using UnityEditor;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using Perforce.P4;
using log4net;
namespace P4Connect
{
// Use the Config Class to hold current configuration and persist it through runs.
[Serializable]
public class Config : ScriptableObject
{
private static readonly ILog log = LogManager.GetLogger(typeof(Config));
public const string P4ConfigDefault = ".p4config";
public const string P4BridgeDllName = "p4bridge.dll";
public const string P4BridgeDylibName = "libp4bridge.dylib";
// Config Class mostly static but is also a singleton
private static Config _pInstance;
public static Config Instance
{
get
{
if (_pInstance == null)
{
//Debug.Log("Config CreateInstance");
_pInstance = ScriptableObject.CreateInstance("Config") as Config;
}
if (_pInstance == null)
{
Debug.Log("P4 Pinstance is null");
}
return _pInstance;
}
}
// Helper property to indicate that P4 can be used
public static bool ValidConfiguration { get { return PerforceEnabled && Valid; } }
// Event triggered when the configuration changes
public delegate void OnPrefsChanged();
public static event OnPrefsChanged PrefsChanged;
public void OnEnable()
{
hideFlags = HideFlags.HideAndDontSave;
_pInstance = this;
}
///
/// Reads connection settings from Prefs at least once
///
public static void Initialize()
{
// If No Config values are set, so load them in from the Config Asset or EditorPrefs
if (string.IsNullOrEmpty(Config.ServerUri))
{
ResetValues();
bool foundConfigAsset = ReadConfigAsset();
if (foundConfigAsset)
{
Debug.Log("Loading Configuration Asset");
Config.SaveMode = SaveSettingsMode.ConfigAsset;
}
else
{
Debug.Log("Loading Configuration EditorPrefs");
Config.SaveMode = SaveSettingsMode.EditorPrefs;
ReadPrefs();
}
LastSavedConnection = new ConnectionConfig(true);
DefaultConnection = new ConnectionConfig(true);
}
if (PerforceEnabled)
{
if (EnableLog)
{
Logger.Initialize(); // initialize logging if not done before
}
if (DefaultConnection.Tested && DefaultConnection.Valid)
{
Valid = true;
LastGoodConnection = new ConnectionConfig(DefaultConnection);
//Debug.Log("Reconnecting ...");
}
else
{
// Create a Default Connection from the Config settings.
Config.DefaultConnection.FromConfig();
Valid = CheckSettings(DefaultConnection);
}
}
}
///
/// Checks the that settings are valid
///
public static bool CheckSettings(ConnectionConfig cfg)
{
//Debug.Log("CheckSettings: " + cfg.ToString());
if (PerforceEnabled)
{
bool goodConnection = TestConnectionConfig(cfg); // Test Configuration
if (!goodConnection)
{
Debug.LogWarning("P4Connect - Perforce integration is enabled but inactive: " + cfg.Summary() + "\n Go to Edit->Perforce Settings to update your settings");
}
else
{
ConnectionWizard.CloseWindows();
PasswordPrompt.CloseWindows();
// Debug.Log("P4Connect - Perforce Integration is Active");
return true;
}
}
return false;
}
///
/// Updates the current configuration state after checking all the settings
///
public static bool TestConnectionConfig(ConnectionConfig cfg)
{
bool returnValue = false;
const string title = "P4Connect - Hold on";
if (Config.LastGoodConnection.Valid && Config.LastGoodConnection.Matches(cfg))
{
//Debug.Log("Using LastGoodConnection");
cfg = LastGoodConnection; // Use LastGoodConnection
return true;
}
try
{
cfg.Tested = true;
if (!cfg.MetafilesValid)
{
EditorUtility.DisplayProgressBar(title, "Checking Meta Files", 0.2f);
cfg.MetafilesValid = P4Connect.VerifySettings.CheckMetaFiles();
}
if (cfg.MetafilesValid && !cfg.ServerValid)
{
EditorUtility.DisplayProgressBar(title, "Checking Server", 0.4f);
cfg.ServerValid = P4Connect.VerifySettings.CheckServerUri(cfg);
}
if (cfg.ServerValid && !cfg.PasswordValid)
{
EditorUtility.DisplayProgressBar(title, "Checking Login", 0.6f);
cfg.PasswordValid = P4Connect.VerifySettings.CheckUsernamePassword(cfg);
}
if (cfg.PasswordValid && !cfg.WorkspaceValid)
{
EditorUtility.DisplayProgressBar(title, "Checking Workspace", 0.8f);
cfg.WorkspaceValid = P4Connect.VerifySettings.CheckWorkspace(cfg);
}
if (cfg.WorkspaceValid && !cfg.ProjectRootValid)
{
EditorUtility.DisplayProgressBar(title, "Checking Project Root", 0.9f);
cfg.ProjectRootValid = P4Connect.VerifySettings.CheckProjectRoot(cfg);
}
returnValue = cfg.ProjectRootValid;
}
catch (Exception ex)
{
log.Debug("TestConnectionConfig exception: " + ex.ToString());
log.Debug("TestConnectionConfig exception", ex);
}
EditorUtility.ClearProgressBar();
if (returnValue == true)
{
// Debug.Log("Saving LastGoodConnection");
Config.LastGoodConnection = cfg;
}
return returnValue;
}
public static void SetProjectRootDirectory()
{
if (Config.ValidConfiguration)
{
//log.Debug("conn status: " + DefaultConnection.Summary() );
Engine.PerformConnectionOperation(con =>
{
// project root in perforce syntax
var spec = FileSpec.LocalSpec(System.IO.Path.Combine(Main.RootPath, "..."));
var mappings = con.P4Client.GetClientFileMappings(spec);
if (mappings != null && mappings.Count > 0)
{
// string ProjectRoot;
ClientProjectRoot = mappings[0].ClientPath.Path;
DepotProjectRoot = mappings[0].DepotPath.Path;
//log.Debug("ClientProjectRoot: " + ClientProjectRoot);
//log.Debug("DepotProjectRoot: " + DepotProjectRoot);
}
else
{
Debug.LogError("Unable to determine Project Root! ");
}
});
}
}
#region Instance Variables
[SerializeField]
private string _serverUri;
[SerializeField]
private string _username;
[SerializeField]
private string _password;
[SerializeField]
private string _workspace;
[SerializeField]
private bool _unityVsSupport;
[SerializeField]
private bool _perforceEnabled;
[SerializeField]
private bool _includeSolutionFiles;
[SerializeField]
private bool _includeProjectFiles;
[SerializeField]
private bool _showPaths;
[SerializeField]
private bool _askBeforeCheckout;
[SerializeField]
private bool _displayStatusIcons;
[SerializeField]
private string _hostname;
[SerializeField]
private string _charset;
[SerializeField]
private string _diffToolPathname;
[SerializeField]
private bool _displayP4Timings;
[SerializeField]
private bool _echoP4Commands;
[SerializeField]
private bool _checkStatusForMenus;
[SerializeField]
private bool _warnOnSpecialCharacters;
[SerializeField]
private int _checkStatusForMenusMaxItems;
[SerializeField]
private int _operationBatchCount;
[SerializeField]
private int _connectionTimeOut;
[SerializeField]
private string _ignoreName;
[SerializeField]
private string _ignoreLines;
[SerializeField]
private bool _useTypemap;
[SerializeField]
private bool _enableLog;
[SerializeField]
private Logger.LogLevel _consoleLogLevel;
[SerializeField]
private string _logPath;
[SerializeField]
private string _clientProjectRootMatch;
[SerializeField]
private IList _projectFileSpec;
[SerializeField]
private string _depotProjectRoot;
[SerializeField]
private bool _valid;
[SerializeField]
private string _clientProjectRoot;
[SerializeField]
private SaveSettingsMode _currentSaveMode = SaveSettingsMode.EditorPrefs;
[SerializeField]
private ConnectionConfig _defaultConnection;
[SerializeField]
private ConnectionConfig _lastGoodConnection;
[SerializeField]
private ConnectionConfig _lastSavedConnection;
#endregion
#region Properties
public static string ServerUri
{
get { return Instance._serverUri; }
set { Instance._serverUri = value; }
}
public static string Username
{
get { return Instance._username; }
set { Instance._username = value; }
}
public static string Password
{
get { return Instance._password; }
set { Instance._password = value; }
}
public static string Workspace
{
get { return Instance._workspace; }
set { Instance._workspace = value; }
}
public static bool UnityVsSupport
{
get { return Instance._unityVsSupport; }
set { Instance._unityVsSupport = value; }
}
public static bool PerforceEnabled
{
get { return Instance._perforceEnabled; }
set { Instance._perforceEnabled = value; }
}
public static bool IncludeSolutionFiles
{
get { return Instance._includeSolutionFiles; }
set { Instance._includeSolutionFiles = value; }
}
public static bool IncludeProjectFiles
{
get { return Instance._includeProjectFiles; }
set { Instance._includeProjectFiles = value; }
}
public static bool ShowPaths
{
get { return Instance._showPaths; }
set { Instance._showPaths = value; }
}
public static bool AskBeforeCheckout
{
get { return Instance._askBeforeCheckout; }
set { Instance._askBeforeCheckout = value; }
}
public static bool DisplayStatusIcons
{
get { return Instance._displayStatusIcons; }
set { Instance._displayStatusIcons = value; }
}
public static string Hostname
{
get { return Instance._hostname; }
set { Instance._hostname = value; }
}
public static string Charset
{
get { return Instance._charset; }
set { Instance._charset = value; }
}
public static string DiffToolPathname
{
get { return Instance._diffToolPathname; }
set { Instance._diffToolPathname = value; }
}
public static bool DisplayP4Timings
{
get { return Instance._displayP4Timings; }
set { Instance._displayP4Timings = value; }
}
public static bool EchoP4Commands
{
get { return Instance._echoP4Commands; }
set { Instance._echoP4Commands = value; }
}
public static bool CheckStatusForMenus
{
get { return Instance._checkStatusForMenus; }
set { Instance._checkStatusForMenus = value; }
}
public static bool WarnOnSpecialCharacters
{
get { return Instance._warnOnSpecialCharacters; }
set { Instance._warnOnSpecialCharacters = value; }
}
public static int CheckStatusForMenusMaxItems
{
get { return Instance._checkStatusForMenusMaxItems; }
set { Instance._checkStatusForMenusMaxItems = value; }
}
public static int OperationBatchCount
{
get { return Instance._operationBatchCount; }
set { Instance._operationBatchCount = value; }
}
public static int ConnectionTimeOut
{
get { return Instance._connectionTimeOut; }
set { Instance._connectionTimeOut = value; }
}
public static string IgnoreName
{
get { return Instance._ignoreName; }
set { Instance._ignoreName = value; }
}
public static string IgnoreLines
{
get { return Instance._ignoreLines; }
set { Instance._ignoreLines = value; }
}
public static bool UseTypemap
{
get { return Instance._useTypemap; }
set { Instance._useTypemap = value; }
}
public static bool EnableLog
{
get { return Instance._enableLog; }
set { Instance._enableLog = value; }
}
public static Logger.LogLevel ConsoleLogLevel
{
get { return Instance._consoleLogLevel; }
set { Instance._consoleLogLevel = value; }
}
public static string LogPath
{
get { return Instance._logPath; }
set { Instance._logPath = value; }
}
public static string ClientProjectRoot // Client path associated with project root (has /...)
{
get
{
return (Instance._clientProjectRoot);
}
set
{
Instance._clientProjectRoot = value;
ClientProjectRootMatch = ClientProjectRoot.Substring(0, Math.Max(0, ClientProjectRoot.Length - 4));
ProjectFileSpec = FileSpec.ClientSpecList(new string[1] { Instance._clientProjectRoot });
}
}
// Client path without the /... stuff.
public static string ClientProjectRootMatch
{
set { Instance._clientProjectRootMatch = value; }
get { return Instance._clientProjectRootMatch; }
}
// File Spec which describes the scope of the project (with /...)
public static IList ProjectFileSpec
{
set { Instance._projectFileSpec = value; }
get { return Instance._projectFileSpec; }
}
// Depot path associated with project root (has /...)
public static string DepotProjectRoot
{
get { return Instance._depotProjectRoot; }
set { Instance._depotProjectRoot = value; }
}
// Connection Settings Have been Tested
public static bool Valid
{
set { Instance._valid = value; }
get { return Instance._valid; }
}
public static SaveSettingsMode SaveMode
{
get { return Instance._currentSaveMode; }
set { Instance._currentSaveMode = value; }
}
public static ConnectionConfig DefaultConnection
{
get
{
if (Instance._defaultConnection == null)
{
Instance._defaultConnection = new ConnectionConfig();
}
return (Instance._defaultConnection);
}
set { Instance._defaultConnection = value; }
}
public static ConnectionConfig LastGoodConnection
{
get
{
if (Instance._lastGoodConnection == null)
{
Instance._lastGoodConnection = new ConnectionConfig();
}
return (Instance._lastGoodConnection);
}
set { Instance._lastGoodConnection = value; }
}
public static ConnectionConfig LastSavedConnection
{
get
{
if (Instance._lastSavedConnection == null)
{
Instance._lastSavedConnection = new ConnectionConfig();
}
return (Instance._lastSavedConnection);
}
set { Instance._lastSavedConnection = value; }
}
#endregion
public enum SaveSettingsMode
{
EditorPrefs,
ConfigAsset,
}
public static void ResetValues()
{
// Set some default values
ServerUri = "localhost:1666";
Username = Environment.UserName.Replace(' ', '_');
Password = "";
Workspace = (Username + "_" + Main.ProjectName + "_" + Environment.MachineName).Replace(' ', '_');
UnityVsSupport = false;
PerforceEnabled = false;
IncludeProjectFiles = false;
IncludeSolutionFiles = false;
ShowPaths = false;
AskBeforeCheckout = false;
DisplayStatusIcons = true;
Hostname = "";
Charset = "";
DiffToolPathname = "";
DisplayP4Timings = false;
EchoP4Commands = false;
CheckStatusForMenus = true;
CheckStatusForMenusMaxItems = 10;
ConnectionTimeOut = 30;
WarnOnSpecialCharacters = true;
IgnoreName = "";
IgnoreLines = "";
UseTypemap = false;
EnableLog = false;
LogPath = DefaultLogFile();
ConsoleLogLevel = Logger.LogLevel.Info;
}
// Get an OS dependent path to the default p4connect log file.
public static string DefaultLogFile()
{
string rv = "";
if (Application.platform == RuntimePlatform.OSXEditor)
{
rv = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library/Logs/Unity/p4connect.log");
}
else // (Application.platform == RuntimePlatform.WindowsEditor)
{
rv = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Unity\\Editor\\p4connect.log");
}
rv = rv.Replace('/', System.IO.Path.DirectorySeparatorChar);
//Debug.Log("DefaultLogFile: " + rv);
return rv;
}
#region P4CONFIG
public static string FindP4ConfigFile(string config)
{
if (!String.IsNullOrEmpty(config))
{
string path = Application.dataPath;
while (path != null)
{
var directoryName = Path.GetDirectoryName(path);
if (!String.IsNullOrEmpty(directoryName))
{
string[] files = System.IO.Directory.GetFiles(directoryName, config);
if (files.Any())
{
return files[0];
}
}
path = directoryName;
}
}
return null;
}
public static void LoadP4ConfigFile(string path)
{
string line;
char[] equalsChars = { '=' };
System.IO.StreamReader file = new System.IO.StreamReader(path);
while ((line = file.ReadLine()) != null)
{
string[] segments = line.Split(equalsChars);
if (segments.Length >= 2)
{
string key = segments[0];
string val = segments[1];
switch (segments[0])
{
case "P4PORT":
{
ServerUri = val;
}
break;
case "P4USER":
{
Username = val;
}
break;
case "P4CLIENT":
{
Workspace = val;
}
break;
case "P4PASSWD":
{
Password = val;
}
break;
case "P4HOST":
{
Hostname = val;
}
break;
case "P4CHARSET":
{
Charset = val;
}
break;
}
}
}
file.Close();
}
#endregion
#region Environment
// Check for the "well known" Perforce environment variables.
public static bool ReadEnvironment()
{
bool found = false;
string value = Environment.GetEnvironmentVariable("P4PORT");
if (value != null)
{
Config.ServerUri = value;
found = true;
}
value = Environment.GetEnvironmentVariable("P4USER");
if (value != null)
{
Config.Username = value;
found = true;
}
value = Environment.GetEnvironmentVariable("P4PASSWD");
if (value != null)
{
Config.Password = value;
found = true;
}
value = Environment.GetEnvironmentVariable("P4CLIENT");
if (value != null)
{
Config.Workspace = value;
found = true;
}
value = Environment.GetEnvironmentVariable("P4HOST");
if (value != null)
{
Config.Hostname = value;
found = true;
}
value = Environment.GetEnvironmentVariable("P4CHARSET");
if (value != null)
{
Config.Charset = value;
found = true;
}
value = Environment.GetEnvironmentVariable("P4IGNORE");
if (value != null)
{
Config.IgnoreName = value;
found = true;
}
//Config.Refresh();
return found;
}
#endregion
#region ConfigAsset
static string configAssetPath = "Assets/P4Connect/Editor/Config.asset";
public static void WriteConfigAsset()
{
ConfigAsset asset = ScriptableObject.CreateInstance();
asset.CopyConfigToAsset();
AssetDatabase.CreateAsset(asset, configAssetPath);
AssetDatabase.SaveAssets();
}
public static bool ReadConfigAsset()
{
ConfigAsset asset = AssetDatabase.LoadAssetAtPath(configAssetPath, (typeof(ConfigAsset))) as ConfigAsset;
if (asset != null)
{
asset.CopyAssetToConfig();
return true;
}
else
{
return false;
}
}
public static void DeleteConfigAsset()
{
if (!AssetDatabase.DeleteAsset(configAssetPath))
{
System.IO.File.Delete(System.IO.Path.Combine(Main.RootPath, configAssetPath));
}
}
/* If the inspector is used to view a Config Asset, these properties control the presentation */
[CustomEditor(typeof(ConfigAsset))]
public class ConfigAssetPropertiesEditor : Editor
{
public override void OnInspectorGUI()
{
GUI.enabled = false;
DrawDefaultInspector();
GUI.enabled = true;
}
}
#endregion
#region Registry Names
// These are the names under which the connection settings are stored in the registry
public const string ServerUriPrefName = "ServerURI";
public const string UserNamePrefName = "UserName";
public const string PasswordPrefName = "Password";
public const string WorkspacePrefName = "Workspace";
public const string PerforceEnabledPrefName = "Enabled";
public const string UnityVsSupportPrefName = "UnityVSSupport";
public const string IncludeProjectFilesPrefName = "IncludeProjectFiles";
public const string IncludeSolutionFilesPrefName = "IncludeSolutionFiles";
public const string ShowPathsPrefName = "ShowPaths";
public const string AskBeforeCheckoutPrefName = "AskBeforeCheckout";
public const string DisplayStatusIconsPrefName = "DisplayStatusIcons";
public const string HostnamePrefName = "Hostname";
public const string DiffToolPathnamePrefName = "DiffToolPathname";
public const string DisplayP4TimingsPrefName = "DisplayTimings";
public const string DisplayP4CommandsPrefName = "DisplayCommands";
public const string CheckStatusForMenusPrefName = "CheckStatusForMenus";
public const string CheckStatusForMenusMaxItemsPrefName = "CheckStatusForMenusMaxItems";
public const string OperationBatchCountPrefName = "OperationBatchCount";
public const string ConnectionTimeOutPrefName = "ConnectionTimeOut";
public const string WarnOnSpecialCharactersPrefName = "WarnOnSpecialCharacters";
public const string UseIgnorePrefName = "UseIgnore";
public const string IgnoreNamePrefName = "IgnoreName";
public const string EnableLogPrefName = "EnableLog";
public const string ConsoleLogLevelPrefName = "ConsoleLogLevel";
public const string LogPathPrefName = "LogPath";
public const string IgnoreLinesPrefName = "IgnoreLines";
public const string UseTypemapPrefName = "UseTypemap";
#endregion
#region EditorPreferences
// Utility method to read connection prefs from the registry
public static void ReadPrefs()
{
// Check if the keys exist, and if so, read the values out
// Otherwise, leave the existing values
if (HasStringPrefNotEmpty(ServerUriPrefName))
ServerUri = GetPrefString(ServerUriPrefName);
if (HasStringPrefNotEmpty(UserNamePrefName))
Username = GetPrefString(UserNamePrefName);
if (HasStringPrefNotEmpty(PasswordPrefName))
{
Password = Secure.DecryptString(GetPrefString(PasswordPrefName));
}
if (HasStringPrefNotEmpty(WorkspacePrefName))
Workspace = GetPrefString(WorkspacePrefName);
if (HasPref(PerforceEnabledPrefName))
PerforceEnabled = GetPrefBool(PerforceEnabledPrefName);
if (HasPref(UnityVsSupportPrefName))
UnityVsSupport = GetPrefBool(UnityVsSupportPrefName);
if (HasPref(IncludeProjectFilesPrefName))
IncludeProjectFiles = GetPrefBool(IncludeProjectFilesPrefName);
if (HasPref(IncludeSolutionFilesPrefName))
IncludeSolutionFiles = GetPrefBool(IncludeSolutionFilesPrefName);
if (HasPref(ShowPathsPrefName))
ShowPaths = GetPrefBool(ShowPathsPrefName);
if (HasPref(AskBeforeCheckoutPrefName))
AskBeforeCheckout = GetPrefBool(AskBeforeCheckoutPrefName);
if (HasPref(DisplayStatusIconsPrefName))
DisplayStatusIcons = GetPrefBool(DisplayStatusIconsPrefName);
if (HasStringPrefNotEmpty(HostnamePrefName))
Hostname = GetPrefString(HostnamePrefName);
if (HasStringPrefNotEmpty(DiffToolPathnamePrefName))
DiffToolPathname = GetPrefString(DiffToolPathnamePrefName);
if (HasPref(DisplayP4TimingsPrefName))
DisplayP4Timings = GetPrefBool(DisplayP4TimingsPrefName);
if (HasPref(DisplayP4CommandsPrefName))
EchoP4Commands = GetPrefBool(DisplayP4CommandsPrefName);
if (HasPref(CheckStatusForMenusPrefName))
CheckStatusForMenus = GetPrefBool(CheckStatusForMenusPrefName);
if (HasPref(CheckStatusForMenusMaxItemsPrefName))
CheckStatusForMenusMaxItems = GetPrefInt(CheckStatusForMenusMaxItemsPrefName);
if (HasPref(OperationBatchCountPrefName))
OperationBatchCount = GetPrefInt(OperationBatchCountPrefName);
if (HasPref(ConnectionTimeOutPrefName))
ConnectionTimeOut = GetPrefInt(ConnectionTimeOutPrefName);
if (HasPref(WarnOnSpecialCharactersPrefName))
WarnOnSpecialCharacters = GetPrefBool(WarnOnSpecialCharactersPrefName);
if (HasStringPrefNotEmpty(IgnoreNamePrefName))
IgnoreName = GetPrefString(IgnoreNamePrefName);
if (HasStringPrefNotEmpty(IgnoreLinesPrefName))
IgnoreLines = GetPrefString(IgnoreLinesPrefName);
if (HasPref(UseTypemapPrefName))
UseTypemap = GetPrefBool(UseTypemapPrefName);
if (HasPref(EnableLogPrefName))
EnableLog = GetPrefBool(EnableLogPrefName);
if (HasPref(ConsoleLogLevelPrefName))
ConsoleLogLevel = (Logger.LogLevel)GetPrefInt(ConsoleLogLevelPrefName);
if (HasStringPrefNotEmpty(LogPathPrefName))
LogPath = GetPrefString(LogPathPrefName);
//Config.Refresh();
// Notify users that prefs changed
if (PrefsChanged != null)
PrefsChanged();
}
// Utility method to write our the connection prefs to the registry
public static void WritePrefs()
{
SetPrefString(ServerUriPrefName, ServerUri);
SetPrefString(UserNamePrefName, Username);
if (!String.IsNullOrEmpty(Password))
{
//SetPrefString(PasswordPrefName, Password);
SetPrefString(PasswordPrefName, Secure.EncryptString(Password));
}
SetPrefString(WorkspacePrefName, Workspace);
SetPrefBool(PerforceEnabledPrefName, PerforceEnabled);
SetPrefBool(UnityVsSupportPrefName, UnityVsSupport);
SetPrefBool(IncludeProjectFilesPrefName, IncludeProjectFiles);
SetPrefBool(IncludeSolutionFilesPrefName, IncludeSolutionFiles);
SetPrefBool(ShowPathsPrefName, ShowPaths);
SetPrefBool(AskBeforeCheckoutPrefName, AskBeforeCheckout);
SetPrefBool(DisplayStatusIconsPrefName, DisplayStatusIcons);
SetPrefString(HostnamePrefName, Hostname);
SetPrefString(DiffToolPathnamePrefName, DiffToolPathname);
SetPrefBool(DisplayP4TimingsPrefName, DisplayP4Timings);
SetPrefBool(DisplayP4CommandsPrefName, EchoP4Commands);
SetPrefBool(CheckStatusForMenusPrefName, CheckStatusForMenus);
SetPrefInt(CheckStatusForMenusMaxItemsPrefName, CheckStatusForMenusMaxItems);
SetPrefInt(OperationBatchCountPrefName, OperationBatchCount);
SetPrefInt(ConnectionTimeOutPrefName, ConnectionTimeOut);
SetPrefBool(WarnOnSpecialCharactersPrefName, WarnOnSpecialCharacters);
SetPrefString(IgnoreNamePrefName, IgnoreName);
SetPrefString(IgnoreLinesPrefName, IgnoreLines);
SetPrefBool(UseTypemapPrefName, UseTypemap);
SetPrefBool(EnableLogPrefName, EnableLog);
SetPrefInt(ConsoleLogLevelPrefName, (int)ConsoleLogLevel);
SetPrefString(LogPathPrefName, LogPath);
}
static string GetFullPrefName(string aPrefName)
{
return "P4Connect_" + Main.ProjectName + "_" + aPrefName;
}
static bool HasPref(string aPrefName)
{
return EditorPrefs.HasKey(GetFullPrefName(aPrefName));
}
static bool HasStringPrefNotEmpty(string aPrefName)
{
return (!String.IsNullOrEmpty(EditorPrefs.GetString(GetFullPrefName(aPrefName))));
}
static void SetPrefString(string aPrefName, string aPref)
{
EditorPrefs.SetString(GetFullPrefName(aPrefName), aPref);
}
static void SetPrefInt(string aPrefName, int aPref)
{
EditorPrefs.SetInt(GetFullPrefName(aPrefName), aPref);
}
static void SetPrefBool(string aPrefName, bool aPref)
{
EditorPrefs.SetBool(GetFullPrefName(aPrefName), aPref);
}
static string GetPrefString(string aPrefName)
{
return EditorPrefs.GetString(GetFullPrefName(aPrefName));
}
static int GetPrefInt(string aPrefName)
{
return EditorPrefs.GetInt(GetFullPrefName(aPrefName));
}
static bool GetPrefBool(string aPrefName)
{
return EditorPrefs.GetBool(GetFullPrefName(aPrefName));
}
// [MenuItem("Edit/Delete Project EditorPrefs", false, 300)]
static void DeleteAllEditorPrefs()
{
// Delete All keys for this project
EditorPrefs.DeleteKey(ServerUriPrefName);
EditorPrefs.DeleteKey(UserNamePrefName);
EditorPrefs.DeleteKey(PasswordPrefName);
EditorPrefs.DeleteKey(WorkspacePrefName);
EditorPrefs.DeleteKey(UnityVsSupportPrefName);
EditorPrefs.DeleteKey(IncludeProjectFilesPrefName);
EditorPrefs.DeleteKey(IncludeSolutionFilesPrefName);
EditorPrefs.DeleteKey(ShowPathsPrefName);
EditorPrefs.DeleteKey(AskBeforeCheckoutPrefName);
EditorPrefs.DeleteKey(DisplayStatusIconsPrefName);
EditorPrefs.DeleteKey(HostnamePrefName);
EditorPrefs.DeleteKey(DiffToolPathnamePrefName);
EditorPrefs.DeleteKey(DisplayP4TimingsPrefName);
EditorPrefs.DeleteKey(DisplayP4CommandsPrefName);
EditorPrefs.DeleteKey(CheckStatusForMenusPrefName);
EditorPrefs.DeleteKey(CheckStatusForMenusMaxItemsPrefName);
EditorPrefs.DeleteKey(OperationBatchCountPrefName);
EditorPrefs.DeleteKey(ConnectionTimeOutPrefName);
EditorPrefs.DeleteKey(WarnOnSpecialCharactersPrefName);
EditorPrefs.DeleteKey(IgnoreNamePrefName);
EditorPrefs.DeleteKey(IgnoreLinesPrefName);
EditorPrefs.DeleteKey(UseTypemapPrefName);
EditorPrefs.DeleteKey(EnableLogPrefName);
EditorPrefs.DeleteKey(ConsoleLogLevelPrefName);
}
#endregion
}
[Serializable]
public class ConfigAsset : ScriptableObject
{
// These are the config values
// We serialize this Class and place it in the P4Connect/Editor Asset Hierarchy
public string ServerUri;
public string Username;
[HideInInspector]
public string Password;
public string Workspace;
public string Hostname;
public string Charset;
public bool UnityVsSupport;
public bool PerforceEnabled;
public bool IncludeProjectFiles;
public bool IncludeSolutionFiles;
public bool ShowPaths;
public bool AskBeforeCheckout;
public bool DisplayStatusIcons;
public string DiffToolPathname;
public bool DisplayP4Timings;
public bool DisplayP4Commands;
public bool CheckStatusForMenus;
public int CheckStatusForMenusMaxItems;
public int ConnectionTimeOut;
public bool WarnOnSpecialCharacters;
public string IgnoreName;
public bool EnableLog;
public Logger.LogLevel ConsoleLogLevel;
public string LogPath;
public string IgnoreLines;
public bool UseTypemap;
// Copy the contents of this ConfigAsset into the P4Connect Config class.
public void CopyAssetToConfig()
{
Config.ServerUri = ServerUri;
Config.Username = Username;
Config.Password = Password;
Config.Workspace = Workspace;
Config.Hostname = Hostname;
Config.Charset = Charset;
Config.UnityVsSupport = UnityVsSupport;
Config.PerforceEnabled = PerforceEnabled;
Config.IncludeProjectFiles = IncludeProjectFiles;
Config.IncludeSolutionFiles = IncludeSolutionFiles;
Config.ShowPaths = ShowPaths;
Config.AskBeforeCheckout = AskBeforeCheckout;
Config.DisplayStatusIcons = DisplayStatusIcons;
Config.DiffToolPathname = DiffToolPathname;
Config.DisplayP4Timings = DisplayP4Timings;
Config.EchoP4Commands = DisplayP4Commands;
Config.CheckStatusForMenus = CheckStatusForMenus;
Config.CheckStatusForMenusMaxItems = CheckStatusForMenusMaxItems;
Config.ConnectionTimeOut = ConnectionTimeOut;
Config.WarnOnSpecialCharacters = WarnOnSpecialCharacters;
Config.IgnoreName = IgnoreName;
Config.EnableLog = EnableLog;
Config.ConsoleLogLevel = ConsoleLogLevel;
Config.LogPath = LogPath;
Config.IgnoreLines = IgnoreLines;
Config.UseTypemap = UseTypemap;
}
// Seed a ConfigAsset with data from the Config Class
public void CopyConfigToAsset()
{
ServerUri = Config.ServerUri;
Username = Config.Username;
Password = Config.Password;
Workspace = Config.Workspace;
Hostname = Config.Hostname;
Charset = Config.Charset;
UnityVsSupport = Config.UnityVsSupport;
PerforceEnabled = Config.PerforceEnabled;
IncludeProjectFiles = Config.IncludeProjectFiles;
IncludeSolutionFiles = Config.IncludeSolutionFiles;
ShowPaths = Config.ShowPaths;
AskBeforeCheckout = Config.AskBeforeCheckout;
DisplayStatusIcons = Config.DisplayStatusIcons;
DiffToolPathname = Config.DiffToolPathname;
DisplayP4Timings = Config.DisplayP4Timings;
DisplayP4Commands = Config.EchoP4Commands;
CheckStatusForMenus = Config.CheckStatusForMenus;
CheckStatusForMenusMaxItems = Config.CheckStatusForMenusMaxItems;
ConnectionTimeOut = Config.ConnectionTimeOut;
WarnOnSpecialCharacters = Config.WarnOnSpecialCharacters;
IgnoreName = Config.IgnoreName;
EnableLog = Config.EnableLog;
ConsoleLogLevel = Config.ConsoleLogLevel;
LogPath = Config.LogPath;
IgnoreLines = Config.IgnoreLines;
UseTypemap = Config.UseTypemap;
}
}
}