using System;
using System.IO;
using System.Linq;
using System.Windows;
using Ubisoft.Perforce;
namespace JD.Perforce
{
public class PerforceUtils
{
///
/// Usual UAF perforce connection file name
///
private const string ConfigFileName = "p4Config.txt";
///
/// Replace slash in string helpers
///
///
///
public string PathToP4Path(string path)
{
return path.Replace(@"\", "/");
}
///
/// 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.
///
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;
}
///
/// Check if the PerforceConnectionProvider is correctly set.
///
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);
}
///
/// Parse the UAF p4Config.txt file to retrive perforce connection settings into a PerforceWrapper.PerforceConnectionProvider instance
///
/// Perforce connection config file path
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;
}
///
/// Try to connect to perforce using config file settings.
///
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;
}
}
}
}