Skip to content

Commit

Permalink
Working version (still with a lot of debug.logs, but should still wor…
Browse files Browse the repository at this point in the history
…k on quest crossplay)
  • Loading branch information
legoandmars committed Apr 21, 2021
1 parent 9acd63a commit f814340
Show file tree
Hide file tree
Showing 9 changed files with 577 additions and 64 deletions.
28 changes: 25 additions & 3 deletions GorillaCosmetics/AssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ public static GorillaHat SelectedHat()
return GorillaHatObjects[selectedHat];
}

public static void SelectMaterial(string name)
{
selectedMaterial = SelectedMaterialFromConfig(name);
}
public static void SelectHat(string name)
{
selectedHat = SelectedHatFromConfig(name);
}

public static GorillaMaterial GetMaterial(int index)
{
if (index > GorillaMaterialObjects.Count) return null;
return GorillaMaterialObjects[index];
}

public static GorillaHat GetHat(int index)
{
if (index > GorillaHatObjects.Count) return null;
return GorillaHatObjects[index];
}

public async static void Load()
{
if (Loaded) return;
Expand All @@ -63,7 +84,7 @@ public async static void Load()
// Parse Configs
selectedMaterial = SelectedMaterialFromConfig(GorillaCosmetics.selectedMaterial.Value);
selectedInfectedMaterial = SelectedMaterialFromConfig(GorillaCosmetics.selectedInfectedMaterial.Value);
selectedHat = SelectedHatFromConfig();
selectedHat = SelectedHatFromConfig(GorillaCosmetics.selectedHat.Value);

// Disable old mirror and use it as a base
GameObject gameMirror = null;
Expand Down Expand Up @@ -268,6 +289,7 @@ public async static void Load()
if (lavaMat != null) DefaultTagMaterial.Material = lavaMat;

Loaded = true;
CosmeticUtils.LocalLoadingCallback();
}

public static int SelectedMaterialFromConfig(string configString)
Expand All @@ -289,9 +311,9 @@ public static int SelectedMaterialFromConfig(string configString)
return 0;
}

