Skip to content

Commit

Permalink
Add object CRC32
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-page committed Oct 25, 2023
1 parent be632b4 commit b12a7f5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
4 changes: 0 additions & 4 deletions Analyzer/AnalyzerTool.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UnityDataTools.Analyzer.SerializedObjects;
using UnityDataTools.Analyzer.SQLite;
using UnityDataTools.FileSystem;
using UnityDataTools.FileSystem.TypeTreeReaders;

namespace UnityDataTools.Analyzer;

Expand Down
3 changes: 2 additions & 1 deletion Analyzer/Resources/Init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CREATE TABLE objects
name TEXT,
game_object INTEGER,
size INTEGER,
crc32 INTEGER,
PRIMARY KEY (id)
);

Expand Down Expand Up @@ -81,7 +82,7 @@ size,
pretty_size,
REPLACE(GROUP_CONCAT(DISTINCT asset_bundle), ',', ',' || CHAR(13)) AS in_bundles
FROM object_view
GROUP BY name, type, size
GROUP BY name, type, size, crc32
HAVING instances > 1
ORDER BY size DESC, instances DESC;

Expand Down
4 changes: 4 additions & 0 deletions Analyzer/SQLite/SQLiteWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private void CreateSQLiteCommands()
m_AddObjectCommand.Parameters.Add("@name", DbType.String);
m_AddObjectCommand.Parameters.Add("@game_object", DbType.Int64);
m_AddObjectCommand.Parameters.Add("@size", DbType.Int64);
m_AddObjectCommand.Parameters.Add("@crc32", DbType.Int32);

m_AddTypeCommand = m_Database.CreateCommand();
m_AddTypeCommand.CommandText = "INSERT INTO types (id, name) VALUES (@id, @name)";
Expand Down Expand Up @@ -193,6 +194,7 @@ public void WriteSerializedFile(string filename, string fullPath)
m_AddObjectCommand.Parameters["@type"].Value = -1;
m_AddObjectCommand.Parameters["@name"].Value = sceneName;
m_AddObjectCommand.Parameters["@size"].Value = 0;
m_AddObjectCommand.Parameters["@crc32"].Value = 0;
m_AddObjectCommand.ExecuteNonQuery();
}
}
Expand Down Expand Up @@ -232,6 +234,7 @@ public void WriteSerializedFile(string filename, string fullPath)

var root = sf.GetTypeTreeRoot(obj.Id);
var offset = obj.Offset;
var crc32 = reader.ComputeCRC(offset, (int)obj.Size);

if (!m_TypeSet.Contains(obj.TypeId))
{
Expand Down Expand Up @@ -275,6 +278,7 @@ public void WriteSerializedFile(string filename, string fullPath)
m_AddObjectCommand.Parameters["@type"].Value = obj.TypeId;
m_AddObjectCommand.Parameters["@name"].Value = name;
m_AddObjectCommand.Parameters["@size"].Value = obj.Size + streamDataSize;
m_AddObjectCommand.Parameters["@crc32"].Value = crc32;
m_AddObjectCommand.ExecuteNonQuery();

// If this is a Scene AssetBundle, add the object as a depencency of the
Expand Down
7 changes: 7 additions & 0 deletions UnityFileSystem/UnityFileReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text;
using Force.Crc32;

namespace UnityDataTools.FileSystem;

Expand Down Expand Up @@ -116,6 +117,12 @@ public byte ReadUInt8(long fileOffset)
return m_Buffer[offset];
}

public uint ComputeCRC(long fileOffset, int size)
{
var offset = GetBufferOffset(fileOffset, size);
return Crc32Algorithm.Compute(m_Buffer, offset, size);
}

public void Dispose()
{
m_File.Dispose();
Expand Down
4 changes: 4 additions & 0 deletions UnityFileSystem/UnityFileSystem.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Crc32.NET" Version="1.2.0" />
</ItemGroup>

</Project>

0 comments on commit b12a7f5

Please sign in to comment.