1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* rmarchiv.tk |
5
|
|
|
* (c) 2016-2017 by Marcel 'ryg' Hering |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace App\Console\Commands; |
9
|
|
|
|
10
|
|
|
use App\Models\GamesFile; |
11
|
|
|
use App\Helpers\PlayerHelper; |
12
|
|
|
use App\Models\PlayerFileHash; |
13
|
|
|
use Illuminate\Console\Command; |
14
|
|
|
use App\Models\PlayerFileGamefileRel; |
15
|
|
|
|
16
|
|
|
class PT extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signature = 'player:test'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'creates the hash table for all rm2k(3) files'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new command instance. |
34
|
|
|
* |
35
|
|
|
* @return void |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Execute the console command. |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function handle() |
48
|
|
|
{ |
49
|
|
|
$this->info('Lade Gamefiles ohne index.json'); |
50
|
|
|
|
51
|
|
|
//Get all game files |
52
|
|
|
$gamefiles = GamesFile::all(); |
53
|
|
|
|
54
|
|
|
$counter = 0; |
|
|
|
|
55
|
|
|
$toindexed = []; |
56
|
|
|
|
57
|
|
|
//loop all gamefiles |
58
|
|
|
foreach ($gamefiles as $gamefile) { |
59
|
|
|
echo $gamefile->id.PHP_EOL; |
60
|
|
|
//Get the maker id |
61
|
|
|
$makerid = $gamefile->game()->first()->maker_id; |
62
|
|
|
|
63
|
|
|
//Run code only for rm2k(3) |
64
|
|
|
//Todo: Engine filter can be done with Query |
|
|
|
|
65
|
|
|
if ($makerid == 2 or $makerid == 3 or $makerid == 9) { |
|
|
|
|
66
|
|
|
//Get path to uploaded files |
67
|
|
|
$path = storage_path('app/public/'.$gamefile->filename); |
68
|
|
|
|
69
|
|
|
//use only zip fiels |
70
|
|
|
//Todo: ZIP filter can be done with Query |
|
|
|
|
71
|
|
|
if ($gamefile->extension == 'zip') { |
72
|
|
|
//Open the ZIP file |
73
|
|
|
$zip = new \ZipArchive(); |
74
|
|
|
$zip->open($path); |
75
|
|
|
|
76
|
|
|
//Run through all files in ZIP |
77
|
|
|
for ($i = 0; $i < $zip->numFiles; $i++) { |
78
|
|
|
//Get the filename with fileindex |
79
|
|
|
$filename = $zip->getNameIndex($i); |
80
|
|
|
|
81
|
|
|
//Filter Directory and _MACOSX from index |
82
|
|
|
if (! ends_with($filename, '/') and ! starts_with($filename, '_MACOSX')) { |
|
|
|
|
83
|
|
|
|
84
|
|
|
//Get root path of the file |
85
|
|
|
$phelper = new PlayerHelper(); |
86
|
|
|
$imp = $phelper->getZipRootPath($filename); |
|
|
|
|
87
|
|
|
|
88
|
|
|
//if root path not '' |
89
|
|
|
if (! $imp == '') { |
90
|
|
|
$rel = new PlayerFileGamefileRel(); |
|
|
|
|
91
|
|
|
$rel->gamefile_id = $gamefile->id; |
92
|
|
|
|
93
|
|
View Code Duplication |
if (! ends_with(strtolower($imp), ['.exe', '.lmu', '.ldb', 'ini', '.dll', 'lmt', 'lsd'])) { |
|
|
|
|
94
|
|
|
$rel->orig_filename = preg_replace('/(\.\w+$)/', '', strtolower($imp)); |
95
|
|
|
} else { |
96
|
|
|
$rel->orig_filename = strtolower($imp); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
//Decompress data |
100
|
|
|
$filedata = $zip->getFromIndex($i); |
101
|
|
|
//create hash of the file |
102
|
|
|
$filehash = hash('sha1', $filedata); |
103
|
|
|
|
104
|
|
|
//get storage path to hashed directory |
105
|
|
|
$newfilepath = storage_path('app/public/games_hashed/'.substr($filehash, 0, 2).'/'); |
106
|
|
|
|
107
|
|
|
//check for directory existance |
108
|
|
|
if (! file_exists($newfilepath)) { |
109
|
|
|
mkdir($newfilepath); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
//write decompressed data to a new file to the storage path |
113
|
|
|
file_put_contents($newfilepath.$filehash, $filedata); |
114
|
|
|
|
115
|
|
|
//check for Database existance of this file |
116
|
|
|
$check = PlayerFileHash::whereFilehash($filehash)->first(); |
117
|
|
|
if (! $check) { |
118
|
|
|
//create a new record to player_file_hash table |
119
|
|
|
$pfh = new PlayerFileHash(); |
|
|
|
|
120
|
|
|
$pfh->filehash = $filehash; |
121
|
|
|
$pfh->save(); |
122
|
|
|
|
123
|
|
|
$rel->file_hash_id = $pfh->id; |
124
|
|
|
} else { |
125
|
|
|
$rel->file_hash_id = $check->id; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$rel->save(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
$zip->close(); |
133
|
|
|
} else { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->info('Es wurden '.$counter.' Gamefiles gefunden.'); |
140
|
|
|
|
141
|
|
|
$i = 0; |
|
|
|
|
142
|
|
|
foreach ($toindexed as $toindex) { |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.