/// <summary> /// 收集本地玩家的输入,并通过服务器发送给其他玩家 OnSyncInput在这调用 /// </summary> /// <returns></returns> private SyncedData UpdateData() { SyncedData result; if (this.replayMode == ReplayMode.LOAD_REPLAY) { result = null; } else { SyncedData syncedData = SyncedData.pool.GetNew(); syncedData.Init(this.localPlayer.ID, this.ticks); //本地玩家ID来构造一个空数据包,等待输入数据填充 this.GetLocalData(syncedData.inputData); //TrueSyncManager.GetLocalData, 调用OnSyncedInput();获取本地玩家输入 this.localPlayer.AddData(syncedData); //本地数据直接塞给本地玩家(controls key:ticks value:syncedData 存储玩家每个tick的数据),下面还需要把本地数据发送给远端 if (this.communicator != null) { //因为_syncedDataCacheUpdateData只有一个,所以只获取了一个SyncedData this.localPlayer.GetSendData(this.ticks, this._syncedDataCacheUpdateData);//从TSplayer的controls里取数据 //把本地玩家的输入数据发给远端玩家 this.communicator.OpRaiseEvent(SEND_CODE, SyncedData.Encode(this._syncedDataCacheUpdateData), true, this.auxActivePlayersIds); } result = syncedData; } return(result); }
/// <summary> /// 解码参考编码 /// </summary> /// <param name="data"></param> /// <returns></returns> public static List <SyncedData> Decode(byte[] data) { List <SyncedData> pool = SyncedData.poolList.GetNew(); pool.Clear(); int i = 0; int tick = BitConverter.ToInt32(data, i); i += 4; byte ownerID = data[i++]; byte dropFromPlayerId = data[i++]; bool dropPlayer = data[i++] == 1; //int num2 = tick; while (i < data.Length) { SyncedData syncedData = SyncedData.pool.GetNew(); syncedData.Init(ownerID, tick--);//这里不理解为啥要tick-- syncedData.inputData.Deserialize(data, ref i); pool.Add(syncedData); } if (pool.Count > 0) { pool[0].dropPlayer = dropPlayer; pool[0].dropFromPlayerId = dropFromPlayerId; } return(pool); }
// 如果不是录像模式;获取本地玩家的输入操作,添加到本地玩家的当前帧同步数据;发送本地玩家操作给其他玩家 private SyncedData UpdateData() { bool flag = this.replayMode == ReplayMode.LOAD_REPLAY; SyncedData result; if (flag) { result = null; } else { // 获取本地玩家的输入操作,添加到本地玩家的当前帧同步数据 SyncedData @new = SyncedData.pool.GetNew(); @new.Init(this.localPlayer.ID, this.ticks); this.GetLocalData(@new.inputData); this.localPlayer.AddData(@new); bool flag2 = this.communicator != null; if (flag2) { // 发送本地玩家操作给其他玩家 this.localPlayer.GetSendData(this.ticks, this._syncedDataCacheUpdateData); // 199 this.communicator.OpRaiseEvent(SEND_CODE, SyncedData.Encode(this._syncedDataCacheUpdateData), true, this.auxActivePlayersIds); } result = @new; } return(result); }
public SyncedData clone() { SyncedData syncedData = new SyncedData(this.inputData.ownerID, this.tick); foreach (KeyValuePair <byte, byte> current in this.inputData.byteTable) { syncedData.inputData.AddByte(current.Key, current.Value); } foreach (KeyValuePair <byte, int> current2 in this.inputData.intTable) { syncedData.inputData.AddInt(current2.Key, current2.Value); } foreach (KeyValuePair <byte, string> current3 in this.inputData.stringTable) { syncedData.inputData.AddString(current3.Key, current3.Value); } foreach (KeyValuePair <byte, FP> current4 in this.inputData.fpTable) { syncedData.inputData.AddFP(current4.Key, current4.Value); } foreach (KeyValuePair <byte, TSVector> current5 in this.inputData.tsVectorTable) { syncedData.inputData.AddTSVector(current5.Key, current5.Value); } return(syncedData); }
private void DropLagPlayers() { List <TSPlayer> list = new List <TSPlayer>(); int refTick = this.GetRefTick(this.GetSyncedDataTick()); bool flag = refTick >= 0; if (flag) { foreach (TSPlayer current in this.players.Values) { bool flag2 = !current.IsDataReady(refTick); if (flag2) { current.dropCount++; this.CheckDrop(current); list.Add(current); } } } foreach (TSPlayer current2 in list) { SyncedData[] sendDataForDrop = current2.GetSendDataForDrop(this.localPlayer.ID, 1); bool flag3 = sendDataForDrop.Length != 0; if (flag3) { this.communicator.OpRaiseEvent(199, SyncedData.Encode(sendDataForDrop), true, null); } } }
private SyncedData UpdateData() { bool flag = this.replayMode == ReplayMode.LOAD_REPLAY; SyncedData result; if (flag) { result = null; } else { SyncedData syncedData = new SyncedData(this.localPlayer.ID, this.ticks); this.GetLocalData(syncedData.inputData); this.localPlayer.AddData(syncedData); bool flag2 = this.communicator != null; if (flag2) { SyncedData[] sendData = this.localPlayer.GetSendData(this.ticks, 1); bool flag3 = sendData.Length != 0; if (flag3) { this.communicator.OpRaiseEvent(199, SyncedData.Encode(sendData), true, this.GetActivePlayers()); } } result = syncedData; } return(result); }
private SyncedData UpdateData() { bool flag = this.replayMode == ReplayMode.LOAD_REPLAY; SyncedData result; if (flag) // 加载回放模式 { result = null; } else // 非加载回放模式 { SyncedData @new = SyncedData.pool.GetNew(); // 从池中获取 @new.Init(this.localPlayer.ID, this.ticks); this.GetLocalData(@new.inputData); // 把玩家操作数据放入SyncedData实例中 this.localPlayer.AddData(@new); // 把SyncedData实例放入本地玩家的操作数据字典中 bool flag2 = this.communicator != null; if (flag2) // 有通信器 { this.localPlayer.GetSendData(this.ticks, this._syncedDataCacheUpdateData); // 把最新的操作数据放入_syncedDataCacheUpdateData中 this.communicator.OpRaiseEvent(199, SyncedData.Encode(this._syncedDataCacheUpdateData), true, this.auxActivePlayersIds); // 发送操作数据 } result = @new; } return(result); }
public SyncedData GetData(int tick) { bool flag = !this.controls.ContainsKey(tick); SyncedData result; if (flag) { bool flag2 = this.controls.ContainsKey(tick - 1); SyncedData syncedData; if (flag2) { syncedData = this.controls[tick - 1].clone(); syncedData.tick = tick; } else { syncedData = new SyncedData(this.ID, tick); } syncedData.fake = true; this.controls[tick] = syncedData; result = syncedData; } else { result = this.controls[tick]; } return(result); }
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 void AddDataProjected(int refTick, int window) { SyncedData syncedData = this.GetData(refTick); for (int i = 1; i <= window; i++) { SyncedData data = this.GetData(refTick + i); bool fake = data.fake; if (fake) { SyncedData syncedData2 = syncedData.clone(); syncedData2.fake = true; syncedData2.tick = refTick + i; bool flag = this.controls.ContainsKey(syncedData2.tick); if (flag) { SyncedData.pool.GiveBack(this.controls[syncedData2.tick]); } this.controls[syncedData2.tick] = syncedData2; } else { bool dirty = data.dirty; if (dirty) { data.dirty = false; syncedData = data; } } } }
public static List <SyncedData> Decode(byte[] data) { List <SyncedData> @new = poolList.GetNew(); @new.Clear(); int i = 0; int num = BitConverter.ToInt32(data, i); i += 4; byte ownerID = data[i++]; byte b = data[i++]; bool flag = data[i++] == 1; int num2 = num; while (i < data.Length) { SyncedData new2 = SyncedData.pool.GetNew(); new2.Init(ownerID, num2--); new2.inputData.Deserialize(data, ref i); @new.Add(new2); } bool flag2 = @new.Count > 0; if (flag2) { @new[0].dropPlayer = flag; @new[0].dropFromPlayerId = b; } return(@new); }
public SyncedData clone() { SyncedData @new = SyncedData.pool.GetNew(); @new.Init(this.inputData.ownerID, this.tick); @new.inputData.CopyFrom(this.inputData); return(@new); }
internal void AddSyncedData(SyncedData[] data) { for (int i = 0; i < data.Length; i++) { SyncedData syncedData = data[i]; this.players[syncedData.inputData.ownerID].AddData(syncedData); } }
public void AddData(SyncedData data) { int tick = data.tick; bool flag = !this.controls.ContainsKey(tick); if (flag) { this.controls[tick] = data; } }
internal void AddSyncedData(List <SyncedData> data) { int i = 0; int count = data.Count; while (i < count) { SyncedData syncedData = data[i].clone(); this.players[syncedData.inputData.ownerID].AddData(syncedData); i++; } }
/// <summary> /// 如果controls含有该key,那么就添加到SyncedDataPool相当于被回收了,没有该key那么就添加到controls /// </summary> /// <param name="data"></param> public void AddData(SyncedData data) { int tick = data.tick; if (this.controls.ContainsKey(tick)) { SyncedData.pool.GiveBack(data); } else { this.controls[tick] = data; this.lastTick = tick; } }
/// <summary> /// DV 添加存在帧同步数据 /// </summary> public void AddData(SyncedData data) { int tick = data.tick; bool flag = this.controls.ContainsKey(tick); if (flag) // 数据已存在,把数据对象回收到池中 { SyncedData.pool.GiveBack(data); } else // 未有数据 { this.controls[tick] = data; this.lastTick = tick;// 设置最后存储的帧 } }
private SyncedData[] GetDataFromTick(int tick, int sendWindow) { bool flag = tick < sendWindow; if (flag) { sendWindow = tick + 1; } SyncedData[] array = new SyncedData[sendWindow]; for (int i = 0; i < sendWindow; i++) { array[i] = this.GetData(tick - i); } return(array); }
/// <summary> /// 添加同步数据SyncedData /// </summary> /// <param name="data"></param> public void AddData(SyncedData data) { int tick = data.tick; //如果controls里有数据,那么就添加到堆栈 if (controls.ContainsKey(tick)) { SyncedData.pool.GiveBack(data); } //没有就添加到controls里 else { controls[tick] = data; lastTick = tick; } }
private static bool checkEqualsTSVectorTable(SyncedData s1, SyncedData s2) { bool result; foreach (KeyValuePair <byte, TSVector> current in s1.inputData.tsVectorTable) { bool flag = !s2.inputData.tsVectorTable.ContainsKey(current.Key) || s1.inputData.tsVectorTable[current.Key] != s2.inputData.tsVectorTable[current.Key]; if (flag) { result = false; return(result); } } result = true; return(result); }
public SyncedData[] GetSendDataForDrop(byte fromPlayerId, int sendWindow) { bool flag = this.controls.Count == 0; SyncedData[] result; if (flag) { result = new SyncedData[0]; } else { SyncedData[] dataFromTick = this.GetDataFromTick(this.controls.Keys.Last <int>(), sendWindow); dataFromTick[0] = dataFromTick[0].clone(); dataFromTick[0].dropFromPlayerId = fromPlayerId; dataFromTick[0].dropPlayer = true; result = dataFromTick; } return(result); }
/// <summary> /// rollback使用 暂时忽略 /// </summary> /// <param name="data"></param> public void AddDataRollback(List <SyncedData> data) { for (int i = 0; i < data.Count; i++) { SyncedData data2 = this.GetData(data[i].tick); bool fake = data2.fake; if (fake) { if (!data2.EqualsData(data[i])) { data[i].dirty = true; SyncedData.pool.GiveBack(controls[data[i].tick]); controls[data[i].tick] = data[i]; break; } data2.fake = false; data2.dirty = false; } SyncedData.pool.GiveBack(data[i]); } }
public bool EqualsData(SyncedData other) { bool flag = !SyncedData.checkEqualsByteTable(this, other) || !SyncedData.checkEqualsByteTable(other, this); bool result; if (flag) { result = false; } else { bool flag2 = !SyncedData.checkEqualsIntTable(this, other) || !SyncedData.checkEqualsIntTable(other, this); if (flag2) { result = false; } else { bool flag3 = !SyncedData.checkEqualsStringTable(this, other) || !SyncedData.checkEqualsStringTable(other, this); if (flag3) { result = false; } else { bool flag4 = !SyncedData.checkEqualsFPTable(this, other) || !SyncedData.checkEqualsFPTable(other, this); if (flag4) { result = false; } else { bool flag5 = !SyncedData.checkEqualsTSVectorTable(this, other) || !SyncedData.checkEqualsTSVectorTable(other, this); result = !flag5; } } } } return(result); }
public void AddDataRollback(SyncedData[] data) { for (int i = 0; i < data.Length; i++) { SyncedData data2 = this.GetData(data[i].tick); bool fake = data2.fake; if (fake) { bool flag = data2.EqualsData(data[i]); if (flag) { data2.fake = false; data2.dirty = false; } else { data[i].dirty = true; this.controls[data[i].tick] = data[i]; } } } }
// 添加数据 public void AddDataRollback(List <SyncedData> data) { for (int i = 0; i < data.Count; i++) { SyncedData data2 = this.GetData(data[i].tick); bool fake = data2.fake; if (fake) // 取出的是假数据 { bool flag = data2.EqualsData(data[i]); if (!flag) // 两个数据不想等 { data[i].dirty = true; // 设置为脏数据 SyncedData.pool.GiveBack(this.controls[data[i].tick]); // 回收该位置的数据 this.controls[data[i].tick] = data[i]; // 放入新数据 break; // 中断,没有下一个数据要处理? } data2.fake = false; data2.dirty = false; } SyncedData.pool.GiveBack(data[i]); } }
private void DropLagPlayers() { List <TSPlayer> list = new List <TSPlayer>(); int refTick = this.GetRefTick(this.GetSyncedDataTick()); bool flag = refTick >= 0; if (flag) { int i = 0; int count = this.activePlayers.Count; while (i < count) { TSPlayer tSPlayer = this.activePlayers[i]; bool flag2 = !tSPlayer.IsDataReady(refTick); if (flag2) { tSPlayer.dropCount++; list.Add(tSPlayer); } i++; } } int j = 0; int count2 = list.Count; while (j < count2) { TSPlayer p = list[j]; this.CheckDrop(p); bool sendDataForDrop = list[j].GetSendDataForDrop(this.localPlayer.ID, this._syncedDataCacheDrop); if (sendDataForDrop) { // 199 this.communicator.OpRaiseEvent(SEND_CODE, SyncedData.Encode(this._syncedDataCacheDrop), true, null); SyncedData.pool.GiveBack(this._syncedDataCacheDrop[0]); } j++; } }
/// <summary> /// Lag:落后, 延迟 /// </summary> private void DropLagPlayers() { Debug.Log("DropLagPlayers"); List <TSPlayer> list = new List <TSPlayer>(); int refTick = GetRefTick(GetSyncedDataTick()); if (refTick >= 0) { int i = 0; int count = activePlayers.Count; while (i < count) { TSPlayer tSPlayer = activePlayers[i]; if (!tSPlayer.IsDataReady(refTick)) { tSPlayer.dropCount++; list.Add(tSPlayer); } i++; } } int j = 0; int count2 = list.Count; while (j < count2) { TSPlayer p = list[j]; CheckDrop(p); bool sendDataForDrop = list[j].GetSendDataForDrop(localPlayer.ID, _syncedDataCacheDrop); if (sendDataForDrop) { communicator.OpRaiseEvent(SEND_CODE, SyncedData.Encode(_syncedDataCacheDrop), true, null); SyncedData.pool.GiveBack(_syncedDataCacheDrop[0]); } j++; } }
/// <summary> /// 收集本地输入,通过服务器发送给其他玩家 /// </summary> /// <returns></returns> private SyncedData UpdateData() { Debug.Log("UpdateData"); SyncedData result; if (replayMode == ReplayMode.LOAD_REPLAY) { result = null; } else { SyncedData @new = SyncedData.pool.GetNew(); @new.Init(localPlayer.ID, ticks); GetLocalData(@new.inputData); //调用OnSyncedInput();输入数据给到@new.inputData里面 localPlayer.AddData(@new); //数据塞给TSPlayer的controls if (communicator != null) { localPlayer.GetSendData(ticks, _syncedDataCacheUpdateData); //从TSplayer的controls里取数据 communicator.OpRaiseEvent(SEND_CODE, SyncedData.Encode(_syncedDataCacheUpdateData), true, auxActivePlayersIds); //将自己的输入数据发给其他玩家 } result = @new; } return(result); }
public bool EqualsData(SyncedData other) { return(this.inputData.EqualsData(other.inputData)); }
private void OnEventDataReceived(byte eventCode, object content) { bool flag = eventCode == 199; if (flag) // 接收操作数据 { byte[] data = content as byte[]; List <SyncedData> list = SyncedData.Decode(data); // 解码 bool flag2 = list.Count > 0; if (flag2) // 有数据 { TSPlayer tSPlayer = this.players[list[0].inputData.ownerID]; // 找到对应的玩家 bool flag3 = !tSPlayer.dropped; if (flag3) // 该玩家没有掉线 { this.OnSyncedDataReceived(tSPlayer, list); // 处理同步数据 bool flag4 = list[0].dropPlayer && tSPlayer.ID != this.localPlayer.ID && !this.players[list[0].dropFromPlayerId].dropped; if (flag4) { tSPlayer.dropCount++; // 统计掉线玩家数量 } } else // 该玩家掉线了,回收数据对象 { int i = 0; int count = list.Count; while (i < count) { SyncedData.pool.GiveBack(list[i]); i++; } } SyncedData.poolList.GiveBack(list); } } else { bool flag5 = eventCode == 198; if (flag5) { byte[] infoBytes = content as byte[]; this.OnChecksumReceived(SyncedInfo.Decode(infoBytes)); } else { bool flag6 = eventCode == 197; if (flag6) { byte[] array = content as byte[]; bool flag7 = array.Length != 0; if (flag7) { bool flag8 = array[0] == 0; if (flag8) { this.Pause(); } else { bool flag9 = array[0] == 1; if (flag9) { this.Run(); } else { bool flag10 = array[0] == 3; if (flag10) { this.End(); } } } } } else { bool flag11 = eventCode == 196; if (flag11) { byte[] infoBytes2 = content as byte[]; SyncedInfo syncedInfo = SyncedInfo.Decode(infoBytes2); this.players[syncedInfo.playerId].sentSyncedStart = true; } } } } }