-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
170 lines (148 loc) · 6.12 KB
/
Program.cs
File metadata and controls
170 lines (148 loc) · 6.12 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
using ICSharpCode.SharpZipLib.GZip;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
namespace WebContentToCode
{
enum Encoding
{
none,
gzip
}
partial struct Config
{
public Encoding Encoding = Encoding.none;
public List<string> FileExtensions = [];
public string outputFileName = "webFiles.h";
public bool usePROGMEM = false;
public bool allowRecursiveProcessing = false;
public bool usePragmaOnce = true;
[GeneratedRegex("[-.]")]
public static partial Regex caseUnifyingRegex();
public const string caseUnifier = "_";
public bool unifyCase = false;
public Config(string[] args)
{
string currentOption = string.Empty;
foreach (string arg in args)
{
if (arg.StartsWith('-'))
{
currentOption = arg;
//Flags (must use continue)
switch (currentOption)
{
case "-progmem":
usePROGMEM = true;
break;
case "-r":
allowRecursiveProcessing = true;
break;
case "-noPragma":
usePragmaOnce = false;
break;
case "-uc":
unifyCase = true;
break;
case "-f":
case "-e":
case "-o":
break;
default:
throw new("Unknown option: " + arg);
}
continue;
}
//Option parameters
switch (currentOption)
{
case "-f":
FileExtensions.Add(arg);
break;
case "-e":
if (Enum.TryParse(arg.ToLower(), out Encoding encoding))
{
Encoding = encoding;
currentOption = string.Empty; //Reset current option (single arg)
break;
}
throw new("Unknown encoding: " + arg);
case "-o":
outputFileName = arg;
currentOption = string.Empty; //Reset current option (single arg)
break;
default:
throw new("Unknown parameter: " + arg);
}
}
}
}
internal class Program
{
private static Config config;
private static byte[] GZipCompress(byte[] inputBytes)
{
using Stream memOutput = new MemoryStream();
using GZipOutputStream zipOut = new(memOutput);
zipOut.Write(inputBytes);
zipOut.Flush();
zipOut.Finish();
byte[] bytes = new byte[memOutput.Length];
memOutput.Seek(0, SeekOrigin.Begin);
memOutput.Read(bytes, 0, bytes.Length);
return bytes;
}
static void Main(string[] args)
{
config = new(args);
List<(string, byte[])> convertedFiles = [];
//Metrics
Stopwatch sw = Stopwatch.StartNew();
int originalByteCount = 0;
string currentDir = Directory.GetCurrentDirectory();
foreach (string file in Directory.EnumerateFiles(currentDir, "*.*", SearchOption.AllDirectories))
{
if (config.allowRecursiveProcessing || Path.GetFileName(file) != config.outputFileName
&& (config.FileExtensions.Count == 0 || config.FileExtensions.Any(file.EndsWith)))
{
Console.WriteLine("Found file: " + file);
byte[] bytes = File.ReadAllBytes(file);
originalByteCount += bytes.Length;
switch (config.Encoding)
{
case Encoding.gzip:
convertedFiles.Add((file, GZipCompress(bytes)));
break;
default:
convertedFiles.Add((Path.GetFileName(file), bytes));
break;
}
}
}
int compressedByteCount = convertedFiles.Sum(x => x.Item2.Length);
float compressionPercent = MathF.Round(compressedByteCount / (float)originalByteCount * 100, 2);
Console.WriteLine($"Writing {compressedByteCount}/{originalByteCount} ({compressionPercent}%) bytes to \"{config.outputFileName}\"...");
//Write to output file (this is so readable)
using (StreamWriter outFile = new(config.outputFileName))
{
if (config.usePragmaOnce)
{
outFile.WriteLine("#pragma once\n");
}
outFile.WriteLine("//This file was generated with WCTC. Do not change.\n");
foreach (var file in convertedFiles)
{
string fileNameNoExtension = Path.GetFileNameWithoutExtension(file.Item1);
string fileName = config.unifyCase ? Config.caseUnifyingRegex().Replace(fileNameNoExtension, Config.caseUnifier) : fileNameNoExtension;
outFile.Write($"const uint8_t {fileName}_{Path.GetExtension(file.Item1)[1..]}[] {(config.usePROGMEM ? "PROGMEM " : string.Empty)}= {{ ");
for (int i = 0; i < file.Item2.Length; i++)
{
outFile.Write("0x" + file.Item2[i].ToString("X2").ToLower() + (i != file.Item2.Length - 1 ? "," : string.Empty));
}
outFile.WriteLine(" };\n");
}
}
Console.WriteLine($"Finished in {sw.Elapsed.TotalSeconds} seconds");
}
}
}