// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Reflection; using System.Text; using EpicGames.Core; namespace EpicGames.Perforce { /// /// Settings for a new Perforce connection /// public interface IPerforceSettings { /// /// Server and port to connect to /// public string ServerAndPort { get; } /// /// Username to log in with /// public string UserName { get; } /// /// Password to use /// public string? Password { get; } /// /// Name of the current host /// public string? HostName { get; } /// /// Name of the client to use /// public string? ClientName { get; } /// /// The invoking application name /// public string? AppName { get; } /// /// The invoking application version /// public string? AppVersion { get; } /// /// Whether to create a native client rather than running the p4 child process, if possible /// public bool PreferNativeClient { get; } /// /// Whether to monitor the connection for commands that are not producing any output /// public bool EnableHangMonitor { get; } } /// /// Settings for a new Perforce connection /// public class PerforceSettings : IPerforceSettings { static readonly Assembly? s_entryAssembly = Assembly.GetEntryAssembly(); string? _appName; string? _appVersion; /// /// The default settings /// public static IPerforceSettings Default { get; } = new PerforceSettings(PerforceEnvironment.Default); /// /// Default app name to use for new settings objects /// public static string? DefaultAppName { get; set; } = s_entryAssembly?.GetName()?.Name; /// /// Default version string to use for new settings objects /// public static string? DefaultAppVersion { get; set; } = s_entryAssembly?.GetCustomAttribute()?.InformationalVersion ?? s_entryAssembly?.GetName()?.Version?.ToString(); /// public string ServerAndPort { get; set; } /// public string UserName { get; set; } /// public string? Password { get; set; } /// public string? HostName { get; set; } /// public string? ClientName { get; set; } /// public string? AppName { get => _appName ?? DefaultAppName; set => _appName = value; } /// public string? AppVersion { get => _appVersion ?? DefaultAppVersion; set => _appVersion = value; } /// public bool PreferNativeClient { get; set; } /// public bool EnableHangMonitor { get; set; } /// /// Default constructor /// public PerforceSettings(IPerforceEnvironment environment) { ServerAndPort = environment.GetValue("P4PORT") ?? "perforce:1666"; UserName = environment.GetValue("P4USER") ?? System.Environment.UserName; Password = environment.GetValue("P4PASSWD"); HostName = environment.GetValue("P4HOST"); ClientName = environment.GetValue("P4CLIENT"); EnableHangMonitor = true; } /// /// Constructor /// /// Server and port to connect with /// Username to connect with public PerforceSettings(string serverAndPort, string userName) { ServerAndPort = serverAndPort; UserName = userName; EnableHangMonitor = true; } /// /// Copy constructor /// public PerforceSettings(IPerforceSettings other) { ServerAndPort = other.ServerAndPort; UserName = other.UserName; Password = other.Password; HostName = other.HostName; ClientName = other.ClientName; _appName = other.AppName; _appVersion = other.AppVersion; PreferNativeClient = other.PreferNativeClient; EnableHangMonitor = other.EnableHangMonitor; } /// /// Get the Perforce settings for a particular directory, reading any necessary P4CONFIG and P4ENVIRO files /// /// /// public static PerforceSettings FromDirectory(DirectoryReference directory) { IPerforceEnvironment environment = PerforceEnvironment.FromDirectory(directory); return new PerforceSettings(environment); } } /// /// Extension methods for setting objects /// public static class PerforceSettingExtensions { /// /// Update common fields in a IPerforceSettings object /// public static IPerforceSettings MergeWith(this IPerforceSettings settings, string? newServerAndPort = null, string? newUserName = null, string? newClientName = null) { PerforceSettings newSettings = new PerforceSettings(settings); if (!String.IsNullOrEmpty(newServerAndPort)) { newSettings.ServerAndPort = newServerAndPort; } if (!String.IsNullOrEmpty(newUserName)) { newSettings.UserName = newUserName; } if (!String.IsNullOrEmpty(newClientName)) { newSettings.ClientName = newClientName; } return newSettings; } /// /// Gets the command line arguments to launch an external program, such as P4V or P4VC /// /// /// /// public static string GetArgumentsForExternalProgram(this IPerforceSettings settings, bool includeClient) { StringBuilder basicCommandArgs = new StringBuilder(); basicCommandArgs.AppendFormat("-p \"{0}\"", settings.ServerAndPort); basicCommandArgs.AppendFormat(" -u \"{0}\"", settings.UserName); if (includeClient && settings.ClientName != null) { basicCommandArgs.AppendFormat(" -c \"{0}\" ", settings.ClientName); } return basicCommandArgs.ToString(); } } }