Skip to content

Commit 47caee3

Browse files
committed
fix(plugin): Version.TryParse→try-catch + LoadPngBytes null→throw + FileShare.ReadWrite追加
【Mono非互換】 - Version.TryParse は .NET 4.0+ で Unity 5.6 Mono に存在しない → try-catch new Version() に置換 (GuessCompressed, Load) 【ArgumentNullException根本原因】 - LoadPngBytes(BinaryReader) が GetPngSize==0 時に null 返却 → BinaryWriter.Write(null) で ArgumentNullException → null返却を廃止、InvalidOperationException を throw - LoadPngBytes(string) の FileStream に FileShare.ReadWrite が不足 → ゲームがファイルを掴んでると読めず GetPngSize==0 に
1 parent caf7d8c commit 47caee3

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎src/Plugin/Extension/ImageHelper.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static void SkipPng(BinaryReader br)
8383
public static byte[] LoadPngBytes(string path)
8484
{
8585
if (path == null) throw new ArgumentNullException(nameof(path));
86-
using (FileStream st = new FileStream(path, FileMode.Open, FileAccess.Read))
86+
using (FileStream st = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
8787
return LoadPngBytes(st);
8888
}
8989

@@ -98,7 +98,7 @@ public static byte[] LoadPngBytes(BinaryReader br)
9898
{
9999
if (br == null) throw new ArgumentNullException(nameof(br));
100100
long pngSize = GetPngSize(br);
101-
if (pngSize == 0) return null;
101+
if (pngSize == 0) throw new InvalidOperationException("Failed to read PNG data from stream (invalid or missing PNG signature/chunks).");
102102
return br.ReadBytes((int)pngSize);
103103
}
104104
}

‎src/Plugin/PngCompression/PngCompression.cs‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@ public long Load(Stream inputStream, Stream outputStream, string token = null, b
177177
switch (token)
178178
{
179179
case Token.StudioToken:
180-
checkfail = !Version.TryParse(binaryReader.ReadString(), out Version v) || !v.Equals(new Version(101, 0, 0, 0));
180+
try
181+
{
182+
checkfail = !new Version(binaryReader.ReadString()).Equals(new Version(101, 0, 0, 0));
183+
}
184+
catch
185+
{
186+
checkfail = true;
187+
}
181188
break;
182189
case Token.CoordinateToken:
183190
default:
@@ -267,9 +274,15 @@ public bool GuessCompressed(BinaryReader binaryReader)
267274
default:
268275
binaryReader.BaseStream.Seek(position, SeekOrigin.Begin);
269276
string st = binaryReader.ReadString();
270-
if (Version.TryParse(st, out Version version))
277+
try
278+
{
279+
Version version = new Version(st);
271280
return version.Major == 101;
272-
return false;
281+
}
282+
catch
283+
{
284+
return false;
285+
}
273286
}
274287
}
275288
catch

0 commit comments

Comments
 (0)