Skin fix -> online fixes

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad
2022-12-23 09:35:29 +00:00
parent 94067f34cf
commit 884bd85495
8 changed files with 44 additions and 127 deletions

View File

@ -15,10 +15,10 @@ set(SRC
org/prismlauncher/launcher/impl/legacy/LegacyFrame.java
org/prismlauncher/exception/ParameterNotFoundException.java
org/prismlauncher/exception/ParseException.java
org/prismlauncher/fix/Fix.java
org/prismlauncher/fix/Fixes.java
org/prismlauncher/fix/skins/SkinFix.java
org/prismlauncher/fix/skins/Handler.java
org/prismlauncher/fix/online/OnlineFixes.java
org/prismlauncher/fix/online/SkinFix.java
org/prismlauncher/fix/online/Handler.java
org/prismlauncher/utils/Base64.java
org/prismlauncher/utils/JsonParser.java
org/prismlauncher/utils/Parameters.java

View File

@ -35,29 +35,14 @@
package org.prismlauncher.fix;
import java.util.Collections;
import java.util.List;
import org.prismlauncher.fix.skins.SkinFix;
import org.prismlauncher.fix.online.OnlineFixes;
import org.prismlauncher.utils.Parameters;
import org.prismlauncher.utils.logging.Log;
public final class Fixes {
private static final Fix[] FIXES = { new SkinFix() };
public static void apply(Parameters params) {
List<String> fixes = params.getList("fixes", Collections.<String>emptyList());
for (Fix fix : FIXES) {
if (fixes.contains(fix.getName()) && fix.isApplicable(params)) {
try {
fix.apply();
} catch (Throwable e) {
Log.error("Could not apply " + fix.getName(), e);
}
}
}
if ("true".equalsIgnoreCase(params.getString("onlineFixes", null)))
OnlineFixes.apply();
}
}

View File

@ -33,31 +33,27 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.fix;
package org.prismlauncher.fix.online;
import org.prismlauncher.utils.Parameters;
import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
public interface Fix {
import org.prismlauncher.utils.UrlUtils;
/**
* Gets the name of the fix. If the name isn't passed into the program, the fix
* won't run.
*
* @return The name
*/
String getName();
final class Handler extends URLStreamHandler {
/**
* Determines whether the fix will be run. This is additional to the name check.
*
* @param params The parameters
* @return <code>true</code> to proceed to applying the fix
*/
boolean isApplicable(Parameters params);
@Override
protected URLConnection openConnection(URL address) throws IOException {
return openConnection(address, null);
}
/**
* Applies the fix.
*/
void apply();
@Override
protected URLConnection openConnection(URL address, Proxy proxy) throws IOException {
address = SkinFix.redirect(address);
return UrlUtils.openHttpConnection(address, proxy);
}
}

View File

@ -33,15 +33,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.fix.skins;
package org.prismlauncher.fix.online;
import java.net.URL;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import org.prismlauncher.fix.Fix;
import org.prismlauncher.utils.Base64;
import org.prismlauncher.utils.Parameters;
import org.prismlauncher.utils.UrlUtils;
import org.prismlauncher.utils.logging.Log;
@ -51,31 +49,19 @@ import org.prismlauncher.utils.logging.Log;
* @see {@link Handler}
* @see {@link UrlUtils}
*/
public final class SkinFix implements Fix, URLStreamHandlerFactory {
public final class OnlineFixes implements URLStreamHandlerFactory {
@Override
public String getName() {
return "legacySkinFix";
}
@Override
public boolean isApplicable(Parameters params) {
public static void apply() {
if (!UrlUtils.isSupported() || !Base64.isSupported()) {
Log.warning("Cannot access the necessary Java internals for skin fix");
Log.warning("Turning off legacy skin fix in Settings > Miscellaneous will silence the warnings");
return false;
return;
}
return true;
}
@Override
public void apply() {
try {
URL.setURLStreamHandlerFactory(this);
URL.setURLStreamHandlerFactory(new OnlineFixes());
} catch (Error e) {
Log.warning("Cannot apply skin fix");
Log.warning("URLStreamHandlerFactory is already set");
Log.warning("Cannot apply skin fix: URLStreamHandlerFactory is already set");
Log.warning("Turning off legacy skin fix in Settings > Miscellaneous will silence the warnings");
}
}

View File

@ -1,56 +1,17 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module. An independent module is a module which is
* not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the
* library, but you are not obliged to do so. If you do not wish to do
* so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.fix.skins;
package org.prismlauncher.fix.online;
import java.io.IOException;
import java.io.InputStream;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Map;
import org.prismlauncher.utils.Base64;
import org.prismlauncher.utils.JsonParser;
import org.prismlauncher.utils.UrlUtils;
@SuppressWarnings("unchecked")
final class Handler extends URLStreamHandler {
final class SkinFix {
private URL redirect(URL address) throws IOException {
static URL redirect(URL address) throws IOException {
String skinOwner = findSkinOwner(address);
if (skinOwner != null)
return convert(skinOwner, "SKIN");
@ -62,18 +23,7 @@ final class Handler extends URLStreamHandler {
return address;
}
@Override
protected URLConnection openConnection(URL address) throws IOException {
return openConnection(address, null);
}
@Override
protected URLConnection openConnection(URL address, Proxy proxy) throws IOException {
address = redirect(address);
return UrlUtils.openHttpConnection(address, proxy);
}
private URL convert(String owner, String name) throws IOException {
private static URL convert(String owner, String name) throws IOException {
Map<String, Object> textures = getTextures(owner);
if (textures != null) {