-
Notifications
You must be signed in to change notification settings - Fork 858
feat: 支持试玩模式 #5942
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: 支持试玩模式 #5942
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,147 @@ | ||||||
| /* | ||||||
| * Hello Minecraft! Launcher | ||||||
| * Copyright (C) 2020 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.auth.demo; | ||||||
|
|
||||||
| import javafx.beans.binding.ObjectBinding; | ||||||
| import org.jackhuang.hmcl.auth.Account; | ||||||
| import org.jackhuang.hmcl.auth.AuthInfo; | ||||||
| import org.jackhuang.hmcl.auth.AuthenticationException; | ||||||
| import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorArtifactProvider; | ||||||
| import org.jackhuang.hmcl.auth.yggdrasil.Texture; | ||||||
| import org.jackhuang.hmcl.auth.yggdrasil.TextureType; | ||||||
| import org.jackhuang.hmcl.game.Arguments; | ||||||
| import org.jackhuang.hmcl.game.LaunchOptions; | ||||||
| import org.jackhuang.hmcl.util.StringUtils; | ||||||
| import org.jackhuang.hmcl.util.ToStringBuilder; | ||||||
| import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter; | ||||||
|
|
||||||
| import java.io.IOException; | ||||||
| import java.util.Map; | ||||||
| import java.util.Optional; | ||||||
| import java.util.UUID; | ||||||
|
|
||||||
| import static java.util.Objects.requireNonNull; | ||||||
| import static org.jackhuang.hmcl.util.Lang.mapOf; | ||||||
| import static org.jackhuang.hmcl.util.Pair.pair; | ||||||
|
|
||||||
| /** | ||||||
| * | ||||||
| * @author huang | ||||||
| */ | ||||||
| public class DemoAccount extends Account { | ||||||
|
|
||||||
| private final AuthlibInjectorArtifactProvider downloader; | ||||||
| private final String username; | ||||||
| private final UUID uuid; | ||||||
|
|
||||||
| protected DemoAccount(AuthlibInjectorArtifactProvider downloader, String username, UUID uuid) { | ||||||
| this.downloader = requireNonNull(downloader); | ||||||
| this.username = requireNonNull(username); | ||||||
| this.uuid = requireNonNull(uuid); | ||||||
|
|
||||||
| if (StringUtils.isBlank(username)) { | ||||||
| throw new IllegalArgumentException("Username cannot be blank"); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public AuthlibInjectorArtifactProvider getDownloader() { | ||||||
| return downloader; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public UUID getUUID() { | ||||||
| return uuid; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public String getUsername() { | ||||||
| return username; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public String getCharacter() { | ||||||
| return username; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public String getIdentifier() { | ||||||
| return username + ":" + username; | ||||||
Xirren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| public AuthInfo logIn() throws AuthenticationException { | ||||||
| // Using "legacy" user type here because "mojang" user type may cause "invalid session token" or "disconnected" when connecting to a game server. | ||||||
| return new DemoAuthInfo(username, uuid, UUIDTypeAdapter.fromUUID(UUID.randomUUID()), AuthInfo.USER_TYPE_MSA, "{}"); | ||||||
|
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. 代码实现与注释描述不符。注释提到使用 "legacy" 用户类型以避免连接服务器时的验证问题,但代码中实际使用的是
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| private class DemoAuthInfo extends AuthInfo { | ||||||
| public DemoAuthInfo(String username, UUID uuid, String accessToken, String userType, String userProperties) { | ||||||
| super(username, uuid, accessToken, userType, userProperties); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public Arguments getLaunchArguments(LaunchOptions options) { | ||||||
| Arguments arguments = new Arguments(); | ||||||
| arguments = arguments.addGameArguments("--demo"); | ||||||
| return arguments; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void close() throws IOException { | ||||||
| // Do nothing. | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public AuthInfo playOffline() throws AuthenticationException { | ||||||
| return logIn(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public Map<Object, Object> toStorage() { | ||||||
| return mapOf( | ||||||
| pair("uuid", UUIDTypeAdapter.fromUUID(uuid)), | ||||||
| pair("username", username) | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public ObjectBinding<Optional<Map<TextureType, Texture>>> getTextures() { | ||||||
| return super.getTextures(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public String toString() { | ||||||
| return new ToStringBuilder(this) | ||||||
| .append("username", username) | ||||||
| .append("uuid", uuid) | ||||||
| .toString(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public int hashCode() { | ||||||
| return username.hashCode(); | ||||||
|
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. |
||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public boolean equals(Object obj) { | ||||||
| if (!(obj instanceof DemoAccount)) | ||||||
| return false; | ||||||
| DemoAccount another = (DemoAccount) obj; | ||||||
| return isPortable() == another.isPortable() && username.equals(another.username); | ||||||
|
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. |
||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Hello Minecraft! Launcher | ||
| * Copyright (C) 2020 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.auth.demo; | ||
|
|
||
| import org.jackhuang.hmcl.auth.AccountFactory; | ||
| import org.jackhuang.hmcl.auth.CharacterSelector; | ||
| import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorArtifactProvider; | ||
| import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.jackhuang.hmcl.util.Lang.tryCast; | ||
|
|
||
| /** | ||
| * | ||
| * @author huangyuhui | ||
| */ | ||
| public final class DemoAccountFactory extends AccountFactory<DemoAccount> { | ||
| private final AuthlibInjectorArtifactProvider downloader; | ||
|
|
||
| public DemoAccountFactory(AuthlibInjectorArtifactProvider downloader) { | ||
| this.downloader = downloader; | ||
| } | ||
|
|
||
| @Override | ||
| public AccountLoginType getLoginType() { | ||
| return AccountLoginType.NONE; | ||
| } | ||
|
|
||
| public DemoAccount create(String username, UUID uuid) { | ||
| return new DemoAccount(downloader, username, uuid); | ||
| } | ||
|
|
||
| @Override | ||
| public DemoAccount create(CharacterSelector selector, String username, String password, ProgressCallback progressCallback, Object additionalData) { | ||
| username = "Player"; | ||
| UUID uuid; | ||
| uuid = getUUIDFromUserName(username); | ||
| return new DemoAccount(downloader, username, uuid); | ||
| } | ||
|
|
||
| @Override | ||
| public DemoAccount fromStorage(Map<Object, Object> storage) { | ||
| String username = tryCast(storage.get("username"), String.class) | ||
| .orElseThrow(() -> new IllegalStateException("Demo account configuration malformed.")); | ||
| UUID uuid = tryCast(storage.get("uuid"), String.class) | ||
| .map(UUIDTypeAdapter::fromString) | ||
| .orElse(getUUIDFromUserName(username)); | ||
|
|
||
| return new DemoAccount(downloader, username, uuid); | ||
| } | ||
|
|
||
| public static UUID getUUIDFromUserName(String username) { | ||
| return UUID.nameUUIDFromBytes(("DemoPlayer:" + username).getBytes(UTF_8)); | ||
| } | ||
|
|
||
| } |
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.
FACTORY_DEMO应该被添加到FACTORIES列表中。如果不添加,该账户类型将不会出现在“添加账户”对话框的选项卡中,用户只能通过微软登录界面的快捷按钮发现此功能,这降低了功能的发现感和一致性。