Skip to content

Commit

Permalink
fix: calling syncvar hook when not connected yet (#77)
Browse files Browse the repository at this point in the history
Also simplifies weaver code and add a new property to Networkbehaviour
  • Loading branch information
Lymdun committed Mar 17, 2020
1 parent d0c01a2 commit e64727b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
6 changes: 2 additions & 4 deletions Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,10 @@ public static MethodDefinition ProcessSyncVarSet(TypeDefinition td, FieldDefinit

if (hookFunctionMethod != null)
{
//if (base.netIdentity.server.localClientActive && !getSyncVarHookGuard(dirtyBit))
//if (base.isLocalClient && !getSyncVarHookGuard(dirtyBit))
Instruction label = setWorker.Create(OpCodes.Nop);
setWorker.Append(setWorker.Create(OpCodes.Ldarg_0));
setWorker.Append(setWorker.Create(OpCodes.Call, Weaver.NetworkBehaviourGetIdentity));
setWorker.Append(setWorker.Create(OpCodes.Call, Weaver.NetworkIdentityGetServer));
setWorker.Append(setWorker.Create(OpCodes.Call, Weaver.NetworkServerGetLocalClientActive));
setWorker.Append(setWorker.Create(OpCodes.Call, Weaver.NetworkBehaviourIsLocalClient));
setWorker.Append(setWorker.Create(OpCodes.Brfalse, label));
setWorker.Append(setWorker.Create(OpCodes.Ldarg_0));
setWorker.Append(setWorker.Create(OpCodes.Ldc_I8, dirtyBit));
Expand Down
2 changes: 2 additions & 0 deletions Assets/Mirror/Editor/Weaver/Weaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Weaver

public static MethodReference NetworkBehaviourIsServer;
public static MethodReference NetworkBehaviourIsClient;
public static MethodReference NetworkBehaviourIsLocalClient;

// custom attribute types
public static TypeReference SyncVarType;
Expand Down Expand Up @@ -285,6 +286,7 @@ static void SetupTargetTypes()

NetworkBehaviourIsServer = Resolvers.ResolveProperty(NetworkBehaviourType, CurrentAssembly, "isServer");
NetworkBehaviourIsClient = Resolvers.ResolveProperty(NetworkBehaviourType, CurrentAssembly, "isClient");
NetworkBehaviourIsLocalClient = Resolvers.ResolveProperty(NetworkBehaviourType, CurrentAssembly, "isLocalClient");

MonoBehaviourType = UnityAssembly.MainModule.GetType("UnityEngine.MonoBehaviour");
ScriptableObjectType = UnityAssembly.MainModule.GetType("UnityEngine.ScriptableObject");
Expand Down
5 changes: 5 additions & 0 deletions Assets/Mirror/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class NetworkBehaviour : MonoBehaviour
/// </summary>
public bool isClient => netIdentity.isClient;

/// <summary>
/// Returns true if we're on host mode.
/// </summary>
public bool isLocalClient => netIdentity.isLocalClient;

/// <summary>
/// This returns true if this object is the one that represents the player on the local machine.
/// <para>In multiplayer games, there are multiple instances of the Player object. The client needs to know which one is for "themselves" so that only that player processes input and potentially has a camera attached. The IsLocalPlayer function will return true only for the player instance that belongs to the player on the local machine, so it can be used to filter out input for non-local players.</para>
Expand Down
5 changes: 5 additions & 0 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public sealed class NetworkIdentity : MonoBehaviour
/// </summary>
public bool isServer => server != null && server.active && netId != 0;

/// <summary>
/// Returns true if we're on host mode.
/// </summary>
public bool isLocalClient => server != null && server.LocalClientActive;

/// <summary>
/// This returns true if this object is the one that represents the player on the local machine.
/// <para>This is set when the server has spawned an object for this particular client.</para>
Expand Down

0 comments on commit e64727b

Please sign in to comment.