-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
executable file
·684 lines (520 loc) · 20.2 KB
/
build.py
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#!/usr/bin/env python3
from __future__ import annotations
import hashlib
import json
import logging
import os
import re
import shutil
import subprocess
from datetime import datetime
from threading import Thread
from time import sleep
from typing import Dict, List, Optional, Set, TYPE_CHECKING, Tuple, TypedDict
from urllib.parse import urlparse
from bs4 import BeautifulSoup
if TYPE_CHECKING:
from selenium.webdriver.remote.webdriver import WebDriver
logger = logging.getLogger(__name__)
INDENT = 2
HERE = os.path.realpath(os.path.dirname(__file__))
SRC = os.path.join(HERE, 'src')
BUILD = os.path.join(HERE, 'build')
ICON_SIZE = 512
TEXT = os.path.join(SRC, 'text')
IDOL_DIRNAME = 'idols'
IMG = os.path.join(SRC, 'img')
IDOLS_DIR = os.path.join(IMG, IDOL_DIRNAME)
BOSSES_DIRNAME = 'bosses'
BOSSES_DIR = os.path.join(IMG, BOSSES_DIRNAME)
THUMBS_DIRNAME = 'idol-thumbs'
THUMBS_DIR = os.path.join(BUILD, 'idol-thumbs')
ICON_PATH = os.path.join(BUILD, 'icon.png')
POSES: Set[str] = set()
SKIN_COLOURS: Set[str] = set()
HAIR_COLOURS: Set[str] = set()
LAYERS: List[str] = []
class Part(TypedDict):
path: str
thumbPath: str
medPath: str
bodyType: str
layer: str
number: str
pose: Optional[str]
skinColour: Optional[str]
hairColour: Optional[str]
def part(
path: str, thumb_path: str, med_path: str, bodytype: str, layer: str, colour: str, number: str,
pose: Optional[str] = None,
) -> Part:
LAYERS.append(layer)
skin_colour: Optional[str] = None
hair_colour: Optional[str] = None
if pose is not None:
POSES.add(pose)
if layer in ('hd', 'bd'):
skin_colour = colour
SKIN_COLOURS.add(colour)
elif layer in ('hbe', 'hb', 'hf', 'hfe', 'ah'):
hair_colour = colour
HAIR_COLOURS.add(colour)
return Part(
path=path,
thumbPath=thumb_path,
medPath=med_path,
bodyType=bodytype,
layer=layer,
number=number,
pose=pose,
skinColour=skin_colour,
hairColour=hair_colour,
)
def build_idols(fake_thumbs: bool = False) -> Tuple[Dict[str, List[Part]], List[str], List[str], List[str]]:
if not os.path.isdir(THUMBS_DIR):
os.mkdir(THUMBS_DIR)
parts: Dict[str, List[Part]] = {}
for d in sorted(
os.scandir(IDOLS_DIR),
key=lambda d: d.name,
):
if not d.is_dir():
continue
for entry in sorted(
os.scandir(os.path.join(IDOLS_DIR, d.name)),
key=lambda de: de.name,
):
if entry.name.startswith('.'):
continue
fn = entry.name.replace('.png', '')
basename, ext = os.path.splitext(entry.name)
thumb_name = '{}-thumb{}'.format(basename, ext)
med_name = '{}-med{}'.format(basename, ext)
img_path = os.path.join(IDOLS_DIR, d.name, entry.name)
thumb_path = os.path.join(THUMBS_DIR, thumb_name)
med_path = os.path.join(THUMBS_DIR, med_name)
this_part = part(
'/'.join(['img', IDOL_DIRNAME, d.name, entry.name]),
'/'.join([THUMBS_DIRNAME, thumb_name]),
'/'.join([THUMBS_DIRNAME, med_name]),
*fn.split('_')
)
parts_list = parts.setdefault(this_part['layer'], [])
parts_list.append(this_part)
if not os.path.exists(thumb_path):
if fake_thumbs:
os.symlink(img_path, thumb_path)
else:
subprocess.check_call(['magick', 'convert', img_path, '-resize', '400x400^', thumb_path])
subprocess.check_call(['optipng', thumb_path])
if not os.path.exists(med_path):
if fake_thumbs:
os.symlink(img_path, med_path)
else:
subprocess.check_call(['magick', 'convert', img_path, '-resize', '1000x1000^', med_path])
subprocess.check_call(['optipng', med_path])
return parts, sorted(POSES), sorted(SKIN_COLOURS), sorted(HAIR_COLOURS)
def build_bosses() -> List[str]:
bosses = []
for fn in sorted(
os.listdir(BOSSES_DIR)
):
if fn.startswith('.') or not fn.endswith('.png'):
continue
boss, ext = os.path.splitext(fn)
bosses.append(boss)
return bosses
def build_bios() -> List[str]:
bios = []
with open(os.path.join(TEXT, 'idol bios.txt')) as f:
for bio in f.readlines():
bio = bio.strip()
if bio:
bios.append(bio)
return bios
class Ability(TypedDict):
bonus: int
healing: bool
words: List[str]
def build_abilities() -> List[List[Ability]]:
parts = []
with open(os.path.join(TEXT, 'idol attack names word list.txt')) as f:
for line in f.readlines():
line = line.strip()
if line.startswith('//') or not line:
continue
elif line.startswith('#'):
subparts: List[Ability] = []
parts.append(subparts)
continue
bonus = 0
healing = False
while line[0] in '+-@':
char = line[0]
if char == '+':
bonus += 1
elif char == '-':
bonus -= 1
elif char == '@':
healing = True
line = line[1:]
parts[-1].append({
'bonus': bonus,
'healing': healing,
'words': line.split('|'),
})
return parts
def build_quotes() -> List[str]:
quotes = []
with open(os.path.join(TEXT, 'idol quotes.txt')) as f:
for line in f.readlines():
line = line.strip()
if line:
quotes.append(line)
return quotes
def build_animations() -> List[str]:
return sorted([
f'img/anim/{f}' for f in os.listdir(os.path.join(IMG, 'anim'))
if f.endswith('.png')
])
class ChapterEvent(TypedDict):
kind: str
class Redirect(ChapterEvent):
loop_range: Optional[List[int]]
target: str
class Chapter(TypedDict):
name: str
contents: List[ChapterEvent] # XXX this should be more specific
redirect: Optional[Redirect]
class Setting(ChapterEvent):
value: str
class StageDirection(ChapterEvent):
adjectives: Optional[Dict[str, str]]
actor: Optional[str]
verb: Optional[str]
class Battle(ChapterEvent):
strength: int
bosses: List[str]
class Setpiece(ChapterEvent):
text: str
class Text(ChapterEvent):
text: str
speakers: List[str]
em: bool
def build_campaign() -> Tuple[Dict[str, List[ChapterEvent]], List[str], List[str]]:
chapters = []
last_loop_referenced = 0
with open(os.path.join(TEXT, 'idol campaign.txt')) as f:
for line in (line.strip() for line in f.readlines() if line.strip()):
if line.startswith('# '):
current_chapter = Chapter(
name=line.lstrip('# '),
contents=[],
redirect=None,
)
chapters.append(current_chapter)
elif line.startswith('## '):
current_chapter['contents'].append(Setting(
kind='setting',
value=line.lstrip('# '),
))
elif line.startswith('### '):
content = line[4:]
adjectives: Optional[Dict[str, str]]
actor: Optional[str]
if ' ' in content:
actor, instructions_raw = (
content.split(' ', 1))
verb, adjectives_raw = (
instructions_raw.split(', ', 1))
adjectives = {
k: v for k, v in (
a.split(' ') for a in adjectives_raw.split(', ')
)
}
else:
verb = content
adjectives = None
actor = None
current_chapter['contents'].append(StageDirection(
kind='direction',
verb=verb,
adjectives=adjectives,
actor=actor,
))
elif line.startswith('#### '):
condition, target = line.lstrip('# ').split(': ')
loop_range = None
if condition != 'always':
loop_range = [int(i) for i in condition.split('-')]
if max(loop_range) > last_loop_referenced:
last_loop_referenced = max(loop_range)
if current_chapter['redirect'] is not None:
raise RuntimeError('{name!r} has more than one redirect'
.format(**current_chapter))
current_chapter['redirect'] = Redirect(
kind='redirect',
loop_range=loop_range,
target=target,
)
elif line.startswith('> battle '):
meta, bosses_string = line.split(':', 1)
_, _, strength_string = meta.split(' ', 2)
current_chapter['contents'].append(Battle(
kind='battle',
strength=int(strength_string),
bosses=[
b.strip() for b in bosses_string.split(',')
],
))
elif line.startswith('! '):
current_chapter['contents'].append(Setpiece(
kind='setpiece',
text=line.lstrip('! '),
))
else:
speaker_match = re.match(r'^([\w, ]+):: (.+)$', line)
if speaker_match:
speaker_string, words = speaker_match.groups()
speakers = [sp.strip() for sp in speaker_string.split(',')]
else:
speakers, words = ([], line)
em_match = re.match(r'^/(.+)/$', words)
if em_match:
words, = em_match.groups()
em = True
else:
em = False
current_chapter['contents'].append(Text(
kind='text',
text=words,
speakers=speakers,
em=em,
))
chapter_contents_by_name = {
chapter['name']: chapter['contents'] for chapter in chapters
}
ordered_names = [chapter['name'] for chapter in chapters]
initial_chapter_order: List[str] = []
final_loop_order: List[str] = []
def add_loop_for_index(loop_index: int, chapter_order: List[str]) -> None:
chapter_index = 0
while True:
try:
chapter = chapters[chapter_index]
except IndexError:
break
chapter_order.append(chapter['name'])
redirect = chapter.get('redirect')
if (
redirect is not None and (
(redirect['loop_range'] is None) or (
redirect['loop_range'][0] <=
loop_index <=
redirect['loop_range'][1]
)
)
):
if redirect['target'] == 'restart':
break
target_name = redirect['target'].replace('goto ', '')
chapter_index = ordered_names.index(target_name)
else:
chapter_index += 1
for loop_index in range(last_loop_referenced + 1):
add_loop_for_index(loop_index, initial_chapter_order)
add_loop_for_index(last_loop_referenced + 1, final_loop_order)
# print('\n'.join(initial_chapter_order))
# print('\n -- and then, indefinitely:\n')
# print('\n'.join(final_loop_order))
return chapter_contents_by_name, initial_chapter_order, final_loop_order
def serve_build_dir() -> None:
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
with ThreadingHTTPServer(('localhost', 8000), SimpleHTTPRequestHandler) as httpd:
httpd.serve_forever()
def set_viewport_size(driver: WebDriver, width: int, height: int) -> None:
window_size = driver.execute_script(
'return ['
'window.outerWidth - window.innerWidth + arguments[0],'
'window.outerHeight - window.innerHeight + arguments[1]'
']', width, height)
driver.set_window_size(*window_size)
def build_icon() -> None:
from selenium.webdriver import Chrome, ChromeOptions
server_thread = Thread(target=serve_build_dir, daemon=True)
server_thread.start()
options = ChromeOptions()
options.add_argument('--headless')
driver = Chrome(options=options)
driver.get('http://localhost:8000/build/#icon')
sleep(2)
set_viewport_size(driver, ICON_SIZE, ICON_SIZE)
sleep(2)
driver.save_screenshot(ICON_PATH)
subprocess.check_call(['optipng', ICON_PATH])
def build_unit_names() -> List[List[List[str]]]:
parts: List[List[List[str]]] = []
with open(os.path.join(TEXT, 'idol unit name generator.txt')) as f:
for line in f.readlines():
words = line.strip().split('|')
if words in (['A'], ['B']):
parts.append([])
elif parts and any(words):
parts[-1].append(words)
return parts
class Kana(TypedDict):
kana: str
weight: float
def build_kana() -> List[Kana]:
kana = []
total_frequency = 0
with open(os.path.join(TEXT, 'kana.txt')) as f:
for line in f.readlines():
if line.startswith('//'):
continue
letters, freq_string = line.split()
freq = int(freq_string)
total_frequency += freq
kana.append((letters, freq))
return [
Kana(kana=k, weight=c / total_frequency) for k, c in kana
]
def build_html() -> str:
with open(os.path.join(SRC, 'index-src.html')) as f:
soup = BeautifulSoup(f, 'html5lib')
f.seek(0)
index_checksum = hashlib.sha256()
index_checksum.update(f.read().encode())
with open(os.path.join(BUILD, 'idol-threat.manifest'), 'wt') as mf:
print('CACHE MANIFEST', file=mf)
print('# {}'.format(index_checksum.hexdigest()[:8]), file=mf)
print('fonts/rumraisin.woff', file=mf)
print('vss-logo.svg', file=mf)
for element, attr in [
(e, a)
for a in ('src', 'href')
for e in soup.select('[{}]'.format(a))
]:
url_str = element[attr]
assert isinstance(url_str, str), url_str
parsed = urlparse(url_str)
if (
parsed.netloc or
parsed.path.startswith('/') or
element[attr] == '#'
):
continue
checksum = hashlib.sha256()
try:
with open(os.path.join(BUILD, parsed.path), 'rb') as cf:
checksum.update(cf.read())
except FileNotFoundError:
logger.warning(f'file {parsed.path} referenced in html but not present in build dir')
else:
element[attr] = url_str + '?v={}'.format(checksum.hexdigest()[:8])
print(element[attr], file=mf)
print('NETWORK:\n/', file=mf)
soup.select('body')[0]['data-built-at'] = re.sub(
r'\.\d+$', '', datetime.now().isoformat(),
)
return str(soup)
class GraduationBonus(TypedDict):
bonus: int
template: str
def build_graduation_bonuses() -> List[GraduationBonus]:
graduation_bonuses = []
with open(os.path.join(TEXT, 'idol graduation bonuses.txt')) as f:
for line in f.readlines():
bonus_str, template = line.strip().split(maxsplit=1)
graduation_bonuses.append(GraduationBonus(
bonus=int(bonus_str),
template=template,
))
return graduation_bonuses
def build_barcodes() -> Dict[str, List[str]]:
barcodes: Dict[str, List[str]] = {}
with open(os.path.join(TEXT, 'barcodes.txt')) as f:
for line in f.readlines():
line = line.strip()
if not line:
continue
if line.startswith('# '):
section = line[2:]
else:
codes = barcodes.setdefault(section, [])
codes.append(line)
return barcodes
def write_parts(fake_thumbs: bool = False) -> None:
parts, poses, skin_colours, hair_colours = build_idols(fake_thumbs=fake_thumbs)
with open('build/parts.json', 'w') as j:
j.write(json.dumps({
'parts': parts,
'poses': poses,
'skinColours': skin_colours,
'hairColours': hair_colours,
}, indent=INDENT))
with open('src/parts.ts', 'w') as p:
p.write(
"import {{ ChapterPage }} from './util'\n"
'export const PARTS = {}\nexport const POSES = {}\nexport const SKIN_COLOURS = {}\n'
'export const HAIR_COLOURS = {}\n'
.format(
json.dumps(parts, indent=INDENT),
json.dumps(poses, indent=INDENT),
json.dumps(skin_colours, indent=INDENT),
json.dumps(hair_colours, indent=INDENT),
)
)
p.write(
'export const CHAPTERS: {} = {}\n'
'export const INITIAL_CHAPTER_ORDER = {}\n'
'export const FINAL_LOOP_ORDER = {};'
.format(
'Record<string, ChapterPage[]>',
*(json.dumps(c, indent=INDENT) for c in build_campaign())
)
)
p.write('export const BOSS_NAMES = {}\n'.format(json.dumps(build_bosses(), indent=INDENT)))
p.write('export const BIOS = {}\n'.format(json.dumps(build_bios(), indent=INDENT)))
p.write('export const ABILITIES = {}\n'.format(json.dumps(build_abilities(), indent=INDENT)))
p.write('export const QUOTES = {}\n'.format(json.dumps(build_quotes(), indent=INDENT)))
p.write('export const ANIMATIONS = {}\n'.format(json.dumps(build_animations(), indent=INDENT)))
p.write('export const UNIT_NAMES = {}\n'.format(json.dumps(build_unit_names(), indent=INDENT)))
p.write('export const KANA = {}\n'.format(json.dumps(build_kana(), indent=INDENT)))
p.write('export const BARCODES = {}\n'.format(json.dumps(build_barcodes(), indent=INDENT)))
p.write('export const GRADUATION_BONUSES = {}\n'.format(
json.dumps(build_graduation_bonuses(), indent=INDENT)
))
if __name__ == '__main__':
import sys
if not os.path.isdir(BUILD):
os.mkdir(BUILD)
if '--icon-only' not in sys.argv:
if '--no-parts' not in sys.argv:
write_parts(fake_thumbs='--fake-thumbs' in sys.argv)
with open(os.path.join(BUILD, 'style.css'), 'wb') as c:
c.write(subprocess.check_output(['npx', 'lessc', os.path.join(SRC, 'style.less')]))
try:
subprocess.check_call(['npm', 'run', 'build'])
except subprocess.CalledProcessError:
pass
imgdir = os.path.join(BUILD, 'img')
if os.path.exists(imgdir):
shutil.rmtree(imgdir)
os.mkdir(imgdir)
for subdir in ['anim', 'backgrounds', 'bosses', 'logo']:
shutil.copytree(os.path.join(IMG, subdir), os.path.join(imgdir, subdir))
for fn in ['placeholder.png', 'placeholder-flat.png', 'splash.jpg', 'vss-logo.svg']:
shutil.copy(os.path.join(IMG, fn), os.path.join(imgdir, fn))
os.mkdir(os.path.join(imgdir, 'vendor'))
for fpath in [('sparkle', 'sparkle.png')]:
shutil.copy(os.path.join(SRC, 'vendor', *fpath), os.path.join(imgdir, 'vendor', fpath[-1]))
os.symlink(os.path.join('..', '..', 'src', 'img', 'idols'), os.path.join(imgdir, 'idols'))
for subdir in ['fonts']:
if os.path.exists(os.path.join(BUILD, subdir)):
shutil.rmtree(os.path.join(BUILD, subdir))
shutil.copytree(os.path.join(SRC, subdir), os.path.join(BUILD, subdir))
with open(os.path.join(BUILD, 'index.html'), 'w') as h:
h.write(build_html())
if '--no-icon' not in sys.argv:
build_icon()