Custom Connections - AngryCarrot789/MemoryEngine360 GitHub Wiki

This page walks you through creating a custom connection to, for example, add support for a specific console (e.g. PS2)

Before doing this, you will need a plugin. See Plugins for more info on how to set one up.

For a full example, check out the XBDM plugin.

Connection Type

Connection support is provided via a RegisteredConnectionType. There are a few requirements:

  • Override DisplayName (e.g. "PS2")

  • Override LongDescription (e.g. "Connects to a PS2 via ... some mechanism ..."),

  • Optionally, override Icon and FooterText. These are used to shown in the list box within the Open Connection view (by pressing CTRL + O), but may also be used elsewhere in the app.

  • Then, override the OpenConnection method, which returns a task that provides the established connection.

    Task<IConsoleConnection?> OpenConnection(UserConnectionInfo? _info, CancellationTokenSource cancellation)
    
    • The cancellation token source becomes cancelled automatically when the user clicks the cancel button or closes the window, but you may cancel it yourself if needed.

    • The _info parameter is a custom model object that your connection type can define. It is the same instance returned by the virtual method UserConnectionInfo? CreateConnectionInfo(IContextData context) (which returns null by default)

    This method can show message dialogs and return null if errors are encountered, e.g.:

    // Note, we pass the CTS's token to the dialog, so that if the CTS becomes cancelled (for some reason), it will force close the dialog.
    // Maybe you want this, or maybe you want the user to always acknowledge the error by clicking OK.
    await IMessageDialogService.Instance.ShowMessage("Error", "Encountered an error while connecting to PS2: " + errorText, dialogCancellation: cancellation.Token);
    return null;
    

Finally, register your connection type via the ConsoleConnectionManager. For example:

ConsoleConnectionManager manager = ApplicationPFX.Instance.ServiceManager.GetService<ConsoleConnectionManager>();
manager.Register("console.ps2.customId", new ConnectionTypePS2());

Custom connection info control

This will let you place a control in the Open Connection view when you select your custom connection type.

  • Define a class deriving from UserConnectionInfo and return a new instance of it in the method CreateConnectionInfo.
  • Define a control (e.g. a UserControl) that implements IConsoleConnectivityControl
  • In your plugin's OnApplicationFullyLoaded register your model with your control type. E.g.:
    OpenConnectionView.Registry.RegisterType<ConnectToPS2Info /* model */>(() => new OpenPS2ConnectionView() /* control */)
    

Then you can use normal code-behind and/or MVP to 'bind' the model and the view. If you wish, you can use MVVM by making the model implement INotifyPropertyChanged.

IConsoleConnectivityControl

This interface provides two methods

  • void OnConnected(OpenConnectionView dialog, UserConnectionInfo info)
    
    Although the name is misleading, this is called when the user selects the list box item for our control. It's called OnConnected because most of our codebase and the PFX toolkit uses the idea of connecting and disconnecting UI controls from the visual parent, even though they may still exist in the visual tree.
  • void OnDisconnected()
    
    This is called when the list box item for our control is no longer selected. This is where you'd unbind model-view bindings (e.g. via the IBinder interface)

There's also two similar methods in UserConnectionInfo, namely OnShown and OnHidden. An example usage would be trying to discover all consoles on your local network in OnShown, and then cancelling the operation in OnHidden (if not already completed of course). This way you save the user having to manually click a refresh button.