Skip to content

Commit

Permalink
Merge pull request #2 from legoandmars/NewModelFormat
Browse files Browse the repository at this point in the history
New model format & fixes
  • Loading branch information
legoandmars authored Feb 28, 2021
2 parents 52a4ef3 + e56bab2 commit fb829e2
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 14 deletions.
5 changes: 3 additions & 2 deletions GorillaCosmetics/AssetLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GorillaCosmetics.Data;
using GorillaCosmetics.Data.Previews;
using GorillaCosmetics.Utils;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -61,14 +62,14 @@ public static void Load()
selectedHat = SelectedHatFromConfig();

// Load Mirror
GameObject Mirror = UnityEngine.Object.Instantiate(AssetBundle.LoadFromFile($"{folder}\\Misc\\Mirror").LoadAsset<GameObject>("_Hat"));
GameObject Mirror = UnityEngine.Object.Instantiate(PackageUtils.AssetBundleFromPackage($"{folder}\\Misc\\Mirror").LoadAsset<GameObject>("_Hat"));
Mirror.transform.localScale = new Vector3(0.29f, 0.29f, 0.29f);
Mirror.transform.position = new Vector3(-68.5f, 11.96f, -81.595f);
Mirror.transform.rotation = Quaternion.Euler(0.21f, -153.2f, -4.6f);
UnityEngine.Object.DontDestroyOnLoad(Mirror);

