Skip to content

Commit

Permalink
初步整合了 TrueSync,未完成
Browse files Browse the repository at this point in the history
  • Loading branch information
Caizc committed Sep 18, 2017
1 parent c11e189 commit 64937f3
Show file tree
Hide file tree
Showing 26 changed files with 713 additions and 97 deletions.
9 changes: 9 additions & 0 deletions Assets/RealSync.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions Assets/RealSync/RealSyncCommunicator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// <summary>
/// 负责与网络通信底层 API 交互的接口,代替 TrueSync 依赖于 PUN 的原生实现
/// </summary>
public class RealSyncCommunicator : ICommunicator
{
/// <summary>
/// 返回本地与服务端之间的通信延迟
/// </summary>
/// <returns>往返时间</returns>
public int RoundTripTime()
{
return 0;
}

/// <summary>
/// 向服务端提交一个广播给指定玩家的事件
/// </summary>
/// <param name="eventCode">事件编码</param>
/// <param name="message">事件中的消息</param>
/// <param name="reliable">是否保证消息传递的可靠性</param>
/// <param name="toPlayers">接收该事件的玩家</param>
public void OpRaiseEvent(byte eventCode, object message, bool reliable, int[] toPlayers)
{
// 组装协议
ProtocolBytes proto = new ProtocolBytes();

// 使用 "TrueSyncData" 来标识通过 TrueSync 处理的消息
proto.AddString("TrueSyncData");

// TODO: 这里直接将 object 强转为 byte[],可能导致该接口不通用,只能提交已经序列化为字节数组的消息
proto.AddBytes((byte[]) message);

// 向 Server 发送消息
NetMgr.srvConn.Send(proto);
}

/// <summary>
/// 添加一个处理接收到的消息的监听器
/// </summary>
/// <param name="onEventReceived">OnEventReceived 委托的实现</param>
public void AddEventListener(OnEventReceived onEventReceived)
{
SpaceBattle.OnEventCall += delegate(byte eventCode, object content, int senderId)
{
onEventReceived(eventCode, content);
};
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Assets/RealSync/RealSyncManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RealSyncManager : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
}
12 changes: 12 additions & 0 deletions Assets/RealSync/RealSyncManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Assets/RealSync/ReplayPicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using UnityEngine;
using TrueSync;

// attached to each room on join list to do a join if the players hit the button
// 重播选择器
public class ReplayPicker : MonoBehaviour {

public static ReplayRecordInfo replayToLoad;

public ReplayRecordInfo replayRecord;

public void Pick () {
replayToLoad = replayRecord;
ReplayRecord.replayMode = ReplayMode.LOAD_REPLAY;

// Menu.instance.ReplayPanel_LoadLevel();
}

}
12 changes: 12 additions & 0 deletions Assets/RealSync/ReplayPicker.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

164 changes: 164 additions & 0 deletions Assets/RealSync/ReplayUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// attached to each room on join list to do a join if the players hit the button
using System;
using System.Collections.Generic;
using System.IO;
using TrueSync;
using UnityEngine;

public class ReplayUtils {

/**
* @brief Represents a specific context for save and load players.
*
* It can be used to separate different levels or lobbies of the game.
**/
public static string replayContext;

/**
* @brief Folder to save/load replays.
**/
private const string REPLAY_FOLDER = "replays";

public static void Init() {
ReplayRecord.ReplayRecordSave += SaveRecord;
}

/**
* @brief Saves the provided replay.
**/
private static void SaveRecord(byte[] replayRecord, int numberOfPlayers) {
string folderPath = CheckReplayFolder().FullName;

#if !UNITY_WEBPLAYER
try {
File.WriteAllBytes(string.Format("{0}/replay_{1}_{2}.tsr", folderPath, DateTime.Now.Ticks, numberOfPlayers), replayRecord);
} catch (Exception) {
}
#endif
}

/**
* @brief Returns a sorted by date list of {@link ReplayRecordInfo} of the saved replay found.
**/
public static List<ReplayRecordInfo> GetContextRecords() {
List<ReplayRecordInfo> result = new List<ReplayRecordInfo>();

#if !UNITY_WEBPLAYER
foreach (FileInfo fileInfo in CheckReplayFolder().GetFiles("*.tsr")) {
ReplayRecordInfo replay = new ReplayRecordInfo(fileInfo);
if (replay != null) {
result.Add(replay);
}
}

result.Sort();
#endif

return result;
}

/**
* @brief Returns a instance of {@from ReplayRecord} saved in the provided file location.
**/
public static ReplayRecord GetReplayRecord(string fileFullName) {
ReplayRecord replay = null;

#if !UNITY_WEBPLAYER
try {
byte[] replayContent = File.ReadAllBytes(fileFullName);
replay = ReplayRecord.GetReplayRecord(replayContent);
} catch (Exception) {
}
#endif

return replay;
}

/**
* @brief Clear all replay records saved.
**/
public static void ClearAllReplayRecords() {
#if !UNITY_WEBPLAYER
foreach (FileInfo fileInfo in CheckReplayFolder().GetFiles("*.tsr")) {
fileInfo.Delete();
}
#endif
}

/**
* @brief Checks whether the replay's folder is created, it creates it otherwise.
**/
private static DirectoryInfo CheckReplayFolder() {
string path = null;
if (replayContext != null && replayContext.Trim().Length > 0) {
path = string.Format("{0}/{1}/{2}", Application.persistentDataPath, REPLAY_FOLDER, replayContext);
} else {
path = string.Format("{0}/{1}", Application.persistentDataPath, REPLAY_FOLDER);
}

DirectoryInfo folderInfo = new DirectoryInfo(path);

if (!folderInfo.Exists) {
folderInfo.Create();
}

return folderInfo;
}

}

/**
* @brief Provides basic information about a {@link ReplayRecord}.
* 重播记录信息
**/
public class ReplayRecordInfo : IComparable<ReplayRecordInfo> {

/**
* @brief Creation time of the replay.
* 创建时间
**/
public DateTime creationDate;

/**
* @brief FullName of replay's file.
* 文件全名
**/
public string fileFullName;

/**
* @brief Number of players.
* 玩家数量
**/
public int numberOfPlayers = 1;

public ReplayRecordInfo(FileInfo fileInfo) {
#if !UNITY_WEBPLAYER
this.creationDate = fileInfo.CreationTime;
#endif
this.fileFullName = fileInfo.FullName;

string[] nameSplited = fileInfo.Name.Split('_');
if (nameSplited.Length >= 3) {
numberOfPlayers = int.Parse(nameSplited[2].Split('.')[0]);
}
}

#region 公共方法
/**
* @brief Returns the related {@link ReplayRecord}.
* 加载
**/
public void Load()
{
ReplayRecord.replayToLoad = ReplayUtils.GetReplayRecord(fileFullName);
}

/**
* @brief Comparison based on creation date (desc order).
**/
public int CompareTo(ReplayRecordInfo other)
{
return other.creationDate.CompareTo(this.creationDate);
}
#endregion 公共方法
}
12 changes: 12 additions & 0 deletions Assets/RealSync/ReplayUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 64937f3

Please sign in to comment.