forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestData.cs
More file actions
134 lines (111 loc) · 5.53 KB
/
Copy pathTestData.cs
File metadata and controls
134 lines (111 loc) · 5.53 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using NUnit.Framework;
namespace UnityDataTools.FileSystem.Tests
{
public static class TestData
{
public class ExpectedValue
{
public ExpectedValue()
{
}
public ExpectedValue(string key, object value)
{
Key = key;
Value = value;
}
public string Key;
public object Value;
}
static TestData()
{
ExpectedValues = new Dictionary<string, Dictionary<string, object>>();
}
public static Dictionary<string, Dictionary<string, object>> ExpectedValues
{
get;
}
static public IEnumerable<string> GetTestFolders()
{
return Directory.EnumerateDirectories(Path.Combine(TestContext.CurrentContext.TestDirectory, "data"));
}
static public void GenerateTestData()
{
var expectedValues = new List<ExpectedValue>();
foreach (var folder in GetTestFolders())
{
expectedValues.Clear();
UnityFileSystem.Init();
using (var archive = UnityFileSystem.MountArchive(Path.Combine(folder, "AssetBundles", "assetbundle"), "/"))
{
expectedValues.Add(new ExpectedValue("NodeCount", archive.Nodes.Count));
foreach (var n in archive.Nodes)
{
expectedValues.Add(new ExpectedValue(n.Path + "-Size", n.Size));
expectedValues.Add(new ExpectedValue(n.Path + "-Flags", n.Flags));
using (var f = UnityFileSystem.OpenFile("/" + n.Path))
{
var buffer = new Byte[100];
f.Read(100, buffer);
expectedValues.Add(new ExpectedValue(n.Path + "-Data", buffer));
}
if (n.Flags.HasFlag(ArchiveNodeFlags.SerializedFile))
{
using var f = UnityFileSystem.OpenSerializedFile("/" + n.Path);
expectedValues.Add(new ExpectedValue(n.Path + "-ObjCount", f.Objects.Count));
expectedValues.Add(new ExpectedValue(n.Path + "-ExtRefCount", f.ExternalReferences.Count));
int i = 0;
foreach (var extRef in f.ExternalReferences)
{
expectedValues.Add(new ExpectedValue($"{n.Path}-ExtRef{i}-Guid", extRef.Guid));
expectedValues.Add(new ExpectedValue($"{n.Path}-ExtRef{i}-Path", extRef.Path));
expectedValues.Add(new ExpectedValue($"{n.Path}-ExtRef{i}-Type", extRef.Type));
++i;
}
var obj = f.Objects.First();
expectedValues.Add(new ExpectedValue($"{n.Path}-FirstObj-Id", obj.Id));
expectedValues.Add(new ExpectedValue($"{n.Path}-FirstObj-Offset", obj.Offset));
expectedValues.Add(new ExpectedValue($"{n.Path}-FirstObj-Size", obj.Size));
expectedValues.Add(new ExpectedValue($"{n.Path}-FirstObj-TypeId", obj.TypeId));
obj = f.Objects.Last();
expectedValues.Add(new ExpectedValue($"{n.Path}-LastObj-Id", obj.Id));
expectedValues.Add(new ExpectedValue($"{n.Path}-LastObj-Offset", obj.Offset));
expectedValues.Add(new ExpectedValue($"{n.Path}-LastObj-Size", obj.Size));
expectedValues.Add(new ExpectedValue($"{n.Path}-LastObj-TypeId", obj.TypeId));
}
}
}
UnityFileSystem.Cleanup();
var di = new DirectoryInfo(folder);
var unityVersion = di.Name;
var outputPath = Path.Combine(di.Parent.Parent.Parent.Parent.Parent.FullName, "data", unityVersion);
Directory.CreateDirectory(outputPath);
XmlSerializer s = new XmlSerializer(typeof(List<ExpectedValue>));
TextWriter writer = new StreamWriter(Path.Combine(outputPath, "ExpectedValues.xml"));
s.Serialize(writer, expectedValues);
writer.Close();
}
}
public static void LoadTestData()
{
var expectedValues = new List<ExpectedValue>();
foreach (var folder in GetTestFolders())
{
expectedValues.Clear();
XmlSerializer s = new XmlSerializer(typeof(List<ExpectedValue>));
TextReader reader = new StreamReader(Path.Combine(folder, "ExpectedValues.xml"));
expectedValues = (List<ExpectedValue>)s.Deserialize(reader);
reader.Close();
ExpectedValues[folder] = new Dictionary<string, object>();
foreach (var e in expectedValues)
{
ExpectedValues[folder][e.Key] = e.Value;
}
}
}
}
}