-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathAddressablesBuildLayoutParser.cs
More file actions
75 lines (67 loc) · 2.45 KB
/
AddressablesBuildLayoutParser.cs
File metadata and controls
75 lines (67 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.IO;
using Microsoft.Data.Sqlite;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityDataTools.Analyzer.SQLite.Handlers;
using UnityDataTools.Analyzer.SQLite.Parsers.Models;
using UnityDataTools.Analyzer.SQLite.Writers;
namespace UnityDataTools.Analyzer.SQLite.Parsers
{
public class AddressablesBuildLayoutParser : ISQLiteFileParser
{
private AddressablesBuildLayoutSQLWriter m_Writer;
public bool Verbose { get; set; }
public bool SkipReferences { get; set; }
public void Dispose()
{
m_Writer.Dispose();
}
public void Init(SqliteConnection db)
{
m_Writer = new AddressablesBuildLayoutSQLWriter(db);
m_Writer.Verbose = Verbose;
}
public bool CanParse(string filename)
{
if (Path.GetExtension(filename) != ".json")
return false;
// Read enough content to check if it contains BuildResultHash
// This handles both minified and pretty-printed JSON
try
{
using (StreamReader reader = new StreamReader(filename))
{
// Read first 4KB which should be enough to find BuildResultHash near the start
char[] buffer = new char[4096];
int charsRead = reader.Read(buffer, 0, buffer.Length);
string content = new string(buffer, 0, charsRead);
// Check if BuildResultHash appears in the content
if (content.Contains("\"BuildResultHash\""))
{
return true;
}
}
}
catch (Exception e)
{
if (Verbose)
{
Console.Error.WriteLine($"Error reading JSON file {filename}: {e.Message}");
}
}
return false;
}
public void Parse(string filename)
{
// only init our writer if we are actually parsing a file
m_Writer.Init();
using (StreamReader reader = File.OpenText(filename))
{
JsonSerializer serializer = new JsonSerializer();
BuildLayout buildLayout = (BuildLayout)serializer.Deserialize(reader, typeof(BuildLayout));
m_Writer.WriteAddressablesBuild(filename, buildLayout);
}
}
}
}