Fix[code]: fixes for stuff that i missed/additional validations

This commit is contained in:
artdeell 2023-10-12 16:53:24 +03:00
parent ae286fb57a
commit e6af83b2e2
8 changed files with 58 additions and 26 deletions

View File

@ -125,7 +125,8 @@ public class LauncherActivity extends BaseActivity {
} }
String normalizedVersionId = AsyncMinecraftDownloader.normalizeVersionId(prof.lastVersionId); String normalizedVersionId = AsyncMinecraftDownloader.normalizeVersionId(prof.lastVersionId);
JMinecraftVersionList.Version mcVersion = AsyncMinecraftDownloader.getListedVersion(normalizedVersionId); JMinecraftVersionList.Version mcVersion = AsyncMinecraftDownloader.getListedVersion(normalizedVersionId);
new AsyncMinecraftDownloader(this, new AsyncMinecraftDownloader().start(
this,
mcVersion, mcVersion,
normalizedVersionId, normalizedVersionId,
new ContextAwareDoneListener(this, normalizedVersionId) new ContextAwareDoneListener(this, normalizedVersionId)
@ -133,10 +134,10 @@ public class LauncherActivity extends BaseActivity {
return false; return false;
}; };
private final TaskCountListener mDoubleLaunchPreventionListener = (tc)->{ private final TaskCountListener mDoubleLaunchPreventionListener = taskCount -> {
// Hide the notification that starts the game if there are tasks executing. // Hide the notification that starts the game if there are tasks executing.
// Prevents the user from trying to launch the game with tasks ongoing. // Prevents the user from trying to launch the game with tasks ongoing.
if(tc > 0) { if(taskCount > 0) {
Tools.runOnUiThread(() -> Tools.runOnUiThread(() ->
mNotificationManager.cancel(NotificationUtils.NOTIFICATION_ID_GAME_START) mNotificationManager.cancel(NotificationUtils.NOTIFICATION_ID_GAME_START)
); );

View File

@ -559,6 +559,7 @@ public final class Tools {
private static void showError(final Context ctx, final int titleId, final String rolledMessage, final Throwable e, final boolean exitIfOk, final boolean showMore) { private static void showError(final Context ctx, final int titleId, final String rolledMessage, final Throwable e, final boolean exitIfOk, final boolean showMore) {
if(e instanceof ContextExecutorTask) { if(e instanceof ContextExecutorTask) {
ContextExecutor.execute((ContextExecutorTask) e); ContextExecutor.execute((ContextExecutorTask) e);
return;
} }
e.printStackTrace(); e.printStackTrace();

View File

@ -14,6 +14,13 @@ import androidx.annotation.Nullable;
// you to specify practically anything. So i made this instead. // you to specify practically anything. So i made this instead.
public class OpenDocumentWithExtension extends ActivityResultContract<Object, Uri> { public class OpenDocumentWithExtension extends ActivityResultContract<Object, Uri> {
private final String mimeType; private final String mimeType;
/**
* Create a new OpenDocumentWithExtension contract.
* If the extension provided to the constructor is not available in the device's MIME
* type database, the filter will default to "all types"
* @param extension the extension to filter by
*/
public OpenDocumentWithExtension(String extension) { public OpenDocumentWithExtension(String extension) {
String extensionMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); String extensionMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if(extensionMimeType == null) extensionMimeType = "*/*"; if(extensionMimeType == null) extensionMimeType = "*/*";

View File

@ -53,6 +53,10 @@ public class DownloadMirror {
DownloadUtils.downloadFileMonitored(urlInput, outputFile, buffer, monitor); DownloadUtils.downloadFileMonitored(urlInput, outputFile, buffer, monitor);
} }
/**
* Check if the current download source is a mirror and not an official source.
* @return true if the source is a mirror, false otherwise
*/
public static boolean isMirrored() { public static boolean isMirrored() {
return !LauncherPreferences.PREF_DOWNLOAD_SOURCE.equals("default"); return !LauncherPreferences.PREF_DOWNLOAD_SOURCE.equals("default");
} }
@ -88,8 +92,14 @@ public class DownloadMirror {
private static int getBaseUrlTail(String wholeUrl) throws MalformedURLException{ private static int getBaseUrlTail(String wholeUrl) throws MalformedURLException{
int protocolNameEnd = wholeUrl.indexOf("://"); int protocolNameEnd = wholeUrl.indexOf("://");
if(protocolNameEnd == -1) throw new MalformedURLException("No protocol"); if(protocolNameEnd == -1)
throw new MalformedURLException("No protocol, or non path-based URL");
protocolNameEnd += 3; protocolNameEnd += 3;
return wholeUrl.indexOf('/', protocolNameEnd); int hostnameEnd = wholeUrl.indexOf('/', protocolNameEnd);
if(protocolNameEnd >= wholeUrl.length() || hostnameEnd == protocolNameEnd)
throw new MalformedURLException("No hostname");
if(hostnameEnd == -1) hostnameEnd = wholeUrl.length();
System.out.println(protocolNameEnd +" "+ hostnameEnd);
return hostnameEnd;
} }
} }

View File

@ -18,6 +18,12 @@ public class MirrorTamperedException extends Exception implements ContextExecuto
AlertDialog.Builder builder = new AlertDialog.Builder(activity); AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.dl_tampered_manifest_title); builder.setTitle(R.string.dl_tampered_manifest_title);
builder.setMessage(Html.fromHtml(activity.getString(R.string.dl_tampered_manifest))); builder.setMessage(Html.fromHtml(activity.getString(R.string.dl_tampered_manifest)));
addButtons(builder);
ShowErrorActivity.installRemoteDialogHandling(activity, builder);
builder.show();
}
private void addButtons(AlertDialog.Builder builder) {
builder.setPositiveButton(R.string.dl_switch_to_official_site,(d,w)->{ builder.setPositiveButton(R.string.dl_switch_to_official_site,(d,w)->{
LauncherPreferences.DEFAULT_PREF.edit().putString("downloadSource", "default").apply(); LauncherPreferences.DEFAULT_PREF.edit().putString("downloadSource", "default").apply();
LauncherPreferences.PREF_DOWNLOAD_SOURCE = "default"; LauncherPreferences.PREF_DOWNLOAD_SOURCE = "default";
@ -28,8 +34,6 @@ public class MirrorTamperedException extends Exception implements ContextExecuto
LauncherPreferences.PREF_VERIFY_MANIFEST = false; LauncherPreferences.PREF_VERIFY_MANIFEST = false;
}); });
builder.setNeutralButton(android.R.string.cancel, (d,w)->{}); builder.setNeutralButton(android.R.string.cancel, (d,w)->{});
ShowErrorActivity.installRemoteDialogHandling(activity, builder);
builder.show();
} }
@Override @Override

View File

@ -88,7 +88,7 @@ public class OptiFineDownloadTask implements Runnable, Tools.DownloaderFeedback,
if(minecraftJsonVersion == null) return false; if(minecraftJsonVersion == null) return false;
try { try {
synchronized (mMinecraftDownloadLock) { synchronized (mMinecraftDownloadLock) {
new AsyncMinecraftDownloader(null, minecraftJsonVersion, minecraftVersion, this); new AsyncMinecraftDownloader().start(null, minecraftJsonVersion, minecraftVersion, this);
mMinecraftDownloadLock.wait(); mMinecraftDownloadLock.wait();
} }
}catch (InterruptedException e) { }catch (InterruptedException e) {

View File

@ -26,29 +26,37 @@ public class NotificationDownloadListener implements ModloaderDownloadListener {
if(mModLoader.requiresGuiInstallation()) { if(mModLoader.requiresGuiInstallation()) {
ModloaderInstallTracker.saveModLoader(mContext, mModLoader, downloadedFile); ModloaderInstallTracker.saveModLoader(mContext, mModLoader, downloadedFile);
Intent mainActivityIntent = new Intent(mContext, LauncherActivity.class); Intent mainActivityIntent = new Intent(mContext, LauncherActivity.class);
Tools.runOnUiThread(() -> NotificationUtils.sendBasicNotification(mContext, sendIntentNotification(mainActivityIntent, R.string.modpack_install_notification_success);
R.string.modpack_install_notification_title,
R.string.modpack_install_notification_success,
mainActivityIntent,
NotificationUtils.PENDINGINTENT_CODE_DOWNLOAD_SERVICE,
NotificationUtils.NOTIFICATION_ID_DOWNLOAD_LISTENER
));
} }
} }
@Override @Override
public void onDataNotAvailable() { public void onDataNotAvailable() {
Tools.runOnUiThread(()->NotificationUtils.sendBasicNotification(mContext, sendEmptyNotification(R.string.modpack_install_notification_data_not_available);
R.string.modpack_install_notification_title,
R.string.modpack_install_notification_success,
null,
NotificationUtils.PENDINGINTENT_CODE_DOWNLOAD_SERVICE,
NotificationUtils.NOTIFICATION_ID_DOWNLOAD_LISTENER
));
} }
@Override @Override
public void onDownloadError(Exception e) { public void onDownloadError(Exception e) {
Tools.showErrorRemote(mContext, R.string.modpack_install_modloader_download_failed, e); Tools.showErrorRemote(mContext, R.string.modpack_install_modloader_download_failed, e);
} }
private void sendIntentNotification(Intent intent, int localeString) {
Tools.runOnUiThread(() -> NotificationUtils.sendBasicNotification(mContext,
R.string.modpack_install_notification_title,
localeString,
intent,
NotificationUtils.PENDINGINTENT_CODE_DOWNLOAD_SERVICE,
NotificationUtils.NOTIFICATION_ID_DOWNLOAD_LISTENER
));
}
private void sendEmptyNotification(int localeString) {
Tools.runOnUiThread(()->NotificationUtils.sendBasicNotification(mContext,
R.string.modpack_install_notification_title,
localeString,
null,
NotificationUtils.PENDINGINTENT_CODE_DOWNLOAD_SERVICE,
NotificationUtils.NOTIFICATION_ID_DOWNLOAD_LISTENER
));
}
} }

View File

@ -48,8 +48,9 @@ public class AsyncMinecraftDownloader {
/* Allows each downloading thread to have its own RECYCLED buffer */ /* Allows each downloading thread to have its own RECYCLED buffer */
private final ConcurrentHashMap<Thread, byte[]> mThreadBuffers = new ConcurrentHashMap<>(5); private final ConcurrentHashMap<Thread, byte[]> mThreadBuffers = new ConcurrentHashMap<>(5);
public AsyncMinecraftDownloader(Activity activity, JMinecraftVersionList.Version version, String realVersion, public void start(Activity activity, JMinecraftVersionList.Version version,
@NonNull DoneListener listener){ // this was there for a reason String realVersion, // this was there for a reason
@NonNull DoneListener listener) {
sExecutorService.execute(() -> { sExecutorService.execute(() -> {
try { try {
downloadGame(activity, version, realVersion); downloadGame(activity, version, realVersion);
@ -399,10 +400,10 @@ public class AsyncMinecraftDownloader {
} }
private boolean verifyManifest(File verJsonDir, JMinecraftVersionList.Version verInfo) { private boolean verifyManifest(File verJsonDir, JMinecraftVersionList.Version verInfo) {
return /*verJsonDir.exists() return verJsonDir.exists()
&& (!LauncherPreferences.PREF_CHECK_LIBRARY_SHA && (!LauncherPreferences.PREF_CHECK_LIBRARY_SHA
|| verInfo.sha1 == null || verInfo.sha1 == null
|| Tools.compareSHA1(verJsonDir, verInfo.sha1));*/false; || Tools.compareSHA1(verJsonDir, verInfo.sha1));
} }
public static String normalizeVersionId(String versionString) { public static String normalizeVersionId(String versionString) {