public static int SelectedHatFromConfig()
public static int SelectedHatFromConfig(string configString)
{
string selectedHatString = GorillaCosmetics.selectedHat.Value.ToLower().Trim();
string selectedHatString = configString.ToLower().Trim();
for (int i = 1; i < GorillaHatObjects.Count; i++)
{
GorillaHat gorillaHatObject = GorillaHatObjects[i];
Expand Down
57 changes: 42 additions & 15 deletions GorillaCosmetics/Data/Behaviours/HatPreviewButton.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using GorillaCosmetics.Utils;
using UnityEngine;
using Newtonsoft.Json;
using Photon.Pun;
using System.Reflection;

namespace GorillaCosmetics.Data.Behaviours
{
Expand All @@ -14,27 +17,51 @@ private void OnTriggerEnter(Collider collider)
// do stuff
if (hat != null)
{
if (hat.Descriptor?.HatName != null)

{
Debug.Log("Swapping to: " + hat.Descriptor.HatName);
GorillaCosmetics.selectedHat.Value = hat.Descriptor.HatName;
AssetLoader.selectedHat = AssetLoader.SelectedHatFromConfig();
}
else
{
// default hat stuff
Debug.Log("Swapping to default hat");
GorillaCosmetics.selectedHat.Value = "Default";
AssetLoader.selectedHat = 0;
}
CosmeticUtils.RefreshAllPlayers();
string hatName = hat.Descriptor.HatName != null && hat.Descriptor.HatName != "None" ? hat.Descriptor.HatName : "None";
Debug.Log("Swapping to: " + hatName);
AssetLoader.SelectHat(hatName);
GorillaCosmetics.selectedHat.Value = hatName;

UpdateHatValue();
}
if (component != null)
{
GorillaTagger.Instance.StartVibration(component.isLeftHand, GorillaTagger.Instance.tapHapticStrength / 2f, GorillaTagger.Instance.tapHapticDuration);
}
}
}

void UpdateHatValue()
{
string name = hat.Descriptor.HatName;
string hatString = "custom:" + name;

GorillaTagger gorillaTagger = GorillaTagger.Instance;
VRRig offlineVRRig = gorillaTagger.offlineVRRig;
if (offlineVRRig == null) offlineVRRig = gorillaTagger.myVRRig; // this will probably break stuff. TOO BAD!

string hatCS = typeof(VRRig).GetField("hat", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(offlineVRRig) as string;
string face = typeof(VRRig).GetField("face", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(offlineVRRig) as string;
string badge = typeof(VRRig).GetField("badge", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(offlineVRRig) as string;

VRRigHatJSON hatJSON = new VRRigHatJSON();
hatJSON.hat = hatString;
hatJSON.material = AssetLoader.SelectedMaterial().Descriptor.MaterialName != null ? AssetLoader.SelectedMaterial().Descriptor.MaterialName : "Default";
string hatMessage = JsonConvert.SerializeObject(hatJSON);

if (offlineVRRig)
{
// locally update it
offlineVRRig.LocalUpdateCosmetics(badge, face, hatMessage);
}
VRRig myVRRig = gorillaTagger.myVRRig;
if (myVRRig)
{
PhotonView photonView = myVRRig.photonView;

photonView.RPC("UpdateCosmetics", RpcTarget.All, new object[] { badge, face, hatMessage });
PhotonNetwork.SendAllOutgoingCommands();
}
}
}
}
66 changes: 51 additions & 15 deletions GorillaCosmetics/Data/Behaviours/MaterialPreviewButton.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using GorillaCosmetics.Utils;
using GorillaCosmetics.Data;
using UnityEngine;

using System.Reflection;
using Newtonsoft.Json;
using Photon.Pun;
namespace GorillaCosmetics.Data.Behaviours
{
public class MaterialPreviewButton : GorillaTriggerBox
Expand All @@ -14,26 +17,59 @@ private void OnTriggerEnter(Collider collider)
// do stuff
if(material != null)
{
if(material.Descriptor.MaterialName != null)
{
Debug.Log("Swapping to: " + material.Descriptor.MaterialName);
GorillaCosmetics.selectedMaterial.Value = material.Descriptor.MaterialName;
AssetLoader.selectedMaterial = AssetLoader.SelectedMaterialFromConfig(material.Descriptor.MaterialName);
}
else
{
Debug.Log("Swapping to default material");
GorillaCosmetics.selectedMaterial.Value = "Default";
AssetLoader.selectedMaterial = 0;
}
CosmeticUtils.RefreshAllPlayers();
//GorillaTagger.Instance.offlineVRRig.ChangeMaterial(GorillaTagger.Instance.offlineVRRig.setMatIndex);
string matName = material.Descriptor.MaterialName != null ? material.Descriptor.MaterialName : "Default";
Debug.Log("Swapping to: " + matName);
AssetLoader.SelectMaterial(matName);
GorillaCosmetics.selectedMaterial.Value = matName;

UpdateMaterialValue();
}
if (component != null)
{
GorillaTagger.Instance.StartVibration(component.isLeftHand, GorillaTagger.Instance.tapHapticStrength / 2f, GorillaTagger.Instance.tapHapticDuration);
}
}
}

private void UpdateMaterialValue()
{
string material = this.material.Descriptor.MaterialName;

GorillaTagger gorillaTagger = GorillaTagger.Instance;
VRRig offlineVRRig = gorillaTagger.offlineVRRig;
if (offlineVRRig == null) offlineVRRig = gorillaTagger.myVRRig; // this will probably break stuff. TOO BAD!

string hatCS = typeof(VRRig).GetField("hat", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(offlineVRRig) as string;
string face = typeof(VRRig).GetField("face", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(offlineVRRig) as string;
string badge = typeof(VRRig).GetField("badge", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(offlineVRRig) as string;

VRRigHatJSON hatJSON = new VRRigHatJSON();
hatJSON.hat = hatCS;
// I don't know if this is right, but I'm not sure how red is doing it so i'm taking my best guess.
Debug.Log(hatCS);
if (hatCS.Contains("}") && hatCS.Contains("{"))
{
// it's probably json. I really should implement a better check for this.
var json = JsonConvert.DeserializeObject<VRRigHatJSON>(hatCS);
hatJSON.hat = json.hat;
}

hatJSON.material = material;
string hatMessage = JsonConvert.SerializeObject(hatJSON);

if (offlineVRRig)
{
// locally update it
offlineVRRig.LocalUpdateCosmetics(badge, face, hatMessage);
}
VRRig myVRRig = gorillaTagger.myVRRig;
if (myVRRig)
{
PhotonView photonView = myVRRig.photonView;

photonView.RPC("UpdateCosmetics", RpcTarget.All, new object[] { badge, face, hatMessage });
PhotonNetwork.SendAllOutgoingCommands();
}
}
}
}
13 changes: 13 additions & 0 deletions GorillaCosmetics/Data/VRRigHatJSON.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GorillaCosmetics.Data
{
[System.Serializable]
public class VRRigHatJSON
{
public string hat;
public string material;
}
}
4 changes: 2 additions & 2 deletions GorillaCosmetics/HarmonyPatches/Patches/HatPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace GorillaCosmetics.HarmonyPatches.Patches
{
[HarmonyPatch(typeof(VRRig))]
/*[HarmonyPatch(typeof(VRRig))]
[HarmonyPatch("Start", MethodType.Normal)]
internal class GorillaHatAwakePatch2
{
private static void Postfix(VRRig __instance)
{
CosmeticUtils.ChangeHat(__instance);
}
}
}*/
}
4 changes: 2 additions & 2 deletions GorillaCosmetics/HarmonyPatches/Patches/MaterialPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace GorillaCosmetics.HarmonyPatches.Patches
{
[HarmonyPatch(typeof(VRRig))]
/*[HarmonyPatch(typeof(VRRig))]
[HarmonyPatch("ChangeMaterial", MethodType.Normal)]
internal class VRRigChangeMaterialPatch
{
Expand All @@ -23,5 +23,5 @@ private static void Postfix(VRRig __instance)
{
CosmeticUtils.ChangeMaterial(__instance, __instance.setMatIndex);
}
}
}*/
}
Loading

0 comments on commit f814340

Please sign in to comment.