From 885b27bfe182a0b1e48720eab3d70eabc2f7749f Mon Sep 17 00:00:00 2001 From: Anton Franzluebbers Date: Fri, 2 Feb 2024 14:43:41 -0500 Subject: [PATCH] persistence for any velnet component --- .github/workflows/deploy_oracle.yml | 2 +- unity_package/Runtime/VEL-Connect.asmdef | 2 +- unity_package/Runtime/VELConnectManager.cs | 19 ++++- unity_package/Runtime/VelNetPersist.cs | 85 +++++++++++++++++++++ unity_package/Runtime/VelNetPersist.cs.meta | 11 +++ unity_package/package.json | 2 +- 6 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 unity_package/Runtime/VelNetPersist.cs create mode 100644 unity_package/Runtime/VelNetPersist.cs.meta diff --git a/.github/workflows/deploy_oracle.yml b/.github/workflows/deploy_oracle.yml index cc2bdca..2cc752d 100644 --- a/.github/workflows/deploy_oracle.yml +++ b/.github/workflows/deploy_oracle.yml @@ -6,7 +6,7 @@ on: paths: ["velconnect/**"] jobs: run_pull: - name: Pull new version + name: Pull new version on Oracle runs-on: ubuntu-latest steps: - name: install ssh keys diff --git a/unity_package/Runtime/VEL-Connect.asmdef b/unity_package/Runtime/VEL-Connect.asmdef index 827a4a0..9d4dc02 100644 --- a/unity_package/Runtime/VEL-Connect.asmdef +++ b/unity_package/Runtime/VEL-Connect.asmdef @@ -1,6 +1,6 @@ { "name": "VEL-Connect", - "rootNamespace": "VEL-Connect", + "rootNamespace": "VELConnect", "references": [ "GUID:1e55e2c4387020247a1ae212bbcbd381", "GUID:343deaaf83e0cee4ca978e7df0b80d21", diff --git a/unity_package/Runtime/VELConnectManager.cs b/unity_package/Runtime/VELConnectManager.cs index 1a4f9e2..1feae4d 100644 --- a/unity_package/Runtime/VELConnectManager.cs +++ b/unity_package/Runtime/VELConnectManager.cs @@ -917,7 +917,7 @@ namespace VELConnect case UnityWebRequest.Result.ConnectionError: case UnityWebRequest.Result.DataProcessingError: case UnityWebRequest.Result.ProtocolError: - Debug.LogError(url + ": Error: " + webRequest.error + "\n" + Environment.StackTrace); + Debug.LogError(url + ": Error: " + webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + Environment.StackTrace); failureCallback?.Invoke(webRequest.error); break; case UnityWebRequest.Result.Success: @@ -929,6 +929,23 @@ namespace VELConnect webRequest.Dispose(); } + public static void SetDataBlock(string blockId, State.DataBlock dataBlock) + { + PostRequestCallback(instance.velConnectUrl + "/data_block/" + blockId, JsonConvert.SerializeObject(dataBlock, Formatting.None, new JsonSerializerSettings + { + NullValueHandling = NullValueHandling.Ignore + })); + } + + public static void GetDataBlock(string blockId, Action successCallback = null, Action failureCallback = null) + { + GetRequestCallback(instance.velConnectUrl + "/data_block/" + blockId, data => + { + State.DataBlock dict = JsonConvert.DeserializeObject(data); + successCallback?.Invoke(dict); + }, failureCallback); + } + private void OnApplicationFocus(bool focus) { UpdateUserCount(!focus); diff --git a/unity_package/Runtime/VelNetPersist.cs b/unity_package/Runtime/VelNetPersist.cs new file mode 100644 index 0000000..e8df8ef --- /dev/null +++ b/unity_package/Runtime/VelNetPersist.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using VelNet; + +namespace VELConnect +{ + public class VelNetPersist : MonoBehaviour + { + public SyncState syncState; + private string Id => $"{Application.productName}_{VelNetManager.Room}_{syncState.networkObject.sceneNetworkId}_{syncState.networkObject.syncedComponents.IndexOf(syncState)}"; + private const float interval = 5f; + private double nextUpdate; + private bool loading; + private const bool debugLogs = false; + + private void Update() + { + if (Time.timeAsDouble > nextUpdate && VelNetManager.InRoom && !loading) + { + nextUpdate = Time.timeAsDouble + interval + UnityEngine.Random.Range(0, interval); + if (syncState.networkObject.IsMine) + { + Save(); + } + } + } + + private void OnEnable() + { + VelNetManager.OnJoinedRoom += OnJoinedRoom; + } + + private void OnDisable() + { + VelNetManager.OnJoinedRoom -= OnJoinedRoom; + } + + private void OnJoinedRoom(string roomName) + { + Load(); + } + + private void Load() + { + loading = true; + if (debugLogs) Debug.Log($"[VelNetPersist] Loading {Id}"); + VELConnectManager.GetDataBlock(Id, data => + { + if (!data.data.TryGetValue("state", out string d)) + { + Debug.LogError($"[VelNetPersist] Failed to parse {Id}"); + return; + } + + if (syncState == null) + { + Debug.LogError("[VelNetPersist] Object doesn't exist anymore"); + } + + syncState.UnpackState(Convert.FromBase64String(d)); + if (debugLogs) Debug.Log($"[VelNetPersist] Loaded {Id}"); + loading = false; + }, s => + { + Debug.LogError(s); + loading = false; + }); + } + + private void Save() + { + if (debugLogs) Debug.Log($"[VelNetPersist] Saving {Id}"); + VELConnectManager.SetDataBlock(Id, new VELConnectManager.State.DataBlock() + { + category = "object_persist", + data = new Dictionary + { + { "name", syncState.networkObject.name }, + { "state", Convert.ToBase64String(syncState.PackState()) } + } + }); + } + } +} \ No newline at end of file diff --git a/unity_package/Runtime/VelNetPersist.cs.meta b/unity_package/Runtime/VelNetPersist.cs.meta new file mode 100644 index 0000000..4d0f949 --- /dev/null +++ b/unity_package/Runtime/VelNetPersist.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cd4cd3f927d9998449837165f749c9c2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity_package/package.json b/unity_package/package.json index 4cd2c74..2bdd01b 100644 --- a/unity_package/package.json +++ b/unity_package/package.json @@ -1,7 +1,7 @@ { "name": "edu.uga.engr.vel.vel-connect", "displayName": "VEL-Connect", - "version": "4.0.0", + "version": "4.0.1", "unity": "2019.1", "description": "Web-based configuration for VR applications", "keywords": [],