JD2022-TU1/main/tools/lib/AbstractPlugin/EngineManager.cs

126 lines
3.5 KiB
C#

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace Plugin
{
/// <summary>
/// Singleton class to encapsulate Ubisoft.PerforceWrapper from Alexandrie.
/// Accessor is GetInstance property.
/// This class manage connection disconnection, and prompt a special P4V like password dialog.
/// </summary>
public class EngineManager : IDisposable, INotifyPropertyChanged
{
#region Events
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Fields
private readonly Plugin.AbstractPlugin _plugin = null;
private readonly EngineConnectionManager _connectionManager = new EngineConnectionManager();
#endregion
#region Public members
#region Properties
public Plugin.AbstractPlugin Plugin
{
get { return _plugin; }
}
public bool IsConnected
{
get
{
return _plugin != null && _plugin.Connected;
}
}
public void NotifyChange(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
public EngineManager(Plugin.AbstractPlugin plugin)
{
if (plugin == null)
throw new Exception("Null plugin object passed to engine Wrapper");
_plugin = plugin;
}
public void Dispose()
{
if (IsConnected)
Disconnect();
//_plugin.Dispose();
}
/// <summary>
/// Connect to engine function. If this function succeed, IsConnected returns true.
/// </summary>
public void Connect()
{
var chooseIPform = new EngineConnectionParameterForm(_connectionManager);
switch (chooseIPform.ShowDialog())
{
case DialogResult.OK:
_plugin.ExpectedIp = _connectionManager.PreferedConnectionSettings.Ip;
if (_plugin.Started)
{
_plugin.Stop();
}
_plugin.ConnectedChanged += EngineConnectionChanged;
_plugin.Start();
break;
case DialogResult.Cancel:
case DialogResult.Abort:
case DialogResult.Retry:
case DialogResult.Ignore:
break;
case DialogResult.Yes:
case DialogResult.No:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Disconnection function
/// </summary>
public void Disconnect()
{
_plugin.Stop();
}
#endregion
/// <summary>
/// Engine connection state changed handler
/// </summary>
private void EngineConnectionChanged(object sender, EventArgs e)
{
try
{
NotifyChange("IsConnected");
if (IsConnected == false)
{
_plugin.ConnectedChanged -= EngineConnectionChanged;
}
}
catch (Exception ex)
{
Console.WriteLine("OnEngineConnectionChanged exception: {0}", ex.Message);
}
}
}
}