Retrieve possessable avatars

This commit is contained in:
Pascal Serrarens 2022-02-18 16:46:15 +01:00
parent 7262f48fb2
commit d5de37a168
12 changed files with 954 additions and 357 deletions

View File

@ -12,7 +12,7 @@ using UnityEditor;
namespace Passer { namespace Passer {
[CustomEditor(typeof(Possessable))] [CustomEditor(typeof(Possessable))]
public class Possession_Editor : Editor { public class Possessable_Editor : Editor {
protected Possessable possession; protected Possessable possession;

View File

@ -375,13 +375,20 @@ namespace Passer.Humanoid {
public void ChangeAvatar(GameObject fpAvatarPrefab, GameObject tpAvatarPrefab) { public void ChangeAvatar(GameObject fpAvatarPrefab, GameObject tpAvatarPrefab) {
remoteAvatar = tpAvatarPrefab; remoteAvatar = tpAvatarPrefab;
if (remoteAvatar == null)
remoteAvatar = fpAvatarPrefab;
LocalChangeAvatar(fpAvatarPrefab); LocalChangeAvatar(fpAvatarPrefab);
if (humanoidNetworking != null) { if (humanoidNetworking != null) {
if (remoteAvatar != null)
humanoidNetworking.ChangeAvatar(this, remoteAvatar.name); if (remoteAvatar != null) {
else Possessable avatarPossessable = remoteAvatar.GetComponent<Possessable>();
humanoidNetworking.ChangeAvatar(this, fpAvatarPrefab.name); if (avatarPossessable == null)
humanoidNetworking.ChangeAvatar(this, fpAvatarPrefab.name);
else
humanoidNetworking.ChangeAvatar(this, avatarPossessable.assetPath, avatarPossessable.siteLocation);
}
} }
} }
@ -2503,7 +2510,8 @@ namespace Passer.Humanoid {
ExtendHumanoids(this); ExtendHumanoids(this);
humanoidNetworking = HumanoidNetworking.GetLocalHumanoidNetworking(); if (humanoidNetworking == null)
humanoidNetworking = HumanoidNetworking.GetLocalHumanoidNetworking();
if (!isRemote && humanoidNetworking != null) if (!isRemote && humanoidNetworking != null)
humanoidNetworking.InstantiateHumanoid(this); humanoidNetworking.InstantiateHumanoid(this);

View File

@ -56,7 +56,7 @@ namespace Passer.Humanoid {
void Grab(HandTarget handTarget, GameObject obj, bool rangeCheck, HandTarget.GrabType grabType = HandTarget.GrabType.HandGrab); void Grab(HandTarget handTarget, GameObject obj, bool rangeCheck, HandTarget.GrabType grabType = HandTarget.GrabType.HandGrab);
void LetGo(HandTarget handTarget); void LetGo(HandTarget handTarget);
void ChangeAvatar(HumanoidControl humanoid, string remoteAvatarName); void ChangeAvatar(HumanoidControl humanoid, string remoteAvatarName, string possessionLocation = null);
void SyncTrackingSpace(HumanoidControl humanoid); void SyncTrackingSpace(HumanoidControl humanoid);
@ -1420,12 +1420,14 @@ namespace Passer.Humanoid {
public ulong nwId; public ulong nwId;
public byte humanoidId; public byte humanoidId;
public string avatarPrefabName; public string avatarPrefabName;
public string possessionLocation;
public ChangeAvatar() { } public ChangeAvatar() { }
public ChangeAvatar(HumanoidControl humanoid, string avatarPrefabName) { public ChangeAvatar(HumanoidControl humanoid, string avatarPrefabName, string possessionLocation) {
nwId = humanoid.nwId; nwId = humanoid.nwId;
humanoidId = (byte)humanoid.humanoidId; humanoidId = (byte)humanoid.humanoidId;
this.avatarPrefabName = avatarPrefabName; this.avatarPrefabName = avatarPrefabName;
this.possessionLocation = possessionLocation;
} }
public ChangeAvatar(byte[] data) : base(data) { } public ChangeAvatar(byte[] data) : base(data) { }
@ -1436,6 +1438,7 @@ namespace Passer.Humanoid {
bw.Write(nwId); bw.Write(nwId);
bw.Write(humanoidId); bw.Write(humanoidId);
bw.Write(avatarPrefabName); bw.Write(avatarPrefabName);
bw.Write(possessionLocation);
byte[] data = ms.ToArray(); byte[] data = ms.ToArray();
return data; return data;
@ -1448,6 +1451,7 @@ namespace Passer.Humanoid {
nwId = br.ReadUInt64(); nwId = br.ReadUInt64();
humanoidId = br.ReadByte(); humanoidId = br.ReadByte();
avatarPrefabName = br.ReadString(); avatarPrefabName = br.ReadString();
possessionLocation = br.ReadString();
} }
// This is the same code as in the base class IMessage // This is the same code as in the base class IMessage
@ -1473,7 +1477,6 @@ namespace Passer.Humanoid {
} }
public static void Receive(this IHumanoidNetworking receivingNetworking, ChangeAvatar msg) { public static void Receive(this IHumanoidNetworking receivingNetworking, ChangeAvatar msg) {
receivingNetworking.DebugLog("Receive Change Avatar");
IHumanoidNetworking networking = GetHumanoidNetworking(receivingNetworking, msg.nwId); IHumanoidNetworking networking = GetHumanoidNetworking(receivingNetworking, msg.nwId);
if (networking == null) { if (networking == null) {
if (receivingNetworking.debug <= DebugLevel.Error) if (receivingNetworking.debug <= DebugLevel.Error)
@ -1493,6 +1496,12 @@ namespace Passer.Humanoid {
return; return;
} }
if (msg.possessionLocation != null) {
Debug.Log("Need to download avatar possession from " + msg.possessionLocation);
VisitorPossessions.RetrievePossessableAsync(msg.possessionLocation, msg.avatarPrefabName, retrievedAvatar => ProcessRetrievedAvatar(networking, remoteHumanoid, retrievedAvatar));
return;
}
GameObject remoteAvatar = (GameObject)Resources.Load(msg.avatarPrefabName); GameObject remoteAvatar = (GameObject)Resources.Load(msg.avatarPrefabName);
if (remoteAvatar == null) { if (remoteAvatar == null) {
if (networking.debug <= DebugLevel.Error) if (networking.debug <= DebugLevel.Error)
@ -1506,6 +1515,18 @@ namespace Passer.Humanoid {
remoteHumanoid.LocalChangeAvatar(remoteAvatar); remoteHumanoid.LocalChangeAvatar(remoteAvatar);
} }
private static void ProcessRetrievedAvatar(IHumanoidNetworking networking, HumanoidControl remoteHumanoid, GameObject retrievedAvatar) {
if (retrievedAvatar == null) {
if (networking.debug <= DebugLevel.Error)
Debug.LogError("Could not retrieve avatar.");
return;
}
if (networking.debug <= DebugLevel.Info)
networking.DebugLog("Receive Change Possessesable Avatar " + retrievedAvatar);
remoteHumanoid.LocalChangeAvatar(retrievedAvatar);
}
#endregion #endregion
@ -1640,31 +1661,12 @@ namespace Passer.Humanoid {
} }
public static IHumanoidNetworking GetLocalHumanoidNetworking() { public static IHumanoidNetworking GetLocalHumanoidNetworking() {
#if hNW_UNET || hNW_PHOTON || hNW_BOLT || hNW_MIRROR
HumanoidPlayer[] humanoidNetworkings = UnityEngine.Object.FindObjectsOfType<HumanoidPlayer>(); HumanoidPlayer[] humanoidNetworkings = UnityEngine.Object.FindObjectsOfType<HumanoidPlayer>();
foreach (IHumanoidNetworking humanoidNetworking in humanoidNetworkings) { foreach (IHumanoidNetworking humanoidNetworking in humanoidNetworkings) {
if (humanoidNetworking.isLocal) if (humanoidNetworking.isLocal)
return humanoidNetworking; return humanoidNetworking;
} }
//#elif hNW_PHOTON
// IHumanoidNetworking[] humanoidNetworkings = UnityEngine.Object.FindObjectsOfType<HumanoidPun>();
// foreach (IHumanoidNetworking humanoidNetworking in humanoidNetworkings) {
// if (humanoidNetworking.isLocal)
// return humanoidNetworking;
// }
//#elif hNW_BOLT
// IHumanoidNetworking[] humanoidNetworkings = UnityEngine.Object.FindObjectsOfType<HumanoidBolt>();
// foreach (IHumanoidNetworking humanoidNetworking in humanoidNetworkings) {
// if (humanoidNetworking.isLocal)
// return humanoidNetworking;
// }
//#elif hNW_MIRROR
// IHumanoidNetworking[] humanoidNetworkings = UnityEngine.Object.FindObjectsOfType<HumanoidMirror>();
// foreach (IHumanoidNetworking humanoidNetworking in humanoidNetworkings) {
// if (humanoidNetworking.isLocal)
// return humanoidNetworking;
// }
#endif
return null; return null;
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace Passer.Humanoid { namespace Passer.Humanoid {
@ -28,13 +29,124 @@ namespace Passer.Humanoid {
public bool fuseTracking { get; set; } public bool fuseTracking { get; set; }
#else #else
public class HumanoidPlayer : MonoBehaviour {
public class HumanoidPlayer : MonoBehaviour, IHumanoidNetworking {
#region Dummy interface
public void Send(bool b) { }
public void Send(byte b) { }
public void Send(int x) { }
public void Send(float f) { }
public void Send(Vector3 v) { }
public void Send(Quaternion q) { }
public bool ReceiveBool() {
return false;
}
public byte ReceiveByte() {
return 0;
}
public int ReceiveInt() {
return 0;
}
public float ReceiveFloat() {
return 0;
}
public Vector3 ReceiveVector3() {
return Vector3.zero;
}
public Quaternion ReceiveQuaternion() {
return Quaternion.identity;
}
public ulong GetObjectIdentity(GameObject obj) {
return 0;
}
public GameObject GetGameObject(ulong objIdentity) {
return this.gameObject;
}
public void InstantiateHumanoid(HumanoidControl humanoid) {
if (debug <= HumanoidNetworking.DebugLevel.Info)
DebugLog("Send Instantiate Humanoid " + humanoid.humanoidId);
HumanoidNetworking.InstantiateHumanoid instantiateHumanoid = new HumanoidNetworking.InstantiateHumanoid(humanoid);
if (createLocalRemotes)
this.Receive(instantiateHumanoid);
}
public void DestroyHumanoid(HumanoidControl humanoid) { }
public void UpdateHumanoidPose(HumanoidControl humanoid) { }
public void Grab(HandTarget handTarget, GameObject obj, bool rangeCheck, HandTarget.GrabType grabType = HandTarget.GrabType.HandGrab) { }
public void LetGo(HandTarget handTarget) { }
public void ChangeAvatar(HumanoidControl humanoid, string avatarPrefabName, string possessionLocation = null) {
if (debug <= HumanoidNetworking.DebugLevel.Info)
Debug.Log(humanoid.nwId + ": Change Avatar: " + avatarPrefabName);
HumanoidNetworking.ChangeAvatar changeAvatar = new HumanoidNetworking.ChangeAvatar(humanoid, avatarPrefabName, possessionLocation);
if (createLocalRemotes)
this.Receive(changeAvatar);
}
public void SyncTrackingSpace(HumanoidControl humanoid) { }
public void ReenableNetworkSync(GameObject obj) { }
public void DisableNetworkSync(GameObject obj) { }
public void DebugLog(string s) {
Debug.Log(s);
}
public void DebugWarning(string s) {
Debug.LogWarning(s);
}
public void DebugError(string s) {
Debug.LogError(s);
}
public float sendRate => 0;
public HumanoidNetworking.Smoothing smoothing => HumanoidNetworking.Smoothing.None;
public bool createLocalRemotes { get => true; set { return; } }
private bool _isLocal;
public bool isLocal => _isLocal; // may need to implement this
public ulong nwId => 0;
public bool syncFingerSwing => false;
public bool syncTracking { get => false; set { return; } }
public bool fuseTracking => false;
public HumanoidNetworking.HumanoidPose lastHumanoidPose { get => null; set { return; } }
#endregion Dummy Interface
public List<HumanoidControl> humanoids { get; set; }
protected virtual void Awake() {
GameObject.DontDestroyOnLoad(this.gameObject);
humanoids = HumanoidNetworking.FindLocalHumanoids();
for (int i = 0; i < humanoids.Count; i++) {
HumanoidControl humanoid = humanoids[i];
if (humanoid.isRemote)
continue;
humanoid.humanoidNetworking = this;
((IHumanoidNetworking)this).InstantiateHumanoid(humanoid);
}
}
#endif #endif
[SerializeField] [SerializeField]
protected HumanoidNetworking.DebugLevel _debug = HumanoidNetworking.DebugLevel.Error; protected HumanoidNetworking.DebugLevel _debug = HumanoidNetworking.DebugLevel.Error;
public HumanoidNetworking.DebugLevel debug { public HumanoidNetworking.DebugLevel debug {
get { return _debug; } get { return _debug; }
} }
} }
#pragma warning restore 0618 #pragma warning restore 0618

View File

@ -30,8 +30,11 @@ namespace Passer.Humanoid {
HumanoidControl humanoidInScene = GetComponentInParent<HumanoidControl>(); HumanoidControl humanoidInScene = GetComponentInParent<HumanoidControl>();
if (humanoidInScene == null) { if (humanoidInScene == null) {
HumanoidControl[] humanoids = FindObjectsOfType<HumanoidControl>(); HumanoidControl[] humanoids = FindObjectsOfType<HumanoidControl>();
if (humanoids.Length == 1) { foreach (HumanoidControl humanoid in humanoids) {
this.humanoid = humanoids[0]; if (humanoid.isRemote == false)
this.humanoid = humanoid;
}
if (this.humanoid != null) {
AttachToHumanoid(this.humanoid); AttachToHumanoid(this.humanoid);
} }
} }

View File

@ -48,8 +48,8 @@ namespace Passer {
return ""; return "";
string siteLocation = SiteNavigator.currentSite.siteLocation; string siteLocation = SiteNavigator.currentSite.siteLocation;
siteLocation = siteLocation.Substring(0, siteLocation.LastIndexOf("/")); //siteLocation = siteLocation.Substring(0, siteLocation.LastIndexOf("/"));
return (siteLocation + "/possessions"); return (siteLocation + "_possessions");
} }
} }

View File

@ -275,6 +275,53 @@ namespace Passer {
} }
} }
public static IEnumerator RetrievePossessableAsync(string possessableLocation, string possessablePath, System.Action<GameObject> callback) {
GameObject prefab;
if (possessableLocation == "") {
yield return null;
}
else {
Debug.Log("Cache size: " + cache.Count);
CachedPossession foundPossession = cache.Find(entry => entry.siteLocation == possessableLocation);
if (foundPossession == null) {
string url = "https://" + possessableLocation + ".windows" + ".site";
Debug.Log("Loading possession: " + url);
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
yield return request.SendWebRequest();
AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(request);
if (assetBundle == null) {
Debug.LogError("Could not load " + url);
yield break;
}
Debug.Log("Load: " + possessablePath);
prefab = assetBundle.LoadAsset<GameObject>(possessablePath);
if (prefab == null) {
Debug.LogError("Could not load " + possessablePath);
yield break;
}
CachedPossession cachedPossession = new CachedPossession() {
siteLocation = possessableLocation,
assetBundle = assetBundle,
};
cache.Add(cachedPossession);
}
else {
Debug.Log("Load from cache: " + possessablePath);
prefab = foundPossession.assetBundle.LoadAsset<GameObject>(possessablePath);
if (prefab == null) {
Debug.LogError("Could not load " + possessablePath);
yield break;
}
}
callback(prefab);
}
}
public static void UnloadPossession() { public static void UnloadPossession() {
lastAssetBundle.Unload(true); lastAssetBundle.Unload(true);
} }

View File

@ -3,5 +3,5 @@ guid: 6d76b63d1c2ee4f4995b2109dc16402f
PrefabImporter: PrefabImporter:
externalObjects: {} externalObjects: {}
userData: userData:
assetBundleName: assetBundleName: avatarshop_possessions
assetBundleVariant: assetBundleVariant:

View File

@ -3,5 +3,5 @@ guid: 4a108532c0ba23c4796003a7390c1ae9
PrefabImporter: PrefabImporter:
externalObjects: {} externalObjects: {}
userData: userData:
assetBundleName: assetBundleName: avatarshop_possessions
assetBundleVariant: assetBundleVariant:

View File

@ -3,5 +3,5 @@ guid: f558cc7556c4ffc4594edb212b1cd0dc
PrefabImporter: PrefabImporter:
externalObjects: {} externalObjects: {}
userData: userData:
assetBundleName: assetBundleName: avatarshop_possessions
assetBundleVariant: assetBundleVariant: