forked from elanthia-online/lich-5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lich.rbw
executable file
·2999 lines (2690 loc) · 96.8 KB
/
lich.rbw
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 ruby
# encoding: US-ASCII
#####
# Copyright (C) 2005-2006 Murray Miron
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# Neither the name of the organization nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#####
# Lich is maintained by Matt Lowe ([email protected])
# Lich version 5 and higher maintained by Elanthia Online and only supports GTK3 Ruby
# process ARGV for constants before loading constants.rb: issue #304
for arg in ARGV
if arg =~ /^--(?:home)=(.+)[\\\/]?$/i
LICH_DIR = $1
elsif arg =~ /^--temp=(.+)[\\\/]?$/i
TEMP_DIR = $1
elsif arg =~ /^--scripts=(.+)[\\\/]?$/i
SCRIPT_DIR = $1
elsif arg =~ /^--maps=(.+)[\\\/]?$/i
MAP_DIR = $1
elsif arg =~ /^--logs=(.+)[\\\/]?$/i
LOG_DIR = $1
elsif arg =~ /^--backup=(.+)[\\\/]?$/i
BACKUP_DIR = $1
elsif arg =~ /^--data=(.+)[\\\/]?$/i
DATA_DIR = $1
end
end
require 'time'
require 'socket'
require 'rexml/document'
require 'rexml/streamlistener'
require 'stringio'
require 'zlib'
require 'drb/drb'
require 'resolv'
require 'digest/md5'
require 'json'
require 'terminal-table'
# TODO: Move all local requires to top of file
require_relative('./lib/constants')
require 'lib/version'
require 'lib/lich'
require 'lib/init'
require 'lib/front-end'
require 'lib/update'
# TODO: Need to split out initiatilzation functions to move require to top of file
require 'lib/gtk'
require 'lib/gui-login'
require 'lib/db_store'
class NilClass
def dup
nil
end
def method_missing(*_args)
nil
end
def split(*_val)
Array.new
end
def to_s
""
end
def strip
""
end
def +(val)
val
end
def closed?
true
end
end
class Numeric
def as_time
sprintf("%d:%02d:%02d", (self / 60).truncate, self.truncate % 60, ((self % 1) * 60).truncate)
end
def with_commas
self.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
end
end
class String
def to_s
self.dup
end
def stream
@stream
end
def stream=(val)
@stream ||= val
end
end
class StringProc
def initialize(string)
@string = string
end
def kind_of?(type)
Proc.new {}.kind_of? type
end
def class
Proc
end
def call(*_a)
proc { eval(@string) }.call
end
def _dump(_d = nil)
@string
end
def inspect
"StringProc.new(#{@string.inspect})"
end
def to_json(*args)
";e #{_dump}".to_json(args)
end
end
class SynchronizedSocket
def initialize(o)
@delegate = o
@mutex = Mutex.new
self
end
def puts(*args, &block)
@mutex.synchronize {
@delegate.puts(*args, &block)
}
end
def puts_if(*args)
@mutex.synchronize {
if yield
@delegate.puts(*args)
return true
else
return false
end
}
end
def write(*args, &block)
@mutex.synchronize {
@delegate.write(*args, &block)
}
end
def method_missing(method, *args, &block)
@delegate.__send__ method, *args, &block
end
end
class LimitedArray < Array
attr_accessor :max_size
def initialize(size = 0, obj = nil)
@max_size = 200
super
end
def push(line)
self.shift while self.length >= @max_size
super
end
def shove(line)
push(line)
end
def history
Array.new
end
end
require_relative("./lib/xmlparser.rb")
class UpstreamHook
@@upstream_hooks ||= Hash.new
@@upstream_hook_sources ||= Hash.new
def UpstreamHook.add(name, action)
unless action.class == Proc
echo "UpstreamHook: not a Proc (#{action})"
return false
end
@@upstream_hook_sources[name] = (Script.current.name || "Unknown")
@@upstream_hooks[name] = action
end
def UpstreamHook.run(client_string)
for key in @@upstream_hooks.keys
begin
client_string = @@upstream_hooks[key].call(client_string)
rescue
@@upstream_hooks.delete(key)
respond "--- Lich: UpstreamHook: #{$!}"
respond $!.backtrace.first
end
return nil if client_string.nil?
end
return client_string
end
def UpstreamHook.remove(name)
@@upstream_hook_sources.delete(name)
@@upstream_hooks.delete(name)
end
def UpstreamHook.list
@@upstream_hooks.keys.dup
end
def UpstreamHook.sources
info_table = Terminal::Table.new :headings => ['Hook', 'Source'],
:rows => @@upstream_hook_sources.to_a,
:style => {:all_separators => true}
Lich::Messaging.mono(info_table.to_s)
end
def UpstreamHook.hook_sources
@@upstream_hook_sources
end
end
class DownstreamHook
@@downstream_hooks ||= Hash.new
@@downstream_hook_sources ||= Hash.new
def DownstreamHook.add(name, action)
unless action.class == Proc
echo "DownstreamHook: not a Proc (#{action})"
return false
end
@@downstream_hook_sources[name] = (Script.current.name || "Unknown")
@@downstream_hooks[name] = action
end
def DownstreamHook.run(server_string)
for key in @@downstream_hooks.keys
return nil if server_string.nil?
begin
server_string = @@downstream_hooks[key].call(server_string.dup) if server_string.is_a?(String)
rescue
@@downstream_hooks.delete(key)
respond "--- Lich: DownstreamHook: #{$!}"
respond $!.backtrace.first
end
end
return server_string
end
def DownstreamHook.remove(name)
@@downstream_hook_sources.delete(name)
@@downstream_hooks.delete(name)
end
def DownstreamHook.list
@@downstream_hooks.keys.dup
end
def DownstreamHook.sources
info_table = Terminal::Table.new :headings => ['Hook', 'Source'],
:rows => @@downstream_hook_sources.to_a,
:style => {:all_separators => true}
Lich::Messaging.mono(info_table.to_s)
end
def DownstreamHook.hook_sources
@@downstream_hook_sources
end
end
module Settings
settings = Hash.new
md5_at_load = Hash.new
mutex = Mutex.new
@@settings = proc { |scope|
unless (script = Script.current)
respond '--- error: Settings: unknown calling script'
next nil
end
unless scope =~ /^#{XMLData.game}\:#{XMLData.name}$|^#{XMLData.game}$|^\:$/
respond '--- error: Settings: invalid scope'
next nil
end
mutex.synchronize {
unless settings[script.name] and settings[script.name][scope]
begin
marshal_hash = Lich.db.get_first_value('SELECT hash FROM script_auto_settings WHERE script=? AND scope=?;', [script.name.encode('UTF-8'), scope.encode('UTF-8')])
rescue SQLite3::BusyException
sleep 0.1
retry
end
settings[script.name] ||= Hash.new
if marshal_hash.nil?
settings[script.name][scope] = Hash.new
else
begin
hash = Marshal.load(marshal_hash)
rescue
respond "--- Lich: error: #{$!}"
respond $!.backtrace[0..1]
exit
end
settings[script.name][scope] = hash
end
md5_at_load[script.name] ||= Hash.new
md5_at_load[script.name][scope] = Digest::MD5.hexdigest(settings[script.name][scope].to_s)
end
}
settings[script.name][scope]
}
@@save = proc {
mutex.synchronize {
sql_began = false
settings.each_pair { |script_name, scopedata|
scopedata.each_pair { |scope, data|
if Digest::MD5.hexdigest(data.to_s) != md5_at_load[script_name][scope]
unless sql_began
begin
Lich.db.execute('BEGIN')
rescue SQLite3::BusyException
sleep 0.1
retry
end
sql_began = true
end
blob = SQLite3::Blob.new(Marshal.dump(data))
begin
Lich.db.execute('INSERT OR REPLACE INTO script_auto_settings(script,scope,hash) VALUES(?,?,?);', [script_name.encode('UTF-8'), scope.encode('UTF-8'), blob])
rescue SQLite3::BusyException
sleep 0.1
retry
rescue
respond "--- Lich: error: #{$!}"
respond $!.backtrace[0..1]
next
end
end
}
unless Script.running?(script_name)
settings.delete(script_name)
md5_at_load.delete(script_name)
end
}
if sql_began
begin
Lich.db.execute('END')
rescue SQLite3::BusyException
sleep 0.1
retry
end
end
}
}
Thread.new {
loop {
sleep 300
begin
@@save.call
rescue
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
end
}
}
def Settings.[](name)
@@settings.call(':')[name]
end
def Settings.[]=(name, value)
@@settings.call(':')[name] = value
end
def Settings.to_hash(scope = ':')
@@settings.call(scope)
end
def Settings.char
@@settings.call("#{XMLData.game}:#{XMLData.name}")
end
def Settings.save
@@save.call
end
end
module GameSettings
def GameSettings.[](name)
Settings.to_hash(XMLData.game)[name]
end
def GameSettings.[]=(name, value)
Settings.to_hash(XMLData.game)[name] = value
end
def GameSettings.to_hash
Settings.to_hash(XMLData.game)
end
end
module CharSettings
def CharSettings.[](name)
Settings.to_hash("#{XMLData.game}:#{XMLData.name}")[name]
end
def CharSettings.[]=(name, value)
Settings.to_hash("#{XMLData.game}:#{XMLData.name}")[name] = value
end
def CharSettings.to_hash
Settings.to_hash("#{XMLData.game}:#{XMLData.name}")
end
end
module Vars
@@vars = Hash.new
md5 = nil
mutex = Mutex.new
@@loaded = false
@@load = proc {
mutex.synchronize {
unless @@loaded
begin
h = Lich.db.get_first_value('SELECT hash FROM uservars WHERE scope=?;', ["#{XMLData.game}:#{XMLData.name}".encode('UTF-8')])
rescue SQLite3::BusyException
sleep 0.1
retry
end
if h
begin
hash = Marshal.load(h)
hash.each { |k, v| @@vars[k] = v }
md5 = Digest::MD5.hexdigest(hash.to_s)
rescue
respond "--- Lich: error: #{$!}"
respond $!.backtrace[0..2]
end
end
@@loaded = true
end
}
nil
}
@@save = proc {
mutex.synchronize {
if @@loaded
if Digest::MD5.hexdigest(@@vars.to_s) != md5
md5 = Digest::MD5.hexdigest(@@vars.to_s)
blob = SQLite3::Blob.new(Marshal.dump(@@vars))
begin
Lich.db.execute('INSERT OR REPLACE INTO uservars(scope,hash) VALUES(?,?);', ["#{XMLData.game}:#{XMLData.name}".encode('UTF-8'), blob])
rescue SQLite3::BusyException
sleep 0.1
retry
end
end
end
}
nil
}
Thread.new {
loop {
sleep 300
begin
@@save.call
rescue
Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
respond "--- Lich: error: #{$!}\n\t#{$!.backtrace[0..1].join("\n\t")}"
end
}
}
def Vars.[](name)
@@load.call unless @@loaded
@@vars[name]
end
def Vars.[]=(name, val)
@@load.call unless @@loaded
if val.nil?
@@vars.delete(name)
else
@@vars[name] = val
end
end
def Vars.list
@@load.call unless @@loaded
@@vars.dup
end
def Vars.save
@@save.call
end
def Vars.method_missing(arg1, arg2 = '')
@@load.call unless @@loaded
if arg1[-1, 1] == '='
if arg2.nil?
@@vars.delete(arg1.to_s.chop)
else
@@vars[arg1.to_s.chop] = arg2
end
else
@@vars[arg1.to_s]
end
end
end
# Script classes move to lib 230305
require_relative('./lib/script.rb')
class Watchfor
def initialize(line, theproc = nil, &block)
return nil unless (script = Script.current)
if line.class == String
line = Regexp.new(Regexp.escape(line))
elsif line.class != Regexp
echo 'watchfor: no string or regexp given'
return nil
end
if block.nil?
if theproc.respond_to? :call
block = theproc
else
echo 'watchfor: no block or proc given'
return nil
end
end
script.watchfor[line] = block
end
def Watchfor.clear
script.watchfor = Hash.new
end
end
## adding util to the list of defs
require 'lib/util.rb'
require 'lib/messaging.rb'
require 'lib/global_defs.rb'
module Buffer
DOWNSTREAM_STRIPPED = 1
DOWNSTREAM_RAW = 2
DOWNSTREAM_MOD = 4
UPSTREAM = 8
UPSTREAM_MOD = 16
SCRIPT_OUTPUT = 32
@@index = Hash.new
@@streams = Hash.new
@@mutex = Mutex.new
@@offset = 0
@@buffer = Array.new
@@max_size = 3000
def Buffer.gets
thread_id = Thread.current.object_id
if @@index[thread_id].nil?
@@mutex.synchronize {
@@index[thread_id] = (@@offset + @@buffer.length)
@@streams[thread_id] ||= DOWNSTREAM_STRIPPED
}
end
line = nil
loop {
if (@@index[thread_id] - @@offset) >= @@buffer.length
sleep 0.05 while ((@@index[thread_id] - @@offset) >= @@buffer.length)
end
@@mutex.synchronize {
if @@index[thread_id] < @@offset
@@index[thread_id] = @@offset
end
line = @@buffer[@@index[thread_id] - @@offset]
}
@@index[thread_id] += 1
break if ((line.stream & @@streams[thread_id]) != 0)
}
return line
end
def Buffer.gets?
thread_id = Thread.current.object_id
if @@index[thread_id].nil?
@@mutex.synchronize {
@@index[thread_id] = (@@offset + @@buffer.length)
@@streams[thread_id] ||= DOWNSTREAM_STRIPPED
}
end
line = nil
loop {
if (@@index[thread_id] - @@offset) >= @@buffer.length
return nil
end
@@mutex.synchronize {
if @@index[thread_id] < @@offset
@@index[thread_id] = @@offset
end
line = @@buffer[@@index[thread_id] - @@offset]
}
@@index[thread_id] += 1
break if ((line.stream & @@streams[thread_id]) != 0)
}
return line
end
def Buffer.rewind
thread_id = Thread.current.object_id
@@index[thread_id] = @@offset
@@streams[thread_id] ||= DOWNSTREAM_STRIPPED
return self
end
def Buffer.clear
thread_id = Thread.current.object_id
if @@index[thread_id].nil?
@@mutex.synchronize {
@@index[thread_id] = (@@offset + @@buffer.length)
@@streams[thread_id] ||= DOWNSTREAM_STRIPPED
}
end
lines = Array.new
loop {
if (@@index[thread_id] - @@offset) >= @@buffer.length
return lines
end
line = nil
@@mutex.synchronize {
if @@index[thread_id] < @@offset
@@index[thread_id] = @@offset
end
line = @@buffer[@@index[thread_id] - @@offset]
}
@@index[thread_id] += 1
lines.push(line) if ((line.stream & @@streams[thread_id]) != 0)
}
return lines
end
def Buffer.update(line, stream = nil)
@@mutex.synchronize {
frozen_line = line.dup
unless stream.nil?
frozen_line.stream = stream
end
frozen_line.freeze
@@buffer.push(frozen_line)
while (@@buffer.length > @@max_size)
@@buffer.shift
@@offset += 1
end
}
return self
end
def Buffer.streams
@@streams[Thread.current.object_id]
end
def Buffer.streams=(val)
if (val.class != Integer) or ((val & 63) == 0)
respond "--- Lich: error: invalid streams value\n\t#{$!.caller[0..2].join("\n\t")}"
return nil
end
@@streams[Thread.current.object_id] = val
end
def Buffer.cleanup
@@index.delete_if { |k, _v| not Thread.list.any? { |t| t.object_id == k } }
@@streams.delete_if { |k, _v| not Thread.list.any? { |t| t.object_id == k } }
return self
end
end
class SharedBuffer
attr_accessor :max_size
def initialize(args = {})
@buffer = Array.new
@buffer_offset = 0
@buffer_index = Hash.new
@buffer_mutex = Mutex.new
@max_size = args[:max_size] || 500
return self
end
def gets
thread_id = Thread.current.object_id
if @buffer_index[thread_id].nil?
@buffer_mutex.synchronize { @buffer_index[thread_id] = (@buffer_offset + @buffer.length) }
end
if (@buffer_index[thread_id] - @buffer_offset) >= @buffer.length
sleep 0.05 while ((@buffer_index[thread_id] - @buffer_offset) >= @buffer.length)
end
line = nil
@buffer_mutex.synchronize {
if @buffer_index[thread_id] < @buffer_offset
@buffer_index[thread_id] = @buffer_offset
end
line = @buffer[@buffer_index[thread_id] - @buffer_offset]
}
@buffer_index[thread_id] += 1
return line
end
def gets?
thread_id = Thread.current.object_id
if @buffer_index[thread_id].nil?
@buffer_mutex.synchronize { @buffer_index[thread_id] = (@buffer_offset + @buffer.length) }
end
if (@buffer_index[thread_id] - @buffer_offset) >= @buffer.length
return nil
end
line = nil
@buffer_mutex.synchronize {
if @buffer_index[thread_id] < @buffer_offset
@buffer_index[thread_id] = @buffer_offset
end
line = @buffer[@buffer_index[thread_id] - @buffer_offset]
}
@buffer_index[thread_id] += 1
return line
end
def clear
thread_id = Thread.current.object_id
if @buffer_index[thread_id].nil?
@buffer_mutex.synchronize { @buffer_index[thread_id] = (@buffer_offset + @buffer.length) }
return Array.new
end
if (@buffer_index[thread_id] - @buffer_offset) >= @buffer.length
return Array.new
end
lines = Array.new
@buffer_mutex.synchronize {
if @buffer_index[thread_id] < @buffer_offset
@buffer_index[thread_id] = @buffer_offset
end
lines = @buffer[(@buffer_index[thread_id] - @buffer_offset)..-1]
@buffer_index[thread_id] = (@buffer_offset + @buffer.length)
}
return lines
end
def rewind
@buffer_index[Thread.current.object_id] = @buffer_offset
return self
end
def update(line)
@buffer_mutex.synchronize {
fline = line.dup
fline.freeze
@buffer.push(fline)
while (@buffer.length > @max_size)
@buffer.shift
@buffer_offset += 1
end
}
return self
end
def cleanup_threads
@buffer_index.delete_if { |k, _v| not Thread.list.any? { |t| t.object_id == k } }
return self
end
end
class SpellRanks
@@list ||= Array.new
@@timestamp ||= 0
@@loaded ||= false
attr_reader :name
attr_accessor :minorspiritual, :majorspiritual, :cleric, :minorelemental, :majorelemental, :minormental, :ranger, :sorcerer, :wizard, :bard, :empath, :paladin, :arcanesymbols, :magicitemuse, :monk
def SpellRanks.load
if File.exist?("#{DATA_DIR}/#{XMLData.game}/spell-ranks.dat")
begin
File.open("#{DATA_DIR}/#{XMLData.game}/spell-ranks.dat", 'rb') { |f|
@@timestamp, @@list = Marshal.load(f.read)
}
# minor mental circle added 2012-07-18; old data files will have @minormental as nil
@@list.each { |rank_info| rank_info.minormental ||= 0 }
# monk circle added 2013-01-15; old data files will have @minormental as nil
@@list.each { |rank_info| rank_info.monk ||= 0 }
@@loaded = true
rescue
respond "--- Lich: error: SpellRanks.load: #{$!}"
Lich.log "error: SpellRanks.load: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
@@list = Array.new
@@timestamp = 0
@@loaded = true
end
else
@@loaded = true
end
end
def SpellRanks.save
begin
File.open("#{DATA_DIR}/#{XMLData.game}/spell-ranks.dat", 'wb') { |f|
f.write(Marshal.dump([@@timestamp, @@list]))
}
rescue
respond "--- Lich: error: SpellRanks.save: #{$!}"
Lich.log "error: SpellRanks.save: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
end
end
def SpellRanks.timestamp
SpellRanks.load unless @@loaded
@@timestamp
end
def SpellRanks.timestamp=(val)
SpellRanks.load unless @@loaded
@@timestamp = val
end
def SpellRanks.[](name)
SpellRanks.load unless @@loaded
@@list.find { |n| n.name == name }
end
def SpellRanks.list
SpellRanks.load unless @@loaded
@@list
end
def SpellRanks.method_missing(arg = nil)
echo "error: unknown method #{arg} for class SpellRanks"
respond caller[0..1]
end
def initialize(name)
SpellRanks.load unless @@loaded
@name = name
@minorspiritual, @majorspiritual, @cleric, @minorelemental, @majorelemental, @ranger, @sorcerer, @wizard, @bard, @empath, @paladin, @minormental, @arcanesymbols, @magicitemuse = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
@@list.push(self)
end
end
module Games
module Unknown
module Game
end
end
module Gemstone
module Game
@@socket = nil
@@mutex = Mutex.new
@@last_recv = nil
@@thread = nil
@@buffer = SharedBuffer.new
@@_buffer = SharedBuffer.new
@@_buffer.max_size = 1000
@@autostarted = false
@@cli_scripts = false
def self.clean_gs_serverstring(server_string)
# The Rift, Scatter is broken...
if server_string =~ /<compDef id='room text'><\/compDef>/
server_string.sub!(/(.*)\s\s<compDef id='room text'><\/compDef>/) { "<compDef id='room desc'>#{$1}</compDef>" }
end
return server_string
end
@atmospherics = false
@combat_count = 0
@end_combat_tags = ["<prompt", "<clearStream", "<component", "<pushStream id=\"percWindow"]
def self.clean_dr_serverstring(server_string)
## Clear out superfluous tags
server_string = server_string.gsub("<pushStream id=\"combat\" /><popStream id=\"combat\" />", "")
server_string = server_string.gsub("<popStream id=\"combat\" /><pushStream id=\"combat\" />", "")
# DR occasionally has poor encoding in text, which causes parsing errors.
# One example of this is in the discern text for the spell Membrach's Greed
# which gets sent as Membrach\x92s Greed. This fixes the bad encoding until
# Simu fixes it.
if server_string =~ /\x92/
Lich.log "Detected poorly encoded apostrophe: #{server_string.inspect}"
server_string.gsub!("\x92", "'")
Lich.log "Changed poorly encoded apostrophe to: #{server_string.inspect}"
end
## Fix combat wrapping components - Why, DR, Why?
server_string = server_string.gsub("<pushStream id=\"combat\" /><component id=", "<component id=")
# server_string = server_string.gsub("<pushStream id=\"combat\" /><prompt ","<prompt ")
# Fixes xml with \r\n in the middle of it like:
# <component id='room exits'>Obvious paths: clockwise, widdershins.\r\n
# <compass></compass></component>\r\n
# We close the first line and in the next segment, we remove the trailing bits
# Because we can only match line by line, this couldn't be fixed in one matching block...
if server_string == "<component id='room exits'>Obvious paths: clockwise, widdershins.\r\n"
Lich.log "Unclosed component tag detected: #{server_string.inspect}"
server_string = "<component id='room exits'>Obvious paths: <d>clockwise</d>, <d>widdershins</d>.<compass></compass></component>"
Lich.log "Unclosed component tag fixed to: #{server_string.inspect}"
# retry
end
# This is an actual DR line "<compass></compass></component>\r\n" which happens when the above is sent... subbing it out since we fix the tag above.
if server_string == "<compass></compass></component>\r\n"
Lich.log "Extraneous closed tag detected: #{server_string.inspect}"
server_string = ""
Lich.log "Extraneous closed tag fixed: #{server_string.inspect}"
end
# "<component id='room objs'> You also see a granite altar with several candles and a water jug on it, and a granite font.\r\n"
# "<component id='room extra'>Placed around the interior, you see: some furniture and other bits of interest.\r\n
# Followed by in a new line.
# "</component>\r\n"
if server_string =~ /^<component id='room (?:objs|extra)'>[^<]*(?!<\/component>)\r\n/
Lich.log "Open-ended room objects component id tag: #{server_string.inspect}"
server_string.gsub!("\r\n", "</component>")
Lich.log "Open-ended room objects component id tag fixed to: #{server_string.inspect}"
end
# "</component>\r\n"
if server_string == "</component>\r\n"
Lich.log "Extraneous closing tag detected and deleted: #{server_string.inspect}"
server_string = ""
end
## Fix duplicate pushStrings
while server_string.include?("<pushStream id=\"combat\" /><pushStream id=\"combat\" />")
server_string = server_string.gsub("<pushStream id=\"combat\" /><pushStream id=\"combat\" />", "<pushStream id=\"combat\" />")
end
if @combat_count > 0
@end_combat_tags.each do |tag|
# server_string = "<!-- looking for tag: #{tag}" + server_string
if server_string.include?(tag)
server_string = server_string.gsub(tag, "<popStream id=\"combat\" />" + tag) unless server_string.include?("<popStream id=\"combat\" />")
@combat_count -= 1
end
if server_string.include?("<pushStream id=\"combat\" />")
server_string = server_string.gsub("<pushStream id=\"combat\" />", "")
end
end
end
@combat_count += server_string.scan("<pushStream id=\"combat\" />").length
@combat_count -= server_string.scan("<popStream id=\"combat\" />").length