forked from denisio/Dayz-Epoch-Linux-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_objects.pl
executable file
·90 lines (75 loc) · 2.26 KB
/
fix_objects.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
#!/usr/bin/perl
#
# Copyright 2014 by Denis Erygin,
#
use DBI;
use JSON::XS;
use warnings;
use strict;
use constant DB_NAME => 'epoch';
use constant DB_LOGIN => 'dayz';
use constant DB_PASSWD => 'dayz';
my $coder = JSON::XS->new->ascii(1)->shrink(1)->allow_nonref(1)->max_depth(5)->max_size(1048576);
my $dbh = connect_to_db ();
unless ($dbh) {
print "Error: Can't connect to mysql db!\n";
exit;
}
parse();
$dbh->disconnect();
#---------------------------------------------------
sub connect_to_db {
my $dbh = DBI->connect('dbi:mysql:'.DB_NAME, DB_LOGIN, DB_PASSWD,
{'PrintError' => 1, 'RaiseError' => 1, 'AutoCommit' => 1})
or die "Can't connect to mysql: $!";
$dbh->{'mysql_auto_reconnect'} = 1;
return $dbh;
}
sub parse_json {
my $str = shift;
return unless $str;
my $data;
eval { $data = $coder->decode ($str); };
print $@,"\n" if $@;
return if $@;
return $data;
}
sub parse {
my $sql = 'SELECT ObjectID, Classname, Worldspace, Inventory, Hitpoints FROM Object_DATA';
my $sth = $dbh->prepare ($sql);
my $res = $sth->execute ();
return unless $res;
my $c = 0;
my %oids = ();
while (my ($oid, $name, $worldSpace, $inventory, $hitpoints) = $sth->fetchrow_array) {
if ($worldSpace) {
unless ( parse_json($worldSpace) ) {
print STDERR "[$oid, $name](worldSpace): $worldSpace\n";
$oids{$oid} = 'Worldspace';
next;
}
}
if ($inventory) {
unless ( parse_json($inventory) ) {
print STDERR "[$oid, $name](inventory): $inventory\n";
$oids{$oid} = 'Inventory';
next;
}
}
if ($hitpoints) {
unless ( parse_json($hitpoints) ) {
print STDERR "[$oid, $name](hitpoints): $hitpoints\n";
$oids{$oid} = 'Hitpoints';
next;
}
}
$c++;
}
$sth->finish;
print STDERR "Total: $c, Corrupted: ".scalar(keys %oids)."\n";
return unless %oids;
while (my ($oid, $field) = each %oids) {
$dbh->do ('UPDATE Object_DATA SET '.$field.'=\'[]\' WHERE ObjectID='.$oid);
}
}