persistence for any velnet component
parent
73c2ccea0a
commit
885b27bfe1
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "VEL-Connect",
|
||||
"rootNamespace": "VEL-Connect",
|
||||
"rootNamespace": "VELConnect",
|
||||
"references": [
|
||||
"GUID:1e55e2c4387020247a1ae212bbcbd381",
|
||||
"GUID:343deaaf83e0cee4ca978e7df0b80d21",
|
||||
|
|
|
|||
|
|
@ -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<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)
|
||||
{
|
||||
UpdateUserCount(!focus);
|
||||
|
|
|
|||
|
|
@ -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()) }
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: cd4cd3f927d9998449837165f749c9c2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -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": [],
|
||||
|
|
|
|||
Loading…
Reference in New Issue