[unity] Replaced full json parsing for version string in SkeletonDataCompatibility.GetVersionInfo() with lightweight regex variant (fallback to full parsing). Affects editor only, built binary always excluded GetVersionInfo() checks.

This commit is contained in:
Harald Csaszar 2020-04-01 15:18:34 +02:00
parent b566bfb0b9
commit fbcd00d41a

View File

@ -30,7 +30,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using UnityEngine; using UnityEngine;
#if UNITY_EDITOR
using System.Globalization; using System.Globalization;
using System.Text.RegularExpressions;
#endif
namespace Spine.Unity { namespace Spine.Unity {
@ -41,6 +44,7 @@ namespace Spine.Unity {
static readonly int[][] compatibleJsonVersions = { new[] { 3, 8, 0 } }; static readonly int[][] compatibleJsonVersions = { new[] { 3, 8, 0 } };
static bool wasVersionDialogShown = false; static bool wasVersionDialogShown = false;
static readonly Regex jsonVersionRegex = new Regex(@"""spine""\s*:\s*""([^""]+)""", RegexOptions.CultureInvariant);
#endif #endif
public enum SourceType { public enum SourceType {
@ -92,23 +96,29 @@ namespace Spine.Unity {
} }
} }
else { else {
object obj = Json.Deserialize(new StringReader(asset.text)); Match match = jsonVersionRegex.Match(asset.text);
if (obj == null) { if (match != null) {
Debug.LogErrorFormat("'{0}' is not valid JSON.", asset.name); fileVersion.rawVersion = match.Groups[1].Value;
return null;
} }
else {
object obj = Json.Deserialize(new StringReader(asset.text));
if (obj == null) {
Debug.LogErrorFormat("'{0}' is not valid JSON.", asset.name);
return null;
}
var root = obj as Dictionary<string, object>; var root = obj as Dictionary<string, object>;
if (root == null) { if (root == null) {
Debug.LogErrorFormat("'{0}' is not compatible JSON. Parser returned an incorrect type while parsing version info.", asset.name); Debug.LogErrorFormat("'{0}' is not compatible JSON. Parser returned an incorrect type while parsing version info.", asset.name);
return null; return null;
} }
if (root.ContainsKey("skeleton")) { if (root.ContainsKey("skeleton")) {
var skeletonInfo = (Dictionary<string, object>)root["skeleton"]; var skeletonInfo = (Dictionary<string, object>)root["skeleton"];
object jv; object jv;
skeletonInfo.TryGetValue("spine", out jv); skeletonInfo.TryGetValue("spine", out jv);
fileVersion.rawVersion = jv as string; fileVersion.rawVersion = jv as string;
}
} }
} }