コード例 #1
0
        public override void Bind(object obj, string name)
        {
            object value = MpReflection.GetValue(obj, name);
            Type   type  = value.GetType();

            SyncSerialization.WriteSyncObject(writer, value, type);
        }
コード例 #2
0
        public static void WriteContext(SyncHandler handler, ByteWriter data)
        {
            if (handler.context == SyncContext.None)
            {
                return;
            }

            if (handler.context.HasFlag(SyncContext.CurrentMap))
            {
                data.MpContext().map = Find.CurrentMap;
            }

            if (handler.context.HasFlag(SyncContext.MapMouseCell))
            {
                data.MpContext().map = Find.CurrentMap;
                SyncSerialization.WriteSync(data, UI.MouseCell());
            }

            if (handler.context.HasFlag(SyncContext.MapSelected))
            {
                SyncSerialization.WriteSync(data, Find.Selector.selected.Cast <ISelectable>().ToList());
            }

            if (handler.context.HasFlag(SyncContext.WorldSelected))
            {
                SyncSerialization.WriteSync(data, Find.WorldSelector.selected.Cast <ISelectable>().ToList());
            }

            if (handler.context.HasFlag(SyncContext.QueueOrder_Down))
            {
                data.WriteBool(KeyBindingDefOf.QueueOrder.IsDownEvent);
            }
        }
コード例 #3
0
 protected void ValidateType(string desc, SyncType type)
 {
     if (type.type != null && !SyncSerialization.CanHandle(type))
     {
         throw new Exception($"Sync handler uses a non-serializable type: {type.type}. Details: {desc}");
     }
 }
コード例 #4
0
        public override void Handle(ByteReader data)
        {
            object target = null;

            if (targetType != null)
            {
                target = SyncSerialization.ReadSyncObject(data, targetType);
                if (target == null)
                {
                    return;
                }
            }

            object value = SyncSerialization.ReadSyncObject(data, fieldType);

            if (cancelIfValueNull && value == null)
            {
                return;
            }

            object index = null;

            if (indexType != null)
            {
                index = SyncSerialization.ReadSyncObject(data, indexType);
            }

            preApply?.Invoke(target, value);

            MpLog.Debug($"Set {memberPath} in {target} to {value}, map {data.MpContext().map}, index {index}");
            MpReflection.SetValue(target, memberPath, value, index);

            postApply?.Invoke(target, value);
        }
コード例 #5
0
        public static bool DesignateMultiCell(Designator __instance, IEnumerable <IntVec3> __0)
        {
            if (!Multiplayer.ShouldSync)
            {
                return(true);
            }

            // No cells implies Finalize(false), which currently doesn't cause side effects
            if (__0.Count() == 0)
            {
                return(true);
            }

            Designator designator = __instance;

            Map map = Find.CurrentMap;
            LoggingByteWriter writer = new LoggingByteWriter();

            writer.Log.Node("Designate multi cell: " + designator.GetType());
            IntVec3[] cellArray = __0.ToArray();

            WriteData(writer, DesignatorMode.MultiCell, designator);
            SyncSerialization.WriteSync(writer, cellArray);

            Multiplayer.Client.SendCommand(CommandType.Designator, map.uniqueID, writer.ToArray());
            Multiplayer.WriterLog.AddCurrentNode(writer);

            return(false);
        }
コード例 #6
0
        public override void Bind(object obj, string name)
        {
            object value = MpReflection.GetValue(obj, name);

            Type type = value.GetType();

            var res = SyncSerialization.ReadSyncObject(reader, type);

            MpReflection.SetValue(obj, name, res);
        }
コード例 #7
0
        internal static T ReadWithImpl <T>(ByteReader data, IList <Type> impls) where T : class
        {
            ushort impl = data.ReadUShort();

            if (impl == ushort.MaxValue)
            {
                return(null);
            }
            return((T)SyncSerialization.ReadSyncObject(data, impls[impl]));
        }
コード例 #8
0
ファイル: SyncAction.cs プロジェクト: rwmt/Multiplayer
        public override void Handle(ByteReader data)
        {
            A target = SyncSerialization.ReadSync <A>(data);
            B arg0   = SyncSerialization.ReadSync <B>(data);
            C arg1   = SyncSerialization.ReadSync <C>(data);

            int descHash = data.ReadInt32();

            var action = func(target, arg0, arg1).Select(t => actionGetter(t)).FirstOrDefault(a => GenText.StableStringHash(a.Method.MethodDesc()) == descHash);

            action?.Invoke();
        }
