Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
Added more parameters for SyncMethod attribute. More verbose debug/er…
Browse files Browse the repository at this point in the history
…ror messages.
  • Loading branch information
Pecius committed Mar 15, 2019
1 parent feb86c6 commit adc4c07
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
8 changes: 4 additions & 4 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
- [QueueOrder_Down](#F-UnofficialMultiplayerAPI-SyncContext-QueueOrder_Down 'UnofficialMultiplayerAPI.SyncContext.QueueOrder_Down')
- [WorldSelected](#F-UnofficialMultiplayerAPI-SyncContext-WorldSelected 'UnofficialMultiplayerAPI.SyncContext.WorldSelected')
- [SyncMethodAttribute](#T-UnofficialMultiplayerAPI-SyncMethodAttribute 'UnofficialMultiplayerAPI.SyncMethodAttribute')
- [#ctor(context)](#M-UnofficialMultiplayerAPI-SyncMethodAttribute-#ctor-UnofficialMultiplayerAPI-SyncContext- 'UnofficialMultiplayerAPI.SyncMethodAttribute.#ctor(UnofficialMultiplayerAPI.SyncContext)')
- [#ctor(context,cancelIfAnyArgNull,cancelIfNoSelectedMapObjects,cancelIfNoSelectedWorldObjects,exposeParameters)](#M-UnofficialMultiplayerAPI-SyncMethodAttribute-#ctor-UnofficialMultiplayerAPI-SyncContext- 'UnofficialMultiplayerAPI.SyncMethodAttribute.#ctor(UnofficialMultiplayerAPI.SyncContext)')

<a name='T-UnofficialMultiplayerAPI-IMultiplayerInit'></a>
## IMultiplayerInit `type`
Expand Down Expand Up @@ -334,7 +334,7 @@ Instructs method to send context along with the call

| Name | Type | Description |
| ---- | ---- | ----------- |
| context | [UnofficialMultiplayerAPI.SyncContext](#T-UnofficialMultiplayerAPI-SyncContext 'UnofficialMultiplayerAPI.SyncContext') | Context |
| context | [UnofficialMultiplayerAPI.SyncContext](#T-UnofficialMultiplayerAPI-SyncContext 'UnofficialMultiplayerAPI.SyncContext') | [SyncContext](#T-UnofficialMultiplayerAPI-SyncContext 'UnofficialMultiplayerAPI.SyncContext') |

##### Remarks

Expand Down Expand Up @@ -625,10 +625,10 @@ UnofficialMultiplayerAPI
An attribute that is used to mark methods for syncing.

<a name='M-UnofficialMultiplayerAPI-SyncMethodAttribute-#ctor-UnofficialMultiplayerAPI-SyncContext-'></a>
### #ctor(context) `constructor`
### #ctor(context,cancelIfAnyArgNull,cancelIfNoSelectedMapObjects,cancelIfNoSelectedWorldObjects,exposeParameters) `constructor`

##### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| context | [UnofficialMultiplayerAPI.SyncContext](#T-UnofficialMultiplayerAPI-SyncContext 'UnofficialMultiplayerAPI.SyncContext') | Context |
| context | [UnofficialMultiplayerAPI.SyncContext](#T-UnofficialMultiplayerAPI-SyncContext 'UnofficialMultiplayerAPI.SyncContext') | Context (see [SyncContext](#T-UnofficialMultiplayerAPI-SyncContext 'UnofficialMultiplayerAPI.SyncContext')) |
22 changes: 14 additions & 8 deletions Source/Client/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,31 @@ public enum SyncContext
public class SyncMethodAttribute : Attribute
{
public SyncContext context;

/// <param name="context">Context</param>
public bool cancelIfAnyArgNull = false;
public bool cancelIfNoSelectedMapObjects = false;
public bool cancelIfNoSelectedWorldObjects = false;
public int[] exposeParameters;

/// <param name="context">Context (see <see cref="SyncContext"/>)</param>
/// <param name="cancelIfAnyArgNull">Instructs SyncMethod to cancel synchronization if any arg is null (see <see cref="ISyncMethod.CancelIfAnyArgNull"/>)</param>
/// <param name="cancelIfNoSelectedMapObjects">Instructs SyncMethod to cancel synchronization if no map objects were selected during call replication(see <see cref="ISyncMethod.CancelIfNoSelectedMapObjects"/>)</param>
/// <param name="cancelIfNoSelectedWorldObjects">Instructs SyncMethod to cancel synchronization if no world objects were selected during call replication(see <see cref="ISyncMethod.CancelIfNoSelectedWorldObjects"/>)</param>
/// <param name="exposeParameters">A list of types to expose (see <see cref="ISyncMethod.ExposeParameter"/>)</param>
public SyncMethodAttribute(SyncContext context = SyncContext.None)
{
this.context = context;
}
}

public struct SyncType
public class SyncType
{
public readonly Type type;
public bool expose;
public bool contextMap;
public bool expose = false;
public bool contextMap = false;

public SyncType(Type type)
{
this.type = type;
this.expose = false;
contextMap = false;
}
}

Expand Down Expand Up @@ -162,7 +168,7 @@ public interface ISyncMethod
/// Instructs method to send context along with the call
/// </summary>
/// <remarks>Context is restored after method is called</remarks>
/// <param name="context">Context</param>
/// <param name="context"><see cref="SyncContext"/></param>
/// <returns><see cref="ISyncMethod"/> self</returns>
ISyncMethod SetContext(SyncContext context);

Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
52 changes: 50 additions & 2 deletions Source/Host/ApiHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,56 @@ private static void RegisterAllMethods()

foreach(MethodInfo method in allMethods)
{
Log.Debug($"Installing {method.Name}");
Sync.RegisterSyncMethod(method, null);
var attribute = method.TryGetAttribute<UnofficialMultiplayerAPI.SyncMethodAttribute>();

Log.Debug($"Installing {method.DeclaringType.FullName}::{method}");
int[] exposeParameters = attribute.exposeParameters;
int paramNum = method.GetParameters().Length;

if (exposeParameters != null)
{
if (exposeParameters.Length != paramNum)
{
Log.Error($"Failed to register a method: Invalid number of parameters to expose in SyncMethod attribute applied to {method.DeclaringType.FullName}::{method}. Expected {paramNum}, got {exposeParameters.Length}");
continue;
}
else if(exposeParameters.Any(p => p < 0 || p >= paramNum))
{
Log.Error($"Failed to register a method: One or more indexes of parameters to expose in SyncMethod attribute applied to {method.DeclaringType.FullName}::{method} is invalid.");
continue;
}
}

SyncMethod sm = Sync.RegisterSyncMethod(method, null);

sm.SetContext((SyncContext)(int)attribute.context);

if (attribute.cancelIfAnyArgNull)
sm.CancelIfAnyArgNull();

if (attribute.cancelIfNoSelectedMapObjects)
sm.CancelIfNoSelectedMapObjects();

if (attribute.cancelIfNoSelectedWorldObjects)
sm.CancelIfNoSelectedWorldObjects();

if(exposeParameters != null)
{
int i = 0;

try
{
for (; i < exposeParameters.Length; i++)
{
Log.Debug($"Exposing parameter {exposeParameters[i]}");
sm.ExposeParameter(exposeParameters[i]);
}
}
catch (Exception exc)
{
Log.Error($"An exception occurred while exposing parameter {i} ({method.GetParameters()[i]}) for method {method.DeclaringType.FullName}::{method}: {exc}");
}
}
}

var initializers = allTypes.Where(t => !t.IsInterface && typeof(UnofficialMultiplayerAPI.IMultiplayerInit).IsAssignableFrom(t));
Expand Down

0 comments on commit adc4c07

Please sign in to comment.