-
Notifications
You must be signed in to change notification settings - Fork 858
feat: 优化世界管理 #5842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: 优化世界管理 #5842
Changes from all commits
78db75d
efdc263
0a2225e
320a9f4
ff993eb
72a1d76
e60d3ab
ce3bb09
bedf2d9
7b8ced5
5f80dc6
bbbe526
a7814fb
ed0eddf
00ddeb1
0a3fd64
a3f5e2e
5a128b6
5aed9d7
3e8a253
792727b
87e1970
db5704e
77e606c
7f40758
239eec0
b3631f0
91a5c7d
e441fef
50d7b1e
f7ee941
91cc515
06e2525
32227a5
a95c861
f947be5
3a69c01
86e3cf2
a131be1
8f10b6c
3b6e9f5
e9bbd0c
b2629f4
547e7d8
7fbcd46
e41f114
0b473bb
fc09d0a
d702b29
deacfeb
dbe04ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我觉得这个类的设计就很奇怪,尤其是 我认为你可以把它改名叫
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个不是表达世界信息的类的,而是表达“即将被导入的世界的”,Import:导入,Importable:可导入的,为什么会认为是扩展的意思呢? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * Hello Minecraft! Launcher | ||
| * Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.jackhuang.hmcl.ui.versions; | ||
|
|
||
| import javafx.scene.image.Image; | ||
| import org.glavo.nbt.io.NBTCodec; | ||
| import org.glavo.nbt.tag.CompoundTag; | ||
| import org.glavo.nbt.tag.LongTag; | ||
| import org.glavo.nbt.tag.StringTag; | ||
| import org.glavo.nbt.tag.TagType; | ||
| import org.jackhuang.hmcl.game.World; | ||
| import org.jackhuang.hmcl.util.io.CompressingUtils; | ||
| import org.jackhuang.hmcl.util.io.FileUtils; | ||
| import org.jackhuang.hmcl.util.io.Unzipper; | ||
| import org.jackhuang.hmcl.util.versioning.GameVersionNumber; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.FileSystem; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /// @author mineDiamond | ||
| public final class ImportableWorld { | ||
| private final Path sourcePath; | ||
| private final String fileName; | ||
| private final boolean isArchive; | ||
| private final boolean hasTopLevelDirectory; | ||
| private String worldName; | ||
| private @Nullable GameVersionNumber gameVersion; | ||
| private @Nullable Image icon; | ||
|
|
||
| public ImportableWorld(Path sourcePath) throws IOException { | ||
| if (Files.isRegularFile(sourcePath)) { | ||
| this.sourcePath = sourcePath; | ||
| this.isArchive = true; | ||
|
|
||
| try (FileSystem fs = CompressingUtils.readonly(this.sourcePath).setAutoDetectEncoding(true).build()) { | ||
| Path root; | ||
| if (Files.isRegularFile(fs.getPath("/level.dat"))) { | ||
| root = fs.getPath("/"); | ||
| hasTopLevelDirectory = false; | ||
| fileName = FileUtils.getName(this.sourcePath); | ||
| } else { | ||
| try (Stream<Path> stream = Files.list(fs.getPath("/"))) { | ||
| List<Path> files = stream.toList(); | ||
| if (files.size() != 1 || !Files.isDirectory(files.get(0))) { | ||
| throw new IOException("Not a valid world zip file"); | ||
| } | ||
|
|
||
| root = files.get(0); | ||
| hasTopLevelDirectory = true; | ||
| fileName = FileUtils.getName(root); | ||
| } | ||
| } | ||
|
|
||
| checkAndLoadLevelData(World.findLevelDatPath(root)); | ||
| this.icon = World.loadIcon(root); | ||
| } | ||
| } else if (Files.isDirectory(sourcePath)) { | ||
| this.sourcePath = sourcePath; | ||
| fileName = FileUtils.getName(this.sourcePath); | ||
| this.isArchive = false; | ||
| this.hasTopLevelDirectory = false; | ||
|
|
||
| checkAndLoadLevelData(World.findLevelDatPath(this.sourcePath)); | ||
| } else { | ||
| throw new IOException("Path " + sourcePath + " cannot be recognized as an archive Minecraft world"); | ||
| } | ||
| } | ||
|
|
||
| private void checkAndLoadLevelData(Path levelDatPath) throws IOException { | ||
| CompoundTag levelData = NBTCodec.of().readTag(levelDatPath, TagType.COMPOUND); | ||
| if (!(levelData.get("Data") instanceof CompoundTag data)) | ||
| throw new IOException("level.dat missing Data"); | ||
|
|
||
| if (data.get("LevelName") instanceof StringTag levelNameTag) { | ||
| this.worldName = levelNameTag.getValue(); | ||
| } else { | ||
| throw new IOException("level.dat missing LevelName"); | ||
| } | ||
|
|
||
| if (data.get("Version") instanceof CompoundTag versionTag && | ||
| versionTag.get("Name") instanceof StringTag nameTag) { | ||
| this.gameVersion = GameVersionNumber.asGameVersion(nameTag.getValue()); | ||
| } | ||
|
|
||
| if (!(data.get("LastPlayed") instanceof LongTag)) | ||
| throw new IOException("level.dat missing LastPlayed"); | ||
| } | ||
|
|
||
| public Path getSourcePath() { | ||
| return sourcePath; | ||
| } | ||
|
|
||
| public String getFileName() { | ||
| return fileName; | ||
| } | ||
|
|
||
| public boolean hasTopLevelDirectory() { | ||
| return hasTopLevelDirectory; | ||
| } | ||
|
|
||
| public String getWorldName() { | ||
| return worldName; | ||
| } | ||
|
|
||
| public @Nullable GameVersionNumber getGameVersion() { | ||
| return gameVersion; | ||
| } | ||
|
|
||
| public @Nullable Image getIcon() { | ||
| return icon; | ||
| } | ||
|
|
||
| public void install(Path savesDir, String name) throws IOException { | ||
| Path targetPath = FileUtils.getNonConflictingDirectory(savesDir, FileUtils.getSafeWorldFolderName(name)); | ||
|
|
||
| if (isArchive) { | ||
| if (hasTopLevelDirectory) { | ||
| new Unzipper(sourcePath, targetPath).setSubDirectory("/" + fileName + "/").unzip(); | ||
| } else { | ||
| new Unzipper(sourcePath, targetPath).unzip(); | ||
| } | ||
| } else { | ||
| FileUtils.copyDirectory(sourcePath, targetPath, path -> !path.contains("session.lock")); | ||
| } | ||
| new World(targetPath).setWorldName(name); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.