forked from AssetRipper/AssetRipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
101 lines (94 loc) · 2.49 KB
/
Program.cs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using AssetRipper.Assets;
using AssetRipper.Assets.Bundles;
using AssetRipper.Assets.Collections;
using AssetRipper.IO.Files;
using AssetRipper.IO.Files.SerializedFiles;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace AssetRipper.Tools.JsonSerializer;
internal static class Program
{
private static readonly string outputDirectory = Path.Combine(AppContext.BaseDirectory, "Output");
static void Main(string[] args)
{
Directory.CreateDirectory(outputDirectory);
if (args.Length == 0)
{
Console.WriteLine("No arguments");
}
else
{
LoadFiles(args);
}
Console.WriteLine("Done!");
Console.ReadKey();
return;
}
private static void LoadFiles(string[] files)
{
foreach (string file in files)
{
LoadFile(file);
}
}
private static void LoadFile(string fullName)
{
Console.WriteLine(fullName);
#if !DEBUG
try
#endif
{
FileBase file = SchemeReader.LoadFile(fullName);
if (file is SerializedFile serializedFile)
{
ExtractJson(serializedFile);
}
else if (file is FileContainer container)
{
file.ReadContents();
foreach (SerializedFile serializedFile1 in container.FetchSerializedFiles())
{
ExtractJson(serializedFile1);
}
}
else
{
Console.WriteLine($"File is {file.GetType()}");
}
}
#if !DEBUG
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
#endif
}
private static void ExtractJson(SerializedFile file)
{
GameBundle bundle = new();
SerializedAssetCollection collection = bundle.AddCollectionFromSerializedFile(file, new JsonAssetFactory());
bundle.InitializeAllDependencyLists();
ExtractJson(file, collection);
}
private static void ExtractJson(SerializedFile file, SerializedAssetCollection collection)
{
JsonArray array = new();
JsonObject root = new()
{
{ "Version", file.Version.ToString() },
{ "Assets", array }
};
foreach ((_, IUnityObjectBase asset) in collection.Assets)
{
JsonObject assetObject = new();
array.Add((JsonNode)assetObject);
assetObject.Add("PathID", asset.PathID);
assetObject.Add("ClassID", asset.ClassID);
assetObject.Add("Fields", ((JsonAsset)asset).Contents);
//Note: this assigns assetObject as the parent of Contents.
//Normally, this would be a cause for concern, but the asset won't be used after this.
}
using FileStream stream = File.Create(Path.Combine(outputDirectory, $"{file.NameFixed}.json"));
System.Text.Json.JsonSerializer.Serialize(stream, root, JsonObjectSerializerContext.Default.JsonObject);
}
}