# Detailed Documentation The following is more detailed documentation. <table> <tbody> <tr> <td><a href="#mp">MP</a></td> <td><a href="#isynccall">ISyncCall</a></td> </tr> <tr> <td><a href="#syncmethodattribute">SyncMethodAttribute</a></td> <td><a href="#syncfieldattribute">SyncFieldAttribute</a></td> </tr> <tr> <td><a href="#syncworkerattribute">SyncWorkerAttribute</a></td> <td><a href="#syncworkerdelegate\">SyncWorkerDelegate</a></td> </tr> <tr> <td><a href="#isyncfield">ISyncField</a></td> <td><a href="#isynchronizable">ISynchronizable</a></td> </tr> <tr> <td><a href="#isyncmethod">ISyncMethod</a></td> <td><a href="#isyncdelegate">ISyncDelegate</a></td> </tr> <tr> <td><a href="#syncworker">SyncWorker</a></td> <td><a href="#synccontext">SyncContext</a></td> </tr> </tbody> </table> ### MP The primary static class that contains methods used to interface with the multiplayer mod. #### API Contains the API version #### enabled ##### Value Returns if API is initialized. #### IsHosting ##### Value Returns if currently running on a host. #### IsInMultiplayer ##### Value Returns if currently running in a multiplayer session (both on client and host). #### PlayerName ##### Value Returns local player's name. #### RegisterSyncDelegate(inType, nestedType, methodName, fields, args) Registers the syncDelegate. Handles anonymous nested types, you will have to figure out the name of your target by decompiling. ##### Returns The sync delegate. | Name | Description | | ---- | ----------- | | inType | *System.Type*<br>In type. | | nestedType | *System.String*<br>Nested type. | | methodName | *System.String*<br>Method name. | | fields | *System.String[]*<br>Fields. | | args | *System.Type[]*<br>Arguments. | #### RegisterSyncDelegate(type, nestedType, method) Registers the syncDelegate. Handles anonymous nested types, you will have to figure out the name of your target by decompiling. ##### Returns The sync delegate. | Name | Description | | ---- | ----------- | | type | *System.Type*<br>Type. | | nestedType | *System.String*<br>Nested type. | | method | *System.String*<br>Method. | #### RegisterSyncField(field) Registers a field for syncing and returns it's <a href="#isyncfield">ISyncField</a>. ##### Remarks It's recommended to use <a href="#syncfieldattribute">SyncFieldAttribute</a> instead, unless you have to otherwise. They must be Watched between MP.WatchBegin and MP.WatchEnd with the MP.Watch* methods | Name | Description | | ---- | ----------- | | field | *System.Reflection.FieldInfo*<br>FieldInfo of a field to register | ##### Returns A new registered <a href="#isyncfield">ISyncField</a> #### RegisterSyncField(targetType, memberPath) Registers a field for syncing and returns it's <a href="#isyncfield">ISyncField</a>. ##### Remarks It's recommended to use <a href="#syncfieldattribute">SyncFieldAttribute</a> instead, unless you have to otherwise. They must be Watched between MP.WatchBegin and MP.WatchEnd with the MP.Watch* methods | Name | Description | | ---- | ----------- | | targetType | *System.Type*<br> Type of the target class that contains the specified member if null, memberPath will point at field from the global namespace in the "Type/fieldName" format. | | memberPath | *System.String*<br>Path to a member. If the member is to be indexed, it has to end with /[] eg. `"myArray/[]"` | ##### Returns A new registered <a href="#isyncfield">ISyncField</a> #### RegisterSyncMethod(method, argTypes) Registers a method for syncing and returns its <a href="#isyncmethod">ISyncMethod</a>. | Name | Description | | ---- | ----------- | | method | *System.Reflection.MethodInfo*<br>MethodInfo of a method to register | | argTypes | *Multiplayer.API.SyncType[]*<br>Method's parameter types | ##### Remarks It's recommended to use <a href="#syncmethodattribute">SyncMethodAttribute</a> instead, unless you have to otherwise. ##### Returns A new registered <a href="#isyncmethod">ISyncMethod</a> ##### Example Register a method for syncing using reflection and set it to debug only. ```cs RegisterSyncMethod(typeof(MyType).GetMethod(nameof(MyType.MyMethod))).SetDebugOnly(); ``` #### RegisterSyncMethod(type, methodOrPropertyName, argTypes) Registers a method for syncing and returns its <a href="#isyncmethod">ISyncMethod</a>. ##### Remarks It's recommended to use <a href="#syncmethodattribute">SyncMethodAttribute</a> instead, unless you have to otherwise. | Name | Description | | ---- | ----------- | | type | *System.Type*<br>Type that contains the method | | methodOrPropertyName | *System.String*<br>Name of the method | | argTypes | *Multiplayer.API.SyncType[]*<br>Method's parameter types | ##### Returns A new registered <a href="#isyncmethod">ISyncMethod</a> #### RegisterSyncWorker\<T\>(syncWorkerDelegate, targetType, isImplicit, shouldConstruct) Registers the SyncWorker based on SyncWorkerDelegate. ##### Remarks It's recommended to use <a href="#syncworkerattribute">SyncWorkerAttribute</a> instead, unless you have to otherwise. | Name | Description | | ---- | ----------- | | syncWorkerDelegate | *Multiplayer.API.SyncWorkerDelegate{\`\`0}*<br>Sync worker delegate. | | targetType | *System.Type*<br>Type to handle. | | isImplicit | *System.Boolean*<br>If set to `true` the SyncWorker will handle the type and all the derivate Types. | | shouldConstruct | *System.Boolean*<br>If set to `true` the SyncWorker will be provided with an instance created with no arguments. | ##### Type Parameters - T - Type to handle. #### Watch(target, fieldName, index) Helper method for <a href="#isyncfield.watch(system.object,system.object)">ISyncField.Watch(System.Object,System.Object)</a> given an instance. | Name | Description | | ---- | ----------- | | target | *System.Object*<br>An object of type set in the <a href="#isyncfield">ISyncField</a> to watch | | fieldName | *System.String*<br><a href="#isyncfield">ISyncField</a> name of the field to watch for changes | | index | *System.Object*<br>Index in the field path set in <a href="#isyncfield">ISyncField</a> | #### Watch(memberPath, target, index) Helper method for <a href="#isyncfield.watch(system.object,system.object)">ISyncField.Watch(System.Object,System.Object)</a> given an instance. | Name | Description | | ---- | ----------- | | memberPath | *System.String*<br><a href="#isyncfield">ISyncField</a> the memberPath of the ISyncField | | target | *System.Object*<br>An object of type set in the <a href="#isyncfield">ISyncField</a> to watch, null for static | | index | *System.Object*<br>Index in the field path set in <a href="#isyncfield">ISyncField</a> | #### Watch(type, fieldName, index) Helper method for <a href="#isyncfield.watch(system.object,system.object)">ISyncField.Watch(System.Object,System.Object)</a> given a type. | Name | Description | | ---- | ----------- | | type | *System.Type*<br>An object of type set in the <a href="#isyncfield">ISyncField</a> to watch, for static types | | fieldName | *System.String*<br><a href="#isyncfield">ISyncField</a> name of the field to watch for changes | | index | *System.Object*<br>Index in the field path set in <a href="#isyncfield">ISyncField</a> | #### WatchBegin Starts a new synchronization stack. ##### Remarks Has to be called before invoking Watch methods. See also <a href="#isyncfield.watch(system.object,system.object)">ISyncField.Watch(System.Object,System.Object)</a>. #### WatchEnd Ends the current synchronization stack and executes it. ##### Remarks Has to be called after invoking Watch methods. See also <a href="#isyncfield.watch(system.object,system.object)">ISyncField.Watch(System.Object,System.Object)</a>. ### SyncContext Context flags which are sent along with a command #### CurrentMap Send current map context #### MapMouseCell Send mouse cell context (emulates mouse position) #### MapSelected Send map selected context (object selected on the map) #### None Default value. (no context) #### QueueOrder_Down Send order queue context (emulates pressing KeyBindingDefOf.QueueOrder) #### WorldSelected Send world selected context (object selected on the world map) ### SyncFieldAttribute An attribute that is used to mark fields for syncing. It will be Watched for changes by the MPApi when instructed. #### Example An example showing how to mark a field for syncing. ```cs public class MyClass { [SyncField] bool myField; ... } ``` ### Constructor(context) | Name | Description | | ---- | ----------- | | context | *Multiplayer.API.SyncContext*<br>Context | #### bufferChanges Instructs SyncField to use a buffer instead of syncing instantly (when <a href="#mp.watchend">MP.WatchEnd</a> is called). #### cancelIfValueNull Instructs SyncField to cancel synchronization if the value of the member it's pointing at is null. #### debugOnly Instructs SyncField to synchronize only in debug mode. #### hostOnly Instructs SyncField to synchronize only if it's invoked by the host. #### inGameLoop Instructs SyncField to sync in game loop. #### version ### SyncMethodAttribute An attribute that is used to mark methods for syncing. The call will be replicated by the MPApi on all clients automatically. #### Example An example showing how to mark a method for syncing. ```cs [SyncMethod] public void MyMethod(...) { ... } ``` ### Constructor(context) | Name | Description | | ---- | ----------- | | context | *Multiplayer.API.SyncContext*<br>Context | #### cancelIfAnyArgNull Instructs SyncMethod to cancel synchronization if any arg is null (see <a href="#isyncmethod.cancelifanyargnull">ISyncMethod.CancelIfAnyArgNull</a>). #### cancelIfNoSelectedMapObjects Instructs SyncMethod to cancel synchronization if no map objects were selected during the call (see <a href="#isyncmethod.cancelifnoselectedmapobjects">ISyncMethod.CancelIfNoSelectedMapObjects</a>). #### cancelIfNoSelectedWorldObjects Instructs SyncMethod to cancel synchronization if no world objects were selected during call replication(see <a href="#isyncmethod.cancelifnoselectedworldobjects">ISyncMethod.CancelIfNoSelectedWorldObjects</a>). #### debugOnly Instructs SyncMethod to synchronize only in debug mode (see <a href="#isyncmethod.setdebugonly">ISyncMethod.SetDebugOnly</a>). #### exposeParameters A list of types to expose (see <a href="#isyncmethod.exposeparameter(system.int32)">ISyncMethod.ExposeParameter(System.Int32)</a>) ### SyncWorker An abstract class that can be both a reader and a writer depending on implementation. #### Remarks See <a href="#isynchronizable">ISynchronizable</a> and <a href="#syncworkerattribute">SyncWorkerAttribute</a> for usage examples. ### Bind(obj) Reads or writes an object inheriting <a href="#isynchronizable">ISynchronizable</a> interface. #### Remarks Does not create a new object. | Name | Description | | ---- | ----------- | | obj | *Multiplayer.API.ISynchronizable@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.Boolean@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.Byte@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.Double@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.Int16@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.Int32@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.Int64@*<br>object to bind | ### Bind(obj, name) Uses reflection to bind a field or property | Name | Description | | ---- | ----------- | | obj | *System.Object*<br> object where the field or property can be found if null, name will point at field from the global namespace | | name | *System.String*<br>path to the field or property | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.SByte@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.Single@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.String@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.UInt16@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.UInt32@*<br>object to bind | ### Bind(obj) Reads or writes an object referenced by obj. | Name | Description | | ---- | ----------- | | obj | *System.UInt64@*<br>object to bind | ### Bind\<T\>(obj) Reads or writes an object referenced by obj #### Remarks Can read/write types using user defined syncers, <a href="#isynchronizable">ISynchronizable</a>s and readers/writers implemented by the multiplayer mod. #### Type Parameters - T - type of the object to bind | Name | Description | | ---- | ----------- | | obj | *\`\`0@*<br>object to bind | ### BindType\<T\>(type) Reads or writes a <a href="#system.type">System.Type</a> referenced by type. #### Type Parameters - T - Base type that type derives from. | Name | Description | | ---- | ----------- | | type | *System.Type@*<br>type to bind | ### isWriting if is currently writing. ### Read\<T\> Read the specified Type from the memory stream, only active during reading. #### Returns The requested Type object. Null if writing. #### Type Parameters - T - The Type to read. ### Write\<T\>(obj) Write the specified obj, only active during writing. | Name | Description | | ---- | ----------- | | obj | *\`\`0*<br>Object to write. | #### Type Parameters - T - Type to write. ## SyncWorkerAttribute An attribute that marks a method as a SyncWorker for a type specified in its second parameter. #### Remarks Method with this attribute has to be static. #### Example An implementation that manually constructs an object. ```cs [SyncWorkerAttribute] public static void MySyncWorker(SyncWorker sync, ref MyClass inst) { if(!sync.isWriting) inst = new MyClass("hello"); sync.bind(ref inst.myField); } ``` An implementation that instead of creating a new object, references its existing one which resides in MyThingComp that inherits ThingComp class. Subclasses of ThingComp are sent as a reference by the multiplayer mod itself. ```cs [SyncWorkerAttribute] public static void MySyncWorker(SyncWorker sync, ref MyClass inst) { if(!sync.isWriting) MyThingComp parent = null; sync.Bind(ref parent); // Receive its parent inst = new MyClass(parent); else sync.Bind(ref inst.parent); // Send its parent sync.bind(ref inst.myField); } ``` ### isImplicit Decides if the type specified in the second parameter should also be used as a syncer for all of its subclasses. ### shouldConstruct Decides if the method should get an already constructed object in case of reading data. ## SyncWorkerDelegate\`1 SyncWorker signature for adding new Types. | Name | Description | | ---- | ----------- | | obj | *\`\`0*<br>Target Type | #### Remarks <a href="#syncworkerattribute">SyncWorkerAttribute</a> for usage examples. ## UninitializedAPI An exception that is thrown if you try to use the API without avaiable host. ## ISyncCall ISyncCall interface. #### Remarks Used internally ### DoSync(target, args) Manually calls the synced method. | Name | Description | | ---- | ----------- | | target | *System.Object*<br>Object currently bound to that method. Null if the method is static. | | args | *System.Object[]*<br>Parameters to call the method with. | #### Returns if the original call should be canceled. ## ISyncDelegate Sync delegate. #### Remarks See <a href="#mp.registersyncdelegate(system.type,system.string,system.string)">MP.RegisterSyncDelegate(System.Type,System.String,System.String)</a> and <a href="#mp.registersyncdelegate(system.type,system.string,system.string,system.string[],system.type[])">MP.RegisterSyncDelegate(System.Type,System.String,System.String,System.String[],System.Type[])</a> to see how to use it. ### CancelIfNoSelectedObjects Cancels if no selected objects. #### Returns self ### RemoveNullsFromLists(listFields) Removes the nulls from lists. #### Returns self | Name | Description | | ---- | ----------- | | listFields | *System.String[]*<br>List fields. | ### SetContext(context) Sets the context. #### Returns self | Name | Description | | ---- | ----------- | | context | *Multiplayer.API.SyncContext*<br>Context. | ### SetDebugOnly Sets the debug only. #### Returns self ## ISyncField SyncField interface. #### Example Creates and registers a SyncField that points to `myField` in object of type `MyType` and enables its change buffer. ```cs MPApi.SyncField(typeof(MyType), "myField").SetBufferChanges(); ``` Creates and registers a SyncField that points to `myField` which resides in `MyStaticClass`. ```cs MPApi.SyncField(null, "MyAssemblyNamespace.MyStaticClass.myField"); ``` Creates and registers a SyncField that points to `myField` that resides in an object stored by myEnumberable defined in an object of type `MyType`. To watch this one you have to supply an index in <a href="#isyncfield.watch(system.object,system.object)">ISyncField.Watch(System.Object,System.Object)</a>. ```cs MPApi.SyncField(typeof(MyType), "myEnumerable/[]/myField"); ``` ### CancelIfValueNull Instructs SyncField to cancel synchronization if the value of the member it's pointing at is null. #### Returns self ### DoSync(target, value, index) Manually syncs a field. | Name | Description | | ---- | ----------- | | target | *System.Object*<br>An object of type set in the <a href="#isyncfield">ISyncField</a>. Set to null if you're watching a static field. | | value | *System.Object*<br>Value to apply to the synced field. | | index | *System.Object*<br>Index in the field path set in <a href="#isyncfield">ISyncField</a> | #### Returns if the change should be canceled. ### InGameLoop Instructs SyncField to sync in game loop. #### Returns self ### PostApply(action) Adds an Action that runs after a field is synchronized. | Name | Description | | ---- | ----------- | | action | *System.Action{System.Object,System.Object}*<br>An action ran after a field is synchronized. Called with target and value. | #### Returns self ### PreApply(action) Adds an Action that runs before a field is synchronized. | Name | Description | | ---- | ----------- | | action | *System.Action{System.Object,System.Object}*<br>An action ran before a field is synchronized. Called with target and value. | #### Returns self ### SetBufferChanges Instructs SyncField to use a buffer instead of syncing instantly (when <a href="#mp.watchend">MP.WatchEnd</a> is called). #### Returns self ### SetDebugOnly Instructs SyncField to synchronize only in debug mode. #### Returns self ### SetHostOnly Instructs SyncField to synchronize only if it's invoked by the host. #### Returns self ### SetVersion(System.Int32) #### Returns self ### Watch(target, index) | Name | Description | | ---- | ----------- | | target | *System.Object*<br>An object of type set in the <a href="#isyncfield">ISyncField</a>. Set to null if you're watching a static field. | | index | *System.Object*<br>Index in the field path set in <a href="#isyncfield">ISyncField</a>. | #### Returns self ## ISynchronizable An interface that allows syncing objects that inherit it. ### Sync(sync) An entry point that is used when object is to be read/written. #### Remarks Requires a default constructor that takes no parameters. Check <a href="#syncworkerattribute">SyncWorkerAttribute</a> to see how to make a syncer that allows for a manual object construction. | Name | Description | | ---- | ----------- | | sync | *Multiplayer.API.SyncWorker*<br>A SyncWorker that will read/write data bound with Bind methods. | #### Example A simple implementation that binds object's fields x, y, z for reading/writing. ```cs public void Sync(SyncWorker sync) { sync.Bind(ref this.x); sync.Bind(ref this.y); sync.Bind(ref this.z); } ``` An implementation that sends field a, but saves it back into field b when it's received. ```cs public void Sync(SyncWorker sync) { if(sync.isWriting) sync.Bind(ref this.a); else sync.Bind(ref this.b); } ``` ### ISyncMethod SyncMethod interface. ##### Remarks See <a href="#syncmethodattribute">SyncMethodAttribute</a>, <a href="#mp.registersyncmethod(system.reflection.methodinfo,multiplayer.api.synctype[])">MP.RegisterSyncMethod(System.Reflection.MethodInfo,Multiplayer.API.SyncType[])</a> and <a href="#mp.registersyncmethod(system.type,system.string,multiplayer.api.synctype[])">MP.RegisterSyncMethod(System.Type,System.String,Multiplayer.API.SyncType[])</a> to see how to use it. #### CancelIfAnyArgNull Instructs SyncMethod to cancel synchronization if any arg is null. ##### Returns self #### CancelIfNoSelectedMapObjects Instructs SyncMethod to cancel synchronization if no map objects were selected during call replication. ##### Returns self #### CancelIfNoSelectedWorldObjects Instructs SyncMethod to cancel synchronization if no world objects were selected during call replication. ##### Returns self #### ExposeParameter(index) Use parameter's type's IExposable interface to transfer its data to other clients. ##### Remarks IExposable is the interface used for saving data to the save which means it utilizes IExposable.ExposeData() method. | Name | Description | | ---- | ----------- | | index | *System.Int32*<br>Index at which parameter is to be marked to expose | ##### Returns self #### MinTime(time) Currently unused in the Multiplayer mod. | Name | Description | | ---- | ----------- | | time | *System.Int32*<br>Milliseconds between resends | ##### Returns self #### SetContext(context) Instructs method to send context along with the call. ##### Remarks Context is restored after method is called. | Name | Description | | ---- | ----------- | | context | *Multiplayer.API.SyncContext*<br>One or more context flags | ##### Returns self #### SetDebugOnly Instructs SyncMethod to synchronize only in debug mode. ##### Returns self #### SetPostInvoke(action) Adds an Action that runs after a call is replicated on client. | Name | Description | | ---- | ----------- | | action | *System.Action{System.Object,System.Object[]}*<br>An action ran after a call is replicated on client. Called with target and value. | ##### Returns self #### SetPreInvoke(action) Adds an Action that runs before a call is replicated on client. | Name | Description | | ---- | ----------- | | action | *System.Action{System.Object,System.Object[]}*<br>An action ran before a call is replicated on client. Called with target and value. | #### Returns self ### SetVersion(version) | Name | Description | | ---- | ----------- | | version | *System.Int32*<br>Handler version | #### Returns self ---