[unity] Added using and finally-dispose guards around all stream instances, see #1260.

This commit is contained in:
Harald Csaszar 2019-01-22 11:27:47 +01:00
parent 92a0d37e9b
commit edb2fbe85d
2 changed files with 37 additions and 21 deletions

View File

@ -514,6 +514,8 @@ namespace Spine.Unity.Editor {
TextReader reader = null; TextReader reader = null;
TextAsset spineJson = AssetDatabase.LoadAssetAtPath<TextAsset>(skeletonDataPath); TextAsset spineJson = AssetDatabase.LoadAssetAtPath<TextAsset>(skeletonDataPath);
Dictionary<string, object> root = null;
try {
if (spineJson != null) { if (spineJson != null) {
reader = new StringReader(spineJson.text); reader = new StringReader(spineJson.text);
} }
@ -522,9 +524,14 @@ namespace Spine.Unity.Editor {
// as a workaround, we provide a fallback reader. // as a workaround, we provide a fallback reader.
reader = new StreamReader(skeletonDataPath); reader = new StreamReader(skeletonDataPath);
} }
var root = Json.Deserialize(reader) as Dictionary<string, object>; root = Json.Deserialize(reader) as Dictionary<string, object>;
}
finally {
if (reader != null)
reader.Dispose();
}
if (!root.ContainsKey("skins")) if (root == null || !root.ContainsKey("skins"))
return requiredPaths; return requiredPaths;
foreach (var skin in (Dictionary<string, object>)root["skins"]) { foreach (var skin in (Dictionary<string, object>)root["skins"]) {
@ -566,15 +573,21 @@ namespace Spine.Unity.Editor {
SkeletonBinary binary = new SkeletonBinary(new AtlasRequirementLoader(requiredPaths)); SkeletonBinary binary = new SkeletonBinary(new AtlasRequirementLoader(requiredPaths));
Stream input = null; Stream input = null;
TextAsset data = AssetDatabase.LoadAssetAtPath<TextAsset>(skeletonDataPath); TextAsset data = AssetDatabase.LoadAssetAtPath<TextAsset>(skeletonDataPath);
if (data == null) { try {
if (data != null) {
input = new MemoryStream(data.bytes);
}
else {
// On a "Reimport All" the order of imports can be wrong, thus LoadAssetAtPath() above could return null. // On a "Reimport All" the order of imports can be wrong, thus LoadAssetAtPath() above could return null.
// as a workaround, we provide a fallback reader. // as a workaround, we provide a fallback reader.
input = File.Open(skeletonDataPath, FileMode.Open, FileAccess.Read); input = File.Open(skeletonDataPath, FileMode.Open, FileAccess.Read);
} }
else {
input = new MemoryStream(data.bytes);
}
binary.ReadSkeletonData(input); binary.ReadSkeletonData(input);
}
finally {
if (input != null)
input.Dispose();
}
binary = null; binary = null;
} }
@ -1000,7 +1013,9 @@ namespace Spine.Unity.Editor {
int[][] compatibleVersions; int[][] compatibleVersions;
if (asset.name.Contains(".skel")) { if (asset.name.Contains(".skel")) {
try { try {
rawVersion = SkeletonBinary.GetVersionString(new MemoryStream(asset.bytes)); using (var memStream = new MemoryStream(asset.bytes)) {
rawVersion = SkeletonBinary.GetVersionString(memStream);
}
isSpineData = !(string.IsNullOrEmpty(rawVersion)); isSpineData = !(string.IsNullOrEmpty(rawVersion));
compatibleVersions = compatibleBinaryVersions; compatibleVersions = compatibleBinaryVersions;
} catch (System.Exception e) { } catch (System.Exception e) {

View File

@ -219,12 +219,13 @@ namespace Spine.Unity {
} }
internal static SkeletonData ReadSkeletonData (byte[] bytes, AttachmentLoader attachmentLoader, float scale) { internal static SkeletonData ReadSkeletonData (byte[] bytes, AttachmentLoader attachmentLoader, float scale) {
var input = new MemoryStream(bytes); using (var input = new MemoryStream(bytes)) {
var binary = new SkeletonBinary(attachmentLoader) { var binary = new SkeletonBinary(attachmentLoader) {
Scale = scale Scale = scale
}; };
return binary.ReadSkeletonData(input); return binary.ReadSkeletonData(input);
} }
}
internal static SkeletonData ReadSkeletonData (string text, AttachmentLoader attachmentLoader, float scale) { internal static SkeletonData ReadSkeletonData (string text, AttachmentLoader attachmentLoader, float scale) {
var input = new StringReader(text); var input = new StringReader(text);