-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-credits.go
584 lines (511 loc) · 14.9 KB
/
generate-credits.go
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package main
import (
//"bytes"
"encoding/csv"
"flag"
"fmt"
"os"
"strings"
)
const CLEAR_TILE_ID = "$20"
var excludeNames = []string{}
var verbose bool
type DataChunk interface {
AsmString(num int) string
String() string
AttributeValue() uint
}
type OP_CODE int
const (
CR_OP_EOC OP_CODE = iota
CR_OP_CLEAR_ROW // Clear 32 tiles
CR_OP_INC_BYTE // Increment byte N times given the start value
CR_OP_RLE // Run Length Encoded
CR_OP_BYTE_LIST // List of bytes, NULL terminated
CR_OP_ATTR // One bite value for the row, as well as End of Data
CR_OP_NAME
CR_OP_EOD
)
type GenericChunk struct {
Comment string
OpCodes []*GenericData
Attribute uint
}
func (c *GenericChunk) AsmString(num int) string {
length := 0
codestrings := []string{}
for _, code := range c.OpCodes {
codestrings = append(codestrings, code.AsmString())
length += code.Length
}
if length > 64 {
panic("Generic code chunk length greater than 64")
}
comment := ""
if len(c.Comment) > 0 {
comment = "; " + c.Comment + "\n"
}
return fmt.Sprintf("%scr_data_chunk_%02d:\n %s\n", comment, num, strings.Join(codestrings, "\n "))
}
func (c GenericChunk) AttributeValue() uint {
return c.Attribute
}
func (c *GenericChunk) String() string {
return "idk, lol"
}
type GenericData struct {
OpCode OP_CODE
Length int
Data []byte
}
// will hold the twitch logo and header and things
func NewGenericData(code OP_CODE, length int, data []byte) *GenericData {
gc := &GenericData{OpCode: code}
switch code {
case CR_OP_INC_BYTE:
if length == 0 || len(data) == 0 {
panic("Missing length or data for CR_OP_INC_BYTE")
}
if len(data) > 1 {
panic("Too much data for CR_OP_INC_BYTE")
}
gc.Length = length
gc.Data = data
case CR_OP_RLE:
if length == 0 || len(data) == 0 {
panic("Missing length or data for CR_OP_RLE")
}
if len(data) > 1 {
panic("Too much data for CR_OP_RLE")
}
gc.Length = length
gc.Data = data
case CR_OP_BYTE_LIST:
if len(data) == 0 {
panic("Missing data for CR_OP_BYTE_LIST")
}
gc.Length = len(data)
gc.Data = data
case CR_OP_ATTR:
if len(data) == 0 {
panic("Missing data for CR_OP_ATTR")
}
if len(data) > 1 {
panic("Too much data for CR_OP_ATTR")
}
gc.Data = data
}
return gc
}
func (c *GenericData) AsmString() string {
switch c.OpCode {
case CR_OP_EOD:
return ".byte CR_OP_EOD"
case CR_OP_EOC:
return ".byte CR_OP_EOC"
case CR_OP_CLEAR_ROW:
return ".byte CR_OP_CLEAR_ROW"
case CR_OP_INC_BYTE:
return fmt.Sprintf(".byte CR_OP_INC_BYTE, %d, $%02X", c.Length, c.Data[0])
case CR_OP_RLE:
return fmt.Sprintf(".byte CR_OP_RLE, %d, $%02X", c.Length, c.Data[0])
case CR_OP_BYTE_LIST:
bytes := []string{}
for _, b := range c.Data {
bytes = append(bytes, fmt.Sprintf("$%02X", b))
}
return fmt.Sprintf(".byte CR_OP_BYTE_LIST, %s, $00", strings.Join(bytes, ", "))
case CR_OP_ATTR:
return fmt.Sprintf(".byte CR_OP_ATTR, $%02X", c.Data[0])
}
panic(fmt.Sprintf("Invalid OP Code: %d", c.OpCode))
}
type Subscriber struct {
Username string
Tier int
}
func NewSub(row []string) (*Subscriber, error) {
if len(row) < 3 {
return nil, fmt.Errorf("Invalid row: %q", row)
}
sub := &Subscriber{
Username: row[0],
Tier: 1,
}
switch string(row[2][5]) {
case "2":
sub.Tier = 2
case "3":
sub.Tier = 3
}
return sub, nil
}
const asmTemplate = "cr_data_chunk_%02d: .byte CR_OP_NAME, $%02X, %q" // prefix, suffix, attribute, name
func (s *Subscriber) AsmString(num int) string {
length := len(s.Username)
half := int(length / 2)
offset := 16 - half
trailing := 32 - (offset + length)
chunkLength := length + offset + trailing
if chunkLength != 32 {
panic(fmt.Sprintf("Chunklength is not 64 bytes (%d)! %q length:%d offset:%d trailing:%d half:%d", chunkLength, s.Username, length, offset, trailing, half))
}
attr_len := (s.Tier-1)<<uint(6) | len(s.Username)
//attr = s.AttributeValue()
//attr = attr << uint(2) | s.AttributeValue()
//tier_attr = tier_attr << uint(2) | s.Tier - 1
//return fmt.Sprintf(asmTemplate, num, offset, trailing, tier_attr, s.Username)
return fmt.Sprintf(asmTemplate, num, attr_len, s.Username)
}
func (s Subscriber) AttributeValue() uint {
return uint(s.Tier - 1)
}
func (s *Subscriber) String() string {
return fmt.Sprintf("%s: Tier %d", s.Username, s.Tier)
}
func main() {
var inputFile string
var inputFileRequired string
var outputName string
var dummyNames bool
var exclude string
flag.StringVar(&inputFile, "i", "", "Input csv file. Dummy names used if nonexistent.")
flag.StringVar(&inputFileRequired, "I", "", "Required input csv file. Error if nonexistent.")
flag.StringVar(&outputName, "o", "credits_data.i", "Output assembly file.")
flag.BoolVar(&dummyNames, "dummy", false, "Use dummy names instead of an input csv file.")
flag.BoolVar(&verbose, "verbose", false, "Verbose output.")
flag.StringVar(&exclude, "x", "", "A comma separated list of names to exclude.")
flag.Parse()
if len(exclude) > 0 {
excludeNames = strings.Split(exclude, ",")
}
if len(inputFileRequired) > 0 {
if len(inputFile) > 0 && inputFileRequired != inputFile {
fmt.Println("ERROR: Cannot provide both an optional and required input file!")
os.Exit(1)
} else if dummyNames {
fmt.Println("ERROR: Cannot provide both a required input file and dummy names option!")
os.Exit(1)
} else {
inputFile = inputFileRequired
}
}
subList := []DataChunk{}
if !exists(inputFile) && !dummyNames {
if len(inputFileRequired) > 0 && inputFile == inputFileRequired {
fmt.Printf("ERROR: The required input file %q does not exist!\n", inputFileRequired)
os.Exit(1)
}
if len(inputFile) > 0 {
fmt.Printf("WARNING: %q does not exist! Using dummy names for credits!\n", inputFile)
} else {
fmt.Println("WARNING: No input file given, using dummy names for credits!")
}
dummyNames = true
}
if !dummyNames {
file, err := os.Open(inputFile)
if err != nil {
fmt.Println("ERROR: Unable to open subscriber-list.csv: ", err)
os.Exit(1)
}
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
fmt.Println("ERROR: Unable to read subscriber-list.csv: ", err)
os.Exit(1)
}
for _, row := range records[1:] {
exclude := false
for _, ex := range excludeNames {
if ex == strings.ToLower(row[0]) {
exclude = true
}
}
if !exclude {
sub, err := NewSub(row)
if err != nil {
fmt.Println("WARNING: Error parsing subscriber: ", err)
} else {
subList = append(subList, sub)
}
}
}
} else {
if verbose {
fmt.Println("Using dummy names")
}
subList = []DataChunk{
&Subscriber{"01 Connie Klein", 1},
&Subscriber{"02 Stephanie Blake", 1},
&Subscriber{"03 Rosie Burgess", 1},
&Subscriber{"04 Ida Harmon", 1},
&Subscriber{"05 Emmett Murray", 1},
&Subscriber{"06 Paul Lowe", 1},
&Subscriber{"07 Christina Clayton", 1},
&Subscriber{"08 Lucille Scott", 1},
&Subscriber{"09 Jennifer Carpenter", 1},
&Subscriber{"10 Doyle Ryan", 1},
&Subscriber{"11 Ricky Robbins", 1},
&Subscriber{"12 Tyler Hammond", 1},
&Subscriber{"13 Rene Palmer", 1},
&Subscriber{"14 Damon Hopkins", 1},
&Subscriber{"15 Sandra Willis", 2},
&Subscriber{"16 Pete Russell", 1},
&Subscriber{"17 Dean Waters", 1},
&Subscriber{"18 Anne Cook", 1},
&Subscriber{"19 Seth Coleman", 1},
&Subscriber{"20 Ellis Walton", 1},
&Subscriber{"21 Doris Cooper", 3},
&Subscriber{"22 Vicky Parker", 1},
&Subscriber{"23 Ernestine Larson", 1},
&Subscriber{"24 Edna Jefferson", 1},
&Subscriber{"25 Judy Garner", 1},
&Subscriber{"26 Leon Barker", 3},
&Subscriber{"27 Claire Rogers", 2},
&Subscriber{"28 Priscilla Caldwell", 3},
&Subscriber{"29 Lillian Carpenter", 1},
&Subscriber{"30 Justin Graves", 1},
&Subscriber{"31 Katrina Walton", 1},
&Subscriber{"32 Cody Ross", 1},
&Subscriber{"33 Nettie Curry", 1},
&Subscriber{"34 Benny Lewis", 1},
&Subscriber{"35 Lindsey Sullivan", 1},
&Subscriber{"36 Lionel Banks", 1},
&Subscriber{"37 Katie Casey", 1},
&Subscriber{"38 Erika Cook", 1},
&Subscriber{"39 Wanda Klein", 1},
&Subscriber{"40 Dewey Rice", 1},
&Subscriber{"41 Jermaine Harrison", 1},
&Subscriber{"42 Mandy Jensen", 1},
&Subscriber{"43 Abraham West", 1},
&Subscriber{"44 Perry Williamson", 1},
&Subscriber{"45 Lindsay Francis", 1},
}
}
headerChunks := []DataChunk{
// a blank row (two tile rows)
&GenericChunk{
Comment: "Blank row before special thanks",
Attribute: uint(0),
OpCodes: []*GenericData{
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
},
},
// top half of header
&GenericChunk{
Comment: "Top half of header",
Attribute: uint(0),
OpCodes: []*GenericData{
// Padding
NewGenericData(CR_OP_RLE, 7, []byte(" ")),
// Twitch logo row one
NewGenericData(CR_OP_INC_BYTE, 3, []byte{0x10}),
// "Twitch.tv/"
NewGenericData(CR_OP_INC_BYTE, 7, []byte{0x02}),
// padding
NewGenericData(CR_OP_RLE, 22, []byte(" ")),
// Twitch logo row two
NewGenericData(CR_OP_INC_BYTE, 3, []byte{0x13}),
// top of "Zorchenhimer"
NewGenericData(CR_OP_INC_BYTE, 15, []byte{0x80}),
// padding
NewGenericData(CR_OP_RLE, 7, []byte(" ")),
// attribute
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
},
},
// bottom half of header
&GenericChunk{
Comment: "Bottom half of header",
Attribute: uint(0),
OpCodes: []*GenericData{
// padding
NewGenericData(CR_OP_RLE, 7, []byte(" ")),
// Twitch logo row three
NewGenericData(CR_OP_INC_BYTE, 3, []byte{0x16}),
// bottom of "Zorchenhimer"
NewGenericData(CR_OP_INC_BYTE, 15, []byte{0x90}),
// padding
NewGenericData(CR_OP_RLE, 7, []byte(" ")),
// blank row
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
// attribute
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
},
},
&GenericChunk{
Comment: "Blank row before Miha's credit",
Attribute: uint(0),
OpCodes: []*GenericData{
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
},
},
// Special thanks
&GenericChunk{
Comment: "Special thanks for MihaBrumecArt",
Attribute: uint(0),
OpCodes: []*GenericData{
// First Row
NewGenericData(CR_OP_RLE, 8, []byte(" ")),
NewGenericData(CR_OP_INC_BYTE, 16, []byte{0xA0}),
NewGenericData(CR_OP_RLE, 16, []byte(" ")),
// Second row
NewGenericData(CR_OP_INC_BYTE, 16, []byte{0xB0}),
NewGenericData(CR_OP_RLE, 8, []byte(" ")),
// attribute
NewGenericData(CR_OP_ATTR, 0, []byte{0x0F}),
},
},
&GenericChunk{
Comment: "Special thanks for MihaBrumecArt",
Attribute: uint(0),
OpCodes: []*GenericData{
// First Row
NewGenericData(CR_OP_RLE, 8, []byte(" ")),
NewGenericData(CR_OP_INC_BYTE, 16, []byte{0xC0}),
NewGenericData(CR_OP_RLE, 16, []byte(" ")),
// Second row
NewGenericData(CR_OP_INC_BYTE, 16, []byte{0xD0}),
NewGenericData(CR_OP_RLE, 8, []byte(" ")),
//NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
// attribute
NewGenericData(CR_OP_ATTR, 0, []byte{0xF}),
},
},
&GenericChunk{
Comment: "Blank row before music credit",
Attribute: uint(0),
OpCodes: []*GenericData{
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
},
},
// Special thanks for music
&GenericChunk{
Comment: "Special thanks for music",
Attribute: uint(0),
OpCodes: []*GenericData{
// First Row
NewGenericData(CR_OP_RLE, 13, []byte(" ")),
NewGenericData(CR_OP_INC_BYTE, 7, []byte{0x09}),
NewGenericData(CR_OP_RLE, 21, []byte(" ")),
// Second row
NewGenericData(CR_OP_INC_BYTE, 14, []byte{0xE1}),
NewGenericData(CR_OP_RLE, 9, []byte(" ")),
// attribute
NewGenericData(CR_OP_ATTR, 0, []byte{0xF0}),
},
},
&GenericChunk{
Comment: "Special thanks for music, pt2",
Attribute: uint(0),
OpCodes: []*GenericData{
NewGenericData(CR_OP_RLE, 9, []byte(" ")),
NewGenericData(CR_OP_INC_BYTE, 14, []byte{0xF1}),
NewGenericData(CR_OP_RLE, 9, []byte(" ")),
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_ATTR, 0, []byte{0xF0}),
},
},
&GenericChunk{
Comment: "Blank row before names",
Attribute: uint(0),
OpCodes: []*GenericData{
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
},
},
}
footerChunks := []DataChunk{
&GenericChunk{
Comment: "Bottom padding for Attribute",
Attribute: uint(0),
OpCodes: []*GenericData{
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
// attribute
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
},
},
&Subscriber{"Thank you!!", 1},
&GenericChunk{
Comment: "Bottom padding for Attribute",
Attribute: uint(0),
OpCodes: []*GenericData{
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
// attribute
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
},
},
&GenericChunk{
Comment: "Bottom padding for Attribute",
Attribute: uint(0),
OpCodes: []*GenericData{
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
NewGenericData(CR_OP_CLEAR_ROW, 0, nil),
// attribute
NewGenericData(CR_OP_ATTR, 0, []byte{0x0}),
NewGenericData(CR_OP_EOD, 0, []byte{0x0}),
},
},
}
allChunks := append(headerChunks, subList...)
allChunks = append(allChunks, footerChunks...)
outFile, err := os.Create(outputName)
if err != nil {
fmt.Println("ERROR: Unable to create credits_data.i: ", err)
os.Exit(1)
}
fmt.Fprintln(outFile, "credits_data_chunks:")
//for i, _ := range allChunks {
// fmt.Fprintf(outFile, " .word cr_data_chunk_%02d\n", i)
//}
//fmt.Fprintln(outFile, "credits_data_chunks_end:\n")
count := 0
//attr := uint(0)
for i, s := range allChunks {
// calculate the attribute byte
//attr = s.AttributeValue()
//attr = attr << uint(2) | s.AttributeValue()
if verbose {
fmt.Printf(" %02d %s\n", i, s.String())
}
fmt.Fprintln(outFile, s.AsmString(i))
//if i % 2 == 0 {
// fmt.Fprintf(outFile, " .byte CR_OP_EOD\n\n")
// if verbose { fmt.Println("no attribute byte") }
//} else {
// Swap the lower and upper bits of the attribute byte
//attrA := attr & 0xF0
//attrB := attr & 0x0F
//attrA = attrA >> uint(4)
//attrB = attrB << uint(4)
//attr = attrA | attrB
// Print the attribute OP code
//fmt.Fprintf(outFile, " .byte CR_OP_ATTR, $%02X\n\n", attr)
//if verbose { fmt.Printf("Attribute byte: %02X\n", attr) }
//}
count += 1
}
fmt.Printf("Chunks in credits: %d\n", count)
}
// exists returns whether the given file or directory exists or not.
// Taken from https://stackoverflow.com/a/10510783
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}