This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlich.rbw
executable file
·12821 lines (12343 loc) · 541 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 (tillmen@lichproject.org)
#
# Based on Lich 4.6.56
LICH_VERSION = '4.13.13f'
TESTING = false
KEEP_SAFE = RUBY_VERSION =~ /^2\.[012]\./
if RUBY_VERSION !~ /^2/
if (RUBY_PLATFORM =~ /mingw|win/) and (RUBY_PLATFORM !~ /darwin/i)
if RUBY_VERSION =~ /^1\.9/
require 'fiddle'
Fiddle::Function.new(DL.dlopen('user32.dll')['MessageBox'], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT).call(0, 'Upgrade Ruby to version 2.0', "Lich v#{LICH_VERSION}", 16)
else
# fixme: This message never shows up on Ruby 1.8 because it errors out on negative lookbehind regex later in the file
require 'dl'
DL.dlopen('user32.dll')['MessageBox', 'LLPPL'].call(0, 'Upgrade Ruby to version 2.0', "Lich v#{LICH_VERSION}", 16)
end
else
puts "Upgrade Ruby to version 2.0"
end
exit
end
require 'time'
require 'socket'
require 'rexml/document'
require 'rexml/streamlistener'
require 'stringio'
require 'zlib'
require 'drb'
require 'resolv'
require 'digest/md5'
require 'json'
begin
# stupid workaround for Windows
# seems to avoid a 10 second lag when starting lnet, without adding a 10 second lag at startup
require 'openssl'
OpenSSL::PKey::RSA.new(512)
end
if (RUBY_PLATFORM =~ /mingw|win/i) and (RUBY_PLATFORM !~ /darwin/i)
#
# Windows API made slightly less annoying
#
require 'fiddle'
require 'fiddle/import'
module Win32
SIZEOF_CHAR = Fiddle::SIZEOF_CHAR
SIZEOF_LONG = Fiddle::SIZEOF_LONG
SEE_MASK_NOCLOSEPROCESS = 0x00000040
MB_OK = 0x00000000
MB_OKCANCEL = 0x00000001
MB_YESNO = 0x00000004
MB_ICONERROR = 0x00000010
MB_ICONQUESTION = 0x00000020
MB_ICONWARNING = 0x00000030
IDIOK = 1
IDICANCEL = 2
IDIYES = 6
IDINO = 7
KEY_ALL_ACCESS = 0xF003F
KEY_CREATE_SUB_KEY = 0x0004
KEY_ENUMERATE_SUB_KEYS = 0x0008
KEY_EXECUTE = 0x20019
KEY_NOTIFY = 0x0010
KEY_QUERY_VALUE = 0x0001
KEY_READ = 0x20019
KEY_SET_VALUE = 0x0002
KEY_WOW64_32KEY = 0x0200
KEY_WOW64_64KEY = 0x0100
KEY_WRITE = 0x20006
TokenElevation = 20
TOKEN_QUERY = 8
STILL_ACTIVE = 259
SW_SHOWNORMAL = 1
SW_SHOW = 5
PROCESS_QUERY_INFORMATION = 1024
PROCESS_VM_READ = 16
HKEY_LOCAL_MACHINE = -2147483646
REG_NONE = 0
REG_SZ = 1
REG_EXPAND_SZ = 2
REG_BINARY = 3
REG_DWORD = 4
REG_DWORD_LITTLE_ENDIAN = 4
REG_DWORD_BIG_ENDIAN = 5
REG_LINK = 6
REG_MULTI_SZ = 7
REG_QWORD = 11
REG_QWORD_LITTLE_ENDIAN = 11
module Kernel32
extend Fiddle::Importer
dlload 'kernel32'
extern 'int GetCurrentProcess()'
extern 'int GetExitCodeProcess(int, int*)'
extern 'int GetModuleFileName(int, void*, int)'
extern 'int GetVersionEx(void*)'
# extern 'int OpenProcess(int, int, int)' # fixme
extern 'int GetLastError()'
extern 'int CreateProcess(void*, void*, void*, void*, int, int, void*, void*, void*, void*)'
end
def Win32.GetLastError
return Kernel32.GetLastError()
end
def Win32.CreateProcess(args)
if args[:lpCommandLine]
lpCommandLine = args[:lpCommandLine].dup
else
lpCommandLine = nil
end
if args[:bInheritHandles] == false
bInheritHandles = 0
elsif args[:bInheritHandles] == true
bInheritHandles = 1
else
bInheritHandles = args[:bInheritHandles].to_i
end
if args[:lpEnvironment].class == Array
# fixme
end
lpStartupInfo = [ 68, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
lpStartupInfo_index = { :lpDesktop => 2, :lpTitle => 3, :dwX => 4, :dwY => 5, :dwXSize => 6, :dwYSize => 7, :dwXCountChars => 8, :dwYCountChars => 9, :dwFillAttribute => 10, :dwFlags => 11, :wShowWindow => 12, :hStdInput => 15, :hStdOutput => 16, :hStdError => 17 }
for sym in [ :lpDesktop, :lpTitle ]
if args[sym]
args[sym] = "#{args[sym]}\0" unless args[sym][-1,1] == "\0"
lpStartupInfo[lpStartupInfo_index[sym]] = Fiddle::Pointer.to_ptr(args[sym]).to_i
end
end
for sym in [ :dwX, :dwY, :dwXSize, :dwYSize, :dwXCountChars, :dwYCountChars, :dwFillAttribute, :dwFlags, :wShowWindow, :hStdInput, :hStdOutput, :hStdError ]
if args[sym]
lpStartupInfo[lpStartupInfo_index[sym]] = args[sym]
end
end
lpStartupInfo = lpStartupInfo.pack('LLLLLLLLLLLLSSLLLL')
lpProcessInformation = [ 0, 0, 0, 0, ].pack('LLLL')
r = Kernel32.CreateProcess(args[:lpApplicationName], lpCommandLine, args[:lpProcessAttributes], args[:lpThreadAttributes], bInheritHandles, args[:dwCreationFlags].to_i, args[:lpEnvironment], args[:lpCurrentDirectory], lpStartupInfo, lpProcessInformation)
lpProcessInformation = lpProcessInformation.unpack('LLLL')
return :return => (r > 0 ? true : false), :hProcess => lpProcessInformation[0], :hThread => lpProcessInformation[1], :dwProcessId => lpProcessInformation[2], :dwThreadId => lpProcessInformation[3]
end
# Win32.CreateProcess(:lpApplicationName => 'Launcher.exe', :lpCommandLine => 'lich2323.sal', :lpCurrentDirectory => 'C:\\PROGRA~1\\SIMU')
# def Win32.OpenProcess(args={})
# return Kernel32.OpenProcess(args[:dwDesiredAccess].to_i, args[:bInheritHandle].to_i, args[:dwProcessId].to_i)
# end
def Win32.GetCurrentProcess
return Kernel32.GetCurrentProcess
end
def Win32.GetExitCodeProcess(args)
lpExitCode = [ 0 ].pack('L')
r = Kernel32.GetExitCodeProcess(args[:hProcess].to_i, lpExitCode)
return :return => r, :lpExitCode => lpExitCode.unpack('L')[0]
end
def Win32.GetModuleFileName(args={})
args[:nSize] ||= 256
buffer = "\0" * args[:nSize].to_i
r = Kernel32.GetModuleFileName(args[:hModule].to_i, buffer, args[:nSize].to_i)
return :return => r, :lpFilename => buffer.gsub("\0", '')
end
def Win32.GetVersionEx
a = [ 156, 0, 0, 0, 0, ("\0" * 128), 0, 0, 0, 0, 0].pack('LLLLLa128SSSCC')
r = Kernel32.GetVersionEx(a)
a = a.unpack('LLLLLa128SSSCC')
return :return => r, :dwOSVersionInfoSize => a[0], :dwMajorVersion => a[1], :dwMinorVersion => a[2], :dwBuildNumber => a[3], :dwPlatformId => a[4], :szCSDVersion => a[5].strip, :wServicePackMajor => a[6], :wServicePackMinor => a[7], :wSuiteMask => a[8], :wProductType => a[9]
end
module User32
extend Fiddle::Importer
dlload 'user32'
extern 'int MessageBox(int, char*, char*, int)'
end
def Win32.MessageBox(args)
args[:lpCaption] ||= "Lich v#{LICH_VERSION}"
return User32.MessageBox(args[:hWnd].to_i, args[:lpText], args[:lpCaption], args[:uType].to_i)
end
module Advapi32
extend Fiddle::Importer
dlload 'advapi32'
extern 'int GetTokenInformation(int, int, void*, int, void*)'
extern 'int OpenProcessToken(int, int, void*)'
extern 'int RegOpenKeyEx(int, char*, int, int, void*)'
extern 'int RegQueryValueEx(int, char*, void*, void*, void*, void*)'
extern 'int RegSetValueEx(int, char*, int, int, char*, int)'
extern 'int RegDeleteValue(int, char*)'
extern 'int RegCloseKey(int)'
end
def Win32.GetTokenInformation(args)
if args[:TokenInformationClass] == TokenElevation
token_information_length = SIZEOF_LONG
token_information = [ 0 ].pack('L')
else
return nil
end
return_length = [ 0 ].pack('L')
r = Advapi32.GetTokenInformation(args[:TokenHandle].to_i, args[:TokenInformationClass], token_information, token_information_length, return_length)
if args[:TokenInformationClass] == TokenElevation
return :return => r, :TokenIsElevated => token_information.unpack('L')[0]
end
end
def Win32.OpenProcessToken(args)
token_handle = [ 0 ].pack('L')
r = Advapi32.OpenProcessToken(args[:ProcessHandle].to_i, args[:DesiredAccess].to_i, token_handle)
return :return => r, :TokenHandle => token_handle.unpack('L')[0]
end
def Win32.RegOpenKeyEx(args)
phkResult = [ 0 ].pack('L')
r = Advapi32.RegOpenKeyEx(args[:hKey].to_i, args[:lpSubKey].to_s, 0, args[:samDesired].to_i, phkResult)
return :return => r, :phkResult => phkResult.unpack('L')[0]
end
def Win32.RegQueryValueEx(args)
args[:lpValueName] ||= 0
lpcbData = [ 0 ].pack('L')
r = Advapi32.RegQueryValueEx(args[:hKey].to_i, args[:lpValueName], 0, 0, 0, lpcbData)
if r == 0
lpcbData = lpcbData.unpack('L')[0]
lpData = String.new.rjust(lpcbData, "\x00")
lpcbData = [ lpcbData ].pack('L')
lpType = [ 0 ].pack('L')
r = Advapi32.RegQueryValueEx(args[:hKey].to_i, args[:lpValueName], 0, lpType, lpData, lpcbData)
lpType = lpType.unpack('L')[0]
lpcbData = lpcbData.unpack('L')[0]
if [REG_EXPAND_SZ, REG_SZ, REG_LINK].include?(lpType)
lpData.gsub!("\x00", '')
elsif lpType == REG_MULTI_SZ
lpData = lpData.gsub("\x00\x00", '').split("\x00")
elsif lpType == REG_DWORD
lpData = lpData.unpack('L')[0]
elsif lpType == REG_QWORD
lpData = lpData.unpack('Q')[0]
elsif lpType == REG_BINARY
# fixme
elsif lpType == REG_DWORD_BIG_ENDIAN
# fixme
else
# fixme
end
return :return => r, :lpType => lpType, :lpcbData => lpcbData, :lpData => lpData
else
return :return => r
end
end
def Win32.RegSetValueEx(args)
if [REG_EXPAND_SZ, REG_SZ, REG_LINK].include?(args[:dwType]) and (args[:lpData].class == String)
lpData = args[:lpData].dup
lpData.concat("\x00")
cbData = lpData.length
elsif (args[:dwType] == REG_MULTI_SZ) and (args[:lpData].class == Array)
lpData = args[:lpData].join("\x00").concat("\x00\x00")
cbData = lpData.length
elsif (args[:dwType] == REG_DWORD) and (args[:lpData].class == Integer)
lpData = [args[:lpData]].pack('L')
cbData = 4
elsif (args[:dwType] == REG_QWORD) and (args[:lpData].class == Integer)
lpData = [args[:lpData]].pack('Q')
cbData = 8
elsif args[:dwType] == REG_BINARY
# fixme
return false
elsif args[:dwType] == REG_DWORD_BIG_ENDIAN
# fixme
return false
else
# fixme
return false
end
args[:lpValueName] ||= 0
return Advapi32.RegSetValueEx(args[:hKey].to_i, args[:lpValueName], 0, args[:dwType], lpData, cbData)
end
def Win32.RegDeleteValue(args)
args[:lpValueName] ||= 0
return Advapi32.RegDeleteValue(args[:hKey].to_i, args[:lpValueName])
end
def Win32.RegCloseKey(args)
return Advapi32.RegCloseKey(args[:hKey])
end
module Shell32
extend Fiddle::Importer
dlload 'shell32'
extern 'int ShellExecuteEx(void*)'
extern 'int ShellExecute(int, char*, char*, char*, char*, int)'
end
def Win32.ShellExecuteEx(args)
# struct = [ (SIZEOF_LONG * 15), 0, 0, 0, 0, 0, 0, SW_SHOWNORMAL, 0, 0, 0, 0, 0, 0, 0 ]
struct = [ (SIZEOF_LONG * 15), 0, 0, 0, 0, 0, 0, SW_SHOW, 0, 0, 0, 0, 0, 0, 0 ]
struct_index = { :cbSize => 0, :fMask => 1, :hwnd => 2, :lpVerb => 3, :lpFile => 4, :lpParameters => 5, :lpDirectory => 6, :nShow => 7, :hInstApp => 8, :lpIDList => 9, :lpClass => 10, :hkeyClass => 11, :dwHotKey => 12, :hIcon => 13, :hMonitor => 13, :hProcess => 14 }
for sym in [ :lpVerb, :lpFile, :lpParameters, :lpDirectory, :lpIDList, :lpClass ]
if args[sym]
args[sym] = "#{args[sym]}\0" unless args[sym][-1,1] == "\0"
struct[struct_index[sym]] = Fiddle::Pointer.to_ptr(args[sym]).to_i
end
end
for sym in [ :fMask, :hwnd, :nShow, :hkeyClass, :dwHotKey, :hIcon, :hMonitor, :hProcess ]
if args[sym]
struct[struct_index[sym]] = args[sym]
end
end
struct = struct.pack('LLLLLLLLLLLLLLL')
r = Shell32.ShellExecuteEx(struct)
struct = struct.unpack('LLLLLLLLLLLLLLL')
return :return => r, :hProcess => struct[struct_index[:hProcess]], :hInstApp => struct[struct_index[:hInstApp]]
end
def Win32.ShellExecute(args)
args[:lpOperation] ||= 0
args[:lpParameters] ||= 0
args[:lpDirectory] ||= 0
args[:nShowCmd] ||= 1
return Shell32.ShellExecute(args[:hwnd].to_i, args[:lpOperation], args[:lpFile], args[:lpParameters], args[:lpDirectory], args[:nShowCmd])
end
begin
module Kernel32
extern 'int EnumProcesses(void*, int, void*)'
end
def Win32.EnumProcesses(args={})
args[:cb] ||= 400
pProcessIds = Array.new((args[:cb]/SIZEOF_LONG), 0).pack(''.rjust((args[:cb]/SIZEOF_LONG), 'L'))
pBytesReturned = [ 0 ].pack('L')
r = Kernel32.EnumProcesses(pProcessIds, args[:cb], pBytesReturned)
pBytesReturned = pBytesReturned.unpack('L')[0]
return :return => r, :pProcessIds => pProcessIds.unpack(''.rjust((args[:cb]/SIZEOF_LONG), 'L'))[0...(pBytesReturned/SIZEOF_LONG)], :pBytesReturned => pBytesReturned
end
rescue
module Psapi
extend Fiddle::Importer
dlload 'psapi'
extern 'int EnumProcesses(void*, int, void*)'
end
def Win32.EnumProcesses(args={})
args[:cb] ||= 400
pProcessIds = Array.new((args[:cb]/SIZEOF_LONG), 0).pack(''.rjust((args[:cb]/SIZEOF_LONG), 'L'))
pBytesReturned = [ 0 ].pack('L')
r = Psapi.EnumProcesses(pProcessIds, args[:cb], pBytesReturned)
pBytesReturned = pBytesReturned.unpack('L')[0]
return :return => r, :pProcessIds => pProcessIds.unpack(''.rjust((args[:cb]/SIZEOF_LONG), 'L'))[0...(pBytesReturned/SIZEOF_LONG)], :pBytesReturned => pBytesReturned
end
end
def Win32.isXP?
return (Win32.GetVersionEx[:dwMajorVersion] < 6)
end
# def Win32.isWin8?
# r = Win32.GetVersionEx
# return ((r[:dwMajorVersion] == 6) and (r[:dwMinorVersion] >= 2))
# end
def Win32.admin?
if Win32.isXP?
return true
else
r = Win32.OpenProcessToken(:ProcessHandle => Win32.GetCurrentProcess, :DesiredAccess => TOKEN_QUERY)
token_handle = r[:TokenHandle]
r = Win32.GetTokenInformation(:TokenInformationClass => TokenElevation, :TokenHandle => token_handle)
return (r[:TokenIsElevated] != 0)
end
end
def Win32.AdminShellExecute(args)
# open ruby/lich as admin and tell it to open something else
if not caller.any? { |c| c =~ /eval|run/ }
r = Win32.GetModuleFileName
if r[:return] > 0
if File.exists?(r[:lpFilename])
Win32.ShellExecuteEx(:lpVerb => 'runas', :lpFile => r[:lpFilename], :lpParameters => "#{File.expand_path($PROGRAM_NAME)} shellexecute #{[Marshal.dump(args)].pack('m').gsub("\n",'')}")
end
end
end
end
end
else
if arg = ARGV.find { |a| a =~ /^--wine=.+$/i }
$wine_bin = arg.sub(/^--wine=/, '')
else
begin
$wine_bin = `which wine`.strip
rescue
$wine_bin = nil
end
end
if arg = ARGV.find { |a| a =~ /^--wine-prefix=.+$/i }
$wine_prefix = arg.sub(/^--wine-prefix=/, '')
elsif ENV['WINEPREFIX']
$wine_prefix = ENV['WINEPREFIX']
elsif ENV['HOME']
$wine_prefix = ENV['HOME'] + '/.wine'
else
$wine_prefix = nil
end
if $wine_bin and File.exists?($wine_bin) and File.file?($wine_bin) and $wine_prefix and File.exists?($wine_prefix) and File.directory?($wine_prefix)
module Wine
BIN = $wine_bin
PREFIX = $wine_prefix
def Wine.registry_gets(key)
hkey, subkey, thingie = /(HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER)\\(.+)\\([^\\]*)/.match(key).captures # fixme: stupid highlights ]/
if File.exists?(PREFIX + '/system.reg')
if hkey == 'HKEY_LOCAL_MACHINE'
subkey = "[#{subkey.gsub('\\', '\\\\\\')}]"
if thingie.nil? or thingie.empty?
thingie = '@'
else
thingie = "\"#{thingie}\""
end
lookin = result = false
File.open(PREFIX + '/system.reg') { |f| f.readlines }.each { |line|
if line[0...subkey.length] == subkey
lookin = true
elsif line =~ /^\[/
lookin = false
elsif lookin and line =~ /^#{thingie}="(.*)"$/i
result = $1.split('\\"').join('"').split('\\\\').join('\\').sub(/\\0$/, '')
break
end
}
return result
else
return false
end
else
return false
end
end
def Wine.registry_puts(key, value)
hkey, subkey, thingie = /(HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER)\\(.+)\\([^\\]*)/.match(key).captures # fixme ]/
if File.exists?(PREFIX)
if thingie.nil? or thingie.empty?
thingie = '@'
else
thingie = "\"#{thingie}\""
end
# gsub sucks for this..
value = value.split('\\').join('\\\\')
value = value.split('"').join('\"')
begin
regedit_data = "REGEDIT4\n\n[#{hkey}\\#{subkey}]\n#{thingie}=\"#{value}\"\n\n"
filename = "#{TEMP_DIR}/wine-#{Time.now.to_i}.reg"
File.open(filename, 'w') { |f| f.write(regedit_data) }
system("#{BIN} regedit #{filename}")
sleep 0.2
File.delete(filename)
rescue
return false
end
return true
end
end
end
end
$wine_bin = nil
$wine_prefix = nil
end
if ARGV[0] == 'shellexecute'
args = Marshal.load(ARGV[1].unpack('m')[0])
Win32.ShellExecute(:lpOperation => args[:op], :lpFile => args[:file], :lpDirectory => args[:dir], :lpParameters => args[:params])
exit
end
begin
require 'sqlite3'
rescue LoadError
if defined?(Win32)
r = Win32.MessageBox(:lpText => "Lich needs sqlite3 to save settings and data, but it is not installed.\n\nWould you like to install sqlite3 now?", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_YESNO | Win32::MB_ICONQUESTION))
if r == Win32::IDIYES
r = Win32.GetModuleFileName
if r[:return] > 0
ruby_bin_dir = File.dirname(r[:lpFilename])
if File.exists?("#{ruby_bin_dir}\\gem.bat")
verb = (Win32.isXP? ? 'open' : 'runas')
# fixme: using --source http://rubygems.org to avoid https because it has been failing to validate the certificate on Windows
r = Win32.ShellExecuteEx(:fMask => Win32::SEE_MASK_NOCLOSEPROCESS, :lpVerb => verb, :lpFile => "#{ruby_bin_dir}\\gem.bat", :lpParameters => 'install sqlite3 --source http://rubygems.org --no-ri --no-rdoc')
if r[:return] > 0
pid = r[:hProcess]
sleep 1 while Win32.GetExitCodeProcess(:hProcess => pid)[:lpExitCode] == Win32::STILL_ACTIVE
r = Win32.MessageBox(:lpText => "Install finished. Lich will restart now.", :lpCaption => "Lich v#{LICH_VERSION}", :uType => Win32::MB_OKCANCEL)
else
# ShellExecuteEx failed: this seems to happen with an access denied error even while elevated on some random systems
r = Win32.ShellExecute(:lpOperation => verb, :lpFile => "#{ruby_bin_dir}\\gem.bat", :lpParameters => 'install sqlite3 --source http://rubygems.org --no-ri --no-rdoc')
if r <= 32
Win32.MessageBox(:lpText => "error: failed to start the sqlite3 installer\n\nfailed command: Win32.ShellExecute(:lpOperation => #{verb.inspect}, :lpFile => \"#{ruby_bin_dir}\\gem.bat\", :lpParameters => \"install sqlite3 --source http://rubygems.org --no-ri --no-rdoc\")\n\nerror code: #{Win32.GetLastError}", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_OK | Win32::MB_ICONERROR))
exit
end
r = Win32.MessageBox(:lpText => "When the installer is finished, click OK to restart Lich.", :lpCaption => "Lich v#{LICH_VERSION}", :uType => Win32::MB_OKCANCEL)
end
if r == Win32::IDIOK
if File.exists?("#{ruby_bin_dir}\\rubyw.exe")
Win32.ShellExecute(:lpOperation => 'open', :lpFile => "#{ruby_bin_dir}\\rubyw.exe", :lpParameters => "\"#{File.expand_path($PROGRAM_NAME)}\"")
else
Win32.MessageBox(:lpText => "error: failed to find rubyw.exe; can't restart Lich for you", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_OK | Win32::MB_ICONERROR))
end
else
# user doesn't want to restart Lich
end
else
Win32.MessageBox(:lpText => "error: Could not find gem.bat in directory #{ruby_bin_dir}", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_OK | Win32::MB_ICONERROR))
end
else
Win32.MessageBox(:lpText => "error: GetModuleFileName failed", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_OK | Win32::MB_ICONERROR))
end
else
# user doesn't want to install sqlite3 gem
end
else
# fixme: no sqlite3 on Linux/Mac
puts "The sqlite3 gem is not installed (or failed to load), you may need to: sudo gem install sqlite3"
end
exit
end
begin
require 'gtk2'
HAVE_GTK = true
rescue LoadError, Gtk::InitError => e
if (ENV['RUN_BY_CRON'].nil? or ENV['RUN_BY_CRON'] == 'false') and ARGV.empty? or ARGV.any? { |arg| arg =~ /^--gui$/ } or not $stdout.isatty
if defined?(Win32)
r = Win32.MessageBox(:lpText => "Lich uses gtk2 to create windows, but it is not installed. You can use Lich from the command line (ruby lich.rbw --help) or you can install gtk2 for a point and click interface.\n\nWould you like to install gtk2 now?", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_YESNO | Win32::MB_ICONQUESTION))
if r == Win32::IDIYES
r = Win32.GetModuleFileName
if r[:return] > 0
ruby_bin_dir = File.dirname(r[:lpFilename])
if File.exists?("#{ruby_bin_dir}\\gem.bat")
verb = (Win32.isXP? ? 'open' : 'runas')
r = Win32.ShellExecuteEx(:fMask => Win32::SEE_MASK_NOCLOSEPROCESS, :lpVerb => verb, :lpFile => "#{ruby_bin_dir}\\gem.bat", :lpParameters => 'install cairo:1.14.3 gtk2:2.2.5 --source http://rubygems.org --no-ri --no-rdoc')
if r[:return] > 0
pid = r[:hProcess]
sleep 1 while Win32.GetExitCodeProcess(:hProcess => pid)[:lpExitCode] == Win32::STILL_ACTIVE
r = Win32.MessageBox(:lpText => "Install finished. Lich will restart now.", :lpCaption => "Lich v#{LICH_VERSION}", :uType => Win32::MB_OKCANCEL)
else
# ShellExecuteEx failed: this seems to happen with an access denied error even while elevated on some random systems
r = Win32.ShellExecute(:lpOperation => verb, :lpFile => "#{ruby_bin_dir}\\gem.bat", :lpParameters => 'install cairo:1.14.3 gtk2:2.2.5 --source http://rubygems.org --no-ri --no-rdoc')
if r <= 32
Win32.MessageBox(:lpText => "error: failed to start the gtk2 installer\n\nfailed command: Win32.ShellExecute(:lpOperation => #{verb.inspect}, :lpFile => \"#{ruby_bin_dir}\\gem.bat\", :lpParameters => \"install cairo:1.14.3 gtk2:2.2.5 --source http://rubygems.org --no-ri --no-rdoc\")\n\nerror code: #{Win32.GetLastError}", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_OK | Win32::MB_ICONERROR))
exit
end
r = Win32.MessageBox(:lpText => "When the installer is finished, click OK to restart Lich.", :lpCaption => "Lich v#{LICH_VERSION}", :uType => Win32::MB_OKCANCEL)
end
if r == Win32::IDIOK
if File.exists?("#{ruby_bin_dir}\\rubyw.exe")
Win32.ShellExecute(:lpOperation => 'open', :lpFile => "#{ruby_bin_dir}\\rubyw.exe", :lpParameters => "\"#{File.expand_path($PROGRAM_NAME)}\"")
else
Win32.MessageBox(:lpText => "error: failed to find rubyw.exe; can't restart Lich for you", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_OK | Win32::MB_ICONERROR))
end
else
# user doesn't want to restart Lich
end
else
Win32.MessageBox(:lpText => "error: Could not find gem.bat in directory #{ruby_bin_dir}", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_OK | Win32::MB_ICONERROR))
end
else
Win32.MessageBox(:lpText => "error: GetModuleFileName failed", :lpCaption => "Lich v#{LICH_VERSION}", :uType => (Win32::MB_OK | Win32::MB_ICONERROR))
end
else
# user doesn't want to install gtk2 gem
end
else
# fixme: no gtk2 on Linux/Mac
puts "The gtk2 gem is not installed (or failed to load), you may need to: sudo gem install gtk2"
end
exit
else
# gtk is optional if command line arguments are given or started in a terminal
HAVE_GTK = false
early_gtk_error = "warning: failed to load GTK\n\t#{$!}\n\t#{$!.backtrace.join("\n\t")}"
end
end
if defined?(Gtk)
module Gtk
# Calling Gtk API in a thread other than the main thread may cause random segfaults
def Gtk.queue &block
GLib::Timeout.add(1) {
begin
block.call
rescue
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue SyntaxError
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue SystemExit
nil
rescue SecurityError
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue ThreadError
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue SystemStackError
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue Exception
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue ScriptError
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue LoadError
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue NoMemoryError
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
rescue
respond "error in Gtk.queue: #{$!}"
Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
end
false # don't repeat timeout
}
end
end
end
module Lich
@@hosts_file = nil
@@lich_db = nil
def Lich.db
if $SAFE == 0
@@lich_db ||= SQLite3::Database.new("#{DATA_DIR}/lich.db3")
else
nil
end
end
def Lich.init_db
begin
Lich.db.execute("CREATE TABLE IF NOT EXISTS script_setting (script TEXT NOT NULL, name TEXT NOT NULL, value BLOB, PRIMARY KEY(script, name));")
Lich.db.execute("CREATE TABLE IF NOT EXISTS script_auto_settings (script TEXT NOT NULL, scope TEXT, hash BLOB, PRIMARY KEY(script, scope));")
Lich.db.execute("CREATE TABLE IF NOT EXISTS lich_settings (name TEXT NOT NULL, value TEXT, PRIMARY KEY(name));")
Lich.db.execute("CREATE TABLE IF NOT EXISTS uservars (scope TEXT NOT NULL, hash BLOB, PRIMARY KEY(scope));")
if KEEP_SAFE
Lich.db.execute("CREATE TABLE IF NOT EXISTS trusted_scripts (name TEXT NOT NULL);")
end
Lich.db.execute("CREATE TABLE IF NOT EXISTS simu_game_entry (character TEXT NOT NULL, game_code TEXT NOT NULL, data BLOB, PRIMARY KEY(character, game_code));")
Lich.db.execute("CREATE TABLE IF NOT EXISTS enable_inventory_boxes (player_id INTEGER NOT NULL, PRIMARY KEY(player_id));")
rescue SQLite3::BusyException
sleep 0.1
retry
end
end
def Lich.class_variable_get(*a); nil; end
def Lich.class_eval(*a); nil; end
def Lich.module_eval(*a); nil; end
def Lich.log(msg)
$stderr.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}: #{msg}"
end
def Lich.msgbox(args)
if defined?(Win32)
if args[:buttons] == :ok_cancel
buttons = Win32::MB_OKCANCEL
elsif args[:buttons] == :yes_no
buttons = Win32::MB_YESNO
else
buttons = Win32::MB_OK
end
if args[:icon] == :error
icon = Win32::MB_ICONERROR
elsif args[:icon] == :question
icon = Win32::MB_ICONQUESTION
elsif args[:icon] == :warning
icon = Win32::MB_ICONWARNING
else
icon = 0
end
args[:title] ||= "Lich v#{LICH_VERSION}"
r = Win32.MessageBox(:lpText => args[:message], :lpCaption => args[:title], :uType => (buttons|icon))
if r == Win32::IDIOK
return :ok
elsif r == Win32::IDICANCEL
return :cancel
elsif r == Win32::IDIYES
return :yes
elsif r == Win32::IDINO
return :no
else
return nil
end
elsif defined?(Gtk)
if args[:buttons] == :ok_cancel
buttons = Gtk::MessageDialog::BUTTONS_OK_CANCEL
elsif args[:buttons] == :yes_no
buttons = Gtk::MessageDialog::BUTTONS_YES_NO
else
buttons = Gtk::MessageDialog::BUTTONS_OK
end
if args[:icon] == :error
type = Gtk::MessageDialog::ERROR
elsif args[:icon] == :question
type = Gtk::MessageDialog::QUESTION
elsif args[:icon] == :warning
type = Gtk::MessageDialog::WARNING
else
type = Gtk::MessageDialog::INFO
end
dialog = Gtk::MessageDialog.new(nil, Gtk::Dialog::MODAL, type, buttons, args[:message])
args[:title] ||= "Lich v#{LICH_VERSION}"
dialog.title = args[:title]
response = nil
dialog.run { |r|
response = r
dialog.destroy
}
if response == Gtk::Dialog::RESPONSE_OK
return :ok
elsif response == Gtk::Dialog::RESPONSE_CANCEL
return :cancel
elsif response == Gtk::Dialog::RESPONSE_YES
return :yes
elsif response == Gtk::Dialog::RESPONSE_NO
return :no
else
return nil
end
elsif $stdout.isatty
$stdout.puts(args[:message])
return nil
end
end
def Lich.get_simu_launcher
if defined?(Win32)
begin
launcher_key = Win32.RegOpenKeyEx(:hKey => Win32::HKEY_LOCAL_MACHINE, :lpSubKey => 'Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command', :samDesired => (Win32::KEY_ALL_ACCESS|Win32::KEY_WOW64_32KEY))[:phkResult]
launcher_cmd = Win32.RegQueryValueEx(:hKey => launcher_key, :lpValueName => 'RealCommand')[:lpData]
if launcher_cmd.nil? or launcher_cmd.empty?
launcher_cmd = Win32.RegQueryValueEx(:hKey => launcher_key)[:lpData]
end
return launcher_cmd
ensure
Win32.RegCloseKey(:hKey => launcher_key) rescue nil
end
elsif defined?(Wine)
launcher_cmd = Wine.registry_gets('HKEY_LOCAL_MACHINE\\Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command\\RealCommand')
unless launcher_cmd and not launcher_cmd.empty?
launcher_cmd = Wine.registry_gets('HKEY_LOCAL_MACHINE\\Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command\\')
end
return launcher_cmd
else
return nil
end
end
def Lich.link_to_sge
if defined?(Win32)
if Win32.admin?
begin
launcher_key = Win32.RegOpenKeyEx(:hKey => Win32::HKEY_LOCAL_MACHINE, :lpSubKey => 'Software\\Simutronics\\Launcher', :samDesired => (Win32::KEY_ALL_ACCESS|Win32::KEY_WOW64_32KEY))[:phkResult]
r = Win32.RegQueryValueEx(:hKey => launcher_key, :lpValueName => 'RealDirectory')
if (r[:return] == 0) and not r[:lpData].empty?
# already linked
return true
end
r = Win32.GetModuleFileName
unless r[:return] > 0
# fixme
return false
end
new_launcher_dir = "\"#{r[:lpFilename]}\" \"#{File.expand_path($PROGRAM_NAME)}\" "
r = Win32.RegQueryValueEx(:hKey => launcher_key, :lpValueName => 'Directory')
launcher_dir = r[:lpData]
r = Win32.RegSetValueEx(:hKey => launcher_key, :lpValueName => 'RealDirectory', :dwType => Win32::REG_SZ, :lpData => launcher_dir)
return false unless (r == 0)
r = Win32.RegSetValueEx(:hKey => launcher_key, :lpValueName => 'Directory', :dwType => Win32::REG_SZ, :lpData => new_launcher_dir)
return (r == 0)
ensure
Win32.RegCloseKey(:hKey => launcher_key) rescue nil
end
else
begin
r = Win32.GetModuleFileName
file = ((r[:return] > 0) ? r[:lpFilename] : 'rubyw.exe')
params = "#{$PROGRAM_NAME.split(/\/|\\/).last} --link-to-sge"
r = Win32.ShellExecuteEx(:lpVerb => 'runas', :lpFile => file, :lpDirectory => LICH_DIR.tr("/", "\\"), :lpParameters => params, :fMask => Win32::SEE_MASK_NOCLOSEPROCESS)
if r[:return] > 0
process_id = r[:hProcess]
sleep 0.2 while Win32.GetExitCodeProcess(:hProcess => process_id)[:lpExitCode] == Win32::STILL_ACTIVE
sleep 3
else
Win32.ShellExecute(:lpOperation => 'runas', :lpFile => file, :lpDirectory => LICH_DIR.tr("/", "\\"), :lpParameters => params)
sleep 6
end
rescue
Lich.msgbox(:message => $!)
end
end
elsif defined?(Wine)
launch_dir = Wine.registry_gets('HKEY_LOCAL_MACHINE\\Software\\Simutronics\\Launcher\\Directory')
return false unless launch_dir
lich_launch_dir = "#{File.expand_path($PROGRAM_NAME)} --wine=#{Wine::BIN} --wine-prefix=#{Wine::PREFIX} "
result = true
if launch_dir
if launch_dir =~ /lich/i
$stdout.puts "--- warning: Lich appears to already be installed to the registry"
Lich.log "warning: Lich appears to already be installed to the registry"
Lich.log 'info: launch_dir: ' + launch_dir
else
result = result && Wine.registry_puts('HKEY_LOCAL_MACHINE\\Software\\Simutronics\\Launcher\\RealDirectory', launch_dir)
result = result && Wine.registry_puts('HKEY_LOCAL_MACHINE\\Software\\Simutronics\\Launcher\\Directory', lich_launch_dir)
end
end
return result
else
return false
end
end
def Lich.unlink_from_sge
if defined?(Win32)
if Win32.admin?
begin
launcher_key = Win32.RegOpenKeyEx(:hKey => Win32::HKEY_LOCAL_MACHINE, :lpSubKey => 'Software\\Simutronics\\Launcher', :samDesired => (Win32::KEY_ALL_ACCESS|Win32::KEY_WOW64_32KEY))[:phkResult]
real_directory = Win32.RegQueryValueEx(:hKey => launcher_key, :lpValueName => 'RealDirectory')[:lpData]
if real_directory.nil? or real_directory.empty?
# not linked
return true
end
r = Win32.RegSetValueEx(:hKey => launcher_key, :lpValueName => 'Directory', :dwType => Win32::REG_SZ, :lpData => real_directory)
return false unless (r == 0)
r = Win32.RegDeleteValue(:hKey => launcher_key, :lpValueName => 'RealDirectory')
return (r == 0)
ensure
Win32.RegCloseKey(:hKey => launcher_key) rescue nil
end
else
begin
r = Win32.GetModuleFileName
file = ((r[:return] > 0) ? r[:lpFilename] : 'rubyw.exe')
params = "#{$PROGRAM_NAME.split(/\/|\\/).last} --unlink-from-sge"
r = Win32.ShellExecuteEx(:lpVerb => 'runas', :lpFile => file, :lpDirectory => LICH_DIR.tr("/", "\\"), :lpParameters => params, :fMask => Win32::SEE_MASK_NOCLOSEPROCESS)
if r[:return] > 0
process_id = r[:hProcess]
sleep 0.2 while Win32.GetExitCodeProcess(:hProcess => process_id)[:lpExitCode] == Win32::STILL_ACTIVE
sleep 3
else
Win32.ShellExecute(:lpOperation => 'runas', :lpFile => file, :lpDirectory => LICH_DIR.tr("/", "\\"), :lpParameters => params)
sleep 6
end
rescue
Lich.msgbox(:message => $!)
end
end
elsif defined?(Wine)
real_launch_dir = Wine.registry_gets('HKEY_LOCAL_MACHINE\\Software\\Simutronics\\Launcher\\RealDirectory')
result = true
if real_launch_dir and not real_launch_dir.empty?
result = result && Wine.registry_puts('HKEY_LOCAL_MACHINE\\Software\\Simutronics\\Launcher\\Directory', real_launch_dir)
result = result && Wine.registry_puts('HKEY_LOCAL_MACHINE\\Software\\Simutronics\\Launcher\\RealDirectory', '')
end
return result
else
return false
end
end
def Lich.link_to_sal
if defined?(Win32)
if Win32.admin?
begin
# fixme: 64 bit browsers?
launcher_key = Win32.RegOpenKeyEx(:hKey => Win32::HKEY_LOCAL_MACHINE, :lpSubKey => 'Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command', :samDesired => (Win32::KEY_ALL_ACCESS|Win32::KEY_WOW64_32KEY))[:phkResult]
r = Win32.RegQueryValueEx(:hKey => launcher_key, :lpValueName => 'RealCommand')
if (r[:return] == 0) and not r[:lpData].empty?
# already linked
return true
end
r = Win32.GetModuleFileName
unless r[:return] > 0
# fixme
return false
end
new_launcher_cmd = "\"#{r[:lpFilename]}\" \"#{File.expand_path($PROGRAM_NAME)}\" %1"
r = Win32.RegQueryValueEx(:hKey => launcher_key)
launcher_cmd = r[:lpData]
r = Win32.RegSetValueEx(:hKey => launcher_key, :lpValueName => 'RealCommand', :dwType => Win32::REG_SZ, :lpData => launcher_cmd)
return false unless (r == 0)
r = Win32.RegSetValueEx(:hKey => launcher_key, :dwType => Win32::REG_SZ, :lpData => new_launcher_cmd)
return (r == 0)
ensure
Win32.RegCloseKey(:hKey => launcher_key) rescue nil
end
else
begin
r = Win32.GetModuleFileName
file = ((r[:return] > 0) ? r[:lpFilename] : 'rubyw.exe')
params = "#{$PROGRAM_NAME.split(/\/|\\/).last} --link-to-sal"
r = Win32.ShellExecuteEx(:lpVerb => 'runas', :lpFile => file, :lpDirectory => LICH_DIR.tr("/", "\\"), :lpParameters => params, :fMask => Win32::SEE_MASK_NOCLOSEPROCESS)
if r[:return] > 0
process_id = r[:hProcess]
sleep 0.2 while Win32.GetExitCodeProcess(:hProcess => process_id)[:lpExitCode] == Win32::STILL_ACTIVE
sleep 3
else
Win32.ShellExecute(:lpOperation => 'runas', :lpFile => file, :lpDirectory => LICH_DIR.tr("/", "\\"), :lpParameters => params)
sleep 6
end
rescue
Lich.msgbox(:message => $!)
end
end
elsif defined?(Wine)
launch_cmd = Wine.registry_gets('HKEY_LOCAL_MACHINE\\Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command\\')
return false unless launch_cmd
new_launch_cmd = "#{File.expand_path($PROGRAM_NAME)} --wine=#{Wine::BIN} --wine-prefix=#{Wine::PREFIX} %1"
result = true
if launch_cmd
if launch_cmd =~ /lich/i
$stdout.puts "--- warning: Lich appears to already be installed to the registry"
Lich.log "warning: Lich appears to already be installed to the registry"
Lich.log 'info: launch_cmd: ' + launch_cmd
else
result = result && Wine.registry_puts('HKEY_LOCAL_MACHINE\\Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command\\RealCommand', launch_cmd)
result = result && Wine.registry_puts('HKEY_LOCAL_MACHINE\\Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command\\', new_launch_cmd)
end
end
return result
else
return false
end
end
def Lich.unlink_from_sal
if defined?(Win32)
if Win32.admin?
begin
launcher_key = Win32.RegOpenKeyEx(:hKey => Win32::HKEY_LOCAL_MACHINE, :lpSubKey => 'Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command', :samDesired => (Win32::KEY_ALL_ACCESS|Win32::KEY_WOW64_32KEY))[:phkResult]
real_directory = Win32.RegQueryValueEx(:hKey => launcher_key, :lpValueName => 'RealCommand')[:lpData]
if real_directory.nil? or real_directory.empty?
# not linked
return true
end
r = Win32.RegSetValueEx(:hKey => launcher_key, :dwType => Win32::REG_SZ, :lpData => real_directory)
return false unless (r == 0)
r = Win32.RegDeleteValue(:hKey => launcher_key, :lpValueName => 'RealCommand')
return (r == 0)
ensure
Win32.RegCloseKey(:hKey => launcher_key) rescue nil
end
else
begin
r = Win32.GetModuleFileName
file = ((r[:return] > 0) ? r[:lpFilename] : 'rubyw.exe')
params = "#{$PROGRAM_NAME.split(/\/|\\/).last} --unlink-from-sal"
r = Win32.ShellExecuteEx(:lpVerb => 'runas', :lpFile => file, :lpDirectory => LICH_DIR.tr("/", "\\"), :lpParameters => params, :fMask => Win32::SEE_MASK_NOCLOSEPROCESS)
if r[:return] > 0
process_id = r[:hProcess]
sleep 0.2 while Win32.GetExitCodeProcess(:hProcess => process_id)[:lpExitCode] == Win32::STILL_ACTIVE
sleep 3
else
Win32.ShellExecute(:lpOperation => 'runas', :lpFile => file, :lpDirectory => LICH_DIR.tr("/", "\\"), :lpParameters => params)
sleep 6
end
rescue
Lich.msgbox(:message => $!)
end
end
elsif defined?(Wine)
real_launch_cmd = Wine.registry_gets('HKEY_LOCAL_MACHINE\\Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command\\RealCommand')
result = true
if real_launch_cmd and not real_launch_cmd.empty?
result = result && Wine.registry_puts('HKEY_LOCAL_MACHINE\\Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command\\', real_launch_cmd)
result = result && Wine.registry_puts('HKEY_LOCAL_MACHINE\\Software\\Classes\\Simutronics.Autolaunch\\Shell\\Open\\command\\RealCommand', '')
end
return result
else
return false
end
end
def Lich.hosts_file
Lich.find_hosts_file if @@hosts_file.nil?
return @@hosts_file
end
def Lich.find_hosts_file
if defined?(Win32)
begin
key = Win32.RegOpenKeyEx(:hKey => Win32::HKEY_LOCAL_MACHINE, :lpSubKey => 'System\\CurrentControlSet\\Services\\Tcpip\\Parameters', :samDesired => Win32::KEY_READ)[:phkResult]