|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
4 | | -using System.Runtime.Serialization; |
5 | | -using System.Xml.Linq; |
6 | | -using System.Linq; |
7 | | -using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Text; |
8 | 5 |
|
9 | 6 | namespace Avalonia.Utilities |
10 | 7 | { |
11 | | - #if !BUILDTASK |
| 8 | +#if !BUILDTASK |
12 | 9 | public |
13 | | - #endif |
| 10 | +#endif |
14 | 11 | static class AvaloniaResourcesIndexReaderWriter |
15 | 12 | { |
16 | | - private const int LastKnownVersion = 1; |
17 | | - public static List<AvaloniaResourcesIndexEntry> Read(Stream stream) |
| 13 | + private const int XmlLegacyVersion = 1; |
| 14 | + private const int BinaryCurrentVersion = 2; |
| 15 | + |
| 16 | + public static List<AvaloniaResourcesIndexEntry> ReadIndex(Stream stream) |
18 | 17 | { |
19 | | - var ver = new BinaryReader(stream).ReadInt32(); |
20 | | - if (ver > LastKnownVersion) |
21 | | - throw new Exception("Resources index format version is not known"); |
22 | | - |
23 | | - var assetDoc = XDocument.Load(stream); |
24 | | - XNamespace assetNs = assetDoc.Root!.Attribute("xmlns")!.Value; |
25 | | - List<AvaloniaResourcesIndexEntry> entries = |
26 | | - (from entry in assetDoc.Root.Element(assetNs + "Entries")!.Elements(assetNs + "AvaloniaResourcesIndexEntry") |
27 | | - select new AvaloniaResourcesIndexEntry |
28 | | - { |
29 | | - Path = entry.Element(assetNs + "Path")!.Value, |
30 | | - Offset = int.Parse(entry.Element(assetNs + "Offset")!.Value), |
31 | | - Size = int.Parse(entry.Element(assetNs + "Size")!.Value) |
32 | | - }).ToList(); |
| 18 | + using var reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true); |
33 | 19 |
|
34 | | - return entries; |
| 20 | + var version = reader.ReadInt32(); |
| 21 | + return version switch |
| 22 | + { |
| 23 | + XmlLegacyVersion => ReadXmlIndex(), |
| 24 | + BinaryCurrentVersion => ReadBinaryIndex(reader), |
| 25 | + _ => throw new Exception($"Unknown resources index format version {version}") |
| 26 | + }; |
35 | 27 | } |
36 | 28 |
|
37 | | - [RequiresUnreferencedCode("AvaloniaResources uses Data Contract Serialization, which might require unreferenced code")] |
38 | | - public static void Write(Stream stream, List<AvaloniaResourcesIndexEntry> entries) |
| 29 | + private static List<AvaloniaResourcesIndexEntry> ReadXmlIndex() |
| 30 | + => throw new NotSupportedException("Found legacy resources index format: please recompile your XAML files"); |
| 31 | + |
| 32 | + private static List<AvaloniaResourcesIndexEntry> ReadBinaryIndex(BinaryReader reader) |
39 | 33 | { |
40 | | - new BinaryWriter(stream).Write(LastKnownVersion); |
41 | | - new DataContractSerializer(typeof(AvaloniaResourcesIndex)).WriteObject(stream, |
42 | | - new AvaloniaResourcesIndex() |
43 | | - { |
44 | | - Entries = entries |
| 34 | + var entryCount = reader.ReadInt32(); |
| 35 | + var entries = new List<AvaloniaResourcesIndexEntry>(entryCount); |
| 36 | + |
| 37 | + for (var i = 0; i < entryCount; ++i) |
| 38 | + { |
| 39 | + entries.Add(new AvaloniaResourcesIndexEntry { |
| 40 | + Path = reader.ReadString(), |
| 41 | + Offset = reader.ReadInt32(), |
| 42 | + Size = reader.ReadInt32() |
45 | 43 | }); |
| 44 | + } |
| 45 | + |
| 46 | + return entries; |
46 | 47 | } |
47 | 48 |
|
48 | | - [RequiresUnreferencedCode("AvaloniaResources uses Data Contract Serialization, which might require unreferenced code")] |
49 | | - public static byte[] Create(Dictionary<string, byte[]> data) |
| 49 | + public static void WriteIndex(Stream output, List<AvaloniaResourcesIndexEntry> entries) |
50 | 50 | { |
51 | | - var sources = data.ToList(); |
52 | | - var offsets = new Dictionary<string, int>(); |
53 | | - var coffset = 0; |
54 | | - foreach (var s in sources) |
| 51 | + using var writer = new BinaryWriter(output, Encoding.UTF8, leaveOpen: true); |
| 52 | + |
| 53 | + WriteIndex(writer, entries); |
| 54 | + } |
| 55 | + |
| 56 | + private static void WriteIndex(BinaryWriter writer, List<AvaloniaResourcesIndexEntry> entries) |
| 57 | + { |
| 58 | + writer.Write(BinaryCurrentVersion); |
| 59 | + writer.Write(entries.Count); |
| 60 | + |
| 61 | + foreach (var entry in entries) |
55 | 62 | { |
56 | | - offsets[s.Key] = coffset; |
57 | | - coffset += s.Value.Length; |
| 63 | + writer.Write(entry.Path ?? string.Empty); |
| 64 | + writer.Write(entry.Offset); |
| 65 | + writer.Write(entry.Size); |
58 | 66 | } |
59 | | - var index = sources.Select(s => new AvaloniaResourcesIndexEntry |
60 | | - { |
61 | | - Path = s.Key, |
62 | | - Size = s.Value.Length, |
63 | | - Offset = offsets[s.Key] |
64 | | - }).ToList(); |
65 | | - var output = new MemoryStream(); |
66 | | - var ms = new MemoryStream(); |
67 | | - AvaloniaResourcesIndexReaderWriter.Write(ms, index); |
68 | | - new BinaryWriter(output).Write((int)ms.Length); |
69 | | - ms.Position = 0; |
70 | | - ms.CopyTo(output); |
71 | | - foreach (var s in sources) |
| 67 | + } |
| 68 | + |
| 69 | + public static void WriteResources(Stream output, List<(string Path, int Size, Func<Stream> Open)> resources) |
| 70 | + { |
| 71 | + var entries = new List<AvaloniaResourcesIndexEntry>(resources.Count); |
| 72 | + var offset = 0; |
| 73 | + |
| 74 | + foreach (var resource in resources) |
72 | 75 | { |
73 | | - output.Write(s.Value,0,s.Value.Length); |
| 76 | + entries.Add(new AvaloniaResourcesIndexEntry |
| 77 | + { |
| 78 | + Path = resource.Path, |
| 79 | + Offset = offset, |
| 80 | + Size = resource.Size |
| 81 | + }); |
| 82 | + offset += resource.Size; |
74 | 83 | } |
75 | 84 |
|
76 | | - return output.ToArray(); |
77 | | - } |
78 | | - } |
| 85 | + using var writer = new BinaryWriter(output, Encoding.UTF8, leaveOpen: true); |
| 86 | + writer.Write(0); // index size placeholder, overwritten below |
79 | 87 |
|
80 | | - [DataContract] |
81 | | -#if !BUILDTASK |
82 | | - public |
83 | | -#endif |
84 | | - class AvaloniaResourcesIndex |
85 | | - { |
86 | | - [DataMember] |
87 | | - public List<AvaloniaResourcesIndexEntry> Entries { get; set; } = new List<AvaloniaResourcesIndexEntry>(); |
| 88 | + var posBeforeEntries = output.Position; |
| 89 | + WriteIndex(writer, entries); |
| 90 | + |
| 91 | + var posAfterEntries = output.Position; |
| 92 | + var indexSize = (int) (posAfterEntries - posBeforeEntries); |
| 93 | + output.Position = 0L; |
| 94 | + writer.Write(indexSize); |
| 95 | + output.Position = posAfterEntries; |
| 96 | + |
| 97 | + foreach (var resource in resources) |
| 98 | + { |
| 99 | + using var resourceStream = resource.Open(); |
| 100 | + resourceStream.CopyTo(output); |
| 101 | + } |
| 102 | + } |
88 | 103 | } |
89 | 104 |
|
90 | | - [DataContract] |
91 | 105 | #if !BUILDTASK |
92 | 106 | public |
93 | 107 | #endif |
94 | 108 | class AvaloniaResourcesIndexEntry |
95 | 109 | { |
96 | | - [DataMember] |
97 | 110 | public string? Path { get; set; } |
98 | | - |
99 | | - [DataMember] |
| 111 | + |
100 | 112 | public int Offset { get; set; } |
101 | | - |
102 | | - [DataMember] |
| 113 | + |
103 | 114 | public int Size { get; set; } |
104 | 115 | } |
105 | 116 | } |
0 commit comments