using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UnityEngine;
using UnityEditor;
using Perforce.P4;
namespace P4Connect
{
public class EZConnect : EditorWindow
{
string t;
Vector2 scrollPos;
[MenuItem("Window/My Window")]
public static void ShowWindow()
{
var window = GetWindow(typeof(EZConnect));
window.Show();
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(300), GUILayout.Height(300));
GUILayout.Label(t);
EditorGUILayout.EndScrollView();
if (GUILayout.Button("Add More Text", GUILayout.Width(100), GUILayout.Height(100)))
{
t += " \nAnd this is more text!";
string p4configpath = find_p4config(Application.dataPath);
if (p4configpath != null)
{
//Debug.Log("P4CONFIG at " + p4configpath);
P4Settings settings = new P4Settings(p4configpath);
// settings.LoadP4ConfigFile(p4configpath);
}
}
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("Clear"))
t = "";
}
/// <summary>
/// Gets an array of users from the server
/// </summary>
/// <returns></returns>
///
public static IList<string> GetUsernames(Repository repo)
{
Options aOptions = new Options();
IList<User> users;
List<string> usernames = new List<string>();
if (Config.DisplayP4Timings)
{
//DateTime startTimestamp = DateTime.Now;
users = repo.GetUsers(aOptions);
//double deltaInnerTime = (DateTime.Now - startTimestamp).TotalMilliseconds;
//repo.Connection.AppendTimingInfo("GetUsers " + deltaInnerTime.ToString() + " ms");
}
else
{
users = repo.GetUsers(aOptions);
}
foreach (User user in users)
{
usernames.Add(user.Id);
// Debug.Log(user.Id);
}
return usernames.ToArray();
}
public static string find_p4config(string path)
{
string directoryName;
string p4config = Environment.GetEnvironmentVariable("P4CONFIG");
//Debug.Log("P4CONFIG ENV =" + p4config);
// Debug.Log("Asset root = " + path);
if (p4config != null)
{
while (path != null)
{
directoryName = Path.GetDirectoryName(path);
string[] files = System.IO.Directory.GetFiles(directoryName, p4config);
if (files.Count() > 0)
{
return files[0];
}
path = directoryName;
}
}
return null;
}
}
public class P4Settings
{
string _host;
string _port;
string _workspace;
string _user;
string _password;
public string host
{
get { return _host; }
set { _host = value; }
}
public string port
{
get { return _port; }
set { _port = value; }
}
public string workspace
{
get { return _workspace; }
set { _workspace = value; }
}
public string user
{
get { return _user; }
set { _user = value; }
}
public string password
{
get { return _password; }
set { _password = value; }
}
public P4Settings()
{
_host = null;
_port = null;
_workspace = null;
_user = null;
_password = null;
}
public P4Settings(string p4configpath) : this()
{
LoadP4ConfigFile(p4configpath);
}
public void LoadP4ConfigFile(string path)
{
string line;
char[] equalsChars = { '=' };
char[] colonChars = { ':' };
// Debug.Log("ConfigFile: " + path);
System.IO.StreamReader file = new System.IO.StreamReader(path);
while ((line = file.ReadLine()) != null)
{
string[] segments = line.Split(equalsChars);
if (segments.Length >= 2)
{
string key = segments[0];
string val = segments[1];
// Debug.Log("Key: " + key);
// Debug.Log("Value: " + val);
switch( segments[0])
{
case "P4PORT":
{
string[] elems = val.Split(colonChars);
if (elems.Length == 2)
{
host = elems[0];
port = elems[1];
}
if (elems.Length == 1) // Must be port alone, add localhost as default
{
host = "localhost";
port = val;
}
}
break;
case "P4USER":
{
user = val;
}
break;
case "P4CLIENT":
{
workspace = val;
}
break;
case "P4PASSWD":
{
password = val;
}
break;
}
}
}
file.Close();
// Debug.Log("ConfigString: " + toString());
}
public string toString()
{
StringBuilder outstr = new StringBuilder("-----");
outstr.Append(" host: " + (_host != null ? _host : "null")).AppendLine();
outstr.Append(" port: " + (_port != null ? _port : "null")).AppendLine();
outstr.Append(" workspace: " + (_workspace != null ? _workspace : "null")).AppendLine();
outstr.Append(" user: " + (_user != null ? _user : "null")).AppendLine();
outstr.Append(" password: " + (_password != null ? _password : "null")).AppendLine();
return (outstr.ToString());
}
}
}