Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
211 lines (180 loc) · 6.42 KB
/
Copy pathProgram.cs
File metadata and controls
211 lines (180 loc) · 6.42 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
using IFTextCompactor;
string? inputPath = null;
ushort startAddress = 0x1000;
string? outputPath = null;
Charset charset = Charset.Ascii;
var argList = args.ToList();
try
{
for (int i = 0; i < argList.Count; i++)
{
string a = argList[i];
switch (a)
{
case "--start":
i = RequireNext(argList, i, "--start");
startAddress = ParseHexAddress(argList[i]);
break;
case "--out":
case "--output":
i = RequireNext(argList, i, "--out");
outputPath = argList[i];
break;
case "--charset":
i = RequireNext(argList, i, "--charset");
charset = ParseCharset(argList[i]);
break;
case "-h":
case "--help":
PrintUsage();
return 0;
default:
if (a.StartsWith("--", StringComparison.Ordinal))
throw new FormatException($"Unknown option: {a}");
if (inputPath is not null)
throw new FormatException($"Unexpected extra argument: {a}");
inputPath = a;
break;
}
}
}
catch (FormatException ex)
{
Console.Error.WriteLine($"Argument error: {ex.Message}");
Console.Error.WriteLine();
PrintUsage();
return 1;
}
if (inputPath is null)
{
PrintUsage();
return 1;
}
if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"Input file not found: {inputPath}");
return 1;
}
outputPath ??= inputPath + ".asm";
List<Entry> rawEntries;
try
{
rawEntries = InputParser.Parse(inputPath);
}
catch (FormatException ex)
{
Console.Error.WriteLine($"Parse error: {ex.Message}");
return 1;
}
if (rawEntries.Count == 0)
{
Console.Error.WriteLine("No entries found in input file.");
return 1;
}
var entries = rawEntries
.Select(e => new Entry
{
Kind = e.Kind,
Name = TextNormalizer.Normalize(e.Name),
Description = TextNormalizer.Normalize(e.Description)
})
.ToList();
try
{
foreach (var e in entries)
{
TextNormalizer.ValidateAscii(e.Name, $"\"{e.Name}\" (name)");
TextNormalizer.ValidateAscii(e.Description, $"\"{e.Name}\" (description)");
}
}
catch (FormatException ex)
{
Console.Error.WriteLine($"Character set error: {ex.Message}");
return 1;
}
CompressionResult result;
CompressionStats stats;
try
{
result = BpeCompressor.Compress(entries, charset);
stats = CompressionStats.Compute(entries, result);
AsmEmitter.Write(outputPath, startAddress, Path.GetFileName(inputPath), result, stats);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Console.Error.WriteLine($"Output error: {ex.Message}");
return 1;
}
PrintStats(inputPath, outputPath, startAddress, charset, entries, stats);
return 0;
// ------------------------------------------------------------
static int RequireNext(List<string> argList, int i, string optionName)
{
if (i + 1 >= argList.Count)
throw new FormatException($"Option {optionName} requires a value.");
return i + 1;
}
static ushort ParseHexAddress(string s)
{
string t = s.Trim();
if (t.StartsWith("$", StringComparison.Ordinal))
t = t[1..];
else if (t.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
t = t[2..];
if (!ushort.TryParse(t, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out ushort value))
throw new FormatException($"Invalid start address: \"{s}\" (expected hex, e.g. $1000).");
return value;
}
static Charset ParseCharset(string s)
{
return s.Trim().ToUpperInvariant() switch
{
"A" or "ASCII" => Charset.Ascii,
"P" or "PETSCII" => Charset.Petscii,
_ => throw new FormatException($"Invalid charset: \"{s}\" (expected A or P).")
};
}
static void PrintUsage()
{
Console.WriteLine("IF-Text-Compactor - text-adventure string compressor for 6502/VIC-20 targets");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" IFTC <inputfile> [--start $1000] [--out file.asm] [--charset A|P]");
Console.WriteLine();
Console.WriteLine(" <inputfile> Text file with LOC:/OBJ: tagged name/description pairs,");
Console.WriteLine(" one tag+name line followed by one description line,");
Console.WriteLine(" entries separated by blank lines.");
Console.WriteLine(" --start <hex> 16-bit start address, e.g. $1000 or 1000. Default $1000.");
Console.WriteLine(" --out <path> Output .asm path. Default: <inputfile>.asm");
Console.WriteLine(" --charset A|P A = plain ASCII bytes (default).");
Console.WriteLine(" P = VIC-20 PETSCII screen codes.");
}
static void PrintStats(
string inputPath,
string outputPath,
ushort startAddress,
Charset charset,
List<Entry> entries,
CompressionStats stats)
{
long inputFileSize = new FileInfo(inputPath).Length;
int locCount = entries.Count(e => e.Kind == EntryKind.Location);
int objCount = entries.Count(e => e.Kind == EntryKind.Object);
Console.WriteLine();
Console.WriteLine("== IF-Text-Compactor ==");
Console.WriteLine($"Input : {inputPath}");
Console.WriteLine($"Output : {outputPath}");
Console.WriteLine($"Start address : ${startAddress:X4}");
Console.WriteLine($"Charset : {(charset == Charset.Petscii ? "PETSCII screen codes" : "ASCII")}");
Console.WriteLine($"Entries : {locCount} locations, {objCount} objects");
Console.WriteLine($"Dictionary entries : {stats.DictionaryCount} / 128");
Console.WriteLine();
Console.WriteLine($"Input file size : {inputFileSize,8:N0} bytes");
Console.WriteLine($"Uncompressed memory : {stats.UncompressedTotal,8:N0} bytes (strings {stats.UncompressedContent:N0} + index tables {stats.UncompressedIndex:N0})");
Console.WriteLine($"Compressed memory : {stats.CompressedTotal,8:N0} bytes (strings+dict {stats.CompressedContent:N0} + index tables {stats.CompressedIndex:N0})");
if (stats.UncompressedTotal > 0)
{
double savedPct = 100.0 * (stats.UncompressedTotal - stats.CompressedTotal) / stats.UncompressedTotal;
Console.WriteLine($"Saved : {stats.UncompressedTotal - stats.CompressedTotal,8:N0} bytes ({savedPct:0.0}%)");
}
}