Skip to content

Commit 770b0b2

Browse files
Further comments and manual adjustments
1 parent c2e6475 commit 770b0b2

2 files changed

Lines changed: 9 additions & 36 deletions

File tree

Analyzer/Util/SerializedFileDetector.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public class SerializedFileInfo
4444
public static class SerializedFileDetector
4545
{
4646
// Version boundaries for format changes
47-
private const uint NewLayoutVersion = 9; // kUnknown_9: Changed from [header][data][metadata] to [header][metadata][data]
48-
private const uint LargeFilesSupportVersion = 22; // kLargeFilesSupport: Changed to 64-bit header
47+
// NOTE: This version is so old that it is extremely unlikely it will work with modern versions of Unity,
48+
// we handle it just for the purpose of trying to report accurate information about the file.
49+
private const uint NewLayoutVersion = 9; // Changed from [header][data][metadata] to [header][metadata][data]
50+
51+
private const uint LargeFilesSupportVersion = 22; // Changed to 64-bit header
4952

5053
// Reasonable version range for SerializedFiles
5154
// Unity versions currently use values in the 20s-30s range
@@ -105,7 +108,7 @@ public static bool TryDetectSerializedFile(string filePath, out SerializedFileIn
105108

106109
// Determine which interpretation gives us a valid version number
107110
uint version;
108-
bool needsSwap; // Whether header fields need byte swapping
111+
bool needsSwap; // Whether header fields need byte swapping (expected to be true when running on most modern systems, which are little-endian)
109112

110113
if (versionLE >= MinVersion && versionLE <= MaxVersion)
111114
{
@@ -250,13 +253,8 @@ public static bool TryDetectSerializedFile(string filePath, out SerializedFileIn
250253
// Allow some tolerance for "stream files" which can have padding
251254
if (fileSize != ulong.MaxValue)
252255
{
253-
// File size should not exceed actual file size by more than 1MB (arbitrary tolerance)
254-
if (fileSize > (ulong)fileLength + 1024 * 1024)
255-
return false;
256-
257-
// File size should be reasonably close to actual size (within 10% or 1MB)
258-
ulong tolerance = Math.Max(fileSize / 10, 1024 * 1024);
259-
if ((ulong)fileLength > fileSize + tolerance)
256+
// File size should not exceed actual file size by more than 1KB (arbitrary tolerance)
257+
if (fileSize > (ulong)fileLength + 1024)
260258
return false;
261259
}
262260

@@ -305,9 +303,6 @@ private static ulong ReadUInt64(byte[] buffer, int offset, bool swap)
305303
return swap ? SwapUInt64(value) : value;
306304
}
307305

308-
/// <summary>
309-
/// Swaps the byte order of a UInt32 value.
310-
/// </summary>
311306
private static uint SwapUInt32(uint value)
312307
{
313308
return ((value & 0x000000FFU) << 24) |
@@ -316,9 +311,6 @@ private static uint SwapUInt32(uint value)
316311
((value & 0xFF000000U) >> 24);
317312
}
318313

319-
/// <summary>
320-
/// Swaps the byte order of a UInt64 value.
321-
/// </summary>
322314
private static ulong SwapUInt64(ulong value)
323315
{
324316
return ((value & 0x00000000000000FFUL) << 56) |

Analyzer/Util/YamlSerializedFileDetector.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,17 @@ namespace UnityDataTools.Analyzer.Util;
88
/// Utility for detecting YAML-format Unity SerializedFiles.
99
///
1010
/// Unity SerializedFiles can be stored in two formats:
11-
/// 1. Binary format (used in builds and by Unity Runtime) - detected by SerializedFileDetector
11+
/// 1. Binary format (produced by builds - read by Unity Runtime, also used for imported artifacts etc) - detected by SerializedFileDetector
1212
/// 2. YAML format (text format used in Editor for .asset, .prefab, .unity files) - detected by this class
1313
///
14-
/// The YAML format is not supported by Unity Runtime and cannot be read by UnityDataTool,
15-
/// which only supports the binary format.
16-
///
1714
/// YAML SerializedFiles begin with the magic string "%YAML 1.1", optionally preceded by
1815
/// a UTF-8 BOM (byte order mark: 0xEF 0xBB 0xBF).
19-
///
20-
/// This implementation is based on Unity's SerializedFile.cpp::FinalizeInitRead() and
21-
/// IsSerializedFileTextFile() from Runtime/Serialize/SerializedFile.cpp (lines 101-117, 668-687).
2216
/// </summary>
2317
public static class YamlSerializedFileDetector
2418
{
25-
// Magic string that identifies YAML-format SerializedFiles
2619
private const string UnityTextMagicString = "%YAML 1.1";
27-
28-
// UTF-8 BOM (Byte Order Mark): EF BB BF
2920
private static readonly byte[] Utf8Bom = new byte[] { 0xEF, 0xBB, 0xBF };
3021

31-
/// <summary>
32-
/// Checks if a file is a YAML-format Unity SerializedFile by reading the magic string
33-
/// at the beginning of the file.
34-
/// </summary>
35-
/// <param name="filePath">Path to the file to check</param>
36-
/// <returns>True if the file starts with "%YAML 1.1" (optionally with UTF-8 BOM), false otherwise</returns>
3722
public static bool IsYamlSerializedFile(string filePath)
3823
{
3924
if (!File.Exists(filePath))
@@ -57,7 +42,6 @@ public static bool IsYamlSerializedFile(string filePath)
5742
if (bytesRead < magicLength)
5843
return false;
5944

60-
// Check if file starts with UTF-8 BOM
6145
int offset = 0;
6246
if (bytesRead >= bomLength && HasUtf8Bom(buffer))
6347
{
@@ -78,9 +62,6 @@ public static bool IsYamlSerializedFile(string filePath)
7862
}
7963
}
8064

81-
/// <summary>
82-
/// Checks if a byte array starts with UTF-8 BOM (0xEF 0xBB 0xBF).
83-
/// </summary>
8465
private static bool HasUtf8Bom(byte[] buffer)
8566
{
8667
if (buffer.Length < Utf8Bom.Length)

0 commit comments

Comments
 (0)