using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Threading; using System.Threading.Tasks; using BepInEx; using BepInEx.Logging; using GLTFast; using GLTFast.Export; using GLTFast.Loading; using GLTFast.Logging; using GLTFast.Materials; using HarmonyLib; using Microsoft.CodeAnalysis; using Unity.Burst; using UnityEngine; using UnityEngine.Rendering; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: IgnoresAccessChecksTo("Assembly-CSharp")] [assembly: IgnoresAccessChecksTo("com.unity.cloud.gltfast")] [assembly: IgnoresAccessChecksTo("Mirror")] [assembly: IgnoresAccessChecksTo("Unity.Burst")] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("Marioalexsan.AtlyssGLTF")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("0.3.1.0")] [assembly: AssemblyInformationalVersion("0.3.1+17141a855e7185d3bc4128d3eff2df783eceb5c8")] [assembly: AssemblyProduct("AtlyssGLTF")] [assembly: AssemblyTitle("Marioalexsan.AtlyssGLTF")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("0.3.1.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace Marioalexsan.AtlyssGLTF { [HarmonyPatch(typeof(ChatBehaviour), "Send_ChatMessage")] internal static class ChatCommands { private static bool Prefix(string _message, ChatBehaviour __instance) { if (string.IsNullOrEmpty(_message)) { return true; } string[] array = _message.Split(" ", StringSplitOptions.RemoveEmptyEntries); if (array.Length == 0 || array[0] != "/gltf") { return true; } if (array.Length < 2) { HostConsole._current.Init_NetworkStatusMessage("Usage: /gltf "); return false; } switch (array[1]) { case "force_readable_meshes": if (array.Length != 2) { HostConsole._current.Init_NetworkStatusMessage("Usage: /gltf force_readable_meshes"); return false; } AtlyssGLTF.ForceReadableMeshes = !AtlyssGLTF.ForceReadableMeshes; HostConsole._current.Init_NetworkStatusMessage("Forced readable meshes is now " + (AtlyssGLTF.ForceReadableMeshes ? "on" : "off") + "."); return false; case "import": if (array.Length != 4) { HostConsole._current.Init_NetworkStatusMessage("Usage: /gltf import "); return false; } AtlyssGLTF.GLTFImportQueue.Enqueue((array[2], Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)AtlyssGLTF.Plugin).Info.Location), array[3]))); return false; case "export": if (array.Length != 4) { HostConsole._current.Init_NetworkStatusMessage("Usage: /gltf export "); return false; } AtlyssGLTF.GLTFExportQueue.Enqueue((array[2], Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)AtlyssGLTF.Plugin).Info.Location), array[3]))); return false; default: HostConsole._current.Init_NetworkStatusMessage("That command does not exist."); return false; } } } [BepInPlugin("Marioalexsan.AtlyssGLTF", "AtlyssGLTF", "0.3.1")] public class AtlyssGLTF : BaseUnityPlugin { private Harmony _harmony; public static AtlyssGLTF Plugin { get; private set; } public static AssetBundle Bundle { get; private set; } public static Shader PBRMetallicRoughness { get; private set; } public static Shader PBRSpecularGlossiness { get; private set; } public static Shader Unlit { get; private set; } public static Shader ExportColor { get; private set; } public static Shader ExportMaskmap { get; private set; } public static Shader ExportMetalGloss { get; private set; } public static Shader ExportNormal { get; private set; } public static Shader ExportOcclusion { get; private set; } public static Shader ExportSmoothness { get; private set; } public static bool ForceReadableMeshes { get; internal set; } = false; internal static ManualLogSource Logger { get; private set; } internal static Queue<(string ObjectName, string Path)> GLTFImportQueue { get; } = new Queue<(string, string)>(); internal static Queue<(string ObjectName, string Path)> GLTFExportQueue { get; } = new Queue<(string, string)>(); public ImportSettings ImportMecanim { get; } = new ImportSettings { AnimationMethod = (AnimationMethod)2 }; private void Awake() { //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Expected O, but got Unknown Plugin = this; Logger = ((BaseUnityPlugin)this).Logger; _harmony = new Harmony("AtlyssGLTF"); _harmony.PatchAll(); string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "gltf"); Bundle = AssetBundle.LoadFromFile(text); if (Object.op_Implicit((Object)(object)Bundle)) { PBRMetallicRoughness = LoadShader("assets/atlyss/shader/gltf/built-in/gltfpbrmetallicroughness.shader"); PBRSpecularGlossiness = LoadShader("assets/atlyss/shader/gltf/built-in/gltfpbrspecularglossiness.shader"); Unlit = LoadShader("assets/atlyss/shader/gltf/built-in/gltfunlit.shader"); ExportColor = LoadShader("assets/atlyss/shader/gltf/export/gltfexportcolor.shader"); ExportMaskmap = LoadShader("assets/atlyss/shader/gltf/export/gltfexportmaskmap.shader"); ExportMetalGloss = LoadShader("assets/atlyss/shader/gltf/export/gltfexportmetalgloss.shader"); ExportNormal = LoadShader("assets/atlyss/shader/gltf/export/gltfexportnormal.shader"); ExportOcclusion = LoadShader("assets/atlyss/shader/gltf/export/gltfexportocclusion.shader"); ExportSmoothness = LoadShader("assets/atlyss/shader/gltf/export/gltfexportsmoothness.shader"); Logger.LogInfo((object)"Asset bundle items:"); string[] allAssetNames = Bundle.GetAllAssetNames(); foreach (string text2 in allAssetNames) { Logger.LogInfo((object)(" - " + text2)); } } else { Logger.LogWarning((object)("Unable to load glTF shader bundle from " + text + ".")); } Logger.LogInfo((object)"Initialized!"); static Shader LoadShader(string path) { Shader val = Bundle.LoadAsset(path); if (!Object.op_Implicit((Object)(object)val)) { Logger.LogWarning((object)("glTF shader load failed: " + path + ".")); } return val; } } public async Task Update() { if (GLTFImportQueue.Count > 0) { (string, string) tuple = GLTFImportQueue.Dequeue(); await ImportGLTFScene(tuple.Item1, tuple.Item2); } if (GLTFExportQueue.Count > 0) { (string, string) tuple2 = GLTFExportQueue.Dequeue(); await ExportGLTFScene(tuple2.Item1, tuple2.Item2); } } private async Task ImportGLTFScene(string objectName, string path) { _ = 1; try { GltfImport gltf = new GltfImport((IDownloadProvider)null, (IDeferAgent)null, (IMaterialGenerator)null, (ICodeLogger)null); await ((GltfImportBase)gltf).Load(new Uri(path), new ImportSettings { AnimationMethod = (AnimationMethod)2 }, default(CancellationToken)); GameObject gameObject = new GameObject(objectName); await ((GltfImportBase)gltf).InstantiateMainSceneAsync(gameObject.transform, default(CancellationToken)); HostConsole._current.Init_NetworkStatusMessage("Imported glTF."); return gameObject; } catch (Exception ex) { HostConsole._current.Init_NetworkStatusMessage("Import failed! Check the console."); Logger.LogError((object)ex); return null; } } private async Task ExportGLTFScene(string objectName, string path) { try { GameObject val = GameObject.Find(objectName); if (!Object.op_Implicit((Object)(object)val)) { HostConsole._current.Init_NetworkStatusMessage("Object not found."); return; } GameObjectExport val2 = new GameObjectExport(new ExportSettings { Deterministic = true, Format = (GltfFormat)1 }, (GameObjectExportSettings)null, (IMaterialExport)null, (IDeferAgent)null, (ICodeLogger)null); val2.AddScene((GameObject[])(object)new GameObject[1] { val }, (string)null); if (!(await val2.SaveToFileAndDispose(path, default(CancellationToken)))) { HostConsole._current.Init_NetworkStatusMessage("Something went wrong while exporting the glTF!"); } HostConsole._current.Init_NetworkStatusMessage("Exported glTF!"); } catch (Exception ex) { HostConsole._current.Init_NetworkStatusMessage("Export failed! Check the console."); Logger.LogError((object)ex); } } } internal static class ModInfo { public const string GUID = "Marioalexsan.AtlyssGLTF"; public const string NAME = "AtlyssGLTF"; public const string VERSION = "0.3.1"; } } namespace Marioalexsan.AtlyssGLTF.HarmonyPatches { internal static class BurstManager { public static void Initialize() { } } [HarmonyPatch(typeof(BurstCompiler), "Compile", new Type[] { typeof(object), typeof(bool) })] internal static class BurstRemove { private unsafe static bool Prefix(object delegateObj, out void* __result) { if (delegateObj.GetType().Assembly == typeof(GltfAsset).Assembly) { __result = (void*)Marshal.GetFunctionPointerForDelegate(delegateObj); return false; } __result = null; return true; } } [HarmonyPatch(typeof(GltfWriter), "AddMesh")] internal static class GltfWriter_AddMesh { private static void Prefix(ref Mesh uMesh, ref VertexAttributeUsage attributeUsage) { if (!uMesh.isReadable) { uMesh = MakeReadableMeshCopy(uMesh); } } public static Mesh MakeReadableMeshCopy(Mesh nonReadableMesh) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Expected O, but got Unknown //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0066: Unknown result type (might be due to invalid IL or missing references) //IL_009b: Unknown result type (might be due to invalid IL or missing references) Mesh val = new Mesh(); ((Object)val).name = ((Object)nonReadableMesh).name + "_copy"; val.indexFormat = nonReadableMesh.indexFormat; CopyVertexBuffers(val, nonReadableMesh); val.subMeshCount = nonReadableMesh.subMeshCount; GraphicsBuffer indexBuffer = nonReadableMesh.GetIndexBuffer(); int num = indexBuffer.stride * indexBuffer.count; byte[] array = new byte[num]; indexBuffer.GetData((Array)array); val.SetIndexBufferParams(indexBuffer.count, nonReadableMesh.indexFormat); val.SetIndexBufferData(array, 0, 0, num, (MeshUpdateFlags)0); indexBuffer.Release(); uint num2 = 0u; for (int i = 0; i < val.subMeshCount; i++) { uint indexCount = nonReadableMesh.GetIndexCount(i); val.SetSubMesh(i, new SubMeshDescriptor((int)num2, (int)indexCount, (MeshTopology)0), (MeshUpdateFlags)0); num2 += indexCount; } CopyBlendShapes(val, nonReadableMesh); val.RecalculateNormals(); val.RecalculateBounds(); val.boneWeights = nonReadableMesh.boneWeights; val.bindposes = nonReadableMesh.bindposes; return val; } private static void CopyBlendShapes(Mesh targetMesh, Mesh sourceMesh) { try { for (int i = 0; i < sourceMesh.blendShapeCount; i++) { string blendShapeName = sourceMesh.GetBlendShapeName(i); int blendShapeFrameCount = sourceMesh.GetBlendShapeFrameCount(i); for (int j = 0; j < blendShapeFrameCount; j++) { float blendShapeFrameWeight = sourceMesh.GetBlendShapeFrameWeight(i, j); Vector3[] array = (Vector3[])(object)new Vector3[sourceMesh.vertexCount]; Vector3[] array2 = (Vector3[])(object)new Vector3[sourceMesh.vertexCount]; Vector3[] array3 = (Vector3[])(object)new Vector3[sourceMesh.vertexCount]; sourceMesh.GetBlendShapeFrameVertices(i, j, array, array2, array3); targetMesh.AddBlendShapeFrame(blendShapeName, blendShapeFrameWeight, array, array2, array3); } } } catch (Exception ex) { Debug.LogException(ex); } } private static void CopyVertexBuffers(Mesh targetMesh, Mesh sourceMesh) { for (int i = 0; i < sourceMesh.vertexBufferCount; i++) { GraphicsBuffer vertexBuffer = sourceMesh.GetVertexBuffer(i); int num = vertexBuffer.stride * vertexBuffer.count; byte[] array = new byte[num]; vertexBuffer.GetData((Array)array); targetMesh.SetVertexBufferParams(sourceMesh.vertexCount, sourceMesh.GetVertexAttributes()); targetMesh.SetVertexBufferData(array, 0, 0, num, i, (MeshUpdateFlags)0); vertexBuffer.Release(); } } } [HarmonyPatch(/*Could not decode attribute arguments.*/)] internal static class ReadableMesh { private static bool Prefix(ref bool __result) { if (!AtlyssGLTF.ForceReadableMeshes) { return true; } __result = true; return false; } } [HarmonyPatch(typeof(ImageExport), "LoadBlitMaterial")] internal static class ImageExport_LoadBlitMaterial { private static bool Prefix(ref string shaderName, ref Material __result) { //IL_007f: Unknown result type (might be due to invalid IL or missing references) //IL_0085: Expected O, but got Unknown Shader val = (Shader)(shaderName switch { "glTFExportColor" => AtlyssGLTF.ExportColor, "glTFExportNormal" => AtlyssGLTF.ExportNormal, "glTFExportMetalGloss" => AtlyssGLTF.ExportMetalGloss, "glTFExportOcclusion" => AtlyssGLTF.ExportOcclusion, "glTFExportSmoothness" => AtlyssGLTF.ExportSmoothness, _ => null, }); if ((Object)(object)val == (Object)null) { return true; } __result = new Material(val); return false; } } [HarmonyPatch(typeof(BuiltInMaterialGenerator), "FinderShaderMetallicRoughness")] internal static class FinderShaderMetallicRoughnessPatch { private static bool Prefix(BuiltInMaterialGenerator __instance, ref Shader __result) { __result = AtlyssGLTF.PBRMetallicRoughness; return false; } } [HarmonyPatch(typeof(BuiltInMaterialGenerator), "FinderShaderSpecularGlossiness")] internal static class FinderShaderSpecularGlossinessPatch { private static bool Prefix(BuiltInMaterialGenerator __instance, ref Shader __result) { __result = AtlyssGLTF.PBRSpecularGlossiness; return false; } } [HarmonyPatch(typeof(BuiltInMaterialGenerator), "FinderShaderUnlit")] internal static class FinderShaderUnlitPatch { private static bool Prefix(BuiltInMaterialGenerator __instance, ref Shader __result) { __result = AtlyssGLTF.Unlit; return false; } } } namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] internal sealed class IgnoresAccessChecksToAttribute : Attribute { public IgnoresAccessChecksToAttribute(string assemblyName) { } } }