예제 #1
0
        internal static void Serialize(object instance, BitWriter writer)
        {
            FieldInfo[] sortedFields;
            MethodInfo  preMethod;

            if (cachedFields.ContainsKey(instance.GetType().FullName))
            {
                sortedFields = cachedFields[instance.GetType().FullName];
            }
            else
            {
                sortedFields = instance.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).OrderBy(x => x.Name).Where(x => !x.IsDefined(typeof(BinaryIgnore), true)).ToArray();
                cachedFields.Add(instance.GetType().FullName, sortedFields);
            }

            if (preSerialize.ContainsKey(instance.GetType().FullName))
            {
                preMethod = preSerialize[instance.GetType().FullName];
            }
            else
            {
                preMethod = instance.GetType().GetMethod("PreSerialize", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                preSerialize.Add(instance.GetType().FullName, preMethod);
            }

            if (preMethod != null)
            {
                preMethod.Invoke(instance, null);
            }

            for (int i = 0; i < sortedFields.Length; i++)
            {
                FieldTypeHelper.WriteFieldType(writer, sortedFields[i].GetValue(instance));
            }
        }
예제 #2
0
        /// <summary>
        /// Serializes a class instance to binary
        /// </summary>
        /// <typeparam name="T">The class type to serialize</typeparam>
        /// <param name="instance">The instance to serialize</param>
        /// <returns>Binary serialized version of the instance</returns>
        public static byte[] Serialize <T>(T instance)
        {
            FieldInfo[] sortedFields;

            if (cachedFields.ContainsKey(instance.GetType().FullName))
            {
                sortedFields = cachedFields[instance.GetType().FullName];
            }
            else
            {
                sortedFields = instance.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).OrderBy(x => x.Name).Where(x => !x.IsDefined(typeof(BinaryIgnore), true)).ToArray();
                cachedFields.Add(instance.GetType().FullName, sortedFields);
            }

            using (BitWriter writer = new BitWriter())
            {
                for (int i = 0; i < sortedFields.Length; i++)
                {
                    FieldType fieldType = FieldTypeHelper.GetFieldType(sortedFields[i].FieldType);
                    if (fieldType == FieldType.Invalid)
                    {
                        Debug.LogWarning("MLAPI: The field " + sortedFields[i].Name + " will not be serialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up.");
                        continue;
                    }
                    FieldTypeHelper.WriteFieldType(writer, sortedFields[i].GetValue(instance), fieldType);
                }
                return(writer.Finalize());
            }
        }
예제 #3
0
 //Writes SyncedVar data in a formatted way so that the SetFormattedSyncedVarData method can read it.
 //The format doesn't NECCECARLY correspond with the "general syncedVar message layout"
 //as this should only be used for reading SyncedVar data that is to be read by the SetFormattedData method
 //*
 //The data contains every syncedvar on every behaviour that belongs to this object
 internal void WriteFormattedSyncedVarData(BitWriter writer)
 {
     for (int i = 0; i < childNetworkedBehaviours.Count; i++)
     {
         childNetworkedBehaviours[i].SyncVarInit();
         if (childNetworkedBehaviours[i].syncedVarFields.Count == 0)
         {
             continue;
         }
         writer.WriteUShort(GetOrderIndex(childNetworkedBehaviours[i])); //Write the behaviourId
         for (int j = 0; j < childNetworkedBehaviours[i].syncedVarFields.Count; j++)
         {
             FieldTypeHelper.WriteFieldType(writer, childNetworkedBehaviours[i].syncedVarFields[j].FieldValue);
         }
     }
 }