feat: 添加新版本世界指定资源包位置支持 (#5552)
This commit is contained in:
@@ -21,6 +21,7 @@ import javafx.beans.property.BooleanProperty;
|
|||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.scene.control.Skin;
|
import javafx.scene.control.Skin;
|
||||||
import javafx.stage.FileChooser;
|
import javafx.stage.FileChooser;
|
||||||
|
import org.jackhuang.hmcl.game.World;
|
||||||
import org.jackhuang.hmcl.mod.Datapack;
|
import org.jackhuang.hmcl.mod.Datapack;
|
||||||
import org.jackhuang.hmcl.task.Schedulers;
|
import org.jackhuang.hmcl.task.Schedulers;
|
||||||
import org.jackhuang.hmcl.task.Task;
|
import org.jackhuang.hmcl.task.Task;
|
||||||
@@ -44,13 +45,13 @@ import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
|||||||
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
|
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
|
||||||
|
|
||||||
public final class DatapackListPage extends ListPageBase<DatapackListPageSkin.DatapackInfoObject> implements WorldManagePage.WorldRefreshable {
|
public final class DatapackListPage extends ListPageBase<DatapackListPageSkin.DatapackInfoObject> implements WorldManagePage.WorldRefreshable {
|
||||||
private final Path worldDir;
|
private final World world;
|
||||||
private final Datapack datapack;
|
private final Datapack datapack;
|
||||||
final BooleanProperty readOnly;
|
final BooleanProperty readOnly;
|
||||||
|
|
||||||
public DatapackListPage(WorldManagePage worldManagePage) {
|
public DatapackListPage(WorldManagePage worldManagePage) {
|
||||||
this.worldDir = worldManagePage.getWorld().getFile();
|
world = worldManagePage.getWorld();
|
||||||
datapack = new Datapack(worldDir.resolve("datapacks"));
|
datapack = new Datapack(world.getFile().resolve("datapacks"));
|
||||||
setItems(MappedObservableList.create(datapack.getPacks(), DatapackListPageSkin.DatapackInfoObject::new));
|
setItems(MappedObservableList.create(datapack.getPacks(), DatapackListPageSkin.DatapackInfoObject::new));
|
||||||
readOnly = worldManagePage.readOnlyProperty();
|
readOnly = worldManagePage.readOnlyProperty();
|
||||||
FXUtils.applyDragListener(this, it -> Objects.equals("zip", FileUtils.getExtension(it)),
|
FXUtils.applyDragListener(this, it -> Objects.equals("zip", FileUtils.getExtension(it)),
|
||||||
@@ -68,7 +69,7 @@ public final class DatapackListPage extends ListPageBase<DatapackListPageSkin.Da
|
|||||||
|
|
||||||
private void installSingleDatapack(Path datapack) {
|
private void installSingleDatapack(Path datapack) {
|
||||||
try {
|
try {
|
||||||
this.datapack.installPack(datapack);
|
this.datapack.installPack(datapack, world.getGameVersion());
|
||||||
} catch (IOException | IllegalArgumentException e) {
|
} catch (IOException | IllegalArgumentException e) {
|
||||||
LOG.warning("Unable to parse datapack file " + datapack, e);
|
LOG.warning("Unable to parse datapack file " + datapack, e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import org.jackhuang.hmcl.util.gson.JsonUtils;
|
|||||||
import org.jackhuang.hmcl.util.io.CompressingUtils;
|
import org.jackhuang.hmcl.util.io.CompressingUtils;
|
||||||
import org.jackhuang.hmcl.util.io.FileUtils;
|
import org.jackhuang.hmcl.util.io.FileUtils;
|
||||||
import org.jackhuang.hmcl.util.io.Unzipper;
|
import org.jackhuang.hmcl.util.io.Unzipper;
|
||||||
|
import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
@@ -57,7 +58,7 @@ public class Datapack {
|
|||||||
return packs;
|
return packs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void installPack(Path sourceDatapackPath, Path targetDatapackDirectory) throws IOException {
|
public static void installPack(Path sourceDatapackPath, Path targetDatapackDirectory, GameVersionNumber gameVersionNumber) throws IOException {
|
||||||
boolean containsMultiplePacks;
|
boolean containsMultiplePacks;
|
||||||
Set<String> packs = new HashSet<>();
|
Set<String> packs = new HashSet<>();
|
||||||
try (FileSystem fs = CompressingUtils.readonly(sourceDatapackPath).setAutoDetectEncoding(true).build()) {
|
try (FileSystem fs = CompressingUtils.readonly(sourceDatapackPath).setAutoDetectEncoding(true).build()) {
|
||||||
@@ -106,7 +107,19 @@ public class Datapack {
|
|||||||
.setSubDirectory("/datapacks/")
|
.setSubDirectory("/datapacks/")
|
||||||
.unzip();
|
.unzip();
|
||||||
|
|
||||||
try (FileSystem outputResourcesZipFS = CompressingUtils.createWritableZipFileSystem(targetDatapackDirectory.getParent().resolve("resources.zip"));
|
Path targetResourceZipPath;
|
||||||
|
// When the version cannot be obtained, the old version logic is used by default.
|
||||||
|
boolean useNewResourcePath = gameVersionNumber != null
|
||||||
|
&& gameVersionNumber.compareTo("26.1-snapshot-6") >= 0;
|
||||||
|
|
||||||
|
if (useNewResourcePath) {
|
||||||
|
Files.createDirectories(targetDatapackDirectory.getParent().resolve("resourcepacks"));
|
||||||
|
targetResourceZipPath = targetDatapackDirectory.getParent().resolve("resourcepacks/resources.zip");
|
||||||
|
} else {
|
||||||
|
targetResourceZipPath = targetDatapackDirectory.getParent().resolve("resources.zip");
|
||||||
|
}
|
||||||
|
|
||||||
|
try (FileSystem outputResourcesZipFS = CompressingUtils.createWritableZipFileSystem(targetResourceZipPath);
|
||||||
FileSystem inputPackZipFS = CompressingUtils.createReadOnlyZipFileSystem(sourceDatapackPath)) {
|
FileSystem inputPackZipFS = CompressingUtils.createReadOnlyZipFileSystem(sourceDatapackPath)) {
|
||||||
Path resourcesZip = inputPackZipFS.getPath("resources.zip");
|
Path resourcesZip = inputPackZipFS.getPath("resources.zip");
|
||||||
if (Files.isRegularFile(resourcesZip)) {
|
if (Files.isRegularFile(resourcesZip)) {
|
||||||
@@ -138,8 +151,8 @@ public class Datapack {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void installPack(Path sourcePackPath) throws IOException {
|
public void installPack(Path sourcePackPath, GameVersionNumber gameVersionNumber) throws IOException {
|
||||||
installPack(sourcePackPath, path);
|
installPack(sourcePackPath, path, gameVersionNumber);
|
||||||
loadFromDir();
|
loadFromDir();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user