Fixed loaded file export on global

Added workaround for isReadable error on scene export to .pmx
Fixed compilation on android
Incremented version
This commit is contained in:
katboi01
2025-10-04 00:27:12 +02:00
parent 5351f82f09
commit 3cb3239316
5 changed files with 98 additions and 33 deletions
+63 -6
View File
@@ -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;
}
/// <summary> Credit to pohype: https://discussions.unity.com/t/reading-meshes-at-runtime-that-are-not-enabled-for-read-write/804189/7 </summary>
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;
}
}
+20 -9
View File
@@ -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);
+1 -1
View File
@@ -133,7 +133,7 @@ public class UmaAssetManager : MonoBehaviour
else
{
AssetBundle bundle;
if (entry.Key == 0)
if (!entry.IsEncrypted)
{
bundle = AssetBundle.LoadFromFile(filePath);
}
+13 -16
View File
@@ -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<UmaDatabaseEntry> 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]);
}
}