forked from erkyrath/plotex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathifomatic.py
More file actions
1397 lines (1232 loc) · 47 KB
/
Copy pathifomatic.py
File metadata and controls
1397 lines (1232 loc) · 47 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
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# IF-o-Matic: run IF games, record HTML screenshots.
# Version 0.8
# Andrew Plotkin <erkyrath@eblong.com>
# This script is in the public domain.
# To use:
#
# python3 ifomatic.py GAME
#
# This launches the game and writes the initial display state to
# ifomat-data/games/IFID/screen.html (where IFID is the game's IFID).
#
# You must have the babel command-line tool in your path. You must also
# have an appropriate interpreter compiled with the RemGlk library in
# your path. Currently the script assumes these are called "glulxer" and
# "fizmo-rem", which are idiosyncratic names I use -- sorry.
#
# The --image option will convert each screen.html file to a screen.png
# file. This requires the phantomjs tool (http://phantomjs.org/) in your
# path.
#
# In its current state, this tool renders text correctly but ignores
# graphics.
#
# (This software is not connected to PlotEx; I'm just distributing them
# from the same folder.)
### standardize on open-source fonts
# We use the print() function for Python 2/3 compatibility
from __future__ import print_function
# We use the Py2 unichr() function. In Py3 there is no such function,
# but we define a back-polyfill. (I'm lazy.)
try:
unichr(32)
except NameError:
unichr = chr
# In Py2, we'll need a bit of extra decoding.
py2_readline = False
try:
unicode
py2_readline = True
except:
pass
import sys
import os, os.path
import optparse
import subprocess
import select
import re
import datetime
import json
import xml.dom.minidom
import zipfile
import time
popt = optparse.OptionParser(usage='ifomatic.py [options] files or ifids ...')
popt.add_option('--dir',
action='store', dest='dir',
default='ifomat-data',
help='data directory (default: ifomat-data)')
popt.add_option('--html',
action='store', dest='htmlfile',
default='ifomat-data/template.html',
help='HTML template to use')
popt.add_option('--width',
action='store', type=int, dest='winwidth',
default=800,
help='window width in pixels (default: 800)')
popt.add_option('--height',
action='store', type=int, dest='winheight',
default=600,
help='window height in pixels (default: 600)')
popt.add_option('--zterp',
action='store', dest='zterp',
default='fizmo-rem',
help='RemGlk Z-code interpreter')
popt.add_option('--gterp',
action='store', dest='gterp',
default='glulxer',
help='RemGlk Glulx interpreter')
popt.add_option('--babel',
action='store', dest='babel',
default='babel',
help='Babel tool')
popt.add_option('--blorbtool',
action='store', dest='blorbtool',
default='blorbtool.py',
help='blorbtool.py script')
popt.add_option('--timeout',
dest='timeout_secs', type=float, default=1.0,
help='timeout interval (default: 1.0 sec)')
popt.add_option('--image',
action='store_true', dest='image',
help='write out screen.png file in addition to screen.html')
popt.add_option('--staged',
action='store_true', dest='staged',
help='write out a screen-N.html file for each command input')
popt.add_option('-v', '--verbose',
action='count', dest='verbose', default=0,
help='display the transcripts as they run')
(opts, args) = popt.parse_args()
class Command:
"""Command is one cycle of a RegTest -- a game input, followed by
tests to run on the game's output.
"""
glk_key_names = {
'left':0xfffffffe, 'right':0xfffffffd, 'up':0xfffffffc,
'down':0xfffffffb, 'return':0xfffffffa, 'delete':0xfffffff9,
'escape':0xfffffff8, 'tab':0xfffffff7, 'pageup':0xfffffff6,
'pagedown':0xfffffff5, 'home':0xfffffff4, 'end':0xfffffff3,
'func1':0xffffffef, 'func2':0xffffffee, 'func3':0xffffffed,
'func4':0xffffffec, 'func5':0xffffffeb, 'func6':0xffffffea,
'func7':0xffffffe9, 'func8':0xffffffe8, 'func9':0xffffffe7,
'func10':0xffffffe6, 'func11':0xffffffe5, 'func12':0xffffffe4,
}
def __init__(self, cmd, type='line'):
self.type = type
if self.type == 'line':
self.cmd = cmd
elif self.type == 'char':
self.cmd = None
if len(cmd) == 0:
self.cmd = '\n'
elif len(cmd) == 1:
self.cmd = cmd
elif cmd.lower() in Command.glk_key_names:
self.cmd = cmd.lower()
elif cmd.lower() == 'space':
self.cmd = ' '
elif cmd.lower().startswith('0x'):
self.cmd = unichr(int(cmd[2:], 16))
else:
try:
self.cmd = unichr(int(cmd))
except:
pass
if self.cmd is None:
raise Exception('Unable to interpret char "%s"' % (cmd,))
elif self.type == 'timer':
self.cmd = None
elif self.type == 'hyperlink':
try:
cmd = int(cmd)
except:
pass
self.cmd = cmd
elif self.type == 'refresh':
self.cmd = None
elif self.type == 'arrange':
self.cmd = None
self.width = None
self.height = None
try:
ls = cmd.split()
self.width = int(ls[0])
self.height = int(ls[1])
except:
pass
elif self.type == 'include':
self.cmd = cmd
elif self.type == 'fileref_prompt':
self.cmd = cmd
elif self.type == 'debug':
self.cmd = cmd
else:
raise Exception('Unknown command type: %s' % (type,))
self.checks = []
def __repr__(self):
return '<Command "%s">' % (self.cmd,)
class GlkWindow:
def __init__(self, id, type, rock):
self.id = id
self.type = type
self.rock = rock
self.gridheight = None
self.gridwidth = None
self.gridlines = None
if self.type == 'grid':
self.gridheight = 0
self.gridwidth = 0
self.gridlines = []
self.buflines = None
if self.type == 'buffer':
self.buflines = []
self.graphwidth = None
self.graphheight = None
self.defcolor = None
self.graphcmds = None
if self.type == 'graphics':
self.graphwidth = 0
self.graphheight = 0
self.defcolor = '#FFF'
self.graphcmds = []
self.input = None
self.terminators = {}
self.reqhyperlink = False
self.reqmouse = False
def __repr__(self):
return '<GlkWindow %d (%s, rock=%d)>' % (self.id, self.type, self.rock)
class GlkWindowInput:
def __init__(self, arg):
self.id = arg.get('id')
self.type = arg.get('type')
self.gen = arg.get('gen')
if self.type == 'line':
self.maxlen = arg.get('maxlen', 1)
### initial, terminators
if self.type == 'grid':
pass ### xpos, ypos
class GlkBufferLine:
def __init__(self):
self.ls = []
self.flowbreak = False
def __repr__(self):
return repr(self.ls)
def append(self, val):
self.ls.append(val)
class GlkSpecialSpan:
@staticmethod
def classforalignment(val):
if val == 'inlineup':
return 'ImageInlineUp'
if val == 'inlinedown':
return 'ImageInlineDown'
if val == 'inlinecenter':
return 'ImageInlineCenter'
if val == 'marginleft':
return 'ImageMarginLeft'
if val == 'marginright':
return 'ImageMarginRight'
return 'ImageInlineUp'
def __init__(self, arg, type=None):
if type is None:
type = arg['special']
self.type = type
if type == 'image':
self.image = int(arg['image'])
self.url = arg.get('url')
self.alignment = arg.get('alignment')
self.alttext = arg.get('alttext')
self.x = None
self.y = None
self.width = None
self.height = None
val = arg.get('x')
if val is not None:
self.x = int(val)
val = arg.get('y')
if val is not None:
self.y = int(val)
val = arg.get('width')
if val is not None:
self.width = int(val)
val = arg.get('height')
if val is not None:
self.height = int(val)
if type == 'fill':
self.color = arg.get('color')
self.x = None
self.y = None
self.width = None
self.height = None
val = arg.get('x')
if val is not None:
self.x = int(val)
val = arg.get('y')
if val is not None:
self.y = int(val)
val = arg.get('width')
if val is not None:
self.width = int(val)
val = arg.get('height')
if val is not None:
self.height = int(val)
def __repr__(self):
return '<GlkSpecialSpan %s>' % (self.type,)
class ResourceMap:
def __init__(self, dir):
self.dir = dir
self.map = {}
if not dir:
return
mappath = os.path.join(dir, 'resourcemap.json')
if not os.path.exists(mappath):
return
fl = open(mappath)
rmap = json.load(fl)
fl.close()
for key, desc in rmap.items():
match = re.match('pict-([0-9]+)', key)
if match:
key = int(match.group(1))
self.map[key] = GlkSpecialSpan(desc, type='image')
def get(self, num):
return self.map.get(num)
class GameState:
"""The GameState class wraps the connection to the interpreter subprocess
(the pipe in and out streams). It's responsible for sending commands
to the interpreter, and receiving the game output back.
Currently this class is set up to manage exactly one each of story,
status, and graphics windows. (A missing window is treated as blank.)
This is not very general -- we should understand the notion of multiple
windows -- but it's adequate for now.
This is a virtual base class. Subclasses should customize the
initialize, perform_input, and accept_output methods.
"""
def __init__(self, infile, outfile, tracefile=None):
self.infile = infile
self.outfile = outfile
self.tracefile = tracefile
def initialize(self):
pass
def perform_input(self, cmd):
raise Exception('perform_input not implemented')
def accept_output(self):
raise Exception('accept_output not implemented')
class GameStateRemGlk(GameState):
"""Wrapper for a RemGlk-based interpreter. This can in theory handle
any I/O supported by Glk. But the current implementation is limited
to line and char input, and no more than one status (grid) and one
graphics window. Multiple story (buffer) windows are accepted, but
their output for a given turn is agglomerated.
"""
@staticmethod
def extract_text(line):
# Extract the text from a line object, ignoring styles.
con = line.get('content')
if not con:
return ''
dat = [ val.get('text', '') for val in con ]
return ''.join(dat)
@staticmethod
def extract_raw(line):
# Extract the content array from a line object.
con = line.get('content')
if not con:
return []
return con
def initialize(self, blorbdir):
self.resourcemap = ResourceMap(blorbdir)
self.winwidth = opts.winwidth
self.winheight = opts.winheight
update = { 'type':'init', 'gen':0,
'metrics': self.create_metrics(),
'support': [ 'timer', 'hyperlinks', 'graphics', 'graphicswin' ],
}
cmd = json.dumps(update)
self.infile.write((cmd+'\n').encode())
self.infile.flush()
self.generation = 0
self.windowdic = {}
def create_metrics(self):
res = {
'width':self.winwidth, 'height':self.winheight,
'gridcharwidth':8.5, 'gridcharheight':16,
'buffercharwidth':7, 'buffercharheight':16,
'gridmarginx':19, 'gridmarginy':12,
'buffermarginx':35, 'buffermarginy':12,
}
return res
def perform_input(self, cmd):
if cmd.type == 'line':
ls = [ winid for (winid, win) in self.windowdic.items()
if win.input and win.input.type == 'line' ]
if not ls:
raise Exception('No window is awaiting line input')
update = { 'type':'line', 'gen':self.generation,
'window':min(ls), 'value':cmd.cmd
}
elif cmd.type == 'char':
ls = [ winid for (winid, win) in self.windowdic.items()
if win.input and win.input.type == 'char' ]
if not ls:
raise Exception('No window is awaiting char input')
val = cmd.cmd
if val == '\n':
val = 'return'
update = { 'type':'char', 'gen':self.generation,
'window':min(ls), 'value':val
}
elif cmd.type == 'hyperlink':
update = { 'type':'hyperlink', 'gen':self.generation,
'window':'###', 'value':cmd.cmd
}
elif cmd.type == 'timer':
update = { 'type':'timer', 'gen':self.generation }
elif cmd.type == 'arrange':
self.winwidth = cmd.width
self.winheight = cmd.height
update = { 'type':'arrange', 'gen':self.generation,
'metrics': self.create_metrics()
}
elif cmd.type == 'refresh':
update = { 'type':'refresh', 'gen':0 }
elif cmd.type == 'fileref_prompt':
if self.specialinput != 'fileref_prompt': ###?
raise Exception('Game is not expecting a fileref_prompt')
update = { 'type':'specialresponse', 'gen':self.generation,
'response':'fileref_prompt', 'value':cmd.cmd
}
elif cmd.type == 'debug':
update = { 'type':'debuginput', 'gen':self.generation,
'value':cmd.cmd
}
else:
raise Exception('Command type not recognized: %s' % (cmd.type))
if opts.verbose >= 2:
ObjPrint.pprint(update)
print()
if self.tracefile:
json.dump(update, self.tracefile, indent=2, sort_keys=True)
self.tracefile.write('\n\n')
cmd = json.dumps(update)
self.infile.write((cmd+'\n').encode())
self.infile.flush()
def accept_output(self):
output = bytearray()
update = None
timeout_time = time.time() + opts.timeout_secs
# Read until a complete JSON object comes through the pipe (or
# we time out).
# We sneakily rely on the fact that RemGlk always uses dicts
# as the JSON object, so it always ends with "}".
while (select.select([self.outfile],[],[],opts.timeout_secs)[0] != []):
ch = self.outfile.read(1)
if ch == b'':
# End of stream. Hopefully we have a valid object.
dat = output.decode('utf-8')
update = json.loads(dat)
break
output += ch
if (output[-1] == ord('}')):
# Test and see if we have a valid object.
dat = output.decode('utf-8')
try:
update = json.loads(dat)
break
except:
pass
if time.time() >= timeout_time:
raise Exception('Timed out awaiting output')
# Parse the update object. This is complicated. For the format,
# see http://eblong.com/zarf/glk/glkote/docs.html
if opts.verbose >= 2:
ObjPrint.pprint(update)
print()
if self.tracefile:
json.dump(update, self.tracefile, indent=2, sort_keys=True)
self.tracefile.write('\n\n')
self.generation = update.get('gen')
inputs = update.get('input')
self.accept_inputcancel(inputs)
windows = update.get('windows')
if windows is not None:
# Handle all the window changes. The argument lists all windows
# that should be open. Any unlisted windows, therefore, get
# closed.
# (If the update has no windows entry, we make no window changes.)
for win in self.windowdic.values():
win.inplace = False
for win in windows:
self.accept_one_window(win)
closewins = [ win for win in self.windowdic.values() if not win.inplace ]
for win in closewins:
del self.windowdic[win.id]
contents = update.get('content')
if contents is not None:
for content in contents:
self.accept_one_content(content)
self.accept_inputset(inputs)
###specialinputs = update.get('specialinput')
###timer = update.get('timer')
def accept_inputcancel(self, arg):
if arg is None:
return
hasinput = {}
for argi in arg:
if argi.get('type'):
hasinput[argi['id']] = argi
for (winid, win) in self.windowdic.items():
if win.input:
argi = hasinput.get('winid')
if (argi is None) or (argi['gen'] > win.input.gen):
# cancel this input.
win.input = None
def accept_inputset(self, arg):
if arg is None:
return
hasinput = {}
hashyperlink = {}
hasmouse = {}
for argi in arg:
id = argi['id']
if argi.get('type'):
hasinput[id] = argi
if argi.get('hyperlink'):
hashyperlink[id] = True
if argi.get('mouse'):
hasmouse[id] = True
for (winid, win) in self.windowdic.items():
win.reqhyperlink = hashyperlink.get(winid)
win.reqmouse = hasmouse.get(winid)
argi = hasinput.get(winid)
if argi is None:
continue
win.input = GlkWindowInput(argi)
### initial, terminators
def accept_one_window(self, arg):
argid = arg['id']
win = self.windowdic.get(argid)
if win is None:
# The window must be created.
win = GlkWindow(argid, arg['type'], arg['rock'])
self.windowdic[argid] = win
win.inplace = True
win.posleft = int(arg['left'])
win.postop = int(arg['top'])
win.poswidth = int(arg['width'])
win.posheight = int(arg['height'])
if win.type == 'grid':
# Make sure we have the correct number of lines.
argheight = arg['gridheight']
argwidth = arg['gridwidth']
if argheight > win.gridheight:
for ix in range(win.gridheight, argheight):
win.gridlines.append([])
if argheight < win.gridheight:
del win.gridlines[ argheight : ]
win.gridheight = argheight
win.gridwidth = argwidth
if win.type == 'graphics':
argheight = arg['graphheight']
argwidth = arg['graphwidth']
win.graphheight = argheight
win.graphwidth = argwidth
def accept_one_content(self, arg):
id = arg.get('id')
win = self.windowdic.get(id)
if not win:
raise Exception('No such window')
if win.input and win.input.type == 'line':
raise Exception('Window is awaiting line input.')
if win.type == 'grid':
# Modify the given lines of the grid window
for (ix, linearg) in enumerate(arg['lines']):
linenum = linearg['line']
linels = win.gridlines[linenum]
linels.clear()
content = linearg.get('content')
if content:
sx = 0
while sx < len(content):
rdesc = content[sx]
sx += 1
if type(rdesc) is dict:
if rdesc.get('special') is not None:
continue
rstyle = rdesc['style']
rtext = rdesc['text']
rlink = rdesc.get('hyperlink')
else:
rstyle = rdesc
rtext = content[sx]
sx += 1
rlink = None
el = (rstyle, rtext, rlink)
linels.append(el)
#print('###', win, win.gridlines)
if win.type == 'buffer':
# Append the given lines onto the end of the buffer window
text = arg.get('text', [])
if arg.get('clear'):
win.buflines.clear()
# Each line we receive has a flag indicating whether it *starts*
# a new paragraph. (If the flag is false, the line gets appended
# to the previous paragraph.)
for textarg in text:
content = textarg.get('content')
linels = None
if textarg.get('append'):
if content is None or not len(content):
continue
if len(win.buflines):
linels = win.buflines[-1]
if linels is None:
linels = GlkBufferLine()
win.buflines.append(linels)
if textarg.get('flowbreak'):
linels.flowbreak = True
if content is None or not len(content):
continue
sx = 0
while sx < len(content):
rdesc = content[sx]
sx += 1
if type(rdesc) is dict:
if rdesc.get('special') is not None:
el = GlkSpecialSpan(rdesc)
linels.append(el)
continue
rstyle = rdesc['style']
rtext = rdesc['text']
rlink = rdesc.get('hyperlink')
else:
rstyle = rdesc
rtext = content[sx]
sx += 1
rlink = None
el = (rstyle, rtext, rlink)
linels.append(el)
### trim the scrollback
#print('###', win, win.buflines)
if win.type == 'graphics':
draw = arg.get('draw', [])
for op in draw:
optype = op['special']
if optype == 'setcolor':
win.defcolor = op['color']
elif optype == 'fill':
# Both color and geometry are optional here.
# We make sure all that is specified in the graphcmds
# entry.
el = GlkSpecialSpan(op)
if el.x is None:
### clear the graphcmds list?
el.x = 0
el.y = 0
el.width = win.graphwidth
el.height = win.graphheight
if el.color is None:
el.color = win.defcolor
win.graphcmds.append(el)
elif optype == 'image':
el = GlkSpecialSpan(op)
win.graphcmds.append(el)
class ObjPrint:
NoneType = type(None)
try:
UnicodeType = unicode
except:
UnicodeType = str
@staticmethod
def pprint(obj):
printer = ObjPrint()
printer.printval(obj, depth=0)
print(''.join(printer.arr))
def __init__(self):
self.arr = []
@staticmethod
def valislong(val):
typ = type(val)
if typ is ObjPrint.NoneType:
return False
elif typ is bool or typ is int or typ is float:
return False
elif typ is str or typ is ObjPrint.UnicodeType:
return (len(val) > 16)
elif typ is list or typ is dict:
return (len(val) > 0)
else:
return True
def printval(self, val, depth=0):
typ = type(val)
if typ is ObjPrint.NoneType:
self.arr.append('None')
elif typ is bool or typ is int or typ is float:
self.arr.append(str(val))
elif typ is str:
self.arr.append(repr(val))
elif typ is ObjPrint.UnicodeType:
st = repr(val)
if st.startswith('u'):
st = st[1:]
self.arr.append(st)
elif typ is list:
if len(val) == 0:
self.arr.append('[]')
else:
anylong = False
for subval in val:
if ObjPrint.valislong(subval):
anylong = True
break
self.arr.append('[')
if anylong:
self.arr.append('\n')
first = True
for subval in val:
if first:
if anylong:
self.arr.append((depth+1)*' ')
else:
if anylong:
self.arr.append(',\n')
self.arr.append((depth+1)*' ')
else:
self.arr.append(', ')
self.printval(subval, depth+1)
first = False
if anylong:
self.arr.append('\n')
self.arr.append(depth*' ')
self.arr.append(']')
elif typ is dict:
if len(val) == 0:
self.arr.append('{}')
else:
anylong = False
for subval in val.values():
if ObjPrint.valislong(subval):
anylong = True
break
self.arr.append('{')
if anylong:
self.arr.append('\n')
first = True
keyls = sorted(val.keys())
for subkey in keyls:
subval = val[subkey]
if first:
if anylong:
self.arr.append((depth+1)*' ')
else:
if anylong:
self.arr.append(',\n')
self.arr.append((depth+1)*' ')
else:
self.arr.append(', ')
self.printval(subkey, depth+1)
self.arr.append(':')
self.printval(subval, depth+1)
first = False
if anylong:
self.arr.append('\n')
self.arr.append(depth*' ')
self.arr.append('}')
else:
raise Exception('unknown type: %r' % (val,))
def append_to_file(path, ln):
if not os.path.exists(path):
fl = open(path, 'w')
else:
fl = open(path, 'a')
fl.write(ln + '\n')
fl.close()
def escape_json(val):
res = ['"']
for ch in val:
if ch == '"' or ch == '\\':
res.append('\\' + ch)
else:
och = ord(ch)
if och < 128:
res.append(chr(och))
else:
res.append('\\u%04x' % (och,))
res.append('"')
return ''.join(res)
def escape_html(val, quotes=False):
"""Apply &-escapes to render arbitrary strings in ASCII-clean HTML.
This is miserably inefficient -- I'm sure there's a built-in Python
function which does it, but I haven't looked it up.
"""
res = []
for ch in val:
if ch == '&':
res.append('&')
elif ch == '>':
res.append('>')
elif ch == '<':
res.append('<')
elif quotes and ch == '"':
res.append('"')
else:
och = ord(ch)
if och < 128:
res.append(chr(och))
else:
res.append('&#'+str(och)+';')
return ''.join(res)
def write_contents(ifid, gamefile, metadata, dirpath):
fl = open(os.path.join(dirpath, 'contents'), 'w')
fl.write('IFID: %s\n' % (ifid,))
fl.write('file: %s\n' % (os.path.abspath(gamefile),))
fl.write('created: %s\n' % (datetime.datetime.now(),))
if 'title' in metadata:
fl.write('title: %s\n' % (metadata['title'],))
fl.close()
def write_html_window(win, state, fl):
"""Write the contents of one Glk window in screen.html.
"""
morestyles = ''
if win.type == 'grid':
cssclass = 'GridWindow'
elif win.type == 'buffer':
cssclass = 'BufferWindow'
elif win.type == 'graphics':
cssclass = 'GraphicsWindow'
morestyles = ' background-color: %s;' % (win.defcolor,)
else:
cssclass = 'UnknownWindow'
posright = state.winwidth - (win.posleft + win.poswidth)
posbottom = state.winheight - (win.postop + win.posheight)
fl.write('<div id="window%d" class="WindowFrame %s WindowRock_%d" style="left: %dpx; top: %dpx; right: %dpx; bottom: %dpx;%s">\n' % (win.id, cssclass, win.rock, win.posleft, win.postop, posright, posbottom, morestyles))
if win.type == 'grid':
for line in win.gridlines:
fl.write('<div class="GridLine">')
for span in line:
(rstyle, rtext, rlink) = span
fl.write('<span class="Style_%s">%s</span>' % (rstyle, escape_html(rtext)))
fl.write('</div>\n')
if win.type == 'buffer':
for line in win.buflines:
cla = 'BufferLine'
if line.flowbreak:
cla = cla + ' FlowBreak'
fl.write('<div class="%s">' % (cla,))
for span in line.ls:
if isinstance(span, GlkSpecialSpan):
if span.type == 'image':
image = state.resourcemap.get(span.image)
if image:
srcval = '%s/%s' % ('blorbdata', image.url,)
srcval = escape_html(srcval, quotes=True)
classval = GlkSpecialSpan.classforalignment(span.alignment)
altval = span.alttext
if not altval:
altval = image.alttext
if not altval:
altval = 'Image %d' % (span.image,)
altval = escape_html(altval, quotes=True)
fl.write('<img src="%s" class="%s" alt="%s" width="%d" height="%d">' % (srcval, classval, altval, span.width, span.height,))
continue
(rstyle, rtext, rlink) = span
fl.write('<span class="Style_%s">%s</span>' % (rstyle, escape_html(rtext)))
if not line.ls:
fl.write(' ');
fl.write('</div>\n')
if win.type == 'graphics':
fl.write('<div class="Canvas" style="width: %dpx; height: %dpx;">' % (win.graphwidth, win.graphheight,))
for op in win.graphcmds:
if op.type == 'fill':
fl.write('<div class="FillRect" style="left: %dpx; top: %dpx; width: %dpx; height: %dpx; background-color: %s;"></div>\n' % (op.x, op.y, op.width, op.height, op.color,))
if op.type == 'image':
image = state.resourcemap.get(op.image)
srcval = '%s/%s' % ('blorbdata', image.url,)
srcval = escape_html(srcval, quotes=True)
altval = op.alttext
if not altval:
altval = image.alttext
if not altval:
altval = 'Image %d' % (op.image,)
altval = escape_html(altval, quotes=True)
width = op.width
height = op.height
if width is None:
width = image.width
if height is None:
height = image.height
fl.write('<img src="%s" alt="%s" width="%d" height="%d" style="left: %dpx; top: %dpx;">' % (srcval, altval, width, height, op.x, op.y))
fl.write('</div>\n')
fl.write('</div>\n')
def write_html(ifid, gamefile, metadata, state, dirpath, fileindex=None):
"""Write out the screen.html file in the game directory. We could
also be writing a screen-N.html intermediate file.
If the --image option is given, we then convert the HTML file to
a PNG file.
"""
if (fileindex is not None) and (not opts.staged):
# If this is an intermediate file and the option for that isn't
# set, skip this file.
return
window_title = metadata.get('title', 'Game Screenshot')
filename = 'screen.html'
if fileindex is not None:
filename = 'screen-%d.html' % (fileindex,)
fl = open(os.path.join(dirpath, filename), 'w')
for ln in htmllines:
if ln == '$WINDOWPORT$':
fl.write('<div id="windowport">\n')
winls = list(state.windowdic.values())
winls.sort(key=lambda win: win.id)
for win in winls:
write_html_window(win, state, fl)
fl.write('</div>\n')
elif '$' in ln:
ln = ln.replace('$TITLE$', window_title)
ln = ln.replace('$WINWIDTH$', str(state.winwidth))
ln = ln.replace('$WINHEIGHT$', str(state.winheight))
fl.write(ln)