Skip to content

Commit 76897d9

Browse files
authored
Merge pull request #21 from factorio-item-browser/bugfix/mod-extract
bugfix/mod-extract
2 parents 8795b1d + 0090fc0 commit 76897d9

File tree

8 files changed

+36
-40
lines changed

8 files changed

+36
-40
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.0.7 - 2020-05-23
4+
5+
### Fixed
6+
7+
- Failing to correctly detect the directory within the mod files for certain mods.
8+
39
## 2.0.6 - 2020-05-15
410

511
### Fixed

composer.lock

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/autoload/base.global.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
ConfigAggregator::ENABLE_CACHE => true,
1818
'debug' => false,
1919
'name' => 'Factorio Item Browser - Export',
20-
'version' => '2.0.6',
20+
'version' => '2.0.7',
2121
];

src/Mod/ModDownloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ protected function handleProcessFinish(ModDownloadProcess $process): void
259259
}
260260

261261
$this->console->writeAction(sprintf('Extracting %s', $process->getMod()->getName()));
262-
$this->modFileManager->extractModZip($process->getDestinationFile());
262+
$this->modFileManager->extractModZip($process->getMod()->getName(), $process->getDestinationFile());
263263
unlink($process->getDestinationFile());
264264
}
265265
}

src/Mod/ModFileManager.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ class ModFileManager
2727
*/
2828
protected const FILENAME_INFO = 'info.json';
2929

30-
/**
31-
* The regular expression used to match the mod directory.
32-
*/
33-
protected const REGEXP_MOD_DIRECTORY = '#^(.*)_\d+\.\d+\.\d+/#';
34-
3530
/**
3631
* The serializer.
3732
* @var SerializerInterface
@@ -65,10 +60,11 @@ public function __construct(SerializerInterface $exportSerializer, string $facto
6560

6661
/**
6762
* Extracts the zip file into the working directory of the mods.
63+
* @param string $modName
6864
* @param string $modZipPath
69-
* @throws ExportException
65+
* @throws InvalidModFileException
7066
*/
71-
public function extractModZip(string $modZipPath): void
67+
public function extractModZip(string $modName, string $modZipPath): void
7268
{
7369
$zipArchive = new ZipArchive();
7470
$success = $zipArchive->open($modZipPath);
@@ -77,26 +73,20 @@ public function extractModZip(string $modZipPath): void
7773
}
7874

7975
try {
80-
$firstStat = $zipArchive->statIndex(0);
81-
if ($firstStat === false || preg_match(self::REGEXP_MOD_DIRECTORY, $firstStat['name'], $match) !== 1) {
82-
throw new InvalidModFileException($modZipPath, 'Unable to determine mod directory.');
83-
}
84-
$modDirectory = $match[0];
85-
$modDirectoryLength = strlen($modDirectory);
86-
$modName = $match[1];
87-
8876
$this->removeModDirectory($modName);
8977

9078
$targetDirectory = $this->getLocalDirectory($modName);
9179
mkdir($targetDirectory, 0777, true);
9280
for ($i = 0; $i < $zipArchive->numFiles; ++$i) {
9381
$stat = $zipArchive->statIndex($i);
82+
9483
if (
9584
$stat !== false
9685
&& substr($stat['name'], -1) !== '/' // Ignore directories
97-
&& substr($stat['name'], 0, $modDirectoryLength) === $modDirectory
86+
&& strpos($stat['name'], '/') !== false
9887
) {
99-
$fileName = $targetDirectory . '/' . substr($stat['name'], $modDirectoryLength);
88+
$fileName = $targetDirectory . substr($stat['name'], strpos($stat['name'], '/'));
89+
10090
if (!is_dir(dirname($fileName))) {
10191
mkdir(dirname($fileName), 0777, true);
10292
}
-361 Bytes
Binary file not shown.

test/src/Mod/ModDownloaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ public function testHandleProcessFinish(): void
584584

585585
$this->modFileManager->expects($this->once())
586586
->method('extractModZip')
587-
->with($this->identicalTo($destinationFile));
587+
->with($this->identicalTo($modName), $this->identicalTo($destinationFile));
588588

589589
$this->assertTrue($directory->hasChild('test.zip'));
590590

test/src/Mod/ModFileManagerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testExtractModZip(): void
8686
->with($this->identicalTo($expectedModName))
8787
->willReturn($targetDirectory);
8888

89-
$manager->extractModZip(__DIR__ . '/../../asset/mod/valid.zip');
89+
$manager->extractModZip('foo', __DIR__ . '/../../asset/mod/valid.zip');
9090

9191
$this->assertSame('abc', file_get_contents(vfsStream::url('root/foo/abc')));
9292
$this->assertSame('def', file_get_contents(vfsStream::url('root/foo/def')));
@@ -105,7 +105,7 @@ public function testExtractModZipWithInvalidZip(): void
105105
$this->expectException(InvalidModFileException::class);
106106

107107
$manager = new ModFileManager($this->serializer, 'foo', 'bar');
108-
$manager->extractModZip(__DIR__ . '/../../asset/mod/invalid.zip');
108+
$manager->extractModZip('foo', __DIR__ . '/../../asset/mod/invalid.zip');
109109
}
110110

111111
/**
@@ -118,7 +118,7 @@ public function testExtractModZipWithInvalidModDirectory(): void
118118
$this->expectException(InvalidModFileException::class);
119119

120120
$manager = new ModFileManager($this->serializer, 'foo', 'bar');
121-
$manager->extractModZip(__DIR__ . '/../../asset/mod/invalidDirectory.zip');
121+
$manager->extractModZip('foo', __DIR__ . '/../../asset/mod/invalidDirectory.zip');
122122
}
123123

124124
/**

0 commit comments

Comments
 (0)