Пример #1
0
        static void PreGameRuleVoteControllerSerialize(On.RoR2.PreGameRuleVoteController.orig_WriteVotes orig, PreGameRuleVoteController self, NetworkWriter writer)
        {
            Array    votes          = self.GetType().GetField("votes", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(self) as Array;
            RuleMask ruleMaskBuffer = self.GetFieldValue <RuleMask>("ruleMaskBuffer");

            for (int i = 0; i < RuleCatalog.ruleCount; i++)
            {
                ruleMaskBuffer[i] = votes.GetValue(i).GetPropertyValue <bool>("hasVoted");
            }
            writer.Write(ruleMaskBuffer);
            for (int i = 0; i < RuleCatalog.ruleCount - RegisteredVoteSelections.Count; i++)
            {
                if (votes.GetValue(i).GetPropertyValue <bool>("hasVoted"))
                {
                    votes.GetValue(i).GetType().InvokeMethod("Serialize", writer, votes.GetValue(i));
                }
            }

            for (int i = 0; i < RuleMaskExtraBytes.Count; i++)
            {
                writer.Write(RuleMaskExtraBytes[i]);
            }
            for (int i = RuleCatalog.ruleCount - RegisteredVoteSelections.Count; i < RuleCatalog.ruleCount; i++)
            {
                if (votes.GetValue(i).GetPropertyValue <bool>("hasVoted"))
                {
                    votes.GetValue(i).GetType().InvokeMethod("Serialize", writer, votes.GetValue(i));
                }
            }
            RuleMaskExtraBytes.Clear();
        }
Пример #2
0
        static void PreGameRuleVoteControllerDeserialize(On.RoR2.PreGameRuleVoteController.orig_ReadVotes orig, PreGameRuleVoteController self, NetworkReader reader)
        {
            Array    votes          = self.GetType().GetField("votes", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(self) as Array;
            RuleMask ruleMaskBuffer = self.GetFieldValue <RuleMask>("ruleMaskBuffer");

            reader.ReadRuleMask(ruleMaskBuffer);
            bool flag = !self.networkUserNetworkIdentity || !self.networkUserNetworkIdentity.isLocalPlayer;

            for (int i = 0; i < RuleCatalog.ruleCount - RegisteredVoteSelections.Count; i++)
            {
                object vote;
                if (ruleMaskBuffer[i])
                {
                    vote = votes.GetValue(i).GetType().InvokeMethod <object>("Deserialize", reader);
                }
                else
                {
                    vote = default(object);
                }
                if (flag)
                {
                    votes.SetValue(vote, i);
                }
            }

            if (TargetedRuleMask != null)
            {
                if (reader.Position < reader.Length)
                {
                    byte[] bytes      = (byte[])TargetedRuleMask.GetType().BaseType.GetField("bytes", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(TargetedRuleMask);
                    int    difference = bytes.Length - ((RuleCatalog.ruleCount - RegisteredVoteSelections.Count) + 7 >> 3);
                    for (int i = bytes.Length - difference; i < bytes.Length; i++)
                    {
                        bytes[i] = reader.ReadByte();
                    }

                    for (int i = RuleCatalog.ruleCount - RegisteredVoteSelections.Count; i < RuleCatalog.ruleCount; i++)
                    {
                        object vote;
                        if (ruleMaskBuffer[i])
                        {
                            vote = votes.GetValue(i).GetType().InvokeMethod <object>("Deserialize", reader);
                        }
                        else
                        {
                            vote = default(object);
                        }
                        if (flag)
                        {
                            votes.SetValue(vote, i);
                        }
                    }
                }
                TargetedRuleMask = null;
            }

            self.GetType().GetField("shouldUpdateGameVotes", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, ((bool)self.GetType().GetField("shouldUpdateGameVotes", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) || flag));
            if (NetworkServer.active)
            {
                self.SetDirtyBit(2u);
            }
        }