using System; using System.ComponentModel; using System.Threading; using System.Windows.Forms; namespace Plugin { /// /// 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. /// 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(); } /// /// Connect to engine function. If this function succeed, IsConnected returns true. /// 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(); } } /// /// Disconnection function /// public void Disconnect() { _plugin.Stop(); } #endregion /// /// Engine connection state changed handler /// 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); } } } }