コード例 #9
0
        protected override object ReadTarget(ByteReader data)
        {
            object target = Activator.CreateInstance(targetType);

            for (int i = 0; i < fieldPaths.Length; i++)
            {
                string path       = fieldPaths[i];
                string noTypePath = fieldPathsNoTypes[i];
                Type   fieldType  = fieldTypes[i];
                object value;

                if (fieldTransformers[i] is SyncTransformer tr)
                {
                    value = tr.Reader.DynamicInvoke(SyncSerialization.ReadSyncObject(data, tr.NetworkType));
                }
                else if (fieldType.IsCompilerGenerated())
                {
                    value = Activator.CreateInstance(fieldType);
                }
                else
                {
                    value = SyncSerialization.ReadSyncObject(data, fieldType);
                }

                if (value == null)
                {
                    if (allowedNull != null && !allowedNull.Contains(noTypePath))
                    {
                        return(null);
                    }
                    if (noTypePath.EndsWith(DELEGATE_THIS))
                    {
                        return(null);
                    }
                    if (cancelIfNull != null && cancelIfNull.Contains(noTypePath))
                    {
                        return(null);
                    }
                }

                if (removeNullsFromLists != null && removeNullsFromLists.Contains(noTypePath) && value is IList list)
                {
                    list.RemoveNulls();
                }

                MpReflection.SetValue(target, path, value);
            }

            return(target);
        }
コード例 #10
0
        internal static void WriteWithImpl <T>(ByteWriter data, object obj, IList <Type> impls) where T : class
        {
            if (obj == null)
            {
                data.WriteUShort(ushort.MaxValue);
                return;
            }

            GetImpl(obj, impls, out Type implType, out int impl);

            if (implType == null)
            {
                throw new SerializationException($"Unknown {typeof(T)} implementation type {obj.GetType()}");
            }

            data.WriteUShort((ushort)impl);
            SyncSerialization.WriteSyncObject(data, obj, implType);
        }
コード例 #11
0
        public static bool DesignateSingleCell(Designator __instance, IntVec3 __0)
        {
            if (!Multiplayer.ShouldSync)
            {
                return(true);
            }

            Designator designator = __instance;

            Map map = Find.CurrentMap;
            LoggingByteWriter writer = new LoggingByteWriter();

            writer.Log.Node("Designate single cell: " + designator.GetType());

            WriteData(writer, DesignatorMode.SingleCell, designator);
            SyncSerialization.WriteSync(writer, __0);

            Multiplayer.Client.SendCommand(CommandType.Designator, map.uniqueID, writer.ToArray());
            Multiplayer.WriterLog.AddCurrentNode(writer);

            return(false);
        }
コード例 #12
0
        /// <summary>
        /// Returns whether the original should be cancelled
        /// </summary>
        public bool DoSync(object target, object value, object index = null)
        {
            if (!(inGameLoop || Multiplayer.ShouldSync))
            {
                return(false);
            }

            LoggingByteWriter writer  = new LoggingByteWriter();
            MpContext         context = writer.MpContext();

            writer.Log.Node(ToString());

            writer.WriteInt32(syncId);

            int mapId = ScheduledCommand.Global;

            if (targetType != null)
            {
                SyncSerialization.WriteSyncObject(writer, target, targetType);
                if (context.map != null)
                {
                    mapId = context.map.uniqueID;
                }
            }

            SyncSerialization.WriteSyncObject(writer, value, fieldType);
            if (indexType != null)
            {
                SyncSerialization.WriteSyncObject(writer, index, indexType);
            }

            writer.Log.Node($"Map id: {mapId}");
            Multiplayer.WriterLog.AddCurrentNode(writer);

            Multiplayer.Client.SendCommand(CommandType.Sync, mapId, writer.ToArray());

            return(true);
        }
コード例 #13
0
ファイル: SyncAction.cs プロジェクト: rwmt/Multiplayer
        private void ActualSync(A target, B arg0, C arg1, Action original)
        {
            LoggingByteWriter writer  = new LoggingByteWriter();
            MpContext         context = writer.MpContext();

            writer.Log.Node("Sync action");

            writer.WriteInt32(syncId);

            SyncSerialization.WriteSync(writer, target);
            SyncSerialization.WriteSync(writer, arg0);
            SyncSerialization.WriteSync(writer, arg1);

            writer.WriteInt32(GenText.StableStringHash(original.Method.MethodDesc()));
            Log.Message(original.Method.MethodDesc());

            int mapId = writer.MpContext().map?.uniqueID ?? -1;

            writer.Log.Node("Map id: " + mapId);
            Multiplayer.WriterLog.AddCurrentNode(writer);

            Multiplayer.Client.SendCommand(CommandType.Sync, mapId, writer.ToArray());
        }
コード例 #14
0
        private static void WriteData(ByteWriter data, DesignatorMode mode, Designator designator)
        {
            SyncSerialization.WriteSync(data, mode);
            SyncSerialization.WriteSyncObject(data, designator, designator.GetType());

            // Read at MapAsyncTimeComp.SetDesignatorState
            // The reading side affects global state so these can't be SyncWorkers

            if (designator is Designator_AreaAllowed)
            {
                SyncSerialization.WriteSync(data, Designator_AreaAllowed.SelectedArea);
            }

            if (designator is Designator_Install install)
            {
                SyncSerialization.WriteSync(data, install.MiniToInstallOrBuildingToReinstall);
            }

            if (designator is Designator_Zone)
            {
                SyncSerialization.WriteSync(data, Find.Selector.SelectedZone);
            }
        }
コード例 #15
0
        public static bool DesignateThing(Designator __instance, Thing __0)
        {
            if (!Multiplayer.ShouldSync)
            {
                return(true);
            }

            Designator designator = __instance;

            Map map = Find.CurrentMap;
            LoggingByteWriter writer = new LoggingByteWriter();

            writer.Log.Node("Designate thing: " + __0 + " " + designator.GetType());

            WriteData(writer, DesignatorMode.Thing, designator);
            SyncSerialization.WriteSync(writer, __0);

            Multiplayer.Client.SendCommand(CommandType.Designator, map.uniqueID, writer.ToArray());
            Multiplayer.WriterLog.AddCurrentNode(writer);

            FleckMaker.ThrowMetaPuffs(__0);

            return(false);
        }
コード例 #16
0
 public void Bind <T>(ref T obj, SyncType type)
 {
     obj = (T)SyncSerialization.ReadSyncObject(reader, type);
 }
コード例 #17
0
 public override void Bind <T>(ref T obj)
 {
     obj = (T)SyncSerialization.ReadSyncObject(reader, typeof(T));
 }
コード例 #18
0
        public static SyncHandler HandleCmd(ByteReader data)
        {
            int         syncId = data.ReadInt32();
            SyncHandler handler;

            try
            {
                handler = Sync.handlers[syncId];
            }
            catch (ArgumentOutOfRangeException)
            {
                Log.Error($"Error: invalid syncId {syncId}/{Sync.handlers.Count}, this implies mismatched mods, ensure your versions match! Stacktrace follows.");
                throw;
            }

            List <object>      prevSelected      = Find.Selector.selected;
            List <WorldObject> prevWorldSelected = Find.WorldSelector.selected;

            bool shouldQueue = false;

            if (handler.context != SyncContext.None)
            {
                if (handler.context.HasFlag(SyncContext.MapMouseCell))
                {
                    IntVec3 mouseCell = SyncSerialization.ReadSync <IntVec3>(data);
                    MouseCellPatch.result = mouseCell;
                }

                if (handler.context.HasFlag(SyncContext.MapSelected))
                {
                    List <ISelectable> selected = SyncSerialization.ReadSync <List <ISelectable> >(data);
                    Find.Selector.selected = selected.Cast <object>().AllNotNull().ToList();
                }

                if (handler.context.HasFlag(SyncContext.WorldSelected))
                {
                    List <ISelectable> selected = SyncSerialization.ReadSync <List <ISelectable> >(data);
                    Find.WorldSelector.selected = selected.Cast <WorldObject>().AllNotNull().ToList();
                }

                if (handler.context.HasFlag(SyncContext.QueueOrder_Down))
                {
                    shouldQueue = data.ReadBool();
                }
            }

            KeyIsDownPatch.shouldQueue = shouldQueue;

            try
            {
                handler.Handle(data);
            }
            finally
            {
                MouseCellPatch.result       = null;
                KeyIsDownPatch.shouldQueue  = null;
                Find.Selector.selected      = prevSelected;
                Find.WorldSelector.selected = prevWorldSelected;
            }

            return(handler);
        }
コード例 #19
0
 public void Bind <T>(ref T obj, SyncType type)
 {
     SyncSerialization.WriteSyncObject(writer, obj, type);
 }
コード例 #20
0
 public override void Bind <T>(ref T obj)
 {
     SyncSerialization.WriteSyncObject(writer, obj, typeof(T));
 }