138 lines
5.4 KiB
C#
138 lines
5.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using Ubisoft.Perforce;
|
|
|
|
namespace JD.Perforce
|
|
{
|
|
public class PerforceUtils
|
|
{
|
|
/// <summary>
|
|
/// Usual UAF perforce connection file name
|
|
/// </summary>
|
|
private const string ConfigFileName = "p4Config.txt";
|
|
|
|
/// <summary>
|
|
/// Replace slash in string helpers
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public string PathToP4Path(string path)
|
|
{
|
|
return path.Replace(@"\", "/");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Try to find the Just dance p4config.txt perforce configuration file.
|
|
/// It looks first in root data directory provided by client (dataRootPath)
|
|
/// and then in its first level subdirectory.
|
|
/// </summary>
|
|
public static string GetConfigFilePath(string dataRootPath)
|
|
{
|
|
string fileConfigPath = null;
|
|
|
|
if (!string.IsNullOrEmpty(dataRootPath))
|
|
{
|
|
var dataRootDir = new DirectoryInfo(dataRootPath);
|
|
if (dataRootDir.Exists)
|
|
{
|
|
fileConfigPath = Path.Combine(dataRootDir.FullName, ConfigFileName);
|
|
if (File.Exists(fileConfigPath) == false)
|
|
{
|
|
var p4ConfigDir = dataRootDir.Parent;
|
|
fileConfigPath = GetConfigFilePath(p4ConfigDir.FullName);
|
|
}
|
|
}
|
|
}
|
|
|
|
return fileConfigPath;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if the PerforceConnectionProvider is correctly set.
|
|
/// </summary>
|
|
public static bool IsPerforceConnectionProviderValid(PerforceWrapper.PerforceConnectionProvider provider)
|
|
{
|
|
if (provider == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return !string.IsNullOrEmpty(provider.Port) && !string.IsNullOrEmpty(provider.UserName) && !string.IsNullOrEmpty(provider.Client);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse the UAF p4Config.txt file to retrive perforce connection settings into a PerforceWrapper.PerforceConnectionProvider instance
|
|
/// </summary>
|
|
/// <param name="configFilePath">Perforce connection config file path</param>
|
|
private PerforceWrapper.PerforceConnectionProvider LoadP4ConfigTextFile(string configFilePath)
|
|
{
|
|
PerforceWrapper.PerforceConnectionProvider perforceConnectionProvider = new PerforceWrapper.PerforceConnectionProvider();
|
|
perforceConnectionProvider.ConnectionInfos["DataRoot"] = configFilePath;
|
|
using (StreamReader file = new StreamReader(configFilePath))
|
|
{
|
|
string line;
|
|
while ((line = file.ReadLine()) != null)
|
|
{
|
|
string[] words = line.Split('=');
|
|
if (words.Length > 1)
|
|
{
|
|
string key = words[0].ToLower().Trim();
|
|
string param = words[1].Trim();
|
|
if (string.IsNullOrEmpty(key) == false)
|
|
{
|
|
if (string.Compare(key, "p4port", StringComparison.Ordinal) == 0)
|
|
{
|
|
perforceConnectionProvider.Port = !string.IsNullOrEmpty(param) ? param : null;
|
|
}
|
|
else if (string.Compare(key, "p4user", StringComparison.Ordinal) == 0)
|
|
{
|
|
perforceConnectionProvider.UserName = !string.IsNullOrEmpty(param) ? param : null;
|
|
}
|
|
else if (string.Compare(key, "p4client", StringComparison.Ordinal) == 0)
|
|
{
|
|
perforceConnectionProvider.Client = !string.IsNullOrEmpty(param) ? param : null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!IsPerforceConnectionProviderValid(perforceConnectionProvider))
|
|
{
|
|
string errorMessage = "The Perforce connection paramaters in " + configFilePath + " are not valid! Please check that P4PORT, P4USER and P4CLIENT are all correctly set.";
|
|
throw new Exception(errorMessage);
|
|
}
|
|
|
|
return perforceConnectionProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Try to connect to perforce using config file settings.
|
|
/// </summary>
|
|
public PerforceWrapper.PerforceConnectionProvider GetPerforceConnectionProvider(string dataRootPath, bool interactiveMode)
|
|
{
|
|
var configFilePath = GetConfigFilePath(dataRootPath);
|
|
if (string.IsNullOrEmpty(configFilePath))
|
|
{
|
|
if (interactiveMode)
|
|
{
|
|
MessageBox.Show("The data root directory should contain a P4Config.txt file\nwith your personal Perforce connection settings.",
|
|
"Perforce config file Error",
|
|
MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
return LoadP4ConfigTextFile(configFilePath);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|