using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Perforce.P4;
using NLog;
namespace DemoInstaller
{
public class P4Server
{
private static Logger logger = LogManager.GetCurrentClassLogger();
private String port;
private String user;
private String password;
private Repository p4;
public P4Server(String port, String user, String password)
{
this.port = port;
this.user = user;
this.password = password;
}
public void Dispose()
{
p4.Connection.Disconnect();
}
public bool Connect()
{
Perforce.P4.Server svr = new Perforce.P4.Server(new ServerAddress(this.port));
this.p4 = new Perforce.P4.Repository(svr);
this.p4.Connection.UserName = user;
return this.p4.Connection.Connect(new Perforce.P4.Options());
}
public bool checkUser()
{
if (!this.p4.Connection.connectionEstablished())
{
return false;
}
if (password.Length > 0)
{
p4.Connection.Login(password);
}
P4Command cmd = new P4Command(p4, "info", true, null);
P4CommandResult result = cmd.Run();
foreach (var item in result.TaggedOutput)
{
foreach (string key in item.Keys)
{
logger.Info("Info {0}: {1}", key, item[key]);
}
}
return userIsSuperuser();
}
// Whether user is super user or not - first user or already has super privileges
public bool userIsSuperuser()
{
string[] args = { "-M" };
var cmd = new P4Command(this.p4, "protects", true, args);
P4CommandResult result;
try
{
result = cmd.Run();
foreach (var item in result.TaggedOutput)
{
foreach (string key in item.Keys)
{
logger.Info("Protects {0}: {1}", key, item[key]);
if (key == "permMax" && item[key] == "super")
{
return true;
}
}
}
} catch (P4Exception e)
{
if (e.Message.Contains("Protections table is empty."))
{
logger.Debug("Protections table is empty");
return createNewProtect();
}
logger.Error("Error protects: {0}", e.Message);
}
return false;
}
// Whether user is super user or not - first user or already has super privileges
private bool createNewProtect()
{
var p4env = String.Format(" -p {0} -u {1} ", port, user);
var p4cmd = String.Format(@"/C ""p4 {0} protect -o | p4 {0} protect -i""",
p4env);
var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "cmd",
Arguments = p4cmd,
UseShellExecute = false,
CreateNoWindow = true
}
};
proc.Start();
proc.WaitForExit(1000);
return proc.ExitCode == 0;
}
// Create user specific typemap
public bool updateTypemap()
{
var p4env = String.Format(" -p {0} -u {1} ", port, user);
var typemap_entries = @"TypeMap:
text //....js
text //....cs
text //...shader
text //....meta
text+l //....cm
text+l //....proc
text+l //....md5mesh
text+l //....md5anim
text+l //....ma
binary //....dll
binary //....exe
binary //....response
binary //....lib
binary //....pdb
binary //....u
binary //....ini
binary //....stub
binary //....ip
binary+l //....prefab
binary+l //....mb
binary+l //....mat
binary+l //....psb
binary+l //....mp3
binary+l //....fbx
binary+l //....unity
binary+l //....asset
binary+l //....aas
binary+l //....tga
binary+l //....jpg
binary+l //....lwo
binary+l //....wav
binary+l //....ogg
binary+l //....demo
binary+l //....roq
binary+l //....doc
binary+l //....xls
binary+l //....celtx
binary+l //....pdf
binary+l //....odt
binary+l //....ods
binary+l //....ppt
binary+l //....skp
binary+lS //....dds
binary+lS //....bnk
binary+lS //....light
binary+lS //....shadow
binary+lS //....ibl
binary+lS //....bik
binary+lS //....upk";
string tempFile = Path.GetTempFileName();
System.IO.File.WriteAllText(tempFile, typemap_entries);
var p4cmdout = String.Format(@"/C ""p4 {0} typemap -o""", p4env);
var p4cmdin = String.Format(@"/C ""p4 {0} typemap -i < ""{1}""""", p4env, tempFile);
var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "cmd",
Arguments = p4cmdin,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
proc.WaitForExit(1000);
var stdout = proc.StandardOutput.ReadToEnd();
var stderr = proc.StandardError.ReadToEnd();
logger.Debug("output: {0}, error: {1}", stdout, stderr);
return proc.ExitCode == 0;
}
}
}