/
eqemu_server.pl
2157 lines (1857 loc) · 76.1 KB
/
eqemu_server.pl
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/perl
###########################################################
#::: General EQEmu Server Administration Script
#::: Purpose - Handles:
#::: Automatic database versioning (bots and normal DB)
#::: Updating server assets (binary, opcodes, maps, configuration files)
#::: Original Author: Akkadius
#::: Contributors: Uleat
#::: Purpose: To upgrade databases with ease and maintain versioning
###########################################################
use Config;
use File::Copy qw(copy);
use POSIX qw(strftime);
use File::Path;
use File::Find;
use Time::HiRes qw(usleep);
#::: Variables
$install_repository_request_url = "https://raw.githubusercontent.com/Akkadius/EQEmuInstall/master/";
$eqemu_repository_request_url = "https://raw.githubusercontent.com/EQEmu/Server/master/";
#::: Globals
$time_stamp = strftime('%m-%d-%Y', gmtime());
$db_run_stage = 0; #::: Sets database run stage check
if($Config{osname}=~/freebsd|linux/i){
$OS = "Linux";
$os_flavor = "";
if(-e "/etc/debian_version"){
$os_flavor = "debian";
}
elsif(-e "/etc/fedora-release"){
$os_flavor = "fedora_core";
}
elsif(-e "/etc/redhat-release"){
$os_flavor = "red_hat";
}
}
if($Config{osname}=~/Win|MS/i){
$OS = "Windows";
}
$has_internet_connection = check_internet_connection();
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
#::: Check for script self update
do_self_update_check_routine();
get_perl_version();
read_eqemu_config_xml();
get_mysql_path();
#::: Remove old eqemu_update.pl
if(-e "eqemu_update.pl"){
unlink("eqemu_update.pl");
}
print "[Info] For EQEmu Server management utilities - run eqemu_server.pl\n" if $ARGV[0] eq "ran_from_world";
#::: Check if db_version table exists...
if(trim(get_mysql_result("SHOW COLUMNS FROM db_version LIKE 'Revision'")) ne "" && $db){
print get_mysql_result("DROP TABLE db_version");
print "[Database] Old db_version table present, dropping...\n\n";
}
check_db_version_table();
check_for_world_bootup_database_update();
sub urlencode {
my ($rv) = @_;
$rv =~ s/([^A-Za-z0-9])/sprintf("%%%2.2X", ord($1))/ge;
return $rv;
}
sub urldecode {
my ($rv) = @_;
$rv =~ s/\+/ /g;
$rv =~ s/%(..)/pack("c",hex($1))/ge;
return $rv;
}
sub analytics_insertion {
$event_name = urlencode($_[0]);
$event_data = urlencode($_[1]);
#::: Check for internet connection before doing analytics
if(!$has_internet_connection || $can_see_analytics_server == -1){
return;
}
#::: Check for analytics server connectivity so that the script doesn't break when its offline
if(!$can_see_analytics_server){
if($OS eq "Linux"){
$count = "c";
}
if($OS eq "Windows"){
$count = "n";
}
if (`ping analytics.akkadius.com -$count 1 -w 500`=~/Reply from|1 received/i) {
$can_see_analytics_server = 1;
}
else {
$can_see_analytics_server = -1;
}
}
$server_name = "";
if($long_name){
$server_name = "&server_name=" . urlencode($long_name);
}
if(!$extended_os){
if($OS eq "Linux"){
$extended_os = `cat /proc/version`;
$extended_os = trim($extended_os);
}
if($OS eq "Windows"){
my $output = `ver`;
my @os_version = split("\n", $output);
foreach my $val (@os_version){
if($val=~/Windows/i){
$extended_os = trim($val);
}
}
}
}
$url = "http://analytics.akkadius.com/";
$url .= "?api_key=24a0bde2e5bacd65bcab06a9ac40b62c";
$url .= "&event=" . $event_name;
$url .= "&event_data=" . $event_data;
$url .= "&OS=" . urlencode($OS);
$url .= "&extended_os=" . urlencode($extended_os);
$url .= $server_name;
# print "Calling url :: '" . $url . "'\n";
if($OS eq "Windows"){
eval('require LWP::UserAgent;');
my $ua = LWP::UserAgent->new;
$ua->timeout(1);
$ua->env_proxy;
my $response = $ua->get($url);
}
if($OS eq "Linux"){
$api_call = `curl -s "$url"`;
}
}
sub show_install_summary_info {
print "[Install] Installation complete...\n";
print "[Install] Server Info (Save somewhere if needed):\n";
if (-e "install_variables.txt") {
$file_to_open = "install_variables.txt";
}
elsif(-e "../install_variables.txt"){
$file_to_open = "../install_variables.txt";
}
open (INSTALL_VARS, $file_to_open);
while (<INSTALL_VARS>){
chomp;
$o = $_;
@data = split(":", $o);
print " - " . $data[0] . "\t" . $data[1] . "\n";
}
close (INSTALL_VARS);
if($OS eq "Windows"){
print "[Install] Windows Utility Scripts:\n";
print " - t_start_server.bat Starts EQEmu server with 30 dynamic zones, UCS & Queryserv, dynamic zones\n";
print " - t_start_server_with_loginserver.bat Starts EQEmu server with 30 zones with loginserver\n";
print " - t_stop_server.bat Stops EQEmu Server (No warning)\n";
print " - t_database_backup.bat Backs up the Database to backups/ folder - do not run during server is online\n";
print " - t_server_crash_report.pl Will parse any zone crashes for reporting to developers\n";
}
if($OS eq "Linux"){
print "[Install] Linux Utility Scripts:\n";
print " - server_start.sh Starts EQEmu server (Quiet) with 30 dynamic zones, UCS & Queryserv, dynamic zones\n";
print " - server_start_dev.sh Starts EQEmu server with 10 dynamic zones, UCS & Queryserv, dynamic zones all verbose\n";
print " - server_stop.sh Stops EQEmu Server (No warning)\n";
print " - server_status.sh Prints the status of the EQEmu Server processes\n";
}
print "[Configure] eqemu_config.xml Edit to change server settings and name\n";
analytics_insertion("install_complete", "null");
}
sub new_server {
$file_count = 0;
opendir(DIR, ".") or die $!;
while (my $file = readdir(DIR)) {
next if ($file =~ m/^\./);
$file_count++;
}
closedir(DIR);
if($file_count > 1 && (!-e "install_variables.txt" && !-e "../install_variables.txt")){
print "[New Server] ERROR: You must run eqemu_server.pl in an empty directory\n";
<>;
exit;
}
if(-e "install_variables.txt" || -e "../install_variables.txt"){
get_installation_variables();
}
while(1){
$database_name = $installation_variables{"mysql_eqemu_db_name"};
$database_user = $installation_variables{"mysql_eqemu_user"};
$database_password = $installation_variables{"mysql_eqemu_password"};
if($database_name ne ""){
$mysql_pass = 1;
}
else {
print "\n";
print "[New Server] For a new server folder install, we assume Perl and MySQL are configured\n";
print "[New Server] This will install a fresh PEQ Database, with all server assets\n";
print "[New Server] You will need to supply database credentials to get started...\n\n";
check_for_input("MySQL User: ");
$database_user = trim($input);
check_for_input("MySQL Password: ");
$database_password = trim($input);
$check_connection = `mysql -u $database_user -p$database_password -N -B -e "SHOW PROCESSLIST" > mysqlcheck.txt`;
$mysql_pass = 0;
open (MYSQL_CHECK, "mysqlcheck.txt");
while (<MYSQL_CHECK>){
chomp;
$o = $_;
if($o=~/Error/i){ $mysql_pass = 0;}
if($o=~/SHOW PROCESSLIST/i){ $mysql_pass = 1; }
}
close (MYSQL_CHECK);
unlink("mysqlcheck.txt");
}
if($mysql_pass == 1){
if((!-e "install_variables.txt" && !-e "../install_variables.txt")){
print "[New Server] Success! We have a database connection\n";
check_for_input("Specify a NEW database name that PEQ will be installed to: ");
$database_name = trim($input);
#::: Write install vars
open (INSTALL_VARS, '>', 'install_variables.txt');
print INSTALL_VARS "";
print INSTALL_VARS "mysql_eqemu_db_name:" . $database_name . "\n";
print INSTALL_VARS "mysql_eqemu_user:" . $database_user . "\n";
print INSTALL_VARS "mysql_eqemu_password:" . $database_password . "\n";
close (INSTALL_VARS);
}
analytics_insertion("new_server::install", $database_name);
if($OS eq "Linux"){
build_linux_source();
}
do_installer_routines();
if($OS eq "Linux"){
print `chmod 755 *.sh`;
}
analytics_insertion("new_server::install_complete", $database_name . " :: Binary DB Version / Local DB Version :: " . $binary_database_version . " / " . $local_database_version);
print "[New Server] New server folder install complete\n";
print "[New Server] Below is your installation info:\n";
show_install_summary_info();
return;
}
else {
print "[New Server] MySQL authorization failed or no MySQL installed\n";
}
}
}
sub build_linux_source {
$build_options = $_[0];
$cmake_options = "";
$source_folder_post_fix = "";
if($build_options =~/bots/i){
$cmake_options .= " -DEQEMU_ENABLE_BOTS=ON";
$source_folder_post_fix = "_bots";
}
$current_directory = `pwd`;
@directories = split('/', $current_directory);
foreach my $val (@directories){
if(trim($val) ne ""){
$last_directory = trim($val);
}
}
my $eqemu_server_directory = "/home/eqemu";
my $source_dir = $eqemu_server_directory . '/' . $last_directory . '_source' . $source_folder_post_fix;
$current_directory = trim($current_directory);
mkdir($source_dir) if (!-e $source_dir);
# print 'server_dir: ' . $eqemu_server_directory . "\n";
# print 'source_dir: ' . $source_dir . "\n";
# print 'current_dir: \'' . $current_directory . "'\n";
chdir($source_dir);
print `git clone https://github.com/EQEmu/Server.git`;
mkdir ($source_dir . "/Server/build") if (!-e $source_dir . "/Server/build");
chdir ($source_dir . "/Server/build");
print "Generating CMake build files...\n";
if($os_flavor eq "fedora_core"){
print `cmake $cmake_options -DEQEMU_BUILD_LUA=ON -DLUA_INCLUDE_DIR=/usr/include/lua-5.1/ -G "Unix Makefiles" ..`;
}
else {
print `cmake $cmake_options -DEQEMU_BUILD_LUA=ON -G "Unix Makefiles" ..`;
}
print "Building EQEmu Server code. This will take a while.";
#::: Build
print `make`;
chdir ($current_directory);
print `ln -s -f $source_dir/Server/build/bin/eqlaunch .`;
print `ln -s -f $source_dir/Server/build/bin/export_client_files .`;
print `ln -s -f $source_dir/Server/build/bin/import_client_files .`;
print `ln -s -f $source_dir/Server/build/bin/libcommon.a .`;
print `ln -s -f $source_dir/Server/build/bin/libluabind.a .`;
print `ln -s -f $source_dir/Server/build/bin/queryserv .`;
print `ln -s -f $source_dir/Server/build/bin/shared_memory .`;
print `ln -s -f $source_dir/Server/build/bin/ucs .`;
print `ln -s -f $source_dir/Server/build/bin/world .`;
print `ln -s -f $source_dir/Server/build/bin/zone .`;
}
sub do_installer_routines {
print "[Install] EQEmu Server Installer... LOADING... PLEASE WAIT...\n";
#::: Make some local server directories...
mkdir('logs');
mkdir('updates_staged');
mkdir('shared');
do_install_config_xml();
read_eqemu_config_xml();
get_installation_variables();
$db_name = "peq";
if($installation_variables{"mysql_eqemu_db_name"}){
$db_name = $installation_variables{"mysql_eqemu_db_name"};
}
#::: Download assets
if($OS eq "Windows"){
fetch_latest_windows_binaries();
get_remote_file($install_repository_request_url . "lua51.dll", "lua51.dll", 1);
get_remote_file($install_repository_request_url . "zlib1.dll", "zlib1.dll", 1);
get_remote_file($install_repository_request_url . "libmysql.dll", "libmysql.dll", 1);
}
map_files_fetch_bulk();
opcodes_fetch();
plugins_fetch();
quest_files_fetch();
lua_modules_fetch();
fetch_utility_scripts();
#::: Database Routines
print "[Database] Creating Database '" . $db_name . "'\n";
print `"$path" --host $host --user $user --password="$pass" -N -B -e "DROP DATABASE IF EXISTS $db_name;"`;
print `"$path" --host $host --user $user --password="$pass" -N -B -e "CREATE DATABASE $db_name"`;
#::: Get Binary DB version
if($OS eq "Windows"){ @db_version = split(': ', `world db_version`); }
if($OS eq "Linux"){ @db_version = split(': ', `./world db_version`); }
$binary_database_version = trim($db_version[1]);
#::: Local DB Version
check_db_version_table();
$local_database_version = trim(get_mysql_result("SELECT version FROM db_version LIMIT 1"));
#::: Download PEQ latest
fetch_peq_db_full();
print "[Database] Fetching Latest Database Updates...\n";
main_db_management();
print "[Database] Applying Latest Database Updates...\n";
main_db_management();
remove_duplicate_rule_values();
if($OS eq "Windows"){
check_windows_firewall_rules();
do_windows_login_server_setup();
}
if($OS eq "Linux"){
do_linux_login_server_setup();
}
}
sub check_for_input {
print "[Input] " . $_[0];
$input = <STDIN>;
chomp $input;
}
sub check_for_world_bootup_database_update {
if($OS eq "Windows"){
@db_version = split(': ', `world db_version`);
}
if($OS eq "Linux"){
@db_version = split(': ', `./world db_version`);
}
$binary_database_version = trim($db_version[1]);
$local_database_version = trim(get_mysql_result("SELECT version FROM db_version LIMIT 1"));
#::: Bots
$bots_binary_version = trim($db_version[2]);
if($bots_binary_version > 0){
$bots_local_db_version = get_bots_db_version();
#::: We ran world - Database needs to update, lets backup and run updates and continue world bootup
if($bots_local_db_version < $bots_binary_version && $ARGV[0] eq "ran_from_world"){
print "[Update] Bots Database not up to date with binaries... Automatically updating...\n";
print "[Update] Issuing database backup first...\n";
database_dump_compress();
print "[Update] Updating bots database...\n";
sleep(1);
bots_db_management();
run_database_check();
print "[Update] Continuing bootup\n";
analytics_insertion("auto database bots upgrade world", $db . " :: Binary DB Version / Local DB Version :: " . $binary_database_version . " / " . $local_database_version);
exit;
}
else {
print "[Update] Bots database up to Date: Continuing World Bootup...\n";
}
}
if($binary_database_version == $local_database_version && $ARGV[0] eq "ran_from_world"){
print "[Update] Database up to date...\n";
exit;
}
else {
#::: We ran world - Database needs to update, lets backup and run updates and continue world bootup
if($local_database_version < $binary_database_version && $ARGV[0] eq "ran_from_world"){
print "[Update] Database not up to date with binaries... Automatically updating...\n";
print "[Update] Issuing database backup first...\n";
database_dump_compress();
print "[Update] Updating database...\n";
sleep(1);
main_db_management();
main_db_management();
print "[Update] Continuing bootup\n";
analytics_insertion("auto database upgrade world", $db . " :: Binary DB Version / Local DB Version :: " . $binary_database_version . " / " . $local_database_version);
exit;
}
#::: Make sure that we didn't pass any arugments to the script
else {
if(!$db){ print "[eqemu_server.pl] No database connection found... Running without\n"; }
show_menu_prompt();
}
}
}
sub check_internet_connection {
if($OS eq "Linux"){
$count = "c";
}
if($OS eq "Windows"){
$count = "n";
}
if (`ping 8.8.8.8 -$count 1 -w 500`=~/TTL|1 received/i) {
# print "[Update] We have a connection to the internet, continuing...\n";
return 1;
}
elsif (`ping 4.2.2.2 -$count 1 -w 500`=~/TTL|1 received/i) {
# print "[Update] We have a connection to the internet, continuing...\n";
return 1;
}
else{
print "[Update] No connection to the internet, can't check update\n";
return;
}
}
sub get_perl_version {
#::: Check Perl version
$perl_version = $^V;
$perl_version =~s/v//g;
print "[Update] Perl Version is " . $perl_version . "\n" if $debug;
if($perl_version > 5.12){
no warnings 'uninitialized';
}
no warnings;
}
sub do_self_update_check_routine {
#::: Check for internet connection before updating
if(!$has_internet_connection){
print "[Update] Cannot check update without internet connection...\n";
return;
}
#::: Check for script changes :: eqemu_server.pl
get_remote_file($eqemu_repository_request_url . "utils/scripts/eqemu_server.pl", "updates_staged/eqemu_server.pl", 0, 1, 1);
if(-e "updates_staged/eqemu_server.pl") {
my $remote_script_size = -s "updates_staged/eqemu_server.pl";
my $local_script_size = -s "eqemu_server.pl";
if($remote_script_size != $local_script_size){
print "[Update] Script has been updated, updating...\n";
my @files;
my $start_dir = "updates_staged/";
find(
sub { push @files, $File::Find::name unless -d; },
$start_dir
);
for my $file (@files) {
if($file=~/eqemu_server/i){
$destination_file = $file;
$destination_file =~s/updates_staged\///g;
print "[Install] Installing :: " . $destination_file . "\n";
unlink($destination_file);
copy_file($file, $destination_file);
if($OS eq "Linux"){
system("chmod 755 eqemu_server.pl");
}
system("perl eqemu_server.pl start_from_world");
}
}
print "[Install] Done\n";
}
else {
print "[Update] No script update necessary...\n";
}
unlink("updates_staged/eqemu_server.pl");
}
}
sub get_installation_variables{
#::: Fetch installation variables before building the config
if($OS eq "Linux"){
open (INSTALL_VARS, "../install_variables.txt");
}
if($OS eq "Windows"){
open (INSTALL_VARS, "install_variables.txt");
}
while (<INSTALL_VARS>){
chomp;
$o = $_;
@data = split(":", $o);
$installation_variables{trim($data[0])} = trim($data[1]);
}
close (INSTALL_VARS);
}
sub do_install_config_xml {
get_installation_variables();
#::: Fetch XML template
get_remote_file($install_repository_request_url . "eqemu_config.xml", "eqemu_config_template.xml");
#::: Open new config file
open (NEW_CONFIG, '>', 'eqemu_config.xml');
$in_database_tag = 0;
#::: Iterate through template and replace variables...
open (FILE_TEMPLATE, "eqemu_config_template.xml");
while (<FILE_TEMPLATE>){
chomp;
$o = $_;
#::: Find replace variables
if($o=~/\<\!--/i){
next;
}
if($o=~/database/i && $o=~/\<\//i){
$in_database_tag = 0;
}
if($o=~/database/i){
$in_database_tag = 1;
}
if($o=~/key/i){
my($replace_key) = $o =~ />(\w+)</;
$new_key = generate_random_password(30);
$o =~ s/$replace_key/$new_key/g;
}
if($o=~/\<longname\>/i){
my($replace_name) = $o =~ /<longname>(.*)<\/longname>/;
$append = '(' . generate_random_password(5) . ')';
$o =~ s/$replace_name/Akkas $OS PEQ Installer $append/g;
}
if($o=~/\<username\>/i && $in_database_tag){
my($replace_username) = $o =~ />(\w+)</;
$o =~ s/$replace_username/$installation_variables{"mysql_eqemu_user"}/g;
}
if($o=~/\<password\>/i && $in_database_tag){
my($replace_password) = $o =~ />(\w+)</;
$o =~ s/$replace_password/$installation_variables{"mysql_eqemu_password"}/g;
}
if($o=~/\<db\>/i){
my($replace_db_name) = $o =~ />(\w+)</;
#::: There is really no reason why this shouldn't be set
if($installation_variables{"mysql_eqemu_db_name"}){
$db_name = $installation_variables{"mysql_eqemu_db_name"};
}
else {
$db_name = "peq";
}
$o =~ s/$replace_db_name/$db_name/g;
}
print NEW_CONFIG $o . "\n";
}
close(FILE_TEMPLATE);
close(NEW_CONFIG);
unlink("eqemu_config_template.xml");
}
sub fetch_utility_scripts {
if($OS eq "Windows"){
get_remote_file($install_repository_request_url . "t_database_backup.bat", "t_database_backup.bat");
get_remote_file($install_repository_request_url . "t_start_server.bat", "t_start_server.bat");
get_remote_file($install_repository_request_url . "t_start_server_with_login_server.bat", "t_start_server_with_login_server.bat");
get_remote_file($install_repository_request_url . "t_stop_server.bat", "t_stop_server.bat");
get_remote_file($install_repository_request_url . "t_server_crash_report.pl", "t_server_crash_report.pl");
get_remote_file($install_repository_request_url . "win_server_launcher.pl", "win_server_launcher.pl");
get_remote_file($install_repository_request_url . "t_start_server_with_login_server.bat", "t_start_server_with_login_server.bat");
}
else {
get_remote_file($install_repository_request_url . "linux/server_launcher.pl", "server_launcher.pl");
get_remote_file($install_repository_request_url . "linux/server_start.sh", "server_start.sh");
get_remote_file($install_repository_request_url . "linux/server_start_dev.sh", "server_start_dev.sh");
get_remote_file($install_repository_request_url . "linux/server_status.sh", "server_status.sh");
get_remote_file($install_repository_request_url . "linux/server_stop.sh", "server_stop.sh");
}
}
sub setup_bots {
if($OS eq "Windows"){
fetch_latest_windows_binaries_bots();
}
if($OS eq "Linux"){
build_linux_source("bots");
}
bots_db_management();
run_database_check();
print "Bots should be setup, run your server and the #bot command should be available in-game\n";
}
sub show_menu_prompt {
$dc = 0;
while (1) {
if($ARGV[0] ne ""){
$input = trim($ARGV[0]);
}
else {
$input = trim($input);
}
$errored_command = 0;
if($input eq "database"){
print "\n>>> Database Menu\n\n";
print " [backup_database] Back up database to backups/ directory\n";
print " [backup_player_tables] Back up player tables to backups/ directory\n";
print " [backup_database_compressed] Back up database compressed to backups/ directory\n";
print " \n";
print " [check_db_updates] Checks for database updates manually\n";
print " [check_bot_db_updates] Checks for bot database updates\n";
print " \n";
print " [aa_tables] Downloads and installs clean slate AA data from PEQ\n";
print " [remove_duplicate_rules] Removes duplicate rules from rule_values table\n";
print " [drop_bots_db_schema] Removes bot database schema\n";
print " \n> main - go back to main menu\n";
print "Enter a command #> ";
$last_menu = trim($input);
}
elsif($input eq "assets"){
print "\n>>> Server Assets Menu\n\n";
print " [maps] Download latest maps\n";
print " [opcodes] Download opcodes (Patches for eq clients)\n";
print " [quests] Download latest quests\n";
print " [plugins] Download latest plugins\n";
print " [lua_modules] Download latest lua_modules\n";
print " [utility_scripts] Download utility scripts to run and operate the EQEmu Server\n";
if($OS eq "Windows"){
print ">>> Windows\n";
print " [windows_server_download] Updates server code from latest stable\n";
print " [windows_server_download_bots] Updates server code (bots enabled) from latest\n";
print " [fetch_dlls] Grabs dll's needed to run windows binaries\n";
print " [setup_loginserver] Sets up loginserver for Windows\n";
}
print " \n> main - go back to main menu\n";
print "Enter a command #> ";
$last_menu = trim($input);
}
elsif($input eq "backup_database"){ database_dump(); $dc = 1; }
elsif($input eq "backup_player_tables"){ database_dump_player_tables(); $dc = 1; }
elsif($input eq "backup_database_compressed"){ database_dump_compress(); $dc = 1; }
elsif($input eq "drop_bots_db_schema"){ do_bots_db_schema_drop(); $dc = 1; }
elsif($input eq "aa_tables"){ aa_fetch(); $dc = 1; }
elsif($input eq "remove_duplicate_rules"){ remove_duplicate_rule_values(); $dc = 1; }
elsif($input eq "maps"){ map_files_fetch_bulk(); $dc = 1; }
elsif($input eq "opcodes"){ opcodes_fetch(); $dc = 1; }
elsif($input eq "plugins"){ plugins_fetch(); $dc = 1; }
elsif($input eq "quests"){ quest_files_fetch(); $dc = 1; }
elsif($input eq "lua_modules"){ lua_modules_fetch(); $dc = 1; }
elsif($input eq "windows_server_download"){ fetch_latest_windows_binaries(); $dc = 1; }
elsif($input eq "windows_server_download_bots"){ fetch_latest_windows_binaries_bots(); $dc = 1; }
elsif($input eq "fetch_dlls"){ fetch_server_dlls(); $dc = 1; }
elsif($input eq "utility_scripts"){ fetch_utility_scripts(); $dc = 1; }
elsif($input eq "check_db_updates"){ main_db_management(); main_db_management(); $dc = 1; }
elsif($input eq "check_bot_db_updates"){ bots_db_management(); run_database_check(); $dc = 1; }
elsif($input eq "setup_loginserver"){ do_windows_login_server_setup(); $dc = 1; }
elsif($input eq "new_server"){ new_server(); $dc = 1; }
elsif($input eq "setup_bots"){ setup_bots(); $dc = 1; }
elsif($input eq "exit"){
exit;
}
elsif($input eq "main"){
print "Returning to main menu...\n";
print_main_menu();
$last_menu = trim($input);
}
elsif($input eq "" && $last_menu ne ""){
$errored_command = 1;
}
elsif($input ne ""){
print "Invalid command '" . $input . "'\n";
$errored_command = 1;
}
else {
print_main_menu();
}
#::: Errored command checking
if($errored_command == 1){
$input = $last_menu;
}
elsif($dc == 1){
analytics_insertion("menu", trim($input));
$dc = 0;
$input = "";
}
else {
$input = <>;
}
#::: If we're processing a CLI command, kill the loop
if($ARGV[0] ne ""){
analytics_insertion("cli", trim($input));
$input = "";
$ARGV[0] = "";
exit;
}
}
}
sub print_main_menu {
print "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
print ">>> EQEmu Server Main Menu >>>>>>>>>>>>\n";
print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n";
print " [database] Enter database management menu \n";
print " [assets] Manage server assets \n";
print " [new_server] New folder EQEmu/PEQ install - Assumes MySQL/Perl installed \n";
print " [setup_bots] Enables bots on server - builds code and database requirements \n";
print "\n";
print " exit \n";
print "\n";
print "Enter a command #> ";
}
sub get_mysql_path {
if($OS eq "Windows"){
$has_mysql_path = `echo %PATH%`;
if($has_mysql_path=~/MySQL|MariaDB/i){
@mysql = split(';', $has_mysql_path);
foreach my $v (@mysql){
if($v=~/MySQL|MariaDB/i){
$v =~s/\n//g;
$path = trim($v) . "/mysql";
last;
}
}
}
}
if($OS eq "Linux"){
$path = `which mysql`;
if ($path eq "") {
$path = `which mariadb`;
}
$path =~s/\n//g;
}
#::: Path not found, error and exit
if($path eq ""){
print "[Error:eqemu_server.pl] MySQL path not found, please add the path for automatic database upgrading to continue... \n\n";
exit;
}
}
sub check_for_database_dump_script{
#::: Check for internet connection before updating
if(!$has_internet_connection){
print "[Update] Cannot check update without internet connection...\n";
return;
}
#::: Check for script changes :: db_dumper.pl
get_remote_file($eqemu_repository_request_url . "utils/scripts/db_dumper.pl", "updates_staged/db_dumper.pl", 0, 1, 1);
if(-e "updates_staged/db_dumper.pl") {
my $remote_script_size = -s "updates_staged/db_dumper.pl";
my $local_script_size = -s "db_dumper.pl";
if($remote_script_size != $local_script_size){
print "[Update] Script has been updated, updating...\n";
my @files;
my $start_dir = "updates_staged/";
find(
sub { push @files, $File::Find::name unless -d; },
$start_dir
);
for my $file (@files) {
if($file=~/db_dumper/i){
$destination_file = $file;
$destination_file =~s/updates_staged\///g;
print "[Install] Installing :: " . $destination_file . "\n";
unlink($destination_file);
copy_file($file, $destination_file);
if($OS eq "Linux"){
system("chmod 755 db_dumper.pl");
}
}
}
print "[Install] Done\n";
}
else {
print "[Update] No script update necessary...\n";
}
unlink("updates_staged/db_dumper.pl");
}
return;
}
sub database_dump {
check_for_database_dump_script();
print "[Database] Performing database backup....\n";
print `perl db_dumper.pl database="$db" loc="backups"`;
}
sub database_dump_player_tables {
check_for_database_dump_script();
print "[Database] Performing database backup of player tables....\n";
get_remote_file($eqemu_repository_request_url . "utils/sql/character_table_list.txt", "backups/character_table_list.txt");
$tables = "";
open (FILE, "backups/character_table_list.txt");
$i = 0;
while (<FILE>){
chomp;
$o = $_;
$tables .= $o . ",";
}
$tables = substr($tables, 0, -1);
print `perl db_dumper.pl database="$db" loc="backups" tables="$tables" backup_name="player_tables_export" nolock`;
print "[Database] Press any key to continue...\n";
<>; #Read from STDIN
}
sub database_dump_compress {
check_for_database_dump_script();
print "[Database] Performing database backup....\n";
print `perl db_dumper.pl database="$db" loc="backups" compress`;
}
sub script_exit{
#::: Cleanup staged folder...
rmtree("updates_staged/");
exit;
}
sub check_db_version_table{
if(get_mysql_result("SHOW TABLES LIKE 'db_version'") eq "" && $db){
print get_mysql_result("
CREATE TABLE db_version (
version int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO db_version (version) VALUES ('1000');");
print "[Database] Table 'db_version' does not exists.... Creating...\n\n";
}
}
#::: Returns Tab Delimited MySQL Result from Command Line
sub get_mysql_result{
my $run_query = $_[0];
if(!$db){ return; }
if($OS eq "Windows"){ return `"$path" --host $host --user $user --password="$pass" $db -N -B -e "$run_query"`; }
if($OS eq "Linux"){
$run_query =~s/`//g;
return `$path --user="$user" --host $host --password="$pass" $db -N -B -e "$run_query"`;
}
}
sub get_mysql_result_from_file{
my $update_file = $_[0];
if(!$db){ return; }
if($OS eq "Windows"){ return `"$path" --host $host --user $user --password="$pass" --force $db < $update_file`; }
if($OS eq "Linux"){ return `"$path" --host $host --user $user --password="$pass" --force $db < $update_file`; }
}
#::: Gets Remote File based on request_url (1st Arg), and saves to destination file (2nd Arg)
#::: Example: get_remote_file($eqemu_repository_request_url . "utils/sql/db_update_manifest.txt", "db_update/db_update_manifest.txt");
sub get_remote_file{
my $request_url = $_[0];
my $destination_file = $_[1];
my $content_type = $_[2];
my $no_retry = $_[3];
my $silent_download = $_[4];
if(!$has_internet_connection){
print "[Download] Cannot download without internet connection...\n";
return;
}
#::: Build file path of the destination file so that we may check for the folder's existence and make it if necessary
if($destination_file=~/\//i){
my @directory_path = split('/', $destination_file);
$build_path = "";
$directory_index = 0;
while($directory_path[$directory_index] && $directory_path[$directory_index + 1]){
$build_path .= $directory_path[$directory_index] . "/";
# print "checking '" . $build_path . "'\n";
#::: If path does not exist, create the directory...
if (!-d $build_path) {
print "[Copy] folder doesn't exist, creating '" . $build_path . "'\n";
mkdir($build_path);
}
if(!$directory_indexr_path[$directory_index + 2] && $directory_indexr_path[$directory_index + 1]){
# print $actual_path . "\n";
$actual_path = $build_path;
last;
}
$directory_index++;
}
}
if($OS eq "Windows"){
#::: For non-text type requests...
if($content_type == 1){
$break = 0;
while($break == 0) {
eval "use LWP::Simple qw(getstore);";
# use LWP::Simple qw(getstore);
# print "request is " . $request_url . "\n";