JD2022-TU1/main/tools/legacy/ResourceTools/PerforceUtils.cs

225 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using P4API;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace ResourceTools
{
static public class PerforceUtils
{
static private string p4CfgFile = "p4config.txt";
static public string dataRootPath { get; set; }
static public void Connect()
{
try
{
ResourceTools.p4.Login(ResourceTools.p4Cfg.Password);
if (!ClientIsValid(ResourceTools.p4))
{
if (!Authentification())
return;
}
ResourceTools.isP4Connected = true;
}
catch
{
if (!Authentification())
return;
}
}
static public bool ClientIsValid(P4Connection p4)
{
P4RecordSet p4rs = p4.Run("clients");
foreach (P4Record p4r in p4rs)
{
if (p4r["client"] == p4.Client && p4r["Host"] == p4.Host && p4r["Owner"] == p4.User)
return true;
}
return false;
}
static private bool Authentification()
{
try
{
PerforceAuthFrm p4Frm = new PerforceAuthFrm();
if (p4Frm.ShowDialog() == DialogResult.OK)
{
ResourceTools.isP4Connected = true;
ResourceTools.p4Cfg.Port = ResourceTools.p4.Port;
ResourceTools.p4Cfg.User = ResourceTools.p4.User;
ResourceTools.p4Cfg.Client = ResourceTools.p4.Client;
SaveP4Config();
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
static public void LoadP4Config()
{
string p4ConfigPath = Path.Combine(dataRootPath, p4CfgFile);
if (File.Exists(p4ConfigPath))
{
string line;
using (StreamReader file = new StreamReader(p4ConfigPath))
{
while ((line = file.ReadLine()) != null)
{
if (line.StartsWith("p4port"))
{
ResourceTools.p4.Port = line.Split('=')[1];
}
if (line.StartsWith("p4user"))
{
ResourceTools.p4.User = line.Split('=')[1];
ResourceTools.p4.Password = ResourceTools.p4.User + "_00";
}
if (line.StartsWith("p4client"))
{
ResourceTools.p4.Client = line.Split('=')[1];
}
}
// Why are we duplicating this config in another member???
ResourceTools.p4Cfg.Port = ResourceTools.p4.Port;
ResourceTools.p4Cfg.User = ResourceTools.p4.User;
ResourceTools.p4Cfg.Password = ResourceTools.p4.User + "_00";
ResourceTools.p4Cfg.Client = ResourceTools.p4.Client;
}
}
else
{
MessageBox.Show("Missing P4 connection parameters!" + Environment.NewLine +
"Please configure UAF by creating the file " + p4ConfigPath + Environment.NewLine);
}
}
static public void SaveP4Config()
{
string p4ConfigPath = Path.Combine(dataRootPath, p4CfgFile);
using (StreamWriter file = new StreamWriter(p4ConfigPath))
{
file.Write("p4port=" + ResourceTools.p4Cfg.Port + "\n");
file.Write("p4user=" + ResourceTools.p4Cfg.User + "\n");
file.Write("p4client=" + ResourceTools.p4Cfg.Client + "\n");
file.Close();
}
}
static public string Encrypt(string clearText, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
static public string Decrypt(string cipherText, string Password)
{
try
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}
catch
{
return String.Empty;
}
}
static public bool IsOpenForEdit(string file)
{
P4RecordSet p4rs = ResourceTools.p4.Run("opened", file);
if (p4rs.Records.Length == 0)
return false;
return true;
}
static public void AddOrCheckout(string file)
{
if (IsOpenForEdit(file))
{
return;
}
string message = String.Empty;
P4RecordSet p4rs = null;
try
{
if (FileExists(file))
{
//Checkout
p4rs = ResourceTools.p4.Run("edit", file);
}
else
{
//Add
p4rs = ResourceTools.p4.Run("add", file);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (p4rs.Messages.Length > 0)
{
foreach (string msg in p4rs.Messages)
{
message += msg + "\n";
}
MessageBox.Show(message, "Perforce Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
static private bool FileExists(string file)
{
P4RecordSet p4rs = ResourceTools.p4.Run("files", file);
if (p4rs.Records.Length == 0)
return false;
return true;
}
}
}