Fixed crash when a version inherits from a nonexistent version. Closes #269

This commit is contained in:
huangyuhui 2018-02-06 16:40:31 +08:00
parent 08a31a92f2
commit 35047477fe
7 changed files with 29 additions and 14 deletions

View File

@ -137,7 +137,7 @@ public class DefaultGameRepository implements GameRepository {
FileUtils.writeText(toJson, Constants.GSON.toJson(fromVersion.setId(to)));
return true;
} catch (IOException | JsonSyntaxException | VersionNotFoundException e) {
} catch (IOException | JsonParseException | VersionNotFoundException e) {
return false;
}
}
@ -158,6 +158,8 @@ public class DefaultGameRepository implements GameRepository {
versions.put(version.getId(), version);
}
SimpleVersionProvider provider = new SimpleVersionProvider();
File[] files = new File(getBaseDirectory(), "versions").listFiles();
if (files != null)
for (File dir : files)
@ -194,15 +196,25 @@ public class DefaultGameRepository implements GameRepository {
try {
FileUtils.writeText(json, Constants.GSON.toJson(version));
} catch (Exception e) {
Logging.LOG.log(Level.WARNING, "Ignoring version {0} because wrong id {1} is set and cannot correct it.", new Object[] { id, version.getId() });
Logging.LOG.log(Level.WARNING, "Ignoring version {0} because wrong id {1} is set and cannot correct it.", new Object[]{id, version.getId()});
continue;
}
}
if (EventBus.EVENT_BUS.fireEvent(new LoadedOneVersionEvent(this, version)) != Event.Result.DENY)
versions.put(id, version);
provider.addVersion(version);
}
for (Version version : provider.getVersionMap().values()) {
try {
Version resolved = version.resolve(provider);
if (EventBus.EVENT_BUS.fireEvent(new LoadedOneVersionEvent(this, resolved)) != Event.Result.DENY)
versions.put(version.getId(), version);
} catch (VersionNotFoundException e) {
Logging.LOG.log(Level.WARNING, "Ignoring version {0} because it inherits from a nonexistent version.");
}
}
loaded = true;
}

View File

@ -46,9 +46,10 @@ public interface GameRepository extends VersionProvider {
*
* @param id the id of version
* @return the version you want
* @throws VersionNotFoundException if no version is id.
*/
@Override
Version getVersion(String id);
Version getVersion(String id) throws VersionNotFoundException;
/**
* How many version are there?
@ -125,7 +126,7 @@ public interface GameRepository extends VersionProvider {
* @param version version id
* @return the minecraft jar
*/
default File getVersionJar(String version) {
default File getVersionJar(String version) throws VersionNotFoundException {
return getVersionJar(getVersion(version).resolve(this));
}
@ -135,8 +136,7 @@ public interface GameRepository extends VersionProvider {
* @param from The id of original version
* @param to The new id of the version
* @throws UnsupportedOperationException if this game repository does not support renaming a version
* @throws java.io.IOException if I/O operation fails.
* @return true if the operation is done successfully.
* @return true if the operation is done successfully, false if version `from` not found, version json is malformed or I/O errors occurred.
*/
boolean renameVersion(String from, String to);
@ -147,7 +147,6 @@ public interface GameRepository extends VersionProvider {
*
* @param version the id of specific version that is relevant to [assetId]
* @param assetId the asset id, you can find it in [AssetIndexInfo.id] [Version.actualAssetIndex.id]
* @throws java.io.IOException if I/O operation fails.
* @return the actual asset directory
*/
File getActualAssetDirectory(String version, String assetId);

View File

@ -44,4 +44,8 @@ public class SimpleVersionProvider implements VersionProvider {
public void addVersion(Version version) {
versionMap.put(version.getId(), version);
}
public Map<String, Version> getVersionMap() {
return versionMap;
}
}

View File

@ -143,11 +143,11 @@ public class Version implements Comparable<Version>, Validation {
/**
* Resolve given version
*/
public Version resolve(VersionProvider provider) {
public Version resolve(VersionProvider provider) throws VersionNotFoundException {
return resolve(provider, new HashSet<>());
}
protected Version resolve(VersionProvider provider, Set<String> resolvedSoFar) {
protected Version resolve(VersionProvider provider, Set<String> resolvedSoFar) throws VersionNotFoundException {
if (inheritsFrom == null)
return this;

View File

@ -39,5 +39,5 @@ public interface VersionProvider {
* @param id the id of version
* @return the version you want
*/
Version getVersion(String id);
Version getVersion(String id) throws VersionNotFoundException;
}

View File

@ -203,7 +203,7 @@ public class FileDownloadTask extends Task {
if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile()))
throw new IOException("Unable to make parent directory " + file);
try {
FileUtils.cutFile(temp, file);
FileUtils.moveFile(temp, file);
} catch (Exception e) {
throw new IOException("Unable to move temp file from " + temp + " to " + file, e);
}

View File

@ -236,7 +236,7 @@ public final class FileUtils {
doCopyFile(srcFile, destFile);
}
public static void cutFile(File srcFile, File destFile) throws IOException {
public static void moveFile(File srcFile, File destFile) throws IOException {
copyFile(srcFile, destFile);
srcFile.delete();
}