// Converts the current recording to a json file suitable for serialization. string SerializedRecording() { ReplayData replayData = new ReplayData(mapId, CommonData.gameWorld.mapHash, positionData.ToArray(), inputData.ToArray()); return(JsonUtility.ToJson(replayData, true)); }
// Static utility function to create ReplayData from stream such as // FileStream or MemoryStream public static ReplayData CreateFromStream(Stream stream) { ReplayData replay = new ReplayData(); replay.Deserialize(stream); return(replay); }
// Uploads the time data to the database, and returns the current top time list. public static Task <UserScore> UploadReplay( long time, LevelMap map, ReplayData replay) { // Get a client-generated unique id based on timestamp and random number. string key = FirebaseDatabase.DefaultInstance.RootReference.Push().Key; Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; string name = (auth.CurrentUser != null && !string.IsNullOrEmpty(auth.CurrentUser.DisplayName)) ? auth.CurrentUser.DisplayName : StringConstants.UploadScoreDefaultName; string userId = (auth.CurrentUser != null && !string.IsNullOrEmpty(auth.CurrentUser.UserId)) ? auth.CurrentUser.UserId : StringConstants.UploadScoreDefaultName; string replayPath = replay != null ? Storage_Replay_Root_Folder + GetPath(map) + key : null; var userScore = new Firebase.Leaderboard.UserScore( userId, name, time, DateTime.Now.Ticks / TimeSpan.TicksPerSecond, new Dictionary <string, object> { { Database_Property_ReplayPath, replayPath } }); UploadConfig config = new UploadConfig() { key = key, storagePath = replayPath, dbRankPath = GetDBRankPath(map) + key, dbSharedReplayPath = GetDBSharedReplayPath(map) + key, shareReplay = replay != null //TODO(chkuang): && GameOption.shareReplay }; if (replay == null) { // Nothing to upload, return user score to upload to leaderboard. return(Task.FromResult(userScore)); } else { return(UploadReplayData(userScore, replay, config) .ContinueWith(task => { if (config.shareReplay) { var dbRef = FirebaseDatabase.DefaultInstance.RootReference; return dbRef.Child(config.dbSharedReplayPath) .SetValueAsync(userScore.ToDictionary()); } else { return null; }; }).ContinueWith(task => userScore)); } }
// Deserialize the binary raw data to ReplayData public void Deserialize(Stream stream) { BinaryFormatter formatter = new BinaryFormatter(); ReplayData deserializedData = formatter.Deserialize(stream) as ReplayData; mapId = deserializedData.mapId; mapHash = deserializedData.mapHash; positionData = deserializedData.positionData; inputData = deserializedData.inputData; }
// Set the replay data to animate public void SetReplayData(ReplayData data) { replayData = data; timestamp = 0; positionDataIndex = 0; // Move the gameobject to the first recorded position to prevent animation pop if (replayData != null && replayData.positionData.Length > 0) { transform.position = replayData.positionData[0].position; transform.rotation = replayData.positionData[0].rotation; } }
// Reset the Map back to its original state. This includes the game time since it started, // and the MapObjects public void ResetMap() { GameStartTime = Time.time; PreviousReplayData = null; foreach (GameObject gameObject in sceneObjects.Values) { MapObjects.MapObject mapObject = gameObject.GetComponentInChildren <MapObjects.MapObject>(); if (mapObject != null) { mapObject.Reset(); } } foreach (Utilities.DestroyOnReset toDestroy in destroyOnReset) { toDestroy.RegisteredInWorld = false; Destroy(toDestroy.gameObject); } destroyOnReset.Clear(); }
// Utility function to download replay record from a given path and deserialize into // ReplayData struct public static Task <ReplayData> DownloadReplayRecordAsync(string replayPath) { TaskCompletionSource <ReplayData> tComplete = new TaskCompletionSource <ReplayData>(); Firebase.Storage.StorageReference storageRef = Firebase.Storage.FirebaseStorage.DefaultInstance.GetReference(replayPath); storageRef.GetStreamAsync(stream => { tComplete.SetResult(ReplayData.CreateFromStream(stream)); }).ContinueWith(task => { if (task.IsFaulted) { tComplete.SetException(task.Exception); } if (task.IsCanceled) { tComplete.SetCanceled(); } }); return(tComplete.Task); }
// Returns the top times, given the level's database path and map id. // Upload the replay data to Firebase Storage private static Task <StorageMetadata> UploadReplayData( UserScore userScore, ReplayData replay, UploadConfig config) { StorageReference storageRef = FirebaseStorage.DefaultInstance.GetReference(config.storagePath); // Serializing replay data to byte array System.IO.MemoryStream stream = new System.IO.MemoryStream(); replay.Serialize(stream); stream.Position = 0; byte[] serializedData = stream.ToArray(); // Add database path and time to metadata for future usage MetadataChange newMetadata = new MetadataChange { CustomMetadata = new Dictionary <string, string> { { "DatabaseReplayPath", config.dbSharedReplayPath }, { "DatabaseRankPath", config.dbRankPath }, { "Time", userScore.Score.ToString() }, { "Shared", config.shareReplay.ToString() }, } }; return(storageRef.PutBytesAsync(serializedData, newMetadata)); }
// Serializes and outputs the current recording to a file. public void OutputToFile(string fileName) { ReplayData replayData = CreateReplayData(); replayData.WriteToFile(fileName); }
private void Start() { GameStartTime = Time.time; PreviousReplayData = null; HasPendingEdits = false; }