From c429bfd6b0458e8b42c69cdb768f5c7867c35688 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Fri, 16 May 2025 20:46:22 +0200 Subject: [PATCH] [unity] Using allocation-avoiding TextAsset.GetData call in editor-only GetVersionInfo and IsJsonFile checks as well. See #2851. --- .../Asset Types/SkeletonDataCompatibility.cs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SkeletonDataCompatibility.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SkeletonDataCompatibility.cs index d9e2b72eb..7e0429342 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SkeletonDataCompatibility.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SkeletonDataCompatibility.cs @@ -27,6 +27,16 @@ * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ +//#define SPINE_ALLOW_UNSAFE // note: this define can be set via Edit - Preferences - Spine. + +#if UNITY_2021_2_OR_NEWER +#define TEXT_ASSET_HAS_GET_DATA_BYTES +#endif + +#if SPINE_ALLOW_UNSAFE && TEXT_ASSET_HAS_GET_DATA_BYTES +#define UNSAFE_DIRECT_ACCESS_TEXT_ASSET_DATA +#endif + using System; using System.Collections.Generic; using System.IO; @@ -106,8 +116,12 @@ namespace Spine.Unity { if (fileVersion.sourceType == SourceType.Binary) { try { - using (MemoryStream memStream = new MemoryStream(asset.bytes)) { - fileVersion.rawVersion = SkeletonBinary.GetVersionString(memStream); +#if UNSAFE_DIRECT_ACCESS_TEXT_ASSET_DATA + using (Stream stream = asset.GetStreamUnsafe()) { +#else + using (MemoryStream stream = new MemoryStream(asset.bytes)) { +#endif + fileVersion.rawVersion = SkeletonBinary.GetVersionString(stream); } } catch (System.Exception e) { problemDescription = string.Format("Failed to read '{0}'. It is likely not a binary Spine SkeletonData file.\n{1}", asset.name, e); @@ -162,8 +176,11 @@ namespace Spine.Unity { } public static bool IsJsonFile (TextAsset file) { +#if TEXT_ASSET_HAS_GET_DATA_BYTES + var content = file.GetData(); +#else byte[] content = file.bytes; - +#endif // check for binary skeleton version number string, starts after 8 byte hash char majorVersionChar = compatibleBinaryVersions[0][0].ToString()[0]; if (content.Length > 10 && content[9] == majorVersionChar && content[10] == '.') @@ -219,5 +236,5 @@ namespace Spine.Unity { spineJson.name, descriptionString), spineJson); } #endif // UNITY_EDITOR - } -} + } + }