-
Notifications
You must be signed in to change notification settings - Fork 896
fix: mod file name wrong after toggles on ModList (#6200) #6216
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
HowXu
wants to merge
5
commits into
HMCL-dev:main
Choose a base branch
from
HowXu:fix/6200-FileNameUpdate-DisabledAndEnabled
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.
+130
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b06a564
fix: mod file name wrong after toggles on ModList
HowXu 7203976
start ci actions
HowXu 4bd5e51
review: fix weak listener cleanup in mod list cell
HowXu 1b71c54
chore: comments refact
HowXu 5d245ba
refactor: reuse active-state listener per cell
HowXu 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
100 changes: 100 additions & 0 deletions
100
HMCL/src/test/java/org/jackhuang/hmcl/ui/versions/ModListPageTest.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,100 @@ | ||
| /* | ||
| * 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 org.jackhuang.hmcl.addon.LocalAddonFile; | ||
| import org.jackhuang.hmcl.addon.mod.LocalModFile; | ||
| import org.jackhuang.hmcl.addon.mod.ModLoaderType; | ||
| import org.jackhuang.hmcl.addon.mod.ModManager; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * Tests for instance mods list file name change. | ||
| * | ||
| * @author dev@howxu.cn | ||
| */ | ||
| final class ModListPageTest { | ||
|
|
||
| @TempDir | ||
| Path tempDir; | ||
|
|
||
| @Test | ||
| void modSubtitleUsesUpdatedFilePathAfterDisabling() throws IOException { | ||
| LocalModFile modFile = createModFile("example.jar", "testmod", ModLoaderType.FABRIC); | ||
|
|
||
| modFile.setActive(false); | ||
|
|
||
| assertEquals("testmod | example.jar.disabled", ModListPageSkin.createModSubtitle(modFile)); | ||
| } | ||
|
|
||
| @Test | ||
| void modSubtitleUsesUpdatedFilePathAfterEnabling() throws IOException { | ||
| LocalModFile modFile = createModFile("example.jar.disabled", "testmod", ModLoaderType.FABRIC); | ||
|
|
||
| modFile.setActive(true); | ||
|
|
||
| assertEquals("testmod | example.jar", ModListPageSkin.createModSubtitle(modFile)); | ||
| } | ||
|
|
||
| @Test | ||
| void modSubtitleShowsCurrentEnabledFilePath() throws IOException { | ||
| LocalModFile modFile = createModFile("example.jar", "testmod", ModLoaderType.FABRIC); | ||
|
|
||
| assertEquals("testmod | example.jar", ModListPageSkin.createModSubtitle(modFile)); | ||
| } | ||
|
|
||
| @Test | ||
| void modSubtitleOmitsUnknownLoaderId() throws IOException { | ||
| LocalModFile modFile = createModFile("unknown.jar", "unknown", ModLoaderType.UNKNOWN); | ||
|
|
||
| assertEquals("unknown.jar", ModListPageSkin.createModSubtitle(modFile)); | ||
| } | ||
|
|
||
| @Test | ||
| void modSubtitleDoesNotChangeWhenFileRenameFails() throws IOException { | ||
| LocalModFile modFile = createModFile("example.jar", "testmod", ModLoaderType.FABRIC); | ||
| Path disabledTarget = tempDir.resolve("example.jar.disabled"); | ||
| Files.createDirectory(disabledTarget); | ||
| Files.writeString(disabledTarget.resolve("child"), "occupied"); | ||
|
|
||
| modFile.setActive(false); | ||
|
|
||
| assertEquals("testmod | example.jar", ModListPageSkin.createModSubtitle(modFile)); | ||
| } | ||
|
|
||
| private LocalModFile createModFile(String fileName, String modId, ModLoaderType modLoaderType) throws IOException { | ||
| Path file = tempDir.resolve(fileName); | ||
| Files.writeString(file, "mod"); | ||
|
|
||
| ModManager modManager = new ModManager(null, "test"); | ||
| return new LocalModFile( | ||
| modManager, | ||
| modManager.getLocalMod(modId, modLoaderType), | ||
| file, | ||
| "Test Mod", | ||
| new LocalAddonFile.Description("Test mod") | ||
| ); | ||
| } | ||
| } |
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.
你这个
activeListener根本没有地方持有强引用啊,不怕被回收掉吗?Uh oh!
There was an error while loading. Please reload this page.
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.
这一段在之前被gemini指出内存泄露,我看了一下说的也在理,当时确实没发现,然后按照gemini的要求改了一下
activeListener 是 ModInfoListCell 的 final 字段,cell 自身持有它的强引用,WeakInvalidationListener 防止 dataItem.active 反过来强引用整个 cell 导致泄漏(不包 weak ,addListener(strongListener) 会让 listener 链上 cell 的强引用,而 listener 通过 lambda 又反向捕获 cell)。
包了 weak 之后,dataItem.active -> weakActiveListener(weak) -> activeListener(strong via cell),cell 才能被 GC。
这个写法我后来看了一下,是gemini按照已有的模式给出的,OptionsListSkin 的 Cell 里 updateStyleListener 大概也是也是这个写法 final listener套一层weak
HMCL/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/OptionsListSkin.java
Line 69 in 0eb1d3c
我认为 weakActiveListener 这个对象本身被 cell 当 final 字段持有,就不会被回收;它内部的弱引用指向的 activeListener 也被 cell 当 final 持有,也不用担心回收。两个对象生命周期和ListView或者Cell本身是一样长的,切走ListView肯定能GC了,只要Cell的生命周期是正常的,这个东西就没什么问题。
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.
哦,是我的错,我错把它看成方法局部变量了。