Merge branch 'main' into optimize

This commit is contained in:
ShirosakiMio 2024-08-19 10:27:57 +08:00
commit 24dbfd6c23
11 changed files with 68 additions and 45 deletions

View File

@ -471,6 +471,8 @@ public final class Accounts {
} else {
return context.getString(R.string.account_methods_microsoft_error_unknown);
}
} else if (exception instanceof MicrosoftService.XBox400Exception) {
return context.getString(R.string.account_methods_microsoft_error_wrong_verify_method);
} else if (exception instanceof MicrosoftService.NoMinecraftJavaEditionProfileException) {
return context.getString(R.string.account_methods_microsoft_error_no_character);
} else if (exception instanceof MicrosoftService.NoXuiException) {

View File

@ -67,6 +67,7 @@
<string name="account_methods_microsoft_error_country_unavailable">Xbox Live 不支持您所在的国家/地区。</string>
<string name="account_methods_microsoft_error_missing_xbox_account">你的微软账户没有链接到 Xbox 账户,请先创建。</string>
<string name="account_methods_microsoft_error_unknown">登录失败</string>
<string name="account_methods_microsoft_error_wrong_verify_method">请在 Microsoft 账户登陆页面使用账户 + 密码登录。请不要使用验证码登录。</string>
<string name="account_methods_microsoft_error_no_character">你的账户尚未获取 Minecraft : Java Edition</string>
<string name="account_methods_microsoft_error_add_family_probably">请检查并确保年龄设置大于 18 岁。</string>
<string name="account_methods_microsoft_close_page">Microsoft 账户登录完成</string>

View File

@ -81,6 +81,7 @@
<string name="account_methods_microsoft_error_country_unavailable">Xbox Live is not available in your current country/region.</string>
<string name="account_methods_microsoft_error_missing_xbox_account">Your Microsoft account does not have a linked Xbox account yet. Please create one before continuing.</string>
<string name="account_methods_microsoft_error_unknown">Failed to log in</string>
<string name="account_methods_microsoft_error_wrong_verify_method">Please log in using your account and password on the Microsoft account login page. Please do not use a verification code to log in.</string>
<string name="account_methods_microsoft_error_no_character">Your account does not own the Minecraft Java Edition.\nThe game profile may not have been created.</string>
<string name="account_methods_microsoft_error_add_family_probably">Please check if the age indicated in your account settings is at least 18 years old. If not and you believe this is an error, you can go to official website to change it.</string>
<string name="account_methods_microsoft_close_page">Microsoft account authorization is now completed.</string>

View File

@ -118,8 +118,11 @@ public class MicrosoftService {
}
private MicrosoftSession authenticateViaLiveAccessToken(String liveAccessToken, String liveRefreshToken) throws IOException, JsonParseException, AuthenticationException {
String uhs;
XBoxLiveAuthenticationResponse xboxResponse, minecraftXstsResponse;
try {
// Authenticate with XBox Live
XBoxLiveAuthenticationResponse xboxResponse = HttpRequest
xboxResponse = HttpRequest
.POST("https://user.auth.xboxlive.com/user/authenticate")
.json(mapOf(
pair("Properties",
@ -127,12 +130,12 @@ public class MicrosoftService {
pair("RpsTicket", "d=" + liveAccessToken))),
pair("RelyingParty", "http://auth.xboxlive.com"), pair("TokenType", "JWT")))
.retry(5)
.accept("application/json").getJson(XBoxLiveAuthenticationResponse.class);
.accept("application/json")
.getJson(XBoxLiveAuthenticationResponse.class);
String uhs = getUhs(xboxResponse, null);
uhs = getUhs(xboxResponse, null);
// Authenticate Minecraft with XSTS
XBoxLiveAuthenticationResponse minecraftXstsResponse = HttpRequest
minecraftXstsResponse = HttpRequest
.POST("https://xsts.auth.xboxlive.com/xsts/authorize")
.json(mapOf(
pair("Properties",
@ -142,6 +145,12 @@ public class MicrosoftService {
.ignoreHttpErrorCode(401)
.retry(5)
.getJson(XBoxLiveAuthenticationResponse.class);
} catch (ResponseCodeException e) {
if (e.getResponseCode() == 400) {
throw new XBox400Exception();
}
throw e;
}
getUhs(minecraftXstsResponse, uhs);
@ -298,6 +307,9 @@ public class MicrosoftService {
public static final long ADD_FAMILY = 2148916238L;
}
public static class XBox400Exception extends AuthenticationException {
}
public static class NoMinecraftJavaEditionProfileException extends AuthenticationException {
}

View File

@ -88,7 +88,7 @@ public class MicrosoftSession {
.orElseThrow(() -> new IllegalArgumentException("accessToken is missing"));
String refreshToken = tryCast(storage.get("refreshToken"), String.class)
.orElseThrow(() -> new IllegalArgumentException("refreshToken is missing"));
Long notAfter = tryCast(storage.get("notAfter"), Long.class).orElse(0L);
Long notAfter = tryCast(storage.get("notAfter"), Number.class).map(Number::longValue).orElse(0L);
String userId = tryCast(storage.get("userid"), String.class)
.orElseThrow(() -> new IllegalArgumentException("userid is missing"));
return new MicrosoftSession(tokenType, accessToken, notAfter, refreshToken, new User(userId), new GameProfile(uuid, name));

View File

@ -29,6 +29,7 @@ import com.tungsten.fclcore.util.gson.Validation;
import com.tungsten.fclcore.util.io.HttpRequest;
import com.tungsten.fclcore.util.versioning.VersionNumber;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@ -61,8 +62,7 @@ public final class NeoForgeBMCLVersionList extends VersionList<NeoForgeRemoteVer
@Override
public Optional<NeoForgeRemoteVersion> getVersion(String gameVersion, String remoteVersion) {
if (gameVersion.equals("1.20.1")) {
remoteVersion = NeoForgeRemoteVersion.fixInvalidVersion(remoteVersion);
remoteVersion = VersionNumber.compare(remoteVersion, "47.1.85") >= 0 ? "1.20.1-" + remoteVersion : remoteVersion;
remoteVersion = NeoForgeRemoteVersion.normalize(remoteVersion);
}
return super.getVersion(gameVersion, remoteVersion);
}
@ -78,16 +78,10 @@ public final class NeoForgeBMCLVersionList extends VersionList<NeoForgeRemoteVer
try {
versions.clear(gameVersion);
for (NeoForgeVersion neoForgeVersion : neoForgeVersions) {
String nf = StringUtils.removePrefix(
neoForgeVersion.version,
"1.20.1".equals(gameVersion) ? "1.20.1-forge-" : "neoforge-" // Som of the version numbers for 1.20.1 are like forge.
);
versions.put(gameVersion, new NeoForgeRemoteVersion(
neoForgeVersion.mcVersion,
nf,
Lang.immutableListOf(
apiRoot + "/neoforge/version/" + neoForgeVersion.version + "/download/installer.jar"
)
NeoForgeRemoteVersion.normalize(neoForgeVersion.version),
Collections.singletonList(apiRoot + "/neoforge/version/" + neoForgeVersion.version + "/download/installer.jar")
));
}
} finally {

View File

@ -31,10 +31,7 @@ public final class NeoForgeOfficialVersionList extends VersionList<NeoForgeRemot
@Override
public Optional<NeoForgeRemoteVersion> getVersion(String gameVersion, String remoteVersion) {
if (gameVersion.equals("1.20.1")) {
remoteVersion = NeoForgeRemoteVersion.fixInvalidVersion(remoteVersion);
if (!remoteVersion.equals("47.1.82")) {
remoteVersion = "1.20.1-" + remoteVersion;
}
remoteVersion = NeoForgeRemoteVersion.normalize(remoteVersion);
}
return super.getVersion(gameVersion, remoteVersion);
}
@ -52,7 +49,7 @@ public final class NeoForgeOfficialVersionList extends VersionList<NeoForgeRemot
for (String version : results[0].versions) {
versions.put("1.20.1", new NeoForgeRemoteVersion(
"1.20.1", StringUtils.removePrefix(version, "1.20.1-"),
"1.20.1", NeoForgeRemoteVersion.normalize(version),
Lang.immutableListOf(
downloadProvider.injectURL("https://maven.neoforged.net/releases/net/neoforged/forge/" + version + "/forge-" + version + "-installer.jar")
)
@ -63,7 +60,7 @@ public final class NeoForgeOfficialVersionList extends VersionList<NeoForgeRemot
int si1 = version.indexOf('.'), si2 = version.indexOf('.', version.indexOf('.') + 1);
String mcVersion = "1." + version.substring(0, Integer.parseInt(version.substring(si1 + 1, si2)) == 0 ? si1 : si2);
versions.put(mcVersion, new NeoForgeRemoteVersion(
mcVersion, version,
mcVersion, NeoForgeRemoteVersion.normalize(version),
Lang.immutableListOf(
downloadProvider.injectURL("https://maven.neoforged.net/releases/net/neoforged/neoforge/" + version + "/neoforge-" + version + "-installer.jar")
)

View File

@ -18,7 +18,7 @@ public class NeoForgeRemoteVersion extends RemoteVersion {
return new NeoForgeInstallTask(dependencyManager, baseVersion, this);
}
public static String fixInvalidVersion(String version) {
public static String normalize(String version) {
if (version.startsWith("1.20.1-")) {
if (version.startsWith("forge-", "1.20.1-".length())) {
return version.substring("1.20.1-forge-".length());

View File

@ -96,6 +96,10 @@ public final class CurseForgeRemoteModRepository implements RemoteModRepository
return "asc";
}
private int calculateTotalPages(Response<List<CurseAddon>> response, int pageSize) {
return (int) Math.ceil((double) Math.min(response.pagination.totalCount, 10000) / pageSize);
}
@Override
public SearchResult search(String gameVersion, @Nullable RemoteModRepository.Category category, int pageOffset, int pageSize, String searchFilter, SortType sortType, SortOrder sortOrder) throws IOException {
int categoryId = 0;
@ -115,7 +119,7 @@ public final class CurseForgeRemoteModRepository implements RemoteModRepository
}.getType());
Stream<RemoteMod> res = response.getData().stream().map(CurseAddon::toMod);
if (sortType != SortType.NAME || searchFilter.isEmpty()) {
return new SearchResult(res, (int)Math.ceil((double)response.pagination.totalCount / pageSize));
return new SearchResult(res, calculateTotalPages(response, pageSize));
}
String lowerCaseSearchFilter = searchFilter.toLowerCase();
@ -137,7 +141,7 @@ public final class CurseForgeRemoteModRepository implements RemoteModRepository
}
return pair(remoteMod, diff);
}).sorted(Comparator.comparingInt(Pair::getValue)).map(Pair::getKey), res, response.pagination.totalCount);
}).sorted(Comparator.comparingInt(Pair::getValue)).map(Pair::getKey), res, calculateTotalPages(response, pageSize));
}
@Override

View File

@ -189,13 +189,18 @@ public abstract class HttpRequest {
os.write(bytes);
}
URL url = new URL(this.url);
if (responseCodeTester != null) {
responseCodeTester.accept(new URL(url), con.getResponseCode());
responseCodeTester.accept(url, con.getResponseCode());
} else {
if (con.getResponseCode() / 100 != 2) {
if (!ignoreHttpCode && !toleratedHttpCodes.contains(con.getResponseCode())) {
String data = NetworkUtils.readData(con);
throw new ResponseCodeException(new URL(url), con.getResponseCode(), data);
try {
throw new ResponseCodeException(url, con.getResponseCode(), NetworkUtils.readData(con));
} catch (IOException e) {
throw new ResponseCodeException(url, con.getResponseCode(), e);
}
}
}
}

View File

@ -33,6 +33,13 @@ public final class ResponseCodeException extends IOException {
this.data = null;
}
public ResponseCodeException(URL url, int responseCode, Throwable cause) {
super("Unable to request url " + url + ", response code: " + responseCode, cause);
this.url = url;
this.responseCode = responseCode;
this.data = null;
}
public ResponseCodeException(URL url, int responseCode, String data) {
super("Unable to request url " + url + ", response code: " + responseCode + ", data: " + data);
this.url = url;