-
Notifications
You must be signed in to change notification settings - Fork 4
/
sleepmask.rb
executable file
·469 lines (411 loc) · 12.8 KB
/
sleepmask.rb
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
#!/usr/bin/env ruby
# vim:set fileencoding=UTF-8:
# Sleepmask
# A dumb terminal wrapper for RemGlk
# Copyright (c) 2012 Justin de Vesine
# Released under the MIT license - See README.md
require 'rubygems'
require 'yajl'
require 'yajl/json_gem'
require 'eventmachine'
require 'optparse'
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
STDOUT.sync = true
execpath = File.dirname(File.realdirpath(File.absolute_path(__FILE__)))
# Sleepmask talks to RemGlk-compiled interpreters. Although it would be most
# useful to allow an external configuration file for these, given the highly
# restricted target audience, this has not been a significant hardship.
$interpreters = {
:glulxe => File.join(execpath, '..', 'glulxe-dev-rem', 'glulxe'),
:bocfel => File.join(execpath, '..', 'bocfel-0.6.2', 'bocfel'),
:git => File.join(execpath, '..', 'git-131', 'git'),
:nitfol => File.join(execpath, '..', 'nitfol-0.5-rem', 'remnitfol'),
:cheapglulxe => File.join(execpath, '..', 'git-131', 'git'),
# :cheapglulxe => File.join(execpath, '..', 'glulxe-dev-rem', 'glulxe'),
:olddebugcheapnitfol => File.join(execpath, '..', 'nitfol-0.5-rem', 'remnitfol') + " -i -no-spell",
:fizmo => File.join(execpath, '..', 'fizmo-rem', 'fizmo-glktermw', 'fizmo-glktermw'),
:fizmodev => File.join(execpath, '..', 'fizmo-rem-dev', 'fizmo-glktermw', 'fizmo-glktermw'),
:debugcheapnitfol => File.join(execpath, '..', 'fizmo-rem', 'fizmo-glktermw', 'fizmo-glktermw'),
#:debugcheapnitfol => File.join(execpath, '..', 'bocfel-0.6.2', 'bocfel'),
:cheaphe => File.join(execpath, '..', 'hugo-rem', 'glk', 'heglk'),
#:cheaptads => File.join(execpath, '..', 'floyd-tads-rem', 'build', 'linux.release', 'tads', 'tadsr'),
#:cheaptads => File.join(execpath, '..', 'garglk-read-only', 'build', 'linux.release', 'tads', 'tadsr'),
:cheaptads => File.join(execpath, '..', 'frobtads-git', 'frobglk') + " -d . -sd . ",
:debugcheaptads => File.join(execpath, '..', 'floyd-tads-rem', 'build', 'linux.debug', 'tads', 'tadsr'),
:frobglk => File.join(execpath, '..', 'frobtads-git', 'frobglk') + " -d . -sd . "
}
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: sleepmask.rb [options] [gamefile]"
options[:debug] = false
options[:verbose] = false
options[:width] = 70
options[:height] = 24
options[:interpreter] = :git
opts.on('-i', '--interpreter TERP', 'Interpreter') do |terp|
if $interpreters.has_key? terp.to_sym
options[:interpreter] = terp.to_sym
else
puts "Error: bad interpreter"
puts "Possible terps are:"
$interpreters.each do |name, val|
puts " #{name}"
end
exit
end
end
opts.on('-w', '--width WIDTH', 'Width of "screen"') do |width|
options[:width] = width.to_i
end
opts.on('-r', '--height HEIGHT', 'Height of "screen"') do |height|
options[:height] = height.to_i
end
opts.on('-v', '--verbose', 'Verbose output') do
options[:verbose] = true
end
opts.on('-d', '--debug', 'Debugging output (even more verbose)') do
options[:debug] = true
end
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit
end
end
optparse.parse!
$options = options
$debug = options[:debug]
if options[:debug]
options[:verbose] = true
end
if ARGV.empty?
puts "%% No gamefile specified, exiting."
exit
end
gamefile = ARGV[0]
gamepath = File.expand_path(gamefile)
if !File.exists? gamepath
puts "%% No gamefile found: #{gamefile}"
exit
end
# Helper to wrap text by words. Does not rewrap text already wrapped to a
# smaller window size.
def word_wrap(text, line_width = nil)
width = line_width.nil? ? $options[:width] : line_width
text.split("\n").collect do |line|
begin
# convert nonbreaking spaces to normal spaces
line.gsub!(/\xc2\xa0/u, ' ')
rescue
end
line.length > width ? line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip : line
end * "\n"
end
class RemHandler < EM::Connection
attr_reader :inputqueue
attr_reader :remqueue
def initialize(inputq, remq)
@inputqueue = inputq
@remqueue = remq
@gen = nil
@windows = {}
@inputs = []
# Use two stacked queues for input to handle an inconsistency in EM queue
# timing - otherwise, under some circumstances, an input could either be
# lost (if it happened too quickly) or improperly sent when the game state
# did not expect it.
@queueinput = Proc.new do |data|
@remqueue.push data
@inputqueue.pop &@queueinput
end
@inputqueue.pop &@queueinput
# Handler for finding the first appropriate input field referenced in the
# game state message and sending back a queued command (or special command,
# as in game saves)
@sendinput = Proc.new do |action|
input_id = nil
gen = nil
msgtype = "line"
msgdetail = nil
msg = action[:msg]
@inputs.each do |input|
if input[:type] == "line"
input_id = input[:id]
gen = input[:gen]
break
elsif input[:type] == "char"
msgtype = "char"
input_id = input[:id]
gen = input[:gen]
break
elsif input[:type] == "fileref_prompt"
msgtype = "specialresponse"
msgdetail = input[:type]
gen = input[:gen]
end
end
end
if !input_id.nil?
if msgtype == "char"
if msg.empty?
value = "return"
elsif msg[0] == "/"
parts = msg.split(' ')
if parts.count == 1
value = msg.slice(1)
else
value = parts[1]
end
if value == "space"
value = " "
end
else
value = msg[0]
end
else
value = msg
end
message = {
:type => msgtype,
:gen => gen.to_i,
:window => input_id.to_i,
:value => value.to_s
}
puts "%% Sending: #{message.to_json}" if $debug
send_data(message.to_json)
elsif msgdetail == "fileref_prompt"
message = {
:type => msgtype,
:gen => gen.to_i,
:response => "fileref_prompt",
:value => msg.to_s
}
puts "%% Sending special response: #{message.to_json}" if $debug
send_data(message.to_json)
else
puts "%% Couldn't send: #{msg}"
end
#send_data(msg)
end
end
def post_init
# Start up the streaming JSON parser
@parser = Yajl::Parser.new(:symbolize_keys => true)
@parser.on_parse_complete = method(:object_parsed)
# Send the initial handshake message to the game
init = {
:type => "init",
:gen => 0,
:metrics => {
:width => $options[:width],
:height => $options[:height]
}
}
send_data(init.to_json)
end
# Convert a raw "run" (a Glk text segment with a given format) to a
# Sleepmask formatted string (using pseudo-html styles, among others)
def run_to_s(run, opts = {})
options = {
:solo => false
}
options.update(opts)
close = false
line = run[:text]
sopen = ""
sclose = ""
case run[:style]
when "emphasized"
run[:style] = "em"
when "user1"
run[:style] = "u1"
when "user2"
run[:style] = "u2"
end
case run[:style]
when "header"
if options[:solo]
sopen = "# "
sclose = ""
else
sopen = "<b>"
sclose = "</b>"
end
when "subheader"
if options[:solo]
sopen = "## "
sclose = ""
else
sopen = "<i>"
sclose = "</i>"
end
when "alert"
if options[:solo]
sopen = "** "
sclose = ""
else
sopen = "<strong>"
sopen = "</strong>"
end
when "input"
sopen = "\n> "
sclose = "\n"
line = line.upcase
when "normal"
sopen = ""
sclose = ""
when "preformatted"
sopen = ""
sclose = ""
else
sopen = "<#{run[:style]}>"
sclose = "</#{run[:style]}>"
end
s = "#{sopen}#{line}#{sclose}"
#s = word_wrap(run[:text]).split("\n").collect do |l|
#"#{sopen}#{l}#{sclose}"
#end * "\n"
return s
end
# When a complete parsed JSON object (one game state change, from the
# game) is read, handle it; this includes sending user input back
# if the game state change allows it.
def object_parsed(obj)
if obj[:type] == "pass"
puts "%% Passing." if $debug
return
end
if obj.has_key? :gen
@gen = obj[:gen]
end
if obj[:type] == "error"
puts "%% Critical error."
puts "%% Message: #{obj[:message]}"
return
end
if obj[:type] != "update"
puts "%% Unknown event type: #{obj[:type]}"
puts obj.inspect
return
end
# Handle creation of "windows" (content areas, including status bars)
if obj.has_key? :windows
obj[:windows].each do |window|
puts "%% Window: #{window.inspect}" if $debug
@windows[window[:id]] = window
if window[:type] == "grid"
@windows[window[:id]][:grid] = [" " * window[:width]] * window[:height]
else
@windows[window[:id]][:buffer] = []
end
end
end
# If the game state allows an input to be sent at this point, send the next
# available command
# remglk <0.2.1 compat
if obj.has_key?(:inputs)
obj[:input] = obj[:inputs]
end
if obj.has_key?(:input) && !obj.has_key?(:specialinput)
@inputs = obj[:input]
puts "Inputs: #{@inputs.inspect}" if $debug
@remqueue.pop &@sendinput
end
# remglk <0.2.1 compat
if obj.has_key?(:contents)
obj[:content] = obj[:contents]
end
if obj.has_key? :content
obj[:content].each do |content|
puts "%% Content: #{content.inspect}" if $debug
# It is a validly crashy error if a game state object comes in with
# content for a window that does not exist.
window = @windows[content[:id]]
if window[:type] == "grid"
content[:lines].each do |line|
window[:grid][line[:line]] = ""
line[:content].each do |run|
window[:grid][line[:line]] += run_to_s(run)
end
end
puts "%% Grid window #{content[:id]}:" if $options[:verbose]
puts "] " + window[:grid].join("\n] ")
puts "%% ---" if $options[:verbose]
else
if content.has_key? :clear and content[:clear]
window[:buffer] = []
puts "%% clearing window #{content[:id]}" if $options[:verbose]
end
if content.has_key? :text and !content[:text].empty?
content[:text].each do |text|
s = ""
if text.has_key? :content and !text[:content].empty?
if text[:content].length == 1
s = run_to_s(text[:content][0], {:solo => true})
else
text[:content].each do |run|
s += run_to_s(run)
end
end
end
if text.has_key? :append and text[:append]
window[:buffer] << "" if window[:buffer].last.nil?
window[:buffer][-1] += s
else
window[:buffer] << s
end
end
end
window[:buffer] = word_wrap(window[:buffer].join("\n")).split("\n")
puts window[:buffer].join("\n")
window[:buffer] = [window[:buffer].last]
end
end
end
# If the game state allows a special input to be sent at this point (usually a
# save/restore filename), handle it here
if obj.has_key? :specialinput
@inputs = [obj[:specialinput]]
@inputs[0][:gen] = @gen
puts "Special inputs: #{@inputs.inspect}" if $debug
puts "%% Enter a #{obj[:specialinput][:filetype]} filename to #{obj[:specialinput][:filemode]}:"
@remqueue.pop &@sendinput
end
#ap obj
end
# Pass incoming data (from the game) to the streaming JSON parser
def receive_data data
puts "%% Parser got: #{data}" if $options[:debug]
@parser << data
end
def unbind
puts "#{$options[:interpreter]} quit with exit status: #{get_status.exitstatus}"
EM.stop
end
end
class KeyboardHandler < EM::Connection
include EM::Protocols::LineText2
attr_reader :queue
def initialize(q)
@queue = q
end
def receive_line(data)
puts "Keyboard got: '#{data}'" if $debug
if data == "/quit"
puts "%% Quitting!"
EM.stop
end
@queue.push({:msg => data})
end
def unbind
EM.stop
end
end
EM.run{
inputq = EM::Queue.new
remq = EM::Queue.new
puts "#{$interpreters[$options[:interpreter]]} '#{gamepath}'" if $options[:debug]
EM.popen("#{$interpreters[$options[:interpreter]]} '#{gamepath}'", RemHandler, inputq, remq)
EM.open_keyboard(KeyboardHandler, inputq)
}