Skip to content

Commit

Permalink
Fix and add more error messages when importing mods
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Oct 27, 2023
1 parent 4c03e1b commit f90a53e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
10 changes: 8 additions & 2 deletions app/Http/Controllers/ModController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -132,14 +133,19 @@ public function getImport($provider = "", $query = "")
'mods' => $search->mods,
'pagination' => $search->pagination
])
->withErrors(new MessageBag($errors));
->withErrors(Session::has('errors') ? Session::get('errors')->merge($errors) : new MessageBag($errors));
}

public function getImportDetails($provider, $modId)
{
$mod = ModProviders::providers()[$provider]::mod($modId);
if ($mod == null) {
return redirect("mod/import/details/$provider")->withErrors(new MessageBag(['Mod not found']));
}

return view('mod.import_details')
->with([
'mod' => ModProviders::providers()[$provider]::mod($modId)
'mod' => $mod
]);
}

Expand Down
25 changes: 15 additions & 10 deletions app/Mods/ModProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class ModProvider
abstract public static function name() : string;
abstract protected static function apiUrl() : string;
abstract public static function search(string $query, int $page = 1) : object;
abstract public static function mod(string $modId) : ImportedModData;
abstract public static function mod(string $modId) : ?ImportedModData;


protected static function apiHeaders() : array
Expand Down Expand Up @@ -60,38 +60,43 @@ private static function installVersion(int $modId, string $slug, ImportedModData
return ["mod_corrupt" => "Unable to open mod file for version $version, its likely corrupt"];
}

$version = "";
$modVersion = "";

// Try load the version from forge
$forgeData = $zip->getFromName('mcmod.info');
if ($forgeData !== false) {
$tmpData = json_decode($forgeData)[0];
$version = "$tmpData->mcversion-$tmpData->version";
$modVersion = "$tmpData->mcversion-$tmpData->version";
}

// Try load the version from fabric
$fabricData = $zip->getFromName('fabric.mod.json');
if ($fabricData !== false) {
$tmpData = json_decode($fabricData);
if ($tmpData === null) {
unlink($tmpFileName);
return ["mod_corrupt" => "Unable to parse fabric.mod.json for version $version, its likely invalid"];
}

$mcVersion = "";
if (property_exists($tmpData, "depends") && property_exists($tmpData->depends, "minecraft")) {
$mcVersion = $tmpData->depends->minecraft;
$mcVersion = preg_replace("/[^0-9\.]/i", "", explode('-', $mcVersion)[0]); // Clean the depend version
$mcVersion = "$mcVersion-";
}
$version = $mcVersion.$tmpData->version;
$modVersion = $mcVersion.$tmpData->version;
}

// Try load the version from rift
$riftData = $zip->getFromName('riftmod.json');
if ($riftData !== false) {
$version = json_decode($riftData)->version;
$modVersion = json_decode($riftData)->version;
}

$zip->close();

// Make sure we have been given a version
if (empty($version)) {
if (empty($modVersion)) {
unlink($tmpFileName);
return ["version_missing" => "Unable to detect version number for $version"];
}
Expand All @@ -100,18 +105,18 @@ private static function installVersion(int $modId, string $slug, ImportedModData
// Check if the version already exists for the mod
if (Modversion::where([
'mod_id' => $modId,
'version' => $version,
'version' => $modVersion,
])->count() > 0) {
unlink($tmpFileName);
return ["version_exists" => "$version already exists"];
return ["version_exists" => "$modVersion already exists"];
}

// Check if the final path isnt a url
$location = config('solder.repo_location');
$finalPath = $location."mods/$slug/$slug-$version.zip";
$finalPath = $location."mods/$slug/$slug-$modVersion.zip";
if (filter_var($finalPath, FILTER_VALIDATE_URL)) {
unlink($tmpFileName);
return ["remote_repo" => "Mod repo in a remote location so unable to download $version"];
return ["remote_repo" => "Mod repo in a remote location so unable to download $modVersion"];
}

// Create the mod dir
Expand Down
12 changes: 8 additions & 4 deletions app/Mods/Providers/Modrinth.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ public static function search(string $query, int $page = 1) : object
];
}

public static function mod(string $modId) : ImportedModData
public static function mod(string $modId) : ?ImportedModData
{
$mod = static::request("/v2/project/$modId");
$mod->versions = static::request("/v2/project/$modId/version");
$mod->members = static::request("/v2/project/$modId/members");
$modIdSafe = urlencode($modId);
$mod = static::request("/v2/project/$modIdSafe");
if ($mod === null) {
return null;
}
$mod->versions = static::request("/v2/project/$modIdSafe/version");
$mod->members = static::request("/v2/project/$modIdSafe/members");
return static::generateModData($mod);
}

Expand Down

0 comments on commit f90a53e

Please sign in to comment.