persistence for any velnet component

dev
Anton Franzluebbers 2024-02-02 14:43:41 -05:00
parent 73c2ccea0a
commit 885b27bfe1
6 changed files with 117 additions and 4 deletions

View File

@ -6,7 +6,7 @@ on:
paths: ["velconnect/**"] paths: ["velconnect/**"]
jobs: jobs:
run_pull: run_pull:
name: Pull new version name: Pull new version on Oracle
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: install ssh keys - name: install ssh keys

View File

@ -1,6 +1,6 @@
{ {
"name": "VEL-Connect", "name": "VEL-Connect",
"rootNamespace": "VEL-Connect", "rootNamespace": "VELConnect",
"references": [ "references": [
"GUID:1e55e2c4387020247a1ae212bbcbd381", "GUID:1e55e2c4387020247a1ae212bbcbd381",
"GUID:343deaaf83e0cee4ca978e7df0b80d21", "GUID:343deaaf83e0cee4ca978e7df0b80d21",

View File

@ -917,7 +917,7 @@ namespace VELConnect
case UnityWebRequest.Result.ConnectionError: case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError: case UnityWebRequest.Result.DataProcessingError:
case UnityWebRequest.Result.ProtocolError: 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); failureCallback?.Invoke(webRequest.error);
break; break;
case UnityWebRequest.Result.Success: case UnityWebRequest.Result.Success:
@ -929,6 +929,23 @@ namespace VELConnect
webRequest.Dispose(); 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<State.DataBlock> successCallback = null, Action<string> failureCallback = null)
{
GetRequestCallback(instance.velConnectUrl + "/data_block/" + blockId, data =>
{
State.DataBlock dict = JsonConvert.DeserializeObject<State.DataBlock>(data);
successCallback?.Invoke(dict);
}, failureCallback);
}
private void OnApplicationFocus(bool focus) private void OnApplicationFocus(bool focus)
{ {
UpdateUserCount(!focus); UpdateUserCount(!focus);

View File

@ -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<string, string>
{
{ "name", syncState.networkObject.name },
{ "state", Convert.ToBase64String(syncState.PackState()) }
}
});
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cd4cd3f927d9998449837165f749c9c2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +1,7 @@
{ {
"name": "edu.uga.engr.vel.vel-connect", "name": "edu.uga.engr.vel.vel-connect",
"displayName": "VEL-Connect", "displayName": "VEL-Connect",
"version": "4.0.0", "version": "4.0.1",
"unity": "2019.1", "unity": "2019.1",
"description": "Web-based configuration for VR applications", "description": "Web-based configuration for VR applications",
"keywords": [], "keywords": [],