-
Notifications
You must be signed in to change notification settings - Fork 847
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
Open
Mine-diamond
wants to merge
28
commits into
HMCL-dev:main
Choose a base branch
from
Mine-diamond:one-world-manage-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+819
−378
Open
feat: 优化世界管理 #5842
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
78db75d
feat: 现在复用WorldManagePage
Mine-diamond efdc263
feat: 优化只读模式实现
Mine-diamond 0a2225e
feat: 修改单复数
Mine-diamond 320a9f4
feat: 添加重命名文件功能(还有bug)
Mine-diamond ff993eb
feat: 修改世界时关闭锁
Mine-diamond 72a1d76
feat: 修改世界锁管理机制
Mine-diamond e60d3ab
feat: 优化重命名世界功能
Mine-diamond ce3bb09
feat: 优化复制世界功能
Mine-diamond bedf2d9
feat: 现在快速启动不会退出世界管理窗口
Mine-diamond 7b8ced5
feat: 优化锁机制
Mine-diamond 5f80dc6
feat: 复制/重命名世界弹窗会将世界默认名称设为默认值
Mine-diamond bbbe526
feat: 复制世界弹窗当值为当前值时不再取消复制
Mine-diamond a7814fb
feat: 优化代码
Mine-diamond ed0eddf
feat: 优化代码
Mine-diamond 00ddeb1
feat: 添加还原存档功能
Mine-diamond 0a3fd64
fix: 修复还原世界位置错误的问题
Mine-diamond a3f5e2e
fix: 修复还原世界位置错误的问题
Mine-diamond 5a128b6
feat: update
Mine-diamond 5aed9d7
feat: 备份文件以倒序显示
Mine-diamond 3e8a253
feat: 数据包不支持文本添加i18n
Mine-diamond 792727b
fix: 修复世界解析错误后无法禁用的错误
Mine-diamond 87e1970
feat: 优化代码
Mine-diamond db5704e
feat: 分离压缩包世界到ArchiveWorld中
Mine-diamond 77e606c
feat: 优化恢复备份功能
Mine-diamond 7f40758
fix: 修复复制世界逻辑的错误
Mine-diamond 239eec0
fix: 修复复制/重命名世界时可能的错误
Mine-diamond b3631f0
fix: 世界锁错误/文件名修建错误
Mine-diamond 91a5c7d
fix: 添加i18n
Mine-diamond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ArchiveWorld.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * 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.io.InputStream; | ||
| import java.nio.file.*; | ||
| import java.util.List; | ||
|
|
||
| import static org.jackhuang.hmcl.util.logging.Logger.LOG; | ||
|
|
||
| /// @author mineDiamond | ||
| public final class ArchiveWorld { | ||
| private final Path file; | ||
| private final String fileName; | ||
| private final boolean hasSubDir; | ||
| private String worldName; | ||
| private @Nullable GameVersionNumber gameVersion; | ||
| private @Nullable Image icon; | ||
|
|
||
| public ArchiveWorld(Path file) throws IOException { | ||
| if (Files.isRegularFile(file)) { | ||
| this.file = file; | ||
|
|
||
| try (FileSystem fs = CompressingUtils.readonly(this.file).setAutoDetectEncoding(true).build()) { | ||
| Path root; | ||
| if (Files.isRegularFile(fs.getPath("/level.dat"))) { | ||
| root = fs.getPath("/"); | ||
| hasSubDir = false; | ||
| fileName = FileUtils.getName(this.file); | ||
| } else { | ||
| List<Path> files = Files.list(fs.getPath("/")).toList(); | ||
| if (files.size() != 1 || !Files.isDirectory(files.get(0))) { | ||
| throw new IOException("Not a valid world zip file"); | ||
| } | ||
|
|
||
| root = files.get(0); | ||
| hasSubDir = true; | ||
| fileName = FileUtils.getName(root); | ||
| } | ||
|
|
||
| Path levelDat = root.resolve("level.dat"); | ||
| if (!Files.exists(levelDat)) { //version 20w14infinite | ||
| levelDat = root.resolve("special_level.dat"); | ||
| } | ||
| if (!Files.exists(levelDat)) { | ||
| throw new IOException("Not a valid world zip file since level.dat or special_level.dat cannot be found."); | ||
| } | ||
| checkAndLoadLevelData(levelDat); | ||
|
|
||
| Path iconFile = root.resolve("icon.png"); | ||
| if (Files.isRegularFile(iconFile)) { | ||
| try (InputStream inputStream = Files.newInputStream(iconFile)) { | ||
| icon = new Image(inputStream, 64, 64, true, false); | ||
| if (icon.isError()) | ||
| throw icon.getException(); | ||
| } catch (Exception e) { | ||
| LOG.warning("Failed to load world icon", e); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| throw new IOException("Path " + file + " cannot be recognized as a 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 getFile() { | ||
| return file; | ||
| } | ||
|
|
||
| public String getFileName() { | ||
| return fileName; | ||
| } | ||
|
|
||
| public boolean hasSubDir() { | ||
| return hasSubDir; | ||
| } | ||
|
|
||
| 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 worldDir; | ||
| try { | ||
| worldDir = savesDir.resolve(name); | ||
| } catch (InvalidPathException e) { | ||
| throw new IOException(e); | ||
| } | ||
|
|
||
| if (Files.isDirectory(worldDir)) { | ||
| throw new FileAlreadyExistsException("World already exists"); | ||
| } | ||
|
|
||
| if (hasSubDir) { | ||
| new Unzipper(file, worldDir).setSubDirectory("/" + fileName + "/").unzip(); | ||
| } else { | ||
| new Unzipper(file, worldDir).unzip(); | ||
| } | ||
| new World(worldDir).rename(name); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ArchiveWorld.installresolves the destination directory directly from the user-providedname(savesDir.resolve(name)), which can still throwInvalidPathExceptionor create illegal folder names on Windows. Given the PR goal of matching Minecraft’s behavior for invalid file names, consider usingFileUtils.getSafeWorldFolderName(name)(and handling conflicts similarly toWorld.rename/copy) for the folder name while separately settingLevelNameto the intended display name.