Repository navigation
Expand file tree
/
Copy pathCodeExport.cs
More file actions
402 lines (342 loc) · 16.1 KB
/
Copy pathCodeExport.cs
File metadata and controls
402 lines (342 loc) · 16.1 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
MIT License
Copyright (c) 2024 Dmitry Muravyev (youtube.com/@DmitryMuravyev)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Diagnostics;
namespace DeltaComp
{
// The class is dedicated to save compressed data to a header file for a specified platform.
public class CodeExport
{
////////////////////////////////////////////////////////////////////////////////////////////////////
// Private constants/attributes/variables
////////////////////////////////////////////////////////////////////////////////////////////////////
// Some static constants
private static class Constants
{
// The maximum number of elements in a string used to write an array of compressed data
// in the form of a hexadecimal byte values list.
public const int ElementsPerRow = 100;
// Write lock request timeout in case we need to write an array by several threads.
public const int WriterLockTimeout = 1000; // You might wanna change timeout value
// The limit for issuing an array size warning.
public const string DataNodeXPath = "//data[@platform='{0}']";
public const string AllPlatformsIdentifier = "all";
public const string ChunkSizeAttribute = "chunkSize";
public const string LimitStringFormat = "// Limit of {0} bytes reached.";
// Error messages
public const string ExportAlreadyRunning = "The export already started. To start a new one, you need to finish the current one.";
public const string SourceFilesNotFoundError = "The source XML and XSLT files were not found.";
public const string SourceFilesNotSpecified = "The names of the source XML and XSLT files are not specified.";
public const string SourceXmlWrongFormat = "Wrong source XML format.";
public const string ExportNotStarted = "Export not started.";
public const string ENSOrInitializedWOArray = "The export was not started or was initialized without exporting the array.";
}
// Export environment
private StreamWriter? _outputStreamWriter = null;
private bool _exportArrayNeeded = false;
private bool _isFirstElementWritten = false;
private int _elementColumn = 0;
private int _elementIndex = 0;
// We will write an array not from the main thread,
// but there will only be one writing thread, so we do not need a lock.
//private ReaderWriterLock locker = new ReaderWriterLock();
////////////////////////////////////////////////////////////////////////////////////////////////////
// Public attributes/variables
////////////////////////////////////////////////////////////////////////////////////////////////////
// Names or full paths to the XML/XSLT files used to generate .h
public string? XmlFile = null;
public string? XsltFile = null;
public int ChunkSizeWarning = -1;
// We will not return an error message while writing array elements,
// so we will save it in a separate variable to read it later.
public string? LastErrorMessage = null;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Implementation
////////////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
public CodeExport(string xmlFile, string xsltFile)
{
// Init base variables
XmlFile = xmlFile;
XsltFile = xsltFile;
}
// Destructor
~CodeExport()
{
_outputStreamWriter?.Dispose();
_outputStreamWriter = null;
}
// Export preparation: basic checks and writing the top block of the header file.
public bool PrepareExport(bool withArray, Dictionary<string, string> arguments, string platform,
string sourceDataName, string baseName, string outputFile, out string errorMessage)
{
// Check if the export has already been started, but not completed.
if (_outputStreamWriter != null)
{
LastErrorMessage = Constants.ExportAlreadyRunning;
errorMessage = LastErrorMessage;
return false;
}
_exportArrayNeeded = withArray;
bool platformDefined = (platform.Length > 0);
// Check if the platform is specified.
if (platformDefined) {
// Check XML/XSLT file names
if ((XmlFile == null) || (XsltFile == null))
{
LastErrorMessage = Constants.SourceFilesNotSpecified;
errorMessage = LastErrorMessage;
return false;
}
// Try to find the XML/XSLT file paths
if (!CheckSourceFilesExist())
{
if (!TrySourceFilesPath(Environment.CurrentDirectory))
{
string? applicationPath = Path.GetDirectoryName(Environment.ProcessPath);
if (applicationPath == null) applicationPath = AppContext.BaseDirectory;
//Debug.WriteLine($"Physical location {AppDomain.CurrentDomain.BaseDirectory}");
if (!TrySourceFilesPath(applicationPath))
{
LastErrorMessage = Constants.SourceFilesNotFoundError;
errorMessage = LastErrorMessage;
return false;
}
}
}
// Fill basic XSLT arguments
XsltArgumentList xsltArgumentList = new XsltArgumentList();
xsltArgumentList.AddParam("FullName", "", sourceDataName);
xsltArgumentList.AddParam("BaseName", "", baseName);
xsltArgumentList.AddParam("Platform", "", platform);
//if (withArray) xsltArgumentList.AddParam("DataSize", "", arraySize.ToString());
// And add an argument with a key/value list to pass the values of the main parameters.
XElement el = new XElement("root", arguments.Select(kv => new XElement("element",
new XAttribute("key", kv.Key), new XAttribute("value", kv.Value))));
xsltArgumentList.AddParam("Defines", "", el.CreateNavigator());
// Load source XML
XmlDocument sourceXml = new XmlDocument();
try
{
sourceXml.Load(XmlFile);
}
catch (Exception ex)
{
LastErrorMessage = ex.Message;
errorMessage = LastErrorMessage;
return false;
}
// Try to get chunk size warning parameter
XmlElement? dataElement =
sourceXml.SelectSingleNode(String.Format(Constants.DataNodeXPath, platform)) as XmlElement;
if (dataElement == null) dataElement =
sourceXml.SelectSingleNode(String.Format(Constants.DataNodeXPath, Constants.AllPlatformsIdentifier)) as XmlElement;
if (dataElement != null)
{
int.TryParse(dataElement.GetAttribute(Constants.ChunkSizeAttribute), out ChunkSizeWarning);
}
XPathNavigator? sourceXmlNavigator = sourceXml.CreateNavigator();
if (sourceXmlNavigator == null)
{
LastErrorMessage = Constants.SourceXmlWrongFormat;
errorMessage = LastErrorMessage;
return false;
}
// Perform transformation
using (XmlTextWriter headerWriter = new XmlTextWriter(outputFile, null))
{
XslCompiledTransform xslt = new XslCompiledTransform();
try
{
xslt.Load(XsltFile);
xslt.Transform(sourceXmlNavigator, xsltArgumentList, headerWriter);
}
catch (Exception ex)
{
LastErrorMessage = ex.Message;
errorMessage = LastErrorMessage;
// All the "finally" blocks (which is below and which is implemented by the "using" block)
// will be executed despite the "return" statement.
return false;
}
finally
{
if ((headerWriter != null) && (headerWriter.BaseStream != null))
{
headerWriter.Flush();
headerWriter.Close();
}
}
}
}
// Preparing to export an array of elements (if we need it).
if (_exportArrayNeeded) {
_isFirstElementWritten = false;
_elementColumn = 0;
try
{
// Open file for appending
_outputStreamWriter = File.AppendText(outputFile);
if (platformDefined)
{
_outputStreamWriter.WriteLine("{");
_outputStreamWriter.Write("\t");
}
else
{
_outputStreamWriter.Write("{");
}
}
catch (Exception ex)
{
LastErrorMessage = ex.Message;
errorMessage = LastErrorMessage;
// Cleenup if something went wrong
if (_outputStreamWriter != null)
{
if (_outputStreamWriter.BaseStream != null) _outputStreamWriter.Close();
_outputStreamWriter.Dispose();
_outputStreamWriter = null;
}
return false;
}
}
LastErrorMessage = null;
errorMessage = "";
return true;
}
// Write next array element
public bool AppendArrayElement(byte element)
{
if ((_outputStreamWriter != null) && _exportArrayNeeded)
{
try
{
//We don't need no locker, let the motherfucker crash...
//locker.AcquireWriterLock(Constants.WriterLockTimeout);
if (_isFirstElementWritten)
{
_outputStreamWriter.Write(", ");
}
else
{
_isFirstElementWritten = true;
}
// For some platforms (e.g. AVR), we have to split array into several parts < 32K
// (later we will do it automatically, but for now we just place a warning).
if ((ChunkSizeWarning > 0) && (_elementIndex >= ChunkSizeWarning))
{
_outputStreamWriter.WriteLine("");
_outputStreamWriter.WriteLine(String.Format(Constants.LimitStringFormat, ChunkSizeWarning));
_outputStreamWriter.Write("\t");
_elementIndex = 0;
_elementColumn = 0;
}
if (_elementColumn >= Constants.ElementsPerRow)
{
_outputStreamWriter.WriteLine("");
_outputStreamWriter.Write("\t");
_elementColumn = 0;
}
_outputStreamWriter.Write("0x{0:X2}", element);
_elementColumn++;
_elementIndex++;
return true;
}
catch (Exception ex)
{
LastErrorMessage = ex.Message;
return false;
}
// Crash motherfucker, crash...
/*
finally
{
locker.ReleaseWriterLock();
}
*/
}
LastErrorMessage = Constants.ENSOrInitializedWOArray;
return false;
}
// Write the footer to the output file and close it.
public bool FinalizeExport(out string errorMessage)
{
if (_outputStreamWriter != null)
{
try
{
if (_exportArrayNeeded) _outputStreamWriter.WriteLine("};");
_outputStreamWriter.WriteLine("");
_outputStreamWriter.WriteLine("// END-OF-FILE");
_outputStreamWriter.Flush();
_outputStreamWriter.Close();
}
catch (Exception ex)
{
LastErrorMessage = ex.Message;
errorMessage = LastErrorMessage;
return false;
}
finally
{
_outputStreamWriter.Dispose();
_outputStreamWriter = null;
}
LastErrorMessage = null;
errorMessage = "";
return true;
}
LastErrorMessage = Constants.ExportNotStarted;
errorMessage = LastErrorMessage;
return false;
}
// Check if XML and XSLT files exist
private bool CheckSourceFilesExist()
{
if (File.Exists(XmlFile) && File.Exists(XsltFile)) return true;
return false;
}
// Check if XML and XSLT files exist in the specified path
private bool TrySourceFilesPath(string path)
{
if ((XmlFile == null) || (XsltFile == null)) return false;
string xmlFullPath = Path.Combine(path, Path.GetFileName(XmlFile).ToString());
string xsltFullPath = Path.Combine(path, Path.GetFileName(XsltFile));
if (File.Exists(xmlFullPath) && File.Exists(xsltFullPath))
{
XmlFile = xmlFullPath;
XsltFile = xsltFullPath;
return true;
}
return false;
}
}
}
// END-OF-FILE