forked from nkast/MonoGame
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSourceFileCollection.cs
220 lines (177 loc) · 7.05 KB
/
SourceFileCollection.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework.Graphics;
namespace Microsoft.Xna.Framework.Content.Pipeline.Builder
{
public sealed class SourceFileCollection
{
public const string Extension = ".kniContent";
public GraphicsProfile Profile { get; set; }
public TargetPlatform Platform { get; set; }
public ContentCompression Compression { get; set; }
public string Config { get; set; }
public List<string> SourceFiles { get; set; }
public List<string> DestFiles { get; set; }
public SourceFileCollection()
{
SourceFiles = new List<string>();
DestFiles = new List<string>();
Config = string.Empty;
}
public void SaveBinary(string filePath)
{
using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
using (var writer = new SourceFileCollectionBinaryWriter(stream))
{
writer.Write(this);
}
}
public static SourceFileCollection LoadBinary(string filePath)
{
try
{
if (!File.Exists(filePath))
return null;
using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
using (var reader = new SourceFileCollectionBinaryReader(stream))
{
SourceFileCollection result = new SourceFileCollection();
reader.Read(result);
return result;
}
}
catch (Exception)
{
return null;
}
}
public int SourceFilesCount { get { return this.SourceFiles.Count; } }
internal void AddFile(string sourceFile, string outputFile)
{
this.SourceFiles.Add(sourceFile);
this.DestFiles.Add(outputFile);
}
public void Merge(SourceFileCollection previousFileCollection)
{
for (int i = 0; i < previousFileCollection.SourceFiles.Count; i++)
{
string prevSourceFile = previousFileCollection.SourceFiles[i];
string prevDestFile = previousFileCollection.DestFiles[i];
bool contains = this.SourceFiles.Exists((sourceFile) => string.Equals(sourceFile, prevSourceFile, StringComparison.InvariantCultureIgnoreCase));
if (!contains)
{
this.SourceFiles.Add(prevSourceFile);
this.DestFiles.Add(prevDestFile);
}
}
}
internal class SourceFileCollectionBinaryWriter : BinaryWriter
{
private const string Header = "KNIC"; // content db
private const short MajorVersion = 3;
private const short MinorVersion = 15;
private const int DataType = 1; // SourceFileCollection data
public SourceFileCollectionBinaryWriter(Stream output) : base(output)
{
}
internal void Write(SourceFileCollection 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
Write((Int32)value.Profile);
Write((Int32)value.Platform);
Write((Int32)value.Compression);
WriteStringOrNull(value.Config);
WritePackedInt(value.SourceFiles.Count);
for (int i = 0; i < value.SourceFiles.Count; i++)
{
WriteStringOrNull(value.SourceFiles[i]);
WriteStringOrNull(value.DestFiles[i]);
}
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 SourceFileCollectionBinaryReader : BinaryReader
{
private const string Header = "KNIC"; // content db
private const short MajorVersion = 3;
private const short MinorVersion = 15;
private const int DataType = 1; // SourceFileCollection data
public SourceFileCollectionBinaryReader(Stream output) : base(output)
{
}
internal void Read(SourceFileCollection 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();
value.Profile = (GraphicsProfile)ReadInt32();
value.Platform = (TargetPlatform)ReadInt32();
value.Compression = (ContentCompression)ReadInt32();
value.Config = ReadStringOrNull();
int filesCount = ReadPackedInt();
value.SourceFiles = new List<string>(filesCount);
value.DestFiles = new List<string>(filesCount);
for (int i = 0; i < filesCount; i++)
{
value.SourceFiles.Add(ReadStringOrNull());
value.DestFiles.Add(ReadStringOrNull());
}
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;
}
}
}
}