// Load Hat Rack
GameObject HatRack = UnityEngine.Object.Instantiate(AssetBundle.LoadFromFile($"{folder}\\Misc\\HatRack").LoadAsset<GameObject>("_Hat"));
GameObject HatRack = UnityEngine.Object.Instantiate(PackageUtils.AssetBundleFromPackage($"{folder}\\Misc\\HatRack").LoadAsset<GameObject>("_Hat"));
HatRack.transform.localScale = new Vector3(3.696f, 3.696f, 0.677f);
HatRack.transform.position = new Vector3(-68.003f, 11.471f, -80.637f);
HatRack.transform.rotation = Quaternion.Euler(-90f, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion GorillaCosmetics/Data/Behaviours/HatPreviewButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private void OnTriggerEnter(Collider collider)
// do stuff
if (hat != null)
{
if (hat.Descriptor != null)
if (hat.Descriptor?.HatName != null)

{
Debug.Log("Swapping to: " + hat.Descriptor.HatName);
Expand Down
2 changes: 1 addition & 1 deletion GorillaCosmetics/Data/Behaviours/MaterialPreviewButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private void OnTriggerEnter(Collider collider)
// do stuff
if(material != null)
{
if(material.Descriptor != null)
if(material.Descriptor.MaterialName != null)
{
Debug.Log("Swapping to: " + material.Descriptor.MaterialName);
GorillaCosmetics.selectedMaterial.Value = material.Descriptor.MaterialName;
Expand Down
15 changes: 11 additions & 4 deletions GorillaCosmetics/Data/GorillaHat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GorillaCosmetics.Data.Descriptors;
using GorillaCosmetics.Utils;
using System;
using UnityEngine;

Expand All @@ -18,15 +19,21 @@ public GorillaHat(string path)
{
try
{
// load
FileName = path;
AssetBundle = AssetBundle.LoadFromFile(path);
var bundleAndJson = PackageUtils.AssetBundleAndJSONFromPackage(FileName);
AssetBundle = bundleAndJson.bundle;
PackageJSON json = bundleAndJson.json;

// get material object and stuff
Hat = AssetBundle.LoadAsset<GameObject>("_Hat");
foreach(Collider collider in Hat.GetComponentsInChildren<Collider>())
foreach (Collider collider in Hat.GetComponentsInChildren<Collider>())
{
collider.enabled = false; // Disable colliders. They can be left in accidentally and cause some really weird issues.
}
Descriptor = Hat.GetComponent<HatDescriptor>();

// Make Descriptor
Descriptor = PackageUtils.ConvertJsonToHat(json);
Debug.Log(Descriptor.AuthorName);
}
catch (Exception err)
{
Expand Down
17 changes: 14 additions & 3 deletions GorillaCosmetics/Data/GorillaMaterial.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using GorillaCosmetics.Data.Descriptors;
using System;
using UnityEngine;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;
using GorillaCosmetics.Utils;

namespace GorillaCosmetics.Data
{
Expand All @@ -20,10 +25,16 @@ public GorillaMaterial(string path)
{
// load
FileName = path;
AssetBundle = AssetBundle.LoadFromFile(path);
var bundleAndJson = PackageUtils.AssetBundleAndJSONFromPackage(FileName);
AssetBundle = bundleAndJson.bundle;
PackageJSON json = bundleAndJson.json;

// get material object and stuff
GameObject materialObject = AssetBundle.LoadAsset<GameObject>("_Material");
Material = materialObject.GetComponent<Renderer>().material;
Descriptor = materialObject.GetComponent<GorillaMaterialDescriptor>();

// Make Descriptor
Descriptor = PackageUtils.ConvertJsonToMaterial(json);
}
catch (Exception err)
{
Expand All @@ -37,7 +48,7 @@ public GorillaMaterial(string path)
Descriptor = new GorillaMaterialDescriptor();
Descriptor.MaterialName = "Default";
Descriptor.CustomColors = true;
Material = Resources.Load<Material>("objects/materials/lightfur");
Material = Resources.Load<Material>("objects/treeroom/materials/lightfur");
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions GorillaCosmetics/Data/PackageJSON.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[System.Serializable]
public class PackageJSON
{
public string androidFileName;
public string pcFileName;
public Descriptor descriptor;
public Config config;
}

[System.Serializable]
public class Descriptor
{
public string objectName;
public string author;
public string description;
}

[System.Serializable]
public class Config
{
public bool customColors;
public bool disableInPublicLobbies;
}
3 changes: 2 additions & 1 deletion GorillaCosmetics/Data/Previews/HatPreview.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GorillaCosmetics.Data.Behaviours;
using GorillaCosmetics.Utils;
using System.IO;
using UnityEngine;

Expand All @@ -21,7 +22,7 @@ public HatPreview(GorillaHat baseHat, Collider collider)
// fake hat time
string folder = Path.GetDirectoryName(typeof(GorillaCosmetics).Assembly.Location);

gameObject = UnityEngine.Object.Instantiate(AssetBundle.LoadFromFile($"{folder}\\Misc\\None").LoadAsset<GameObject>("_Hat"));
gameObject = UnityEngine.Object.Instantiate(PackageUtils.AssetBundleFromPackage($"{folder}\\Misc\\None").LoadAsset<GameObject>("_Hat"));
}
gameObject.transform.SetParent(collider.transform);
gameObject.transform.localPosition = Vector3.zero;
Expand Down
2 changes: 1 addition & 1 deletion GorillaCosmetics/GorillaCosmetics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace GorillaCosmetics
{
[BepInPlugin("org.legoandmars.gorillatag.gorillacosmetics", "Gorilla Cosmetics", "1.1.0")]
[BepInPlugin("org.legoandmars.gorillatag.gorillacosmetics", "Gorilla Cosmetics", "2.0.0")]
public class GorillaCosmetics : BaseUnityPlugin
{
public static ConfigEntry<string> selectedMaterial;
Expand Down
6 changes: 6 additions & 0 deletions GorillaCosmetics/GorillaCosmetics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<Reference Include="Assembly-CSharp">
<HintPath>..\Libs\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\Libs\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PhotonRealtime">
<HintPath>..\Libs\PhotonRealtime.dll</HintPath>
</Reference>
Expand All @@ -32,6 +35,9 @@
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\Libs\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.JSONSerializeModule">
<HintPath>..\Libs\UnityEngine.JSONSerializeModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.PhysicsModule">
<HintPath>..\Libs\UnityEngine.PhysicsModule.dll</HintPath>
</Reference>
Expand Down
71 changes: 71 additions & 0 deletions GorillaCosmetics/Utils/PackageUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System.IO.Compression;
using System.IO;
using System.Linq;
using GorillaCosmetics.Data.Descriptors;
using GorillaCosmetics.Data;
using Newtonsoft.Json;

namespace GorillaCosmetics.Utils
{
public static class PackageUtils
{
public static (AssetBundle bundle, PackageJSON json) AssetBundleAndJSONFromPackage(string path)
{
AssetBundle bundle = null;
PackageJSON json = null;
using (ZipArchive archive = ZipFile.OpenRead(path))
{
var jsonEntry = archive.Entries.First(i => i.Name == "package.json");
if (jsonEntry != null)
{
var stream = new StreamReader(jsonEntry.Open(), Encoding.Default);
string jsonString = stream.ReadToEnd();
json = JsonConvert.DeserializeObject<PackageJSON>(jsonString);
}
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (json != null && entry.Name == json.pcFileName)
{
//here the file
var SeekableStream = new MemoryStream();
entry.Open().CopyTo(SeekableStream);
SeekableStream.Position = 0;
bundle = AssetBundle.LoadFromStream(SeekableStream);
}
}
}
return (bundle, json);
}

public static AssetBundle AssetBundleFromPackage(string path)
{
return AssetBundleAndJSONFromPackage(path).bundle;
}

public static GorillaMaterialDescriptor ConvertJsonToMaterial(PackageJSON json)
{
GorillaMaterialDescriptor Descriptor = new GorillaMaterialDescriptor();
Descriptor.MaterialName = json.descriptor.objectName;
Descriptor.AuthorName = json.descriptor.author;
Descriptor.Description = json.descriptor.description;
Descriptor.CustomColors = json.config.customColors;
Descriptor.DisablePublicLobbies = json.config.disableInPublicLobbies;
return Descriptor;
}

public static HatDescriptor ConvertJsonToHat(PackageJSON json)
{
HatDescriptor Descriptor = new HatDescriptor();
Descriptor.HatName = json.descriptor.objectName;
Descriptor.AuthorName = json.descriptor.author;
Descriptor.Description = json.descriptor.description;
Descriptor.CustomColors = json.config.customColors;
Descriptor.DisablePublicLobbies = json.config.disableInPublicLobbies;
return Descriptor;
}
}
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ If you don't want to manually install, you can install this mod with the [Monke

If your game isn't modded with BepinEx, DO THAT FIRST! Simply go to the [latest BepinEx release](https://github.com/BepInEx/BepInEx/releases) and extract BepinEx_x64_VERSION.zip directly into your game's folder, then run the game once to install BepinEx properly.

This mod also depends on Newtonsoft.Json, so go to my latest [Newtonsoft.Json release](https://github.com/legoandmars/Newtonsoft.Json/releases/latest) and follow the instructions there.

Next, go to the [latest release of this mod](https://github.com/legoandmars/GorillaCosmetics/releases/latest) and extract it directly into your game's folder. Make sure it's extracted directly into your game's folder and not into a subfolder!

The mod is now installed!
Expand Down Expand Up @@ -67,12 +69,13 @@ More detailed information is found in the Unity Project's README.
## For Developers
This project is built with C# using .NET Standard.

For references, create a Libs folder in the same folder as the project solution. Inside of this folder you'll need to copy:
Make sure to install the mod first so you have all of the required files. For references, create a Libs folder in the same folder as the project solution. Inside of this folder you'll need to copy:

```
0Harmony.dll
BepInEx.dll
BepInEx.Harmony.dll
Newtonsoft.Json
```
from `Gorilla Tag\BepInEx\core`, and
```
Expand Down

0 comments on commit fb829e2

Please sign in to comment.