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; TSPlayer tSPlayer = new TSPlayer(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 AbstractLockstep(FP deltaTime, ICommunicator communicator, IPhysicsManager physicsManager, int syncWindow, int panicWindow, int rollbackWindow, TrueSyncEventCallback OnGameStarted, TrueSyncEventCallback OnGamePaused, TrueSyncEventCallback OnGameUnPaused, TrueSyncEventCallback OnGameEnded, TrueSyncPlayerDisconnectionCallback OnPlayerDisconnection, TrueSyncUpdateCallback OnStepUpdate, TrueSyncInputCallback GetLocalData) { this.deltaTime = deltaTime; this.syncWindow = syncWindow; this.panicWindow = panicWindow; this.rollbackWindow = rollbackWindow; this.totalWindow = syncWindow + rollbackWindow; this.StepUpdate = OnStepUpdate; this.OnGameStarted = OnGameStarted; this.OnGamePaused = OnGamePaused; this.OnGameUnPaused = OnGameUnPaused; this.OnGameEnded = OnGameEnded; this.OnPlayerDisconnection = OnPlayerDisconnection; this.GetLocalData = GetLocalData; this.ticks = 0; this.players = new SortedDictionary <byte, TSPlayer>(); this.communicator = communicator; bool flag = communicator != null; if (flag) { this.communicator.AddEventListener(new OnEventReceived(this.OnEventDataReceived)); } this.physicsManager = physicsManager; this.compoundStats = new CompoundStats(); this.bufferSyncedInfo = new GenericBufferWindow <SyncedInfo>(3); this.checksumOk = true; this.simulationState = AbstractLockstep.SimulationState.NOT_STARTED; this.bodiesToDestroy = new Dictionary <int, List <IBody> >(); this.delegatesToExecute = new Dictionary <int, List <Delegate> >(); this.ReplayRecord = ReplayRecord.replayToLoad; this.ReplayMode = ReplayRecord.replayMode; this.time = FP.Zero; StateTracker.AddTracking(this, "time"); }
public static ReplayRecord GetReplayRecord(string replayContent) { ReplayRecord result = null; try { result = JsonUtility.FromJson <ReplayRecord>(replayContent); } catch (Exception) { } return(result); }
public static ReplayRecord GetReplayRecord(byte[] replayContent) { ReplayRecord result = null; try { result = ReplayRecord.FromReplayBytes(replayContent); } catch (Exception) { } return(result); }
public static void SaveRecord(ReplayRecord replay) { if (ReplayRecordSave != null) { try { ReplayRecordSave(ReplayRecord.ToReplayBytes(replay), replay.players.Count); } catch (Exception) { } } }
public static void SaveRecord(ReplayRecord replay) { bool flag = ReplayRecord.ReplayRecordSave == null; if (!flag) { try { ReplayRecord.ReplayRecordSave(JsonUtility.ToJson(replay), replay.players.Count); } catch (Exception) { } } }
private static byte[] ToReplayBytes(ReplayRecord replay) { List <byte> list = new List <byte>(); Dictionary <byte, TSPlayer> .Enumerator enumerator = replay.players.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <byte, TSPlayer> current = enumerator.Current; TSPlayer 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()); }
void Start() { instance = this; Application.runInBackground = true; if (ReplayRecord.replayMode == ReplayMode.LOAD_REPLAY) { ReplayRecord replayRecord = ReplayRecord.replayToLoad; if (replayRecord == null) { Debug.LogError("Replay Record can't be loaded"); gameObject.SetActive(false); return; } } ICommunicator communicator = null; if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) { Debug.LogWarning("You are not connected to Photon. TrueSync will start in offline mode."); } else { communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer); } TrueSyncConfig activeConfig = ActiveConfig; lockstep = AbstractLockstep.NewInstance( lockedTimeStep, communicator, PhysicsManager.instance, activeConfig.syncWindow, activeConfig.panicWindow, activeConfig.rollbackWindow, OnGameStarted, OnGamePaused, OnGameUnPaused, OnGameEnded, OnPlayerDisconnection, OnStepUpdate, GetLocalData ); if (activeConfig.showStats) { this.gameObject.AddComponent <TrueSyncStats>().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); foreach (PhotonPlayer p in players) { lockstep.AddPlayer((byte)p.ID, p.NickName, p.IsLocal); } } } generalBehaviours = new List <TrueSyncManagedBehaviour>(); foreach (TrueSyncBehaviour tsb in FindObjectsOfType <TrueSyncBehaviour>()) { generalBehaviours.Add(NewManagedBehavior(tsb)); } initBehaviors(); initGeneralBehaviors(generalBehaviours, false); PhysicsManager.instance.OnRemoveBody(OnRemovedRigidBody); startState = StartState.BEHAVIOR_INITIALIZED; }
void Start() { instance = this; Application.runInBackground = true; ICommunicator communicator = null; if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) { Debug.LogWarning("You are not connected to Photon. TrueSync will start in offline mode."); } else { communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer); } TrueSyncConfig activeConfig = ActiveConfig; //帧同步逻辑的回调都交由TrueSyncManagedBehaviour执行 //例如:AbstractLockstep.OnGameStarted->TrueSyncManager.OnGameStarted->TrueSyncManagedBehaviour.OnGameStarted()(这是个staic method,之后遍历每一个TrueSyncManagedBehaviour实例的OnSyncedStart) 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 <TrueSyncStats>().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); } } } TrueSyncBehaviour[] behavioursArray = FindObjectsOfType <TrueSyncBehaviour>(); 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; }
void Start() { instance = this; Application.runInBackground = true; //离线状态 就AbstractLockstep的OnEventDataReceived接收网络数据不会执行 //更换网络接口 可以从ICommunicator接口 入手 ICommunicator communicator = null; if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) { Debug.LogWarning("You are not connected to Photon. TrueSync will start in offline mode."); } else { communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer); } TrueSyncConfig 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 <TrueSyncStats>().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); //更新players activePlayers } } } //搜寻场景预先挂载的TrueSyncBehaviour脚本,为它生成TrueSyncManagedBehaviour脚本 //可能这部分脚本有些属于玩家 有些属于公共部分,都根据OwnerIndex来区分 TrueSyncBehaviour[] behavioursArray = FindObjectsOfType <TrueSyncBehaviour>(); for (int index = 0, length = behavioursArray.Length; index < length; index++) { generalBehaviours.Add(NewManagedBehavior(behavioursArray[index]));//一个TrueSyncBehaviour对应TrueSyncManagedBehaviour } initBehaviors(); //初始化玩家prefab和挂在prefab上的TrueSyncBehaviour initGeneralBehaviors(generalBehaviours, false); //公用和玩家的区分,公用分给generalBehaviours,玩家分给behaviorsByPlayer PhysicsManager.instance.OnRemoveBody(OnRemovedRigidBody); startState = StartState.BEHAVIOR_INITIALIZED;//初始化完毕状态 }
void Start() { instance = this; Application.runInBackground = true; ICommunicator communicator = null; // if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) { // Debug.LogWarning("You are not connected to Photon. TrueSync will start in offline mode."); // } else { // communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer); // } if (KBEngine.KBEngineApp.app.networkInterface().connected == false) { Debug.LogWarning("You are not connected to Server. TrueSync will start in offline mode."); } else { communicator = new RealSyncCommunicator(); } // if (NetMgr.Instance.srvConn.status == Connection.Status.None) // { // Debug.LogWarning("You are not connected to Server. TrueSync will start in offline mode."); // } // else // { // communicator = new RealSyncCommunicator(); // } TrueSyncConfig 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 <TrueSyncStats>().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()); } } } TrueSyncBehaviour[] behavioursArray = FindObjectsOfType <TrueSyncBehaviour>(); 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; }