[unity] Using allocation-avoiding TextAsset.GetData call in editor-only GetVersionInfo and IsJsonFile checks as well. See #2851.

This commit is contained in:
Harald Csaszar 2025-05-16 20:46:22 +02:00
parent 5a0d0ed696
commit c429bfd6b0

View File

@ -27,6 +27,16 @@
* SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -106,8 +116,12 @@ namespace Spine.Unity {
if (fileVersion.sourceType == SourceType.Binary) { if (fileVersion.sourceType == SourceType.Binary) {
try { try {
using (MemoryStream memStream = new MemoryStream(asset.bytes)) { #if UNSAFE_DIRECT_ACCESS_TEXT_ASSET_DATA
fileVersion.rawVersion = SkeletonBinary.GetVersionString(memStream); using (Stream stream = asset.GetStreamUnsafe()) {
#else
using (MemoryStream stream = new MemoryStream(asset.bytes)) {
#endif
fileVersion.rawVersion = SkeletonBinary.GetVersionString(stream);
} }
} catch (System.Exception e) { } 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); 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) { public static bool IsJsonFile (TextAsset file) {
#if TEXT_ASSET_HAS_GET_DATA_BYTES
var content = file.GetData<byte>();
#else
byte[] content = file.bytes; byte[] content = file.bytes;
#endif
// check for binary skeleton version number string, starts after 8 byte hash // check for binary skeleton version number string, starts after 8 byte hash
char majorVersionChar = compatibleBinaryVersions[0][0].ToString()[0]; char majorVersionChar = compatibleBinaryVersions[0][0].ToString()[0];
if (content.Length > 10 && content[9] == majorVersionChar && content[10] == '.') if (content.Length > 10 && content[9] == majorVersionChar && content[10] == '.')
@ -219,5 +236,5 @@ namespace Spine.Unity {
spineJson.name, descriptionString), spineJson); spineJson.name, descriptionString), spineJson);
} }
#endif // UNITY_EDITOR #endif // UNITY_EDITOR
} }
} }