Skip to content

Commit 6d461bf

Browse files
fix: ilpp stripping and coreclr compatibility [NCCBUG-209] (#2614)
* update excluding getTypeName, * update Make sure internal classes can access NetworkBehaviour.NetworkVariableFields in runtime builds. * update to get RPCS working temporarily. * style Adding comments about the RPC stuff since it is a bit more complicated than the previous two issues. * style updating and adding comments to regions of code changed in order to easily distinguish the changes made for each issue type. * update Missed two files. * Updated the ILPP code to work correctly with CoreCLR: - delegates are no longer fields, but types - internal virtual functions can't be overridden in child classes anymore, so they have to be changed from `IsAssembly` to `IsFamilyOrAssembly` in ILPP - Internal fields changed to `IsFamily` can no longer be accessed within the assembly, and need to be `IsFamilyOrAssembly` instead. With those changes, previous workarounds are no longer needed, so I reverted them. * update removing commented out code. wrapping the rpc table generation log info so it only logs to the console when log mode is set to developer. * style Fixing whitespace issue. * revert __RpcParams to internal * update adding change log entry * update Moving the version number back down to v1.5.1 * Fixed validator failures. * Update package.json Reverting back to v1.5.2 to validate compatibility testing * Add Validation exceptions for 1.5.2 * Apparently the meta file is also needed. --------- Co-authored-by: Kitty Draper <kitty.draper@unity3d.com>
1 parent dfe7138 commit 6d461bf

File tree

9 files changed

+52
-21
lines changed

9 files changed

+52
-21
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1818
- Fixed issue where `NetworkObject.SpawnWithObservers` was not being honored for late joining clients. (#2623)
1919
- Fixed issue where invoking `NetworkManager.Shutdown` multiple times, depending upon the timing, could cause an exception. (#2622)
2020
- Fixed issue where removing ownership would not notify the server that it gained ownership. This also resolves the issue where an owner authoritative NetworkTransform would not properly initialize upon removing ownership from a remote client. (#2618)
21+
- Fixed ILPP issues when using CoreCLR and for certain dedicated server builds. (#2614)
2122
- Fixed an ILPP compile error when creating a generic NetworkBehaviour singleton with a static T instance. (#2603)
2223

2324
### Changed

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
396396
#endif
397397
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef;
398398

399+
private MethodReference m_RuntimeInitializeOnLoadAttribute_Ctor;
400+
399401
private MethodReference m_ExceptionCtorMethodReference;
400402
private MethodReference m_List_NetworkVariableBase_Add;
401403

@@ -509,6 +511,8 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
509511
}
510512
}
511513

514+
m_RuntimeInitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(RuntimeInitializeOnLoadMethodAttribute).GetConstructor(new Type[] { }));
515+
512516
TypeDefinition networkManagerTypeDef = null;
513517
TypeDefinition networkBehaviourTypeDef = null;
514518
TypeDefinition networkVariableBaseTypeDef = null;
@@ -1200,19 +1204,14 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
12001204

12011205
if (rpcHandlers.Count > 0 || rpcNames.Count > 0)
12021206
{
1203-
var staticCtorMethodDef = typeDefinition.GetStaticConstructor();
1204-
if (staticCtorMethodDef == null)
1205-
{
1206-
staticCtorMethodDef = new MethodDefinition(
1207-
".cctor", // Static Constructor (constant-constructor)
1208-
MethodAttributes.HideBySig |
1209-
MethodAttributes.SpecialName |
1210-
MethodAttributes.RTSpecialName |
1207+
var staticCtorMethodDef = new MethodDefinition(
1208+
$"InitializeRPCS_{typeDefinition.Name}",
1209+
MethodAttributes.Assembly |
12111210
MethodAttributes.Static,
12121211
typeDefinition.Module.TypeSystem.Void);
1213-
staticCtorMethodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
1214-
typeDefinition.Methods.Add(staticCtorMethodDef);
1215-
}
1212+
staticCtorMethodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
1213+
staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_RuntimeInitializeOnLoadAttribute_Ctor));
1214+
typeDefinition.Methods.Add(staticCtorMethodDef);
12161215

12171216
var instructions = new List<Instruction>();
12181217
var processor = staticCtorMethodDef.Body.GetILProcessor();
@@ -1254,7 +1253,8 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
12541253
baseGetTypeNameMethod.ReturnType)
12551254
{
12561255
ImplAttributes = baseGetTypeNameMethod.ImplAttributes,
1257-
SemanticsAttributes = baseGetTypeNameMethod.SemanticsAttributes
1256+
SemanticsAttributes = baseGetTypeNameMethod.SemanticsAttributes,
1257+
IsFamilyOrAssembly = true
12581258
};
12591259

12601260
var processor = newGetTypeNameMethod.Body.GetILProcessor();

com.unity.netcode.gameobjects/Editor/CodeGen/RuntimeAccessModifiersILPP.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ private void ProcessNetworkManager(TypeDefinition typeDefinition, string[] assem
9898
fieldDefinition.IsPublic = true;
9999
}
100100
}
101+
102+
foreach (var nestedTypeDefinition in typeDefinition.NestedTypes)
103+
{
104+
if (nestedTypeDefinition.Name == nameof(NetworkManager.RpcReceiveHandler))
105+
{
106+
nestedTypeDefinition.IsNestedPublic = true;
107+
}
108+
}
101109
}
102110

103111
private void ProcessNetworkBehaviour(TypeDefinition typeDefinition)
@@ -114,7 +122,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition)
114122
{
115123
if (fieldDefinition.Name == nameof(NetworkBehaviour.__rpc_exec_stage) || fieldDefinition.Name == nameof(NetworkBehaviour.NetworkVariableFields))
116124
{
117-
fieldDefinition.IsFamily = true;
125+
fieldDefinition.IsFamilyOrAssembly = true;
118126
}
119127
}
120128

@@ -130,6 +138,11 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition)
130138
{
131139
methodDefinition.IsFamily = true;
132140
}
141+
142+
if (methodDefinition.Name == nameof(NetworkBehaviour.__getTypeName))
143+
{
144+
methodDefinition.IsFamilyOrAssembly = true;
145+
}
133146
}
134147
}
135148
}

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ internal enum __RpcExecStage
1818
Server = 1,
1919
Client = 2
2020
}
21-
22-
2321
// NetworkBehaviourILPP will override this in derived classes to return the name of the concrete type
2422
internal virtual string __getTypeName() => nameof(NetworkBehaviour);
2523

@@ -98,7 +96,6 @@ internal void __endSendServerRpc(ref FastBufferWriter bufferWriter, uint rpcMeth
9896
}
9997

10098
bufferWriter.Dispose();
101-
10299
#if DEVELOPMENT_BUILD || UNITY_EDITOR
103100
if (NetworkManager.__rpc_name_table.TryGetValue(rpcMethodId, out var rpcMethodName))
104101
{
@@ -230,7 +227,6 @@ internal void __endSendClientRpc(ref FastBufferWriter bufferWriter, uint rpcMeth
230227
}
231228

232229
bufferWriter.Dispose();
233-
234230
#if DEVELOPMENT_BUILD || UNITY_EDITOR
235231
if (NetworkManager.__rpc_name_table.TryGetValue(rpcMethodId, out var rpcMethodName))
236232
{

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,6 @@ internal NetworkBehaviour GetNetworkBehaviourAtOrderIndex(ushort index)
11981198
{
11991199
NetworkLog.LogError($"{nameof(NetworkBehaviour)} index {index} was out of bounds for {name}. NetworkBehaviours must be the same, and in the same order, between server and client.");
12001200
}
1201-
12021201
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
12031202
{
12041203
var currentKnownChildren = new System.Text.StringBuilder();
@@ -1211,7 +1210,6 @@ internal NetworkBehaviour GetNetworkBehaviourAtOrderIndex(ushort index)
12111210
}
12121211
NetworkLog.LogInfo(currentKnownChildren.ToString());
12131212
}
1214-
12151213
return null;
12161214
}
12171215

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public void Serialize(FastBufferWriter writer, int targetVersion)
105105
{
106106
networkVariable.WriteDelta(writer);
107107
}
108-
109108
NetworkBehaviour.NetworkManager.NetworkMetrics.TrackNetworkVariableDeltaSent(
110109
TargetClientId,
111110
NetworkBehaviour.NetworkObject,
@@ -207,7 +206,6 @@ public void Handle(ref NetworkContext context)
207206
networkBehaviour.__getTypeName(),
208207
context.MessageSize);
209208

210-
211209
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
212210
{
213211
if (m_ReceivedNetworkVariableData.Position > (readStartPos + varSize))

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/RpcMessages.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public static void Handle(ref NetworkContext context, ref RpcMetadata metadata,
7272
catch (Exception ex)
7373
{
7474
Debug.LogException(new Exception("Unhandled RPC exception!", ex));
75+
if (networkManager.LogLevel == LogLevel.Developer)
76+
{
77+
Debug.Log($"RPC Table Contents");
78+
foreach (var entry in NetworkManager.__rpc_func_table)
79+
{
80+
Debug.Log($"{entry.Key} | {entry.Value.Method.Name}");
81+
}
82+
}
7583
}
7684
}
7785
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"ErrorExceptions": [
3+
{
4+
"ValidationTest": "API Validation",
5+
"ExceptionMessage": "Additions require a new minor or major version.",
6+
"PackageVersion": "1.5.2"
7+
}
8+
],
9+
"WarningExceptions": []
10+
}

com.unity.netcode.gameobjects/ValidationExceptions.json.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)