Skip to content

Latest commit

 

History

History
84 lines (70 loc) · 3.15 KB

PluginManager.md

File metadata and controls

84 lines (70 loc) · 3.15 KB

PluginManager API

1. Overview:

The SDK provides a set of plugin interfaces and uses PluginManager class to support custom implementation of some SDK components. Currently the following plugin interfaces are supported:

PlayFab.ISerializerPlugin // - allows to provide a custom JSON serializer
PlayFab.ITransportPlugin // - allows to provide a custom network HTTP client

2. Prerequisites:

The SDK is downloaded and set up (see README.md and CSharpGettingStarted.md). Some initialization code is added that sets PlayFab Title ID (e.g. Program.cs as in the Getting Started guide).

3. Usage sample:

User can optionally add one or more plugins (custom implementations of interfaces listed above):

public class MyJsonSerializer : ISerializerPlugin
{
    public T DeserializeObject<T>(string serialized)
    {
        // custom implementation
        throw new NotImplementedException();
    }

    public T DeserializeObject<T>(string serialized, object serializerStrategy)
    {
        // custom implementation
        throw new NotImplementedException();
    }

    public object DeserializeObject(string serialized)
    {
        // custom implementation
        throw new NotImplementedException();
    }

    public string SerializeObject(object obj)
    {
        // custom implementation
        throw new NotImplementedException();
    }

    public string SerializeObject(object obj, object serializerStrategy)
    {
        // custom implementation
        throw new NotImplementedException();
    }
}

public class MyNetworkTransportClient : ITransportPlugin
{
    public Task<object> DoPost(string urlPath, object request, Dictionary<string, string> headers)
    {
        // custom implementation
        throw new NotImplementedException();
    }
}

Then use PluginManager class to set custom plugins before using any other PlayFab API, for example, in the initialization portion of the code, e.g. where PlayFab Title ID is set:

    static void Main(string[] args)
    {
        PlayFabSettings.TitleId = "144"; // Please change this value to your own titleId from PlayFab Game Manager

        // We recommend using the Polly wrapped transport client as this
        // will help your services follow better fault tolerance practices 
        // and proper retrying logic against the PlayFab services. For more
        // information, see the PlayFabPollyHttp class summary.
        PluginManager.SetPlugin(new PlayFabPollyHttp(), PluginContract.PlayFab_Transport);

        // Optionally set your own custom JSON serializer
        PluginManager.SetPlugin(new MyJsonSerializer(), PluginContract.PlayFab_Serializer);

        // Optionally set your own custom HTTP network client
        PluginManager.SetPlugin(new MyNetworkTransportClient(), PluginContract.PlayFab_Transport);

        // ...
    }

The SDK will be using these custom plugins instead of the built-in ones. A currently set plugin for any "contract" supported by PlayFab SDK can be obtained from PluginManager class, for example:

var currentTransportClient = PluginManager.GetPlugin<ITransportPlugin>(PluginContract.PlayFab_Transport);