forked from nkast/MonoGame
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
PackageReferencesCollection.cs
174 lines (143 loc) · 5.38 KB
/
PackageReferencesCollection.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (C)2024 Nick Kastellanos
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
namespace Microsoft.Xna.Framework.Content.Pipeline.Builder
{
public sealed class PackageReferencesCollection
{
public const string Extension = ".kniContent";
public List<Package> Packages { get; set; }
public PackageReferencesCollection()
{
Packages = new List<Package>();
}
public void SaveBinary(string filePath)
{
using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
using (var writer = new PackageReferencesCollectionBinaryWriter(stream))
{
writer.Write(this);
}
}
public static PackageReferencesCollection LoadBinary(string filePath)
{
try
{
using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
using (var writer = new PackageReferencesCollectionBinaryReader(stream))
{
PackageReferencesCollection result = new PackageReferencesCollection();
writer.Read(result);
return result;
}
}
catch (Exception)
{
return null;
}
}
public int PackagesCount { get { return this.Packages.Count; } }
internal void AddPackage(Package package)
{
this.Packages.Add(package);
}
internal class PackageReferencesCollectionBinaryWriter : BinaryWriter
{
private const string Header = "KNIC"; // content db
private const short MajorVersion = 3;
private const short MinorVersion = 15;
private const int DataType = 3; // PackageReferencesCollection data
public PackageReferencesCollectionBinaryWriter(Stream output) : base(output)
{
}
internal void Write(PackageReferencesCollection value)
{
Write((byte)Header[0]);
Write((byte)Header[1]);
Write((byte)Header[2]);
Write((byte)Header[3]);
Write((Int16)MajorVersion);
Write((Int16)MinorVersion);
Write((Int32)DataType);
Write((Int32)0); // reserved
WritePackedInt(value.Packages.Count);
for (int i = 0; i < value.Packages.Count; i++)
{
Write(value.Packages[i].Name);
Write(value.Packages[i].Version);
}
return;
}
protected void WritePackedInt(int value)
{
// write zigzag encoded int
int zzint = ((value << 1) ^ (value >> 31));
Write7BitEncodedInt(zzint);
}
private void WriteStringOrNull(string value)
{
if (value != null)
{
Write(true);
Write(value);
}
else
Write(false);
}
}
internal class PackageReferencesCollectionBinaryReader : BinaryReader
{
private const string Header = "KNIC"; // content db
private const short MajorVersion = 3;
private const short MinorVersion = 15;
private const int DataType = 3; // PackageReferencesCollection data
public PackageReferencesCollectionBinaryReader(Stream output) : base(output)
{
}
internal void Read(PackageReferencesCollection value)
{
if (ReadByte() != Header[0]
|| ReadByte() != Header[1]
|| ReadByte() != Header[2]
|| ReadByte() != Header[3])
throw new Exception("Invalid file.");
if (ReadInt16() != MajorVersion
|| ReadInt16() != MinorVersion)
throw new Exception("Invalid file version.");
int dataType = ReadInt32();
if (dataType != DataType)
throw new Exception("Invalid data type.");
int reserved0 = ReadInt32();
int packagesCount = ReadPackedInt();
value.Packages = new List<Package>(packagesCount);
for (int i = 0; i < packagesCount; i++)
{
Package package;
package.Name = ReadString();
package.Version = ReadString();
value.Packages.Add(package);
}
return;
}
private int ReadPackedInt()
{
unchecked
{
// read zigzag encoded int
int zzint = Read7BitEncodedInt();
return ((int)((uint)zzint >> 1) ^ (-(zzint & 1)));
}
}
private string ReadStringOrNull()
{
if (ReadBoolean())
return ReadString();
else
return null;
}
}
}
}