Skip to content

Commit

Permalink
Create a wrapper around URIs to fix issues with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
comp500 committed Aug 11, 2019
1 parent 465e497 commit 5a54a90
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 97 deletions.
6 changes: 3 additions & 3 deletions src/main/java/link/infra/packwiz/installer/DownloadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import link.infra.packwiz.installer.metadata.IndexFile;
import link.infra.packwiz.installer.metadata.ManifestFile;
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
import link.infra.packwiz.installer.metadata.hash.GeneralHashingSource;
import link.infra.packwiz.installer.metadata.hash.Hash;
import link.infra.packwiz.installer.metadata.hash.HashUtils;
Expand All @@ -12,7 +13,6 @@
import okio.Source;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -74,7 +74,7 @@ void updateFromCache(ManifestFile.File cachedFile) {
}
}

void downloadMetadata(IndexFile parentIndexFile, URI indexUri) {
void downloadMetadata(IndexFile parentIndexFile, SpaceSafeURI indexUri) {
if (failure != null) return;
if (metadataRequired) {
try {
Expand All @@ -101,7 +101,7 @@ void downloadMetadata(IndexFile parentIndexFile, URI indexUri) {
}
}

void download(String packFolder, URI indexUri) {
void download(String packFolder, SpaceSafeURI indexUri) {
if (failure != null) return;

// Ensure it is removed
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/link/infra/packwiz/installer/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package link.infra.packwiz.installer;

import link.infra.packwiz.installer.metadata.SpaceSafeURI;
import link.infra.packwiz.installer.ui.CLIHandler;
import link.infra.packwiz.installer.ui.IUserInterface;
import link.infra.packwiz.installer.ui.InstallWindow;
import org.apache.commons.cli.*;

import javax.swing.*;
import java.awt.*;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {
Expand Down Expand Up @@ -37,7 +37,7 @@ public Main(String[] args) {
}
}

protected void startup(String[] args) {
private void startup(String[] args) {
Options options = new Options();
addNonBootstrapOptions(options);
addBootstrapOptions(options);
Expand Down Expand Up @@ -99,7 +99,7 @@ protected void startup(String[] args) {
}

try {
uOptions.downloadURI = new URI(unparsedArgs[0]);
uOptions.downloadURI = new SpaceSafeURI(unparsedArgs[0]);
} catch (URISyntaxException e) {
// TODO: better error message?
ui.handleExceptionAndExit(e);
Expand All @@ -124,6 +124,7 @@ protected void startup(String[] args) {
}

// Called by packwiz-installer-bootstrap to set up the help command
@SuppressWarnings("WeakerAccess")
public static void addNonBootstrapOptions(Options options) {
options.addOption("s", "side", true, "Side to install mods from (client/server, defaults to client)");
options.addOption(null, "title", true, "Title of the installer window");
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/link/infra/packwiz/installer/UpdateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import link.infra.packwiz.installer.metadata.IndexFile;
import link.infra.packwiz.installer.metadata.ManifestFile;
import link.infra.packwiz.installer.metadata.PackFile;
import link.infra.packwiz.installer.metadata.SpaceSafeURI;
import link.infra.packwiz.installer.metadata.hash.GeneralHashingSource;
import link.infra.packwiz.installer.metadata.hash.Hash;
import link.infra.packwiz.installer.metadata.hash.HashUtils;
Expand All @@ -20,7 +21,6 @@
import okio.Source;

import java.io.*;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
Expand All @@ -35,7 +35,7 @@ public class UpdateManager {
private boolean cancelledStartGame = false;

public static class Options {
URI downloadURI = null;
SpaceSafeURI downloadURI = null;
String manifestFile = "packwiz.json"; // TODO: make configurable
String packFolder = ".";
Side side = Side.CLIENT;
Expand Down Expand Up @@ -136,9 +136,9 @@ private void start() {
ui.submitProgress(new InstallProgress("Checking local files..."));

// Invalidation checking must be done here, as it must happen before pack/index hashes are checked
List<URI> invalidatedUris = new ArrayList<>();
List<SpaceSafeURI> invalidatedUris = new ArrayList<>();
if (manifest.cachedFiles != null) {
for (Map.Entry<URI, ManifestFile.File> entry : manifest.cachedFiles.entrySet()) {
for (Map.Entry<SpaceSafeURI, ManifestFile.File> entry : manifest.cachedFiles.entrySet()) {
boolean invalid = false;
// if isn't optional, or is optional but optionValue == true
if (!entry.getValue().isOptional || entry.getValue().optionValue) {
Expand All @@ -152,7 +152,7 @@ private void start() {
}
}
if (invalid) {
URI fileUri = entry.getKey();
SpaceSafeURI fileUri = entry.getKey();
System.out.println("File " + fileUri.toString() + " invalidated, marked for redownloading");
invalidatedUris.add(fileUri);
}
Expand Down Expand Up @@ -202,7 +202,7 @@ private void checkOptions() {
// TODO: implement
}

private void processIndex(URI indexUri, Hash indexHash, String hashFormat, ManifestFile manifest, List<URI> invalidatedUris) {
private void processIndex(SpaceSafeURI indexUri, Hash indexHash, String hashFormat, ManifestFile manifest, List<SpaceSafeURI> invalidatedUris) {
if (manifest.indexFileHash != null && manifest.indexFileHash.equals(indexHash) && invalidatedUris.isEmpty()) {
System.out.println("Modpack files are already up to date!");
return;
Expand Down Expand Up @@ -238,9 +238,9 @@ private void processIndex(URI indexUri, Hash indexHash, String hashFormat, Manif
}

ui.submitProgress(new InstallProgress("Checking local files..."));
Iterator<Map.Entry<URI, ManifestFile.File>> it = manifest.cachedFiles.entrySet().iterator();
Iterator<Map.Entry<SpaceSafeURI, ManifestFile.File>> it = manifest.cachedFiles.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<URI, ManifestFile.File> entry = it.next();
Map.Entry<SpaceSafeURI, ManifestFile.File> entry = it.next();
if (entry.getValue().cachedLocation != null) {
boolean alreadyDeleted = false;
// Delete if option value has been set to false
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/link/infra/packwiz/installer/metadata/IndexFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package link.infra.packwiz.installer.metadata;

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.moandjiezana.toml.Toml;
import link.infra.packwiz.installer.metadata.hash.GeneralHashingSource;
Expand All @@ -10,7 +9,6 @@
import okio.Okio;
import okio.Source;

import java.net.URI;
import java.nio.file.Paths;
import java.util.List;

Expand All @@ -20,20 +18,19 @@ public class IndexFile {
public List<File> files;

public static class File {
@JsonAdapter(SpaceSafeURIParser.class)
public URI file;
public SpaceSafeURI file;
@SerializedName("hash-format")
public String hashFormat;
public String hash;
public URI alias;
public SpaceSafeURI alias;
public boolean metafile;
public boolean preserve;

public transient ModFile linkedFile;
public transient URI linkedFileURI;
public transient SpaceSafeURI linkedFileURI;
public transient boolean optionValue = true;

public void downloadMeta(IndexFile parentIndexFile, URI indexUri) throws Exception {
public void downloadMeta(IndexFile parentIndexFile, SpaceSafeURI indexUri) throws Exception {
if (!metafile) {
return;
}
Expand All @@ -51,14 +48,14 @@ public void downloadMeta(IndexFile parentIndexFile, URI indexUri) throws Excepti
}
}

public Source getSource(URI indexUri) throws Exception {
public Source getSource(SpaceSafeURI indexUri) throws Exception {
if (metafile) {
if (linkedFile == null) {
throw new Exception("Linked file doesn't exist!");
}
return linkedFile.getSource(linkedFileURI);
} else {
URI newLoc = HandlerManager.getNewLoc(indexUri, file);
SpaceSafeURI newLoc = HandlerManager.getNewLoc(indexUri, file);
if (newLoc == null) {
throw new Exception("Index file URI is invalid");
}
Expand Down Expand Up @@ -94,13 +91,13 @@ public String getName() {
return "Invalid file";
}

public URI getDestURI() {
public SpaceSafeURI getDestURI() {
if (alias != null) {
return alias;
}
if (metafile && linkedFile != null) {
// TODO: URIs are bad
return file.resolve(linkedFile.filename.replace(" ", "%20"));
return file.resolve(linkedFile.filename);
} else {
return file;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import link.infra.packwiz.installer.UpdateManager;
import link.infra.packwiz.installer.metadata.hash.Hash;

import java.net.URI;
import java.util.Map;

public class ManifestFile {
public Hash packFileHash = null;
public Hash indexFileHash = null;
public Map<URI, File> cachedFiles;
public Map<SpaceSafeURI, File> cachedFiles;
// If the side changes, EVERYTHING invalidates. FUN!!!
public UpdateManager.Options.Side cachedSide = UpdateManager.Options.Side.CLIENT;

Expand Down
14 changes: 5 additions & 9 deletions src/main/java/link/infra/packwiz/installer/metadata/ModFile.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package link.infra.packwiz.installer.metadata;

import java.net.URI;
import java.util.Map;

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

import link.infra.packwiz.installer.UpdateManager.Options.Side;
import link.infra.packwiz.installer.metadata.hash.Hash;
import link.infra.packwiz.installer.metadata.hash.HashUtils;
import link.infra.packwiz.installer.request.HandlerManager;
import okio.Source;

import java.util.Map;

public class ModFile {
public String name;
public String filename;
public Side side;

public Download download;
public static class Download {
@JsonAdapter(SpaceSafeURIParser.class)
public URI url;
public SpaceSafeURI url;
@SerializedName("hash-format")
public String hashFormat;
public String hash;
Expand All @@ -36,14 +32,14 @@ public static class Option {
public boolean defaultValue;
}

public Source getSource(URI baseLoc) throws Exception {
public Source getSource(SpaceSafeURI baseLoc) throws Exception {
if (download == null) {
throw new Exception("Metadata file doesn't have download");
}
if (download.url == null) {
throw new Exception("Metadata file doesn't have a download URI");
}
URI newLoc = HandlerManager.getNewLoc(baseLoc, download.url);
SpaceSafeURI newLoc = HandlerManager.getNewLoc(baseLoc, download.url);
if (newLoc == null) {
throw new Exception("Metadata file URI is invalid");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package link.infra.packwiz.installer.metadata;

import java.net.URI;
import java.util.Map;

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

import java.util.Map;

public class PackFile {
public String name;

public IndexFileLoc index;
public static class IndexFileLoc {
@JsonAdapter(SpaceSafeURIParser.class)
public URI file;
public SpaceSafeURI file;
@SerializedName("hash-format")
public String hashFormat;
public String hash;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package link.infra.packwiz.installer.metadata;

import com.google.gson.annotations.JsonAdapter;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

// The world's worst URI wrapper
@JsonAdapter(SpaceSafeURIParser.class)
public class SpaceSafeURI implements Comparable<SpaceSafeURI>, Serializable {
private final URI u;

public SpaceSafeURI(String str) throws URISyntaxException {
u = new URI(str.replace(" ", "%20"));
}

public SpaceSafeURI(URI uri) {
this.u = uri;
}

public SpaceSafeURI(String scheme, String authority, String path, String query, String fragment) throws URISyntaxException {
// TODO: do all components need to be replaced?
scheme = scheme.replace(" ", "%20");
authority = authority.replace(" ", "%20");
path = path.replace(" ", "%20");
query = query.replace(" ", "%20");
fragment = fragment.replace(" ", "%20");
u = new URI(scheme, authority, path, query, fragment);
}

public String getPath() {
return u.getPath().replace("%20", " ");
}

public String toString() {
return u.toString().replace("%20", " ");
}

@SuppressWarnings("WeakerAccess")
public SpaceSafeURI resolve(String path) {
return new SpaceSafeURI(u.resolve(path.replace(" ", "%20")));
}

public SpaceSafeURI resolve(SpaceSafeURI loc) {
return new SpaceSafeURI(u.resolve(loc.u));
}

public SpaceSafeURI relativize(SpaceSafeURI loc) {
return new SpaceSafeURI(u.relativize(loc.u));
}

@Override
public boolean equals(Object obj) {
if (obj instanceof SpaceSafeURI) {
return u.equals(((SpaceSafeURI) obj).u);
}
return false;
}

@Override
public int compareTo(SpaceSafeURI uri) {
return u.compareTo(uri.u);
}

public String getScheme() {
return u.getScheme();
}

public String getAuthority() {
return u.getAuthority();
}

public String getHost() {
return u.getHost();
}

public URL toURL() throws MalformedURLException {
return u.toURL();
}
}
Loading

0 comments on commit 5a54a90

Please sign in to comment.