mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-09 03:46:18 -04:00
Skip parsing json file if it does not exist
This commit is contained in:
parent
93f943d40c
commit
68a02966d0
@ -21,11 +21,12 @@ import com.google.gson.JsonParseException;
|
|||||||
import org.jackhuang.hmcl.event.*;
|
import org.jackhuang.hmcl.event.*;
|
||||||
import org.jackhuang.hmcl.mod.ModManager;
|
import org.jackhuang.hmcl.mod.ModManager;
|
||||||
import org.jackhuang.hmcl.task.Schedulers;
|
import org.jackhuang.hmcl.task.Schedulers;
|
||||||
import org.jackhuang.hmcl.util.Logging;
|
|
||||||
import org.jackhuang.hmcl.util.ToStringBuilder;
|
import org.jackhuang.hmcl.util.ToStringBuilder;
|
||||||
import org.jackhuang.hmcl.util.gson.JsonUtils;
|
import org.jackhuang.hmcl.util.gson.JsonUtils;
|
||||||
import org.jackhuang.hmcl.util.io.FileUtils;
|
import org.jackhuang.hmcl.util.io.FileUtils;
|
||||||
|
|
||||||
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -171,13 +172,13 @@ public class DefaultGameRepository implements GameRepository {
|
|||||||
List<File> jsons = FileUtils.listFilesByExtension(removedFile, "json");
|
List<File> jsons = FileUtils.listFilesByExtension(removedFile, "json");
|
||||||
jsons.forEach(f -> {
|
jsons.forEach(f -> {
|
||||||
if (!f.delete())
|
if (!f.delete())
|
||||||
Logging.LOG.warning("Unable to delete file " + f);
|
LOG.warning("Unable to delete file " + f);
|
||||||
});
|
});
|
||||||
// remove the version from version list regardless of whether the directory was removed successfully or not.
|
// remove the version from version list regardless of whether the directory was removed successfully or not.
|
||||||
try {
|
try {
|
||||||
FileUtils.deleteDirectory(removedFile);
|
FileUtils.deleteDirectory(removedFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Logging.LOG.log(Level.WARNING, "Unable to remove version folder: " + file, e);
|
LOG.log(Level.WARNING, "Unable to remove version folder: " + file, e);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -202,17 +203,23 @@ public class DefaultGameRepository implements GameRepository {
|
|||||||
// we will find the only json and rename it to correct name.
|
// we will find the only json and rename it to correct name.
|
||||||
if (!json.exists()) {
|
if (!json.exists()) {
|
||||||
List<File> jsons = FileUtils.listFilesByExtension(dir, "json");
|
List<File> jsons = FileUtils.listFilesByExtension(dir, "json");
|
||||||
if (jsons.size() == 1)
|
if (jsons.size() == 1) {
|
||||||
|
LOG.info("Renaming json file " + jsons.get(0) + " to " + json);
|
||||||
if (!jsons.get(0).renameTo(json)) {
|
if (!jsons.get(0).renameTo(json)) {
|
||||||
Logging.LOG.warning("Cannot rename json file " + jsons.get(0) + " to " + json + ", ignoring version " + id);
|
LOG.warning("Cannot rename json file, ignoring version " + id);
|
||||||
return Stream.empty();
|
return Stream.empty();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
LOG.info("No available json file found, ignoring version " + id);
|
||||||
|
return Stream.empty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Version version;
|
Version version;
|
||||||
try {
|
try {
|
||||||
version = Objects.requireNonNull(readVersionJson(json));
|
version = Objects.requireNonNull(readVersionJson(json));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
LOG.log(Level.WARNING, "Malformed version json " + id, e);
|
||||||
// JsonSyntaxException or IOException or NullPointerException(!!)
|
// JsonSyntaxException or IOException or NullPointerException(!!)
|
||||||
if (EventBus.EVENT_BUS.fireEvent(new GameJsonParseFailedEvent(this, json, id)) != Event.Result.ALLOW)
|
if (EventBus.EVENT_BUS.fireEvent(new GameJsonParseFailedEvent(this, json, id)) != Event.Result.ALLOW)
|
||||||
return Stream.empty();
|
return Stream.empty();
|
||||||
@ -220,7 +227,7 @@ public class DefaultGameRepository implements GameRepository {
|
|||||||
try {
|
try {
|
||||||
version = Objects.requireNonNull(readVersionJson(json));
|
version = Objects.requireNonNull(readVersionJson(json));
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
Logging.LOG.log(Level.SEVERE, "User corrected version json is still malformed");
|
LOG.log(Level.SEVERE, "User corrected version json is still malformed", e2);
|
||||||
return Stream.empty();
|
return Stream.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -230,7 +237,7 @@ public class DefaultGameRepository implements GameRepository {
|
|||||||
try {
|
try {
|
||||||
FileUtils.writeText(json, JsonUtils.GSON.toJson(version));
|
FileUtils.writeText(json, JsonUtils.GSON.toJson(version));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Logging.LOG.log(Level.WARNING, "Ignoring version " + id + " because wrong id " + version.getId() + " is set and cannot correct it.");
|
LOG.log(Level.WARNING, "Ignoring version " + id + " because wrong id " + version.getId() + " is set and cannot correct it.", e);
|
||||||
return Stream.empty();
|
return Stream.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -246,7 +253,7 @@ public class DefaultGameRepository implements GameRepository {
|
|||||||
EventBus.EVENT_BUS.fireEvent(new LoadedOneVersionEvent(this, resolved)) != Event.Result.DENY)
|
EventBus.EVENT_BUS.fireEvent(new LoadedOneVersionEvent(this, resolved)) != Event.Result.DENY)
|
||||||
versions.put(version.getId(), version);
|
versions.put(version.getId(), version);
|
||||||
} catch (VersionNotFoundException e) {
|
} catch (VersionNotFoundException e) {
|
||||||
Logging.LOG.log(Level.WARNING, "Ignoring version " + version.getId() + " because it inherits from a nonexistent version.");
|
LOG.log(Level.WARNING, "Ignoring version " + version.getId() + " because it inherits from a nonexistent version.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +285,7 @@ public class DefaultGameRepository implements GameRepository {
|
|||||||
try {
|
try {
|
||||||
return reconstructAssets(version, assetId);
|
return reconstructAssets(version, assetId);
|
||||||
} catch (IOException | JsonParseException e) {
|
} catch (IOException | JsonParseException e) {
|
||||||
Logging.LOG.log(Level.SEVERE, "Unable to reconstruct asset directory", e);
|
LOG.log(Level.SEVERE, "Unable to reconstruct asset directory", e);
|
||||||
return getAssetDirectory(version, assetId);
|
return getAssetDirectory(version, assetId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user