Skip to content

Commit

Permalink
1.4.5.Prelease.0
Browse files Browse the repository at this point in the history
IMPORTANT: this update requires you to delete your FishNet folder when importing from GitHub!!

- Fixed NetworkAnimatorEditor not properly displaying parameters for AnimatorOverrideControllers.
- Improved TimeManager.Tick is now set on clients during the authentication process.
- Improved TimeManager readability.
- Improved description of several codegen warnings.
- Improved TimeManager.RoundTripTime accuracy.
- Added TargetRpc and ObserversRpc attributes can be used on the same method.
- Added NetworkManager.SpawnPacking, to control transform compression during spawns.
- Added NetworkTransform.Packing, to control transform compression during updates.
- Fixed automatic code stripping not working on some Unity versions.
- Improved OnStartServer/Network no longer calls before active scene is set during a scene load.
- Fixed IEnumerator Awake codegen errors when inheritance involved multiple assemblies.
- Fixed Server and Client attribute methods not logging when NetworkBehaviour was not initialized.
  • Loading branch information
FirstGearGames committed Apr 11, 2022
1 parent 78c1e36 commit 3f7f315
Show file tree
Hide file tree
Showing 58 changed files with 1,619 additions and 828 deletions.
1 change: 1 addition & 0 deletions Assets/FishNet/CodeGenerating/BuildInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static bool StripBuild
{
get
{


/* This is to protect non pro users from enabling this
* without the extra logic code. */
Expand Down
5 changes: 3 additions & 2 deletions Assets/FishNet/CodeGenerating/Helpers/CodegenSession.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FishNet.CodeGenerating.Processing;
using FishNet.CodeGenerating.Processing.Rpc;
using MonoFN.Cecil;
using System.Collections.Generic;
using Unity.CompilationPipeline.Common.Diagnostics;
Expand Down Expand Up @@ -46,7 +47,7 @@ internal static class CodegenSession
[System.ThreadStatic]
internal static QolAttributeProcessor QolAttributeProcessor;
[System.ThreadStatic]
internal static NetworkBehaviourRpcProcessor NetworkBehaviourRpcProcessor;
internal static RpcProcessor RpcProcessor;
[System.ThreadStatic]
internal static NetworkBehaviourSyncProcessor NetworkBehaviourSyncProcessor;
[System.ThreadStatic]
Expand Down Expand Up @@ -107,7 +108,7 @@ internal static bool Reset(ModuleDefinition module)
CustomSerializerProcessor = new CustomSerializerProcessor();
NetworkBehaviourProcessor = new NetworkBehaviourProcessor();
QolAttributeProcessor = new QolAttributeProcessor();
NetworkBehaviourRpcProcessor = new NetworkBehaviourRpcProcessor();
RpcProcessor = new RpcProcessor();
NetworkBehaviourSyncProcessor = new NetworkBehaviourSyncProcessor();
NetworkBehaviourPredictionProcessor = new NetworkBehaviourPredictionProcessor();
DifferentAssemblySyncVars = new List<FieldDefinition>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static void ClearMethodWithRet(this MethodDefinition md)
{
md.Body.Instructions.Clear();
ILProcessor processor = md.Body.GetILProcessor();
CodegenSession.ObjectHelper.CreateRetDefault(md);
processor.Add(CodegenSession.ObjectHelper.CreateRetDefault(md));
}

/// <summary>
Expand Down
26 changes: 17 additions & 9 deletions Assets/FishNet/CodeGenerating/Helpers/GeneralHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,27 +544,35 @@ internal List<Instruction> CreateDebugWithCanLogInstructions(ILProcessor process
return instructions;


//If null skip..
Instruction skipDebugInst = processor.Create(OpCodes.Nop);
VariableDefinition nmVariableDef = CreateVariable(processor.Body.Method, typeof(NetworkManager));
VariableDefinition networkManagerVd = CreateVariable(processor.Body.Method, typeof(NetworkManager));
//Using InstanceFinder(static).
if (useStatic)
{
//Store nm refrence.
//Store instancefinder to nm variable.
instructions.Add(processor.Create(OpCodes.Call, InstanceFinder_NetworkManager_MethodRef));
instructions.Add(processor.Create(OpCodes.Stloc, nmVariableDef));
instructions.Add(processor.Create(OpCodes.Stloc, networkManagerVd));
}
//Using networkBehaviour.
else
{
//Store nm reference.
instructions.Add(processor.Create(OpCodes.Ldarg_0));
instructions.Add(processor.Create(OpCodes.Call, NetworkBehaviour_NetworkManager_MethodRef));
instructions.Add(processor.Create(OpCodes.Stloc, nmVariableDef));
instructions.Add(processor.Create(OpCodes.Stloc, networkManagerVd));
//If was set to null then try to log with instancefinder.
Instruction skipStaticSetInst = processor.Create(OpCodes.Nop);
//if (nmVd == null) nmVd = InstanceFinder.NetworkManager.
instructions.Add(processor.Create(OpCodes.Ldloc, networkManagerVd));
instructions.Add(processor.Create(OpCodes.Brtrue_S, skipStaticSetInst));
//Store instancefinder to nm variable.
instructions.Add(processor.Create(OpCodes.Call, InstanceFinder_NetworkManager_MethodRef));
instructions.Add(processor.Create(OpCodes.Stloc, networkManagerVd));
instructions.Add(skipStaticSetInst);
}

//null check nm reference.
instructions.Add(processor.Create(OpCodes.Ldloc, nmVariableDef));
Instruction skipDebugInst = processor.Create(OpCodes.Nop);
//null check nm reference. If null then skip logging.
instructions.Add(processor.Create(OpCodes.Ldloc, networkManagerVd));
instructions.Add(processor.Create(OpCodes.Brfalse_S, skipDebugInst));

//Only need to call CanLog if not using networkmanager logging.
Expand All @@ -577,7 +585,7 @@ internal List<Instruction> CreateDebugWithCanLogInstructions(ILProcessor process
instructions.Add(processor.Create(OpCodes.Brfalse_S, skipDebugInst));
}

instructions.Add(processor.Create(OpCodes.Ldloc, nmVariableDef));
instructions.Add(processor.Create(OpCodes.Ldloc, networkManagerVd));
instructions.AddRange(debugPrint);
instructions.Add(skipDebugInst);

Expand Down
12 changes: 6 additions & 6 deletions Assets/FishNet/CodeGenerating/Helpers/ObjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,9 @@ internal void CreateRpcDelegate(bool runLocally, TypeDefinition typeDef, MethodD
/// <summary>
/// Creates exit method condition if local client is not owner.
/// </summary>
/// <param name="processor"></param>
/// <param name="retIfOwner">True if to ret when owner, false to ret when not owner.</param>
/// <returns>Returns Ret instruction.</returns>
internal Instruction CreateLocalClientIsOwnerCheck(MethodDefinition methodDef, LoggingType loggingType, bool retIfOwner, bool insertFirst)
internal Instruction CreateLocalClientIsOwnerCheck(MethodDefinition methodDef, LoggingType loggingType, bool canDisableLogging, bool retIfOwner, bool insertFirst)
{
List<Instruction> instructions = new List<Instruction>();
/* This is placed after the if check.
Expand All @@ -301,9 +300,10 @@ internal Instruction CreateLocalClientIsOwnerCheck(MethodDefinition methodDef, L
//If logging is not disabled.
if (loggingType != LoggingType.Off)
{
string disableLoggingText = (canDisableLogging) ? DISABLE_LOGGING_TEXT : string.Empty;
string msg = (retIfOwner) ?
$"Cannot complete action because you are the owner of this object. {DISABLE_LOGGING_TEXT}." :
$"Cannot complete action because you are not the owner of this object. {DISABLE_LOGGING_TEXT}.";
$"Cannot complete action because you are the owner of this object. {disableLoggingText}." :
$"Cannot complete action because you are not the owner of this object. {disableLoggingText}.";

instructions.AddRange(
CodegenSession.GeneralHelper.CreateDebugWithCanLogInstructions(processor, msg, loggingType, false, true)
Expand Down Expand Up @@ -385,7 +385,7 @@ internal void CreateIsClientCheck(MethodDefinition methodDef, LoggingType loggin
//If warning then also append warning text.
if (loggingType != LoggingType.Off)
{
string msg = "Cannot complete action because client is not active. This may also occur if the object is not yet initialized or if it does not contain a NetworkObject component.";
string msg = $"Cannot complete action because client is not active. This may also occur if the object is not yet initialized or if it does not contain a NetworkObject component. {DISABLE_LOGGING_TEXT}.";
instructions.AddRange(
CodegenSession.GeneralHelper.CreateDebugWithCanLogInstructions(processor, msg, loggingType, useStatic, true)
);
Expand Down Expand Up @@ -436,7 +436,7 @@ internal void CreateIsServerCheck(MethodDefinition methodDef, LoggingType loggin
//If warning then also append warning text.
if (loggingType != LoggingType.Off)
{
string msg = "Cannot complete action because server is not active. This may also occur if the object is not yet initialized or if it does not contain a NetworkObject component.";
string msg = $"Cannot complete action because server is not active. This may also occur if the object is not yet initialized or if it does not contain a NetworkObject component. {DISABLE_LOGGING_TEXT}";
instructions.AddRange(
CodegenSession.GeneralHelper.CreateDebugWithCanLogInstructions(processor, msg, loggingType, useStatic, true)
);
Expand Down
5 changes: 2 additions & 3 deletions Assets/FishNet/CodeGenerating/ILCore/FishNetILPP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Linq;
using System.Text;
using Unity.CompilationPipeline.Common.ILPostProcessing;
using UnityEngine;

namespace FishNet.CodeGenerating.ILCore
{
Expand Down Expand Up @@ -337,7 +336,7 @@ private bool CreateNetworkBehaviours()
foreach (TypeDefinition td in CodegenSession.Module.Types)
{
CodegenSession.NetworkBehaviourSyncProcessor.ReplaceGetSets(td, allProcessedSyncs);
CodegenSession.NetworkBehaviourRpcProcessor.RedirectBaseCalls();
CodegenSession.RpcProcessor.RedirectBaseCalls();
}
}

Expand Down Expand Up @@ -392,7 +391,7 @@ void SetChildRpcCounts(Dictionary<TypeDefinition, uint> typeDefCounts, List<Type
do
{
//How many RPCs are in copyTd.
uint copyCount = CodegenSession.NetworkBehaviourRpcProcessor.GetRpcCount(copyTd);
uint copyCount = CodegenSession.RpcProcessor.GetRpcCount(copyTd);

/* If not found it this is the first time being
* processed. When this occurs set the value
Expand Down
2 changes: 1 addition & 1 deletion Assets/FishNet/CodeGenerating/Processing.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ private void ClientCreateReconcileReader(TypeDefinition typeDef, MethodDefinitio
* unreliably. It's possible they will arrive after
* an owner change. */
// if (!base.IsOwner) return;
CodegenSession.ObjectHelper.CreateLocalClientIsOwnerCheck(createdMd, LoggingType.Off, false, false);
CodegenSession.ObjectHelper.CreateLocalClientIsOwnerCheck(createdMd, LoggingType.Off, true, false, false);

//uint receivedTick = data.DATA_TICK_FIELD_NAME.
VariableDefinition receivedTickVd = CodegenSession.GeneralHelper.CreateVariable(createdMd, typeof(uint));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using MonoFN.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using UnityDebug = UnityEngine.Debug;

namespace FishNet.CodeGenerating.Processing
{
Expand Down Expand Up @@ -87,7 +88,7 @@ internal bool Process(TypeDefinition typeDef, List<(SyncType, ProcessedSync)> al
//25ms

/* RPCs. */
modified |= CodegenSession.NetworkBehaviourRpcProcessor.Process(copyTypeDef, ref rpcCount);
modified |= CodegenSession.RpcProcessor.Process(copyTypeDef, ref rpcCount);
//30ms
/* //perf rpcCounts can be optimized by having different counts
* for target, observers, server, replicate, and reoncile rpcs. Since
Expand Down Expand Up @@ -150,7 +151,7 @@ internal bool NonNetworkBehaviourHasInvalidAttributes(Collection<TypeDefinition>
foreach (MethodDefinition md in typeDef.Methods)
{
//Has RPC attribute but doesn't inherit from NB.
if (CodegenSession.NetworkBehaviourRpcProcessor.GetRpcAttribute(md, false, out _) != null)
if (CodegenSession.RpcProcessor.Attributes.HasRpcAttributes(md))
{
CodegenSession.LogError($"{typeDef.FullName} has one or more RPC attributes but does not inherit from NetworkBehaviour.");
error = true;
Expand Down Expand Up @@ -317,6 +318,7 @@ private bool CreateOrModifyAwakeMethods(TypeDefinition typeDef)
//First check if any are public, if so that's the attribute all of them will use.
bool usePublic = false;

//Determine if awake needs to be public or protected.
TypeDefinition copyTypeDef = typeDef;
do
{
Expand All @@ -329,37 +331,33 @@ private bool CreateOrModifyAwakeMethods(TypeDefinition typeDef)
{
usePublic = true;
break;
}
}
}
copyTypeDef = TypeDefinitionExtensions.GetNextBaseClassToProcess(copyTypeDef);

} while (copyTypeDef != null);


MethodAttributes attributes = (usePublic) ? PUBLIC_VIRTUAL_ATTRIBUTES : PROTECTED_VIRTUAL_ATTRIBUTES;

//Now update all scopes/create methods.
copyTypeDef = typeDef;
do
{
{
MethodDefinition tmpMd = copyTypeDef.GetMethod(ObjectHelper.AWAKE_METHOD_NAME);
//Exist, make it public virtual.
if (tmpMd != null)
{
if (tmpMd.ReturnType != copyTypeDef.Module.TypeSystem.Void)
{
{
CodegenSession.LogError($"IEnumerator Awake methods are not supported within NetworkBehaviours.");
return false;
}
tmpMd.Attributes = PUBLIC_VIRTUAL_ATTRIBUTES;

tmpMd.Attributes = attributes;
}
//Does not exist, add it.
else
{
tmpMd = new MethodDefinition(ObjectHelper.AWAKE_METHOD_NAME, PUBLIC_VIRTUAL_ATTRIBUTES, CodegenSession.Module.TypeSystem.Void);
tmpMd = new MethodDefinition(ObjectHelper.AWAKE_METHOD_NAME, attributes, CodegenSession.Module.TypeSystem.Void);
tmpMd = new MethodDefinition(ObjectHelper.AWAKE_METHOD_NAME, attributes, copyTypeDef.Module.TypeSystem.Void);
copyTypeDef.Methods.Add(tmpMd);
ILProcessor processor = tmpMd.Body.GetILProcessor();
processor.Emit(OpCodes.Ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ internal bool Process(TypeDefinition typeDef)
{
bool modified = false;
List<MethodDefinition> methods = typeDef.Methods.ToList();
foreach (MethodDefinition methodDef in methods)
foreach (MethodDefinition md in methods)
{
//Has RPC attribute, doesn't quality for a quality of life attribute.
if (CodegenSession.NetworkBehaviourRpcProcessor.GetRpcAttribute(methodDef, false, out _) != null)
if (CodegenSession.RpcProcessor.Attributes.HasRpcAttributes(md))
continue;

QolAttributeType qolType;
CustomAttribute qolAttribute = GetQOLAttribute(methodDef, out qolType);
CustomAttribute qolAttribute = GetQOLAttribute(md, out qolType);
if (qolAttribute == null)
continue;

Expand All @@ -36,7 +36,7 @@ internal bool Process(TypeDefinition typeDef)
continue;
}

CreateAttributeMethod(methodDef, qolAttribute, qolType);
CreateAttributeMethod(md, qolAttribute, qolType);
modified = true;
}

Expand Down Expand Up @@ -105,8 +105,7 @@ private CustomAttribute GetQOLAttribute(MethodDefinition methodDef, out QolAttri
private void CreateAttributeMethod(MethodDefinition methodDef, CustomAttribute qolAttribute, QolAttributeType qolType)
{
bool inheritsNetworkBehaviour = methodDef.DeclaringType.InheritsNetworkBehaviour();
TypeDefinition typeDef = methodDef.DeclaringType;


//True to use InstanceFInder.
bool useStatic = (methodDef.IsStatic || !inheritsNetworkBehaviour);

Expand All @@ -128,7 +127,7 @@ private void CreateAttributeMethod(MethodDefinition methodDef, CustomAttribute q
}
//If (!base.IsOwner);
if (requireOwnership)
CodegenSession.ObjectHelper.CreateLocalClientIsOwnerCheck(methodDef, logging, false, true);
CodegenSession.ObjectHelper.CreateLocalClientIsOwnerCheck(methodDef, logging, true, false, true);
//Otherwise normal IsClient check.
else
CodegenSession.ObjectHelper.CreateIsClientCheck(methodDef, logging, useStatic, true);
Expand All @@ -141,7 +140,7 @@ private void CreateAttributeMethod(MethodDefinition methodDef, CustomAttribute q
{
LoggingType logging = qolAttribute.GetField("Logging", LoggingType.Warning);
CodegenSession.ObjectHelper.CreateIsServerCheck(methodDef, logging, useStatic, true);
}
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions Assets/FishNet/CodeGenerating/Processing/Rpc.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions Assets/FishNet/CodeGenerating/Processing/Rpc/AttributeData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using FishNet.CodeGenerating.Helping;
using FishNet.Object.Helping;
using MonoFN.Cecil;
using System.Collections.Generic;

namespace FishNet.CodeGenerating.Processing.Rpc
{
internal static class AttributeDataExtensions
{

/// <summary>
/// Returns RpcTypes in datas.
/// </summary>
public static List<RpcType> GetRpcTypes(this List<AttributeData> datas)
{
//RpcTypes for originalMd.
List<RpcType> rpcTypes = new List<RpcType>();
foreach (AttributeData ad in datas)
rpcTypes.Add(ad.RpcType);

return rpcTypes;
}

/// <summary>
/// Gets CustomAttribute for rpcType
/// </summary>
public static CustomAttribute GetAttribute(this List<AttributeData> datas, RpcType rpcType)
{
for (int i = 0; i < datas.Count; i++)
{
if (datas[i].RpcType == rpcType)
return datas[i].Attribute;
}

CodegenSession.LogError($"RpcType {rpcType} not found in datas.");
return null;
}


/// <summary>
/// Returns RpcType as flag through combining datas.
/// </summary>
/// <param name="datas"></param>
/// <returns></returns>
public static RpcType GetCombinedRpcType(this List<AttributeData> datas)
{
RpcType result = RpcType.None;
for (int i = 0; i < datas.Count; i++)
result |= datas[i].RpcType;

return result;
}
}

internal class AttributeData
{
public readonly CustomAttribute Attribute;
public readonly RpcType RpcType;

public AttributeData(CustomAttribute attribute, RpcType rpcType)
{
Attribute = attribute;
RpcType = rpcType;
}

}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f7f315

Please sign in to comment.