Skip to content

fix: 安装模组未进行文件名存在检查#5879

Open
CiiLu wants to merge 1 commit intoHMCL-dev:mainfrom
CiiLu:EEEE
Open

fix: 安装模组未进行文件名存在检查#5879
CiiLu wants to merge 1 commit intoHMCL-dev:mainfrom
CiiLu:EEEE

Conversation

@CiiLu
Copy link
Copy Markdown
Contributor

@CiiLu CiiLu commented Mar 29, 2026

No description provided.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a missing destination filename collision check when downloading/installing mods (and other downloadable content using the same prompt) so users don’t unintentionally overwrite an existing file in the target subdirectory.

Changes:

  • Add java.nio.file.Files usage to check whether the chosen destination filename already exists.
  • Add a second input Validator to the download filename prompt to reject existing filenames.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 155 to +157
}), i18n("message.downloading"), TaskCancellationAction.NORMAL);
handler.resolve();
}, file.getFile().getFilename(), new Validator(i18n("install.new_game.malformed"), FileUtils::isNameValid));
}, file.getFile().getFilename(), new Validator(i18n("install.new_game.malformed"), FileUtils::isNameValid), new Validator(i18n("profile.already_exists"), (it) -> !Files.exists(runDirectory.resolve(subdirectoryName).resolve(it))));
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new filename-exists validator only runs when the text changes (InputDialogPane uses FXUtils.setValidateWhileTextChanged), so the OK button can remain enabled if the destination file appears after the last keystroke. Since FileDownloadTask ultimately moves the temp file with REPLACE_EXISTING, this can still overwrite an existing file. Consider re-checking Files.exists(dest) inside the prompt callback (before starting the task) and rejecting via handler.reject(...) when it exists, so the no-overwrite guarantee holds at execution time.

Copilot uses AI. Check for mistakes.
}), i18n("message.downloading"), TaskCancellationAction.NORMAL);
handler.resolve();
}, file.getFile().getFilename(), new Validator(i18n("install.new_game.malformed"), FileUtils::isNameValid));
}, file.getFile().getFilename(), new Validator(i18n("install.new_game.malformed"), FileUtils::isNameValid), new Validator(i18n("profile.already_exists"), (it) -> !Files.exists(runDirectory.resolve(subdirectoryName).resolve(it))));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validator 会在输入字符时触发,把 IO 操作放在这里不合适吧?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants