Skip to content

mpaperno/TouchPortal-CS-API

Repository files navigation

Touch Portal C# and .NET API

Made for Touch Portal Nuget .NET License Discord


Touch Portal API for making plugins with .NET

Built based on documentation at Touch Portal Plugin API.

Current Touch Portal API support level: v10.0 (Touch Portal v4.3)

Originally a fork of https://github.com/oddbear/TouchPortalSDK, optimized for performance, usability, and good behavior.

Download / Install

  • The API is available via a nuget package along with a separate debug symbols package.
  • An equivalent .zip archive is also available for download in Releases. This includes debug symbols in external .pdb files.
  • Pre-built libraries are available for .NET versions 6, 7, and 8 via both delivery methods and include generated documentation XML.

Getting started:

The simplest way of getting started, is to implement ITouchPortalEventHandler and use TouchPortalFactory to create an instance of TouchPortalClient. Then TouchPortalClient.Connect() to Touch Portal before sending or receiving events.

public class SamplePlugin : ITouchPortalEventHandler
{
    // Replace "Plugin.Id" with your unique id.
    public string PluginId => "Plugin.Id";  

    private readonly ITouchPortalClient _client;
    private readonly ILogger<SamplePlugin> _logger;

    public SamplePlugin()
    {
        _client = TouchPortalFactory.CreateClient(this);
        _logger = new Logger<SamplePlugin>(default);
    }

    public void Run()
    {
        // Connect to Touch Portal on startup.
        _client.Connect();
    }
    
    // Event received when plugin connects to Touch Portal.
    public void OnInfoEvent(InfoEvent message)
    {
        _logger.LogInformation(
          "[InfoEvent] VersionCode: '{TpVersionCode}', VersionString: '{TpVersionString}', SDK: '{SdkVersion}', PluginVersion: '{PluginVersion}', Status: '{Status}'",
          message.TpVersionCode, message.TpVersionString, message.SdkVersion, message.PluginVersion, message.Status
        );
        
        // Update a static state defined in entry.tp
        _client.StateUpdate($"{PluginId}.staticState1", "Connected!");

        // Add a dynamic state
        _client.CreateState($"{PluginId}.dynamicState1", "Test dynamic state 1", "Test 123");
    }

    // Event triggered when one of this plugin's actions, defined in entry.tp, is triggered.
    public void OnActionEvent(ActionEvent message) {
        _logger.LogInformation("{@message}", message);_
        // Handle the action....
    }
    // ...
}

More complete example in the Sample project of this repository. For more documentation see the original Wiki.

Compatibility With Original

Drop-in replacement for oddbear's TouchPortalSDK as of his v0.30.0-beta2, except:

  • The ActionEvent.Data property, which was an array or key-value pairs, is now a Dictionary<string, string> with each data ID mapped to its corresponding value. See the SamplePllugin.cs changes on this commit for how to update (but now you can also message.Data.TryGetValue("myDataId", out string value), for example).

Since oddbear's TouchPortalSDK v 0.30.0 release version, the paths have diverged further, most notably in the handling of TP Connectors.

Change Log

See CHANGELOG.md.

Plugins Using This API

Working examples at: