Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.0 kB
1
Indexable
Never
Index: Assets/Plugins/Mirror/Core/Attributes.cs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Assets/Plugins/Mirror/Core/Attributes.cs b/Assets/Plugins/Mirror/Core/Attributes.cs
--- a/Assets/Plugins/Mirror/Core/Attributes.cs	(revision 541cb8f70ffe86029cf95cc5a7a6a3b723c60f2c)
+++ b/Assets/Plugins/Mirror/Core/Attributes.cs	(date 1706020484572)
@@ -11,6 +11,7 @@
     public class SyncVarAttribute : PropertyAttribute
     {
         public string hook;
+        public bool initialOnly;
     }
 
     /// <summary>
Index: Assets/Plugins/Mirror/Editor/Weaver/Processors/SyncVarAttributeProcessor.cs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Assets/Plugins/Mirror/Editor/Weaver/Processors/SyncVarAttributeProcessor.cs b/Assets/Plugins/Mirror/Editor/Weaver/Processors/SyncVarAttributeProcessor.cs
--- a/Assets/Plugins/Mirror/Editor/Weaver/Processors/SyncVarAttributeProcessor.cs	(revision 541cb8f70ffe86029cf95cc5a7a6a3b723c60f2c)
+++ b/Assets/Plugins/Mirror/Editor/Weaver/Processors/SyncVarAttributeProcessor.cs	(date 1706021031121)
@@ -45,7 +45,18 @@
 
             return FindHookMethod(td, syncVar, hookFunctionName, ref WeavingFailed);
         }
+        
+        // Get initialOnly if any
+        public bool GetInitialOnly(FieldDefinition syncVar)
+        {
+            CustomAttribute syncVarAttr = syncVar.GetCustomAttribute<SyncVarAttribute>();
 
+            if (syncVarAttr == null)
+                return false;
+
+            return syncVarAttr.GetField("initialOnly", false);
+        }
+
         // Create a field definition for a field that will store the Action<T, T> delegate instance for the syncvar hook method (only instantiate delegate once)
         public FieldDefinition CreateNewActionFieldDefinitionFromHookMethod(FieldDefinition syncVarField)
         {
@@ -251,7 +262,29 @@
 
             return get;
         }
+        
+        MethodDefinition GenerateSyncVarSetterInitialOnly(FieldDefinition fd, string originalName)
+        {
+            var set = new MethodDefinition($"set_Network{originalName}", MethodAttributes.Public |
+                                                                        MethodAttributes.SpecialName |
+                                                                        MethodAttributes.HideBySig,
+                weaverTypes.Import(typeof(void)));
 
+            var valueParam = new ParameterDefinition("value", ParameterAttributes.None, fd.FieldType);
+            set.Parameters.Add(valueParam);
+            set.SemanticsAttributes = MethodSemanticsAttributes.Setter;
+
+            ILProcessor worker = set.Body.GetILProcessor();
+            var fr = fd.DeclaringType.HasGenericParameters ? fd.MakeHostInstanceGeneric() : fd;
+            
+            worker.Emit(OpCodes.Ldarg_0);
+            worker.Emit(OpCodes.Ldarg, valueParam);
+            worker.Emit(OpCodes.Stfld, fr);
+            worker.Emit(OpCodes.Ret);
+            
+            return set;
+        }
+        
         // for [SyncVar] health, weaver generates
         //
         //   NetworkHealth
@@ -390,6 +423,7 @@
         public void ProcessSyncVar(TypeDefinition td, FieldDefinition fd, Dictionary<FieldDefinition, FieldDefinition> syncVarNetIds, Dictionary<FieldDefinition, (FieldDefinition hookDelegateField, MethodDefinition hookMethod)> syncVarHookDelegates, long dirtyBit, ref bool WeavingFailed)
         {
             string originalName = fd.Name;
+            bool initialOnly = GetInitialOnly(fd);
 
             // GameObject/NetworkIdentity SyncVars have a new field for netId
             FieldDefinition netIdField = null;
@@ -416,7 +450,9 @@
             }
 
             MethodDefinition get = GenerateSyncVarGetter(fd, originalName, netIdField);
-            MethodDefinition set = GenerateSyncVarSetter(td, fd, originalName, dirtyBit, netIdField, syncVarHookDelegates, ref WeavingFailed);
+            MethodDefinition set = initialOnly
+                    ? GenerateSyncVarSetterInitialOnly(fd, originalName)
+                    : GenerateSyncVarSetter(td, fd, originalName, dirtyBit, netIdField, syncVarHookDelegates, ref WeavingFailed);
 
             //NOTE: is property even needed? Could just use a setter function?
             //create the property
@@ -430,7 +466,10 @@
             td.Methods.Add(get);
             td.Methods.Add(set);
             td.Properties.Add(propertyDefinition);
-            syncVarAccessLists.replacementSetterProperties[fd] = set;
+            if (!initialOnly)
+            { 
+                syncVarAccessLists.replacementSetterProperties[fd] = set;
+            }
 
             // replace getter field if GameObject/NetworkIdentity so it uses
             // netId instead
Leave a Comment