private static ReplayRecord FromReplayBytes(byte[] replayBytes)
        {
            ReplayRecord replayRecord = new ReplayRecord();
            int          i            = 0;

            while (i < replayBytes.Length)
            {
                byte b   = replayBytes[i++];
                int  num = BitConverter.ToInt32(replayBytes, i);
                i += 4;
                string @string = Encoding.ASCII.GetString(replayBytes, i, num);
                i += num;
                FPPlayer tSPlayer = new FPPlayer(b, @string);
                replayRecord.players.Add(b, tSPlayer);
                int num2 = BitConverter.ToInt32(replayBytes, i);
                i += 4;
                for (int j = 0; j < num2; j++)
                {
                    SyncedData @new = SyncedData.pool.GetNew();
                    @new.tick = BitConverter.ToInt32(replayBytes, i);
                    i        += 4;
                    @new.inputData.Deserialize(replayBytes, ref i);
                    @new.inputData.ownerID = b;
                    tSPlayer.controls.Add(@new.tick, @new);
                }
            }
            return(replayRecord);
        }
        public static ReplayRecord GetReplayRecord(byte[] replayContent)
        {
            ReplayRecord result = null;

            try
            {
                result = ReplayRecord.FromReplayBytes(replayContent);
            }
            catch (Exception)
            {
            }
            return(result);
        }
        public static void SaveRecord(ReplayRecord replay)
        {
            bool flag = ReplayRecord.ReplayRecordSave == null;

            if (!flag)
            {
                try
                {
                    ReplayRecord.ReplayRecordSave(ReplayRecord.ToReplayBytes(replay), replay.players.Count);
                }
                catch (Exception)
                {
                }
            }
        }
        private static byte[] ToReplayBytes(ReplayRecord replay)
        {
            List <byte> list = new List <byte>();

            Dictionary <byte, FPPlayer> .Enumerator enumerator = replay.players.GetEnumerator();
            while (enumerator.MoveNext())
            {
                KeyValuePair <byte, FPPlayer> current = enumerator.Current;
                FPPlayer value = current.Value;
                list.Add(value.playerInfo.id);
                Utils.GetBytes(value.playerInfo.name.Length, list);
                list.AddRange(Encoding.ASCII.GetBytes(value.playerInfo.name));
                Utils.GetBytes(value.controls.Count, list);
                Dictionary <int, SyncedData> .Enumerator enumerator2 = value.controls.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    KeyValuePair <int, SyncedData> current2 = enumerator2.Current;
                    Utils.GetBytes(current2.Key, list);
                    current2 = enumerator2.Current;
                    current2.Value.inputData.Serialize(list);
                }
            }
            return(list.ToArray());
        }
Beispiel #5
0
        void Start()
        {
            instance = this;
            Application.runInBackground = true;

            ICommunicator communicator = null;

//            if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) {
//                Debug.LogWarning("You are not connected to Photon. FrameSync will start in offline mode.");
//            } else {
//                communicator = new PhotonFrameSyncCommunicator(PhotonNetwork.networkingPeer);
//            }


//             if (NetMgr.Instance.srvConn.status == Connection.Status.None)
//             {
//                 Debug.LogWarning("You are not connected to Server. FrameSync will start in offline mode.");
//             }
//             else
//             {
//                 communicator = new RealSyncCommunicator();
//             }

            FrameSyncConfig activeConfig = ActiveConfig;

            lockstep = AbstractLockstep.NewInstance(
                lockedTimeStep.AsFloat(),
                communicator,
                PhysicsManager.instance,
                activeConfig.syncWindow,
                activeConfig.panicWindow,
                activeConfig.rollbackWindow,
                OnGameStarted,
                OnGamePaused,
                OnGameUnPaused,
                OnGameEnded,
                OnPlayerDisconnection,
                OnStepUpdate,
                GetLocalData,
                ProvideInputData
                );

            if (ReplayRecord.replayMode == ReplayMode.LOAD_REPLAY)
            {
                //               ReplayPicker.replayToLoad.Load();

                ReplayRecord replayRecord = ReplayRecord.replayToLoad;
                if (replayRecord == null)
                {
                    Debug.LogError("Replay Record can't be loaded");
                    gameObject.SetActive(false);
                    return;
                }
                else
                {
                    lockstep.ReplayRecord = replayRecord;
                }
            }

            if (activeConfig.showStats)
            {
                this.gameObject.AddComponent <FrameSyncStats>().Lockstep = lockstep;
            }

            scheduler = new CoroutineScheduler(lockstep);

            if (ReplayRecord.replayMode != ReplayMode.LOAD_REPLAY)
            {
                if (communicator == null)
                {
                    lockstep.AddPlayer(0, "Local_Player", true);
                }
                else
                {
                    //                    List<PhotonPlayer> players = new List<PhotonPlayer>(PhotonNetwork.playerList);
                    //                    players.Sort(UnityUtils.playerComparer);
                    //
                    //                    for (int index = 0, length = players.Count; index < length; index++) {
                    //                        PhotonPlayer p = players[index];
                    //                        lockstep.AddPlayer((byte) p.ID, p.NickName, p.IsLocal);
                    //                    }

//                     for (int i = 0; i < GameData.Instance.RoomPlayers.Count; i++)
//                     {
//                         KBEngine.Avatar player = GameData.Instance.RoomPlayers[i];
//                         lockstep.AddPlayer((byte)(i + 1), player.id.ToString(), player.isPlayer());
//                     }
                }
            }

            FrameSyncBehaviour[] behavioursArray = FindObjectsOfType <FrameSyncBehaviour>();
            for (int index = 0, length = behavioursArray.Length; index < length; index++)
            {
                generalBehaviours.Add(NewManagedBehavior(behavioursArray[index]));
            }

            initBehaviors();
            initGeneralBehaviors(generalBehaviours, false);

            PhysicsManager.instance.OnRemoveBody(OnRemovedRigidBody);

            startState = StartState.BEHAVIOR_INITIALIZED;
        }