diff --git a/Assets/Scripts/Exporters/ModelExporter.cs b/Assets/Scripts/Exporters/ModelExporter.cs index e86d0dd..cce5c23 100644 --- a/Assets/Scripts/Exporters/ModelExporter.cs +++ b/Assets/Scripts/Exporters/ModelExporter.cs @@ -8,10 +8,10 @@ using System; using System.Collections.Generic; using System.IO; using UnityEngine; +using UnityEngine.Rendering; +using static BillboardBuilder; using static LibMMD.Model.Morph; using static LibMMD.Model.SkinningOperator; - -using static BillboardBuilder; //using UnityGLTF; @@ -359,8 +359,16 @@ public class ModelExporter var mesh = meshfilter.mesh; if (!mesh.isReadable) { - UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error); - continue; + UmaViewerUI.Instance.ShowMessage($"Mesh {mesh.name} was not readable. Attempting to export", UIMessageType.Warning); + try + { + mesh = MakeReadableMeshCopy(mesh); + } + catch + { + UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error); + continue; + } } var vertices = mesh.vertices; var normals = mesh.normals; @@ -402,8 +410,16 @@ public class ModelExporter var mesh = smr.sharedMesh; if (!mesh.isReadable) { - UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error); - continue; + UmaViewerUI.Instance.ShowMessage($"Mesh {mesh.name} was not readable. Attempting to export", UIMessageType.Warning); + try + { + mesh = MakeReadableMeshCopy(mesh); + } + catch + { + UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error); + continue; + } } var vertices = mesh.vertices; var normals = mesh.normals; @@ -498,4 +514,45 @@ public class ModelExporter { return bones.Contains(bone) ? bones.IndexOf(bone) : 0; } + + /// Credit to pohype: https://discussions.unity.com/t/reading-meshes-at-runtime-that-are-not-enabled-for-read-write/804189/7 + public static Mesh MakeReadableMeshCopy(Mesh nonReadableMesh) + { + Mesh meshCopy = new Mesh(); + meshCopy.indexFormat = nonReadableMesh.indexFormat; + + // Handle vertices + GraphicsBuffer verticesBuffer = nonReadableMesh.GetVertexBuffer(0); + int totalSize = verticesBuffer.stride * verticesBuffer.count; + byte[] data = new byte[totalSize]; + verticesBuffer.GetData(data); + meshCopy.SetVertexBufferParams(nonReadableMesh.vertexCount, nonReadableMesh.GetVertexAttributes()); + meshCopy.SetVertexBufferData(data, 0, 0, totalSize); + verticesBuffer.Release(); + + // Handle triangles + meshCopy.subMeshCount = nonReadableMesh.subMeshCount; + GraphicsBuffer indexesBuffer = nonReadableMesh.GetIndexBuffer(); + int tot = indexesBuffer.stride * indexesBuffer.count; + byte[] indexesData = new byte[tot]; + indexesBuffer.GetData(indexesData); + meshCopy.SetIndexBufferParams(indexesBuffer.count, nonReadableMesh.indexFormat); + meshCopy.SetIndexBufferData(indexesData, 0, 0, tot); + indexesBuffer.Release(); + + // Restore submesh structure + uint currentIndexOffset = 0; + for (int i = 0; i < meshCopy.subMeshCount; i++) + { + uint subMeshIndexCount = nonReadableMesh.GetIndexCount(i); + meshCopy.SetSubMesh(i, new SubMeshDescriptor((int)currentIndexOffset, (int)subMeshIndexCount)); + currentIndexOffset += subMeshIndexCount; + } + + // Recalculate normals and bounds + meshCopy.RecalculateNormals(); + meshCopy.RecalculateBounds(); + + return meshCopy; + } } diff --git a/Assets/Scripts/Settings/UISettingsAssets.cs b/Assets/Scripts/Settings/UISettingsAssets.cs index eeda84a..33433b5 100644 --- a/Assets/Scripts/Settings/UISettingsAssets.cs +++ b/Assets/Scripts/Settings/UISettingsAssets.cs @@ -67,10 +67,16 @@ public class UISettingsAssets : MonoBehaviour { Directory.CreateDirectory(Path.GetDirectoryName(outName)); - //修改(导出解密ab包) - using var assetStream = new UmaAssetBundleStream(asset.FilePath, asset.FKey); - using var outputStream = new FileStream(outName, FileMode.Create); - assetStream.CopyTo(outputStream); + if (asset.IsEncrypted) + { + using var assetStream = new UmaAssetBundleStream(asset.FilePath, asset.FKey); + using var outputStream = new FileStream(outName, FileMode.Create); + assetStream.CopyTo(outputStream); + } + else + { + File.Copy(asset.FilePath, outName); + } } } UmaViewerUI.Instance.ShowMessage($"{LoadedAssetEntries.Count} files copied", UIMessageType.Success); @@ -101,11 +107,16 @@ public class UISettingsAssets : MonoBehaviour { Directory.CreateDirectory(Path.GetDirectoryName(outName)); - //修改(导出解密ab包) - using var assetStream = new UmaAssetBundleStream(asset.FilePath, asset.FKey); - using var outputStream = new FileStream(outName, FileMode.Create); - assetStream.CopyTo(outputStream); - + if (asset.IsEncrypted) + { + using var assetStream = new UmaAssetBundleStream(asset.FilePath, asset.FKey); + using var outputStream = new FileStream(outName, FileMode.Create); + assetStream.CopyTo(outputStream); + } + else + { + File.Copy(asset.FilePath, outName); + } } } UmaViewerUI.Instance.ShowMessage($"{assets.Count} files copied", UIMessageType.Success); diff --git a/Assets/Scripts/UmaAssetManager.cs b/Assets/Scripts/UmaAssetManager.cs index 34ede96..0655bb9 100644 --- a/Assets/Scripts/UmaAssetManager.cs +++ b/Assets/Scripts/UmaAssetManager.cs @@ -133,7 +133,7 @@ public class UmaAssetManager : MonoBehaviour else { AssetBundle bundle; - if (entry.Key == 0) + if (!entry.IsEncrypted) { bundle = AssetBundle.LoadFromFile(filePath); } diff --git a/Assets/Scripts/UmaDatabase/UmaDatabaseEntry.cs b/Assets/Scripts/UmaDatabase/UmaDatabaseEntry.cs index baf3dcf..2d9a6bb 100644 --- a/Assets/Scripts/UmaDatabase/UmaDatabaseEntry.cs +++ b/Assets/Scripts/UmaDatabase/UmaDatabaseEntry.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Windows.Forms; -using UnityEngine; using Object = UnityEngine.Object; public class UmaDatabaseEntry @@ -16,6 +14,8 @@ public class UmaDatabaseEntry public long Key; private byte[] fKey; + public bool IsEncrypted => Key != 0; + [NonSerialized] public List CachedPrerequisites = null; @@ -23,28 +23,25 @@ public class UmaDatabaseEntry { get { - if (fKey == null && Key != 0) + if (fKey == null && IsEncrypted) { var baseKeys = Config.Instance.ABKey; int baseLen = baseKeys.Length; var keys = new byte[baseLen * 8]; - if (Key != 0) + byte[] keyBytes = BitConverter.GetBytes(Key); + if (!BitConverter.IsLittleEndian) { - byte[] keyBytes = BitConverter.GetBytes(Key); - if (!BitConverter.IsLittleEndian) + Array.Reverse(keyBytes); + } + for (int i = 0; i < baseLen; ++i) + { + byte b = baseKeys[i]; + int baseOffset = i << 3; // i * 8 + for (int j = 0; j < 8; ++j) { - Array.Reverse(keyBytes); - } - for (int i = 0; i < baseLen; ++i) - { - byte b = baseKeys[i]; - int baseOffset = i << 3; // i * 8 - for (int j = 0; j < 8; ++j) - { - keys[baseOffset + j] = (byte)(b ^ keyBytes[j]); - } + keys[baseOffset + j] = (byte)(b ^ keyBytes[j]); } } diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index d22efac..f512d78 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -141,7 +141,7 @@ PlayerSettings: loadStoreDebugModeEnabled: 0 visionOSBundleVersion: 1.0 tvOSBundleVersion: 1.0 - bundleVersion: 2.1.32 + bundleVersion: 2.1.33 preloadedAssets: [] metroInputSource: 0 wsaTransparentSwapchain: 0