Step by Step Guide
Guide Intro
This guide's purpose is to show you how to create, configure and use your first Photon Server plugin.
This guide is intended for users who are discovering Photon Server plugins for the first time.
This guide should be followed carefully step by step and in order.
This guide does not replace the manual.
Your First Plugin
In this first part we will create and deploy a minimal plugin.
Extract the SDK. The resulting folder path will be referred to as "{pluginsSdkFolder}" throught this guide.
Create a new Visual Studio project (Visual Studio 2019 Community edition was used in this guide):
- Add a new project of type "Class Library (.NET Framework)"
- Project name: "MyFirstPlugin"
- Location: "{pluginsSdkFolder}\src-server\Plugins"
- Check: "Place solution and project in the same directory"
- Framework: ".NET Framework 4"
Add dependencies:
Only one library is needed: "PhotonHivePlugin.dll".
To add it from Visual Studio:- Right click on "References" from the project
- Click "Add Reference.."
- Choose "Browse"
- Click "Browse" and choose "{pluginsSdkFolder}\src-server\Plugins\lib\PhotonHivePlugin.dll"
- Confirm with "OK"
Add plugin class:
- Rename the class file automatically created by VS from "Class.cs" to "MyFirstPlugin.cs".
- VS will suggest to rename the class also, accept. Otherwise rename the class yourself from
Class
toMyFirstPlugin
. - Extend
PluginBase
.
C#
using Photon.Hive.Plugin; namespace MyFirstPlugin { public class MyFirstPlugin : PluginBase { } }
Set plugin name:
C#
public override string Name { get { return "MyFirstPlugin"; } }
- The reserved plugin names are "Default" and "ErrorPlugin".
Add a log message in a callback:
C#
public override void OnCreateGame(ICreateGameCallInfo info) { this.PluginHost.LogInfo(string.Format("OnCreateGame {0} by user {1}", info.Request.GameId, info.UserId)); }
Call one callback processing method:
C#
public override void OnCreateGame(ICreateGameCallInfo info) { this.PluginHost.LogInfo(string.Format("OnCreateGame {0} by user {1}", info.Request.GameId, info.UserId)); info.Continue(); // same as base.OnCreateGame(info); }
Add plugin factory class:
- Add a new class named
MyPluginFactory
- Make it public
C#
using System.Collections.Generic; using Photon.Hive.Plugin; namespace MyFirstPlugin { public class MyPluginFactory { } }
- Add a new class named
Implement interface
IPluginFactory
:C#
using System.Collections.Generic; using Photon.Hive.Plugin; namespace MyFirstPlugin { public class MyPluginFactory : IPluginFactory { public IGamePlugin Create(IPluginHost gameHost, string pluginName, Dictionary<string, string> config, out string errorMsg) { throw new NotImplementedException(); } } }
Create and return plugin:
C#
public IGamePlugin Create(IPluginHost gameHost, string pluginName, Dictionary<string, string> config, out string errorMsg) { MyFirstPlugin plugin = new MyFirstPlugin(); if (plugin.SetupInstance(gameHost, config, out errorMsg)) { return plugin; } return null; }
Build solution (F6).
Update plugins configuration:
- Open "{pluginsSdkFolder}\deploy\LoadBalancing\GameServer\bin\Photon.LoadBalancing.dll.config"
- Update "PluginSettings" node as follows:
XML
<PluginSettings Enabled="true"> <Plugins> <Plugin Name="MyFirstPlugin" AssemblyName="MyFirstPlugin.dll" Type="MyFirstPlugin.MyPluginFactory" /> </Plugins> </PluginSettings>
Read more about plugins configuration on self-hosted Photon Server.
Copy binaries to expected path:
Copy everything from "{pluginsSdkFolder}\src-server\Plugins\MyFirstPlugin\bin\Debug" to "{pluginsSdkFolder}\deploy\Plugins\MyFirstPlugin\bin".
Open [PhotonControl]. It can be found at "{pluginsSdkFolder}\bin_Win64\PhotonControl.exe".
Start Photon Server as an application:
Check the logs:
You can directly open the logs from PhotonControl:
In the "{pluginsSdkFolder}\deploy\log\GSGame.log" you should see the plugin configuration parsed and processed successfully:
Plain Old Text
2019-07-04 15:18:40,272 [1] INFO Photon.Hive.Plugin.PluginManager - Plugin configured: name=MyFirstPlugin 2019-07-04 15:18:40,326 [1] INFO Photon.Hive.Plugin.PluginManager - Loaded Assembly Name=MyFirstPlugin, Version=1.0.0.0, Culture=, PublicKey token=, Path=D:\ExitGames\SDKs\Plugins\Photon-OnPremise-Server-Plugin-SDK_v4-0-29-11263\deploy\Plugins\MyFirstPlugin\\bin\MyFirstPlugin.dll 2019-07-04 15:18:40,327 [1] INFO Photon.Hive.Plugin.PluginManager - Referenced Assembly Name=mscorlib, Version=4.0.0.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89, Path= 2019-07-04 15:18:40,329 [1] INFO Photon.Hive.Plugin.PluginManager - Referenced Assembly Name=PhotonHivePlugin, Version=1.0.15.11060, Culture=, PublicKey token=, Path= 2019-07-04 15:18:40,330 [1] INFO Photon.Hive.Plugin.PluginManager - Plugin Type MyFirstPlugin.MyPluginFactory from assembly MyFirstPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null was successfuly created 2019-07-04 15:18:40,331 [1] INFO Photon.Hive.Plugin.PluginManager - Plugin manager (version=1.0.15.11060) is setup. type=MyFirstPlugin.MyPluginFactory;path=D:\ExitGames\SDKs\Plugins\Photon-OnPremise-Server-Plugin-SDK_v4-0-29-11263\deploy\Plugins\MyFirstPlugin\\bin\MyFirstPlugin.dll;version=1.0.15.11060
Connect to Photon Server. Use any client SDK.
Create a room. Call the appropriate method for your SDK which implements the CreateRoom operation.
Recheck the logs:
In the "{pluginsSdkFolder}\deploy\log\GSGame.log" you should see the plugin creation followed by your custom log message from the plugin callback:Plain Old Text
2019-07-04 15:25:40,540 [11] INFO Photon.Hive.Plugin.PluginManager - Plugin successfully created:Type:MyFirstPlugin.MyPluginFactory, path:D:\ExitGames\SDKs\Plugins\Photon-OnPremise-Server-Plugin-SDK_v4-0-29-11263\deploy\Plugins\MyFirstPlugin\\bin\MyFirstPlugin.dll 2019-07-04 15:25:42,435 [11] INFO Photon.Hive.HiveGame.HiveHostGame.Plugin - OnCreateGame 870c3120-bcdd-4091-b072-8e92ea07bec5 by user 495fca21-b67e-4103-b1ce-c6ce945e6207
Debug Mode
In this second part we will show you our recommended workflow for plugins development.
Close the Visual Studio project and open it again but this time by double clicking on "{pluginsSdkFolder}\src-server\Plugins\MyFirstPlugin\MyFirstPlugin.sln" solution file.
This step is important to be able to use paths relative to the solution folder path in step 3.Update the plugin project's "Post-Build Events":
The idea here is to automatically update plugins binaries by coping and pasting from project build output directory to expected path by the Photon configuration.
Enter this command which does exactly this just after a successful build in the "Post-Build Events" text area as shown in the screenshot.
We use relative paths for convenience and portability.Plain Old Text
xcopy /Y /Q "$(TargetDir)*.*" "$(SolutionDir)..\..\..\deploy\Plugins\MyFirstPlugin\bin\"
Do not forget to save.
Update the plugin project's "Debug" actions:
The idea here is to automatically start Photon Server when debugging plugin starts and stopping when it stops.
Configure the "Start action" and "Start options" as shown in the screenshot.
We use relative paths for convenience and portability."Start external program":
Plain Old Text
..\..\..\deploy\bin_Win64\PhotonSocketServer.exe
"Command line arguments":
Plain Old Text
/run LoadBalancing /configPath ..\..\..\..\..\deploy\bin_Win64
Do not forget to save.
Add a breakpoint:
Stop Photon Server from [PhotonControl]:
Start Photon Server from Visual Studio (F5):
You can check the server is started using TaskManager. The process is named Photon.
Connect to Photon Server. Use any client SDK.
Create a room. Call the appropriate method for your SDK which implements the CreateRoom operation.
Wait for the breakpoint to be triggered: