2019-07-10 06:34:10 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2019-07-10 06:34:10 +00:00
|
|
|
*
|
2019-07-11 21:30:35 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2019-07-10 06:34:10 +00:00
|
|
|
*
|
2019-07-11 21:30:35 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2019-07-10 06:34:10 +00:00
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
package org.geysermc.geyser.network;
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2022-10-30 00:23:21 +00:00
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.BedrockDisconnectReasons;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.ExperimentData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.PacketCompressionAlgorithm;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.ResourcePackType;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.BedrockPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.LoginPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ModalFormResponsePacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.MovePlayerPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.NetworkSettingsPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.PlayStatusPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.RequestNetworkSettingsPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ResourcePackChunkDataPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ResourcePackChunkRequestPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ResourcePackClientResponsePacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ResourcePackDataInfoPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ResourcePackStackPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ResourcePacksInfoPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.SetTitlePacket;
|
|
|
|
import org.cloudburstmc.protocol.common.PacketSignal;
|
2023-02-15 05:01:33 +00:00
|
|
|
import org.geysermc.geyser.Constants;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2022-03-20 02:55:29 +00:00
|
|
|
import org.geysermc.geyser.api.network.AuthType;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.configuration.GeyserConfiguration;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.pack.ResourcePack;
|
|
|
|
import org.geysermc.geyser.pack.ResourcePackManifest;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.registry.BlockRegistries;
|
|
|
|
import org.geysermc.geyser.registry.Registries;
|
2022-04-21 01:37:50 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2022-07-09 22:39:02 +00:00
|
|
|
import org.geysermc.geyser.session.PendingMicrosoftAuthentication;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
2022-04-21 01:37:50 +00:00
|
|
|
import org.geysermc.geyser.util.LoginEncryptionUtils;
|
|
|
|
import org.geysermc.geyser.util.MathUtils;
|
2023-02-15 05:01:33 +00:00
|
|
|
import org.geysermc.geyser.util.VersionCheckUtils;
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2020-09-17 00:08:26 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.InputStream;
|
2022-11-24 20:19:55 +00:00
|
|
|
import java.util.ArrayDeque;
|
|
|
|
import java.util.Deque;
|
2023-02-15 05:01:33 +00:00
|
|
|
import java.util.OptionalInt;
|
2020-09-17 00:08:26 +00:00
|
|
|
|
2019-08-02 01:38:21 +00:00
|
|
|
public class UpstreamPacketHandler extends LoggingPacketHandler {
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2022-11-24 20:19:55 +00:00
|
|
|
private Deque<String> packsToSent = new ArrayDeque<>();
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public UpstreamPacketHandler(GeyserImpl geyser, GeyserSession session) {
|
2021-11-20 21:34:30 +00:00
|
|
|
super(geyser, session);
|
2019-08-02 01:38:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 00:23:21 +00:00
|
|
|
private PacketSignal translateAndDefault(BedrockPacket packet) {
|
2022-12-23 21:18:48 +00:00
|
|
|
Registries.BEDROCK_PACKET_TRANSLATORS.translate(packet.getClass(), packet, session);
|
|
|
|
return PacketSignal.HANDLED; // PacketSignal.UNHANDLED will log a WARN publicly
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 00:39:29 +00:00
|
|
|
@Override
|
2022-10-30 00:23:21 +00:00
|
|
|
PacketSignal defaultHandler(BedrockPacket packet) {
|
2021-08-17 00:39:29 +00:00
|
|
|
return translateAndDefault(packet);
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:18:48 +00:00
|
|
|
private boolean newProtocol = false; // TEMPORARY
|
|
|
|
|
|
|
|
private boolean setCorrectCodec(int protocolVersion) {
|
2022-10-30 00:23:21 +00:00
|
|
|
BedrockCodec packetCodec = GameProtocol.getBedrockCodec(protocolVersion);
|
2022-09-18 19:18:48 +00:00
|
|
|
if (packetCodec == null) {
|
|
|
|
String supportedVersions = GameProtocol.getAllSupportedBedrockVersions();
|
|
|
|
if (protocolVersion > GameProtocol.DEFAULT_BEDROCK_CODEC.getProtocolVersion()) {
|
|
|
|
// Too early to determine session locale
|
2023-02-15 05:01:33 +00:00
|
|
|
String disconnectMessage = GeyserLocale.getLocaleStringLog("geyser.network.outdated.server", supportedVersions);
|
|
|
|
// If the latest release matches this version, then let the user know.
|
|
|
|
OptionalInt latestRelease = VersionCheckUtils.getLatestBedrockRelease();
|
|
|
|
if (latestRelease.isPresent() && latestRelease.getAsInt() == protocolVersion) {
|
|
|
|
// Random note: don't make the disconnect message too long or Bedrock will cut it off on smaller screens
|
|
|
|
disconnectMessage += "\n" + GeyserLocale.getLocaleStringLog("geyser.version.new.on_disconnect", Constants.GEYSER_DOWNLOAD_LOCATION);
|
|
|
|
}
|
|
|
|
session.disconnect(disconnectMessage);
|
2022-09-18 19:18:48 +00:00
|
|
|
return false;
|
|
|
|
} else if (protocolVersion < GameProtocol.DEFAULT_BEDROCK_CODEC.getProtocolVersion()) {
|
|
|
|
session.disconnect(GeyserLocale.getLocaleStringLog("geyser.network.outdated.client", supportedVersions));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 00:23:21 +00:00
|
|
|
session.getUpstream().getSession().setCodec(packetCodec);
|
2022-09-18 19:18:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-15 01:17:08 +00:00
|
|
|
@Override
|
2022-10-30 00:23:21 +00:00
|
|
|
public void onDisconnect(String reason) {
|
|
|
|
// Use our own disconnect messages for these reasons
|
|
|
|
if (BedrockDisconnectReasons.CLOSED.equals(reason)) {
|
|
|
|
this.session.getUpstream().getSession().setDisconnectReason(GeyserLocale.getLocaleStringLog("geyser.network.disconnect.closed_by_remote_peer"));
|
|
|
|
} else if (BedrockDisconnectReasons.TIMEOUT.equals(reason)) {
|
|
|
|
this.session.getUpstream().getSession().setDisconnectReason(GeyserLocale.getLocaleStringLog("geyser.network.disconnect.timed_out"));
|
|
|
|
}
|
2022-12-23 21:18:48 +00:00
|
|
|
this.session.disconnect(this.session.getUpstream().getSession().getDisconnectReason());
|
2022-10-30 00:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PacketSignal handle(RequestNetworkSettingsPacket packet) {
|
2022-09-18 19:18:48 +00:00
|
|
|
if (setCorrectCodec(packet.getProtocolVersion())) {
|
|
|
|
newProtocol = true;
|
|
|
|
} else {
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2022-09-18 19:18:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:17:08 +00:00
|
|
|
// New since 1.19.30 - sent before login packet
|
|
|
|
PacketCompressionAlgorithm algorithm = PacketCompressionAlgorithm.ZLIB;
|
|
|
|
|
|
|
|
NetworkSettingsPacket responsePacket = new NetworkSettingsPacket();
|
|
|
|
responsePacket.setCompressionAlgorithm(algorithm);
|
|
|
|
responsePacket.setCompressionThreshold(512);
|
|
|
|
session.sendUpstreamPacketImmediately(responsePacket);
|
|
|
|
|
|
|
|
session.getUpstream().getSession().setCompression(algorithm);
|
2022-12-23 21:18:48 +00:00
|
|
|
session.getUpstream().getSession().setCompressionLevel(this.geyser.getConfig().getBedrock().getCompressionLevel());
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2022-09-15 01:17:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
@Override
|
2022-10-30 00:23:21 +00:00
|
|
|
public PacketSignal handle(LoginPacket loginPacket) {
|
2021-11-20 21:34:30 +00:00
|
|
|
if (geyser.isShuttingDown()) {
|
2021-11-12 14:02:14 +00:00
|
|
|
// Don't allow new players in if we're no longer operating
|
2021-11-20 23:29:46 +00:00
|
|
|
session.disconnect(GeyserLocale.getLocaleStringLog("geyser.core.shutdown.kick.message"));
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2021-11-12 14:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 21:18:48 +00:00
|
|
|
// session.getUpstream().getSession().getCodec() == null
|
|
|
|
|
2022-09-18 19:18:48 +00:00
|
|
|
if (!newProtocol) {
|
|
|
|
if (!setCorrectCodec(loginPacket.getProtocolVersion())) { // REMOVE WHEN ONLY 1.19.30 IS SUPPORTED OR 1.20
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2020-08-11 14:00:14 +00:00
|
|
|
}
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 17:51:48 +00:00
|
|
|
// Set the block translation based off of version
|
2021-07-13 01:19:40 +00:00
|
|
|
session.setBlockMappings(BlockRegistries.BLOCKS.forVersion(loginPacket.getProtocolVersion()));
|
|
|
|
session.setItemMappings(Registries.ITEMS.forVersion(loginPacket.getProtocolVersion()));
|
2021-03-09 17:51:48 +00:00
|
|
|
|
2021-08-30 17:55:01 +00:00
|
|
|
LoginEncryptionUtils.encryptPlayerConnection(session, loginPacket);
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2021-12-06 00:03:47 +00:00
|
|
|
if (session.isClosed()) {
|
|
|
|
// Can happen if Xbox validation fails
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2021-12-06 00:03:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
PlayStatusPacket playStatus = new PlayStatusPacket();
|
|
|
|
playStatus.setStatus(PlayStatusPacket.Status.LOGIN_SUCCESS);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(playStatus);
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
geyser.getSessionManager().addPendingSession(session);
|
2021-11-12 14:02:14 +00:00
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket();
|
2020-09-17 00:08:26 +00:00
|
|
|
for(ResourcePack resourcePack : ResourcePack.PACKS.values()) {
|
|
|
|
ResourcePackManifest.Header header = resourcePack.getManifest().getHeader();
|
2020-12-09 16:30:59 +00:00
|
|
|
resourcePacksInfo.getResourcePackInfos().add(new ResourcePacksInfoPacket.Entry(
|
|
|
|
header.getUuid().toString(), header.getVersionString(), resourcePack.getFile().length(),
|
2022-09-03 18:12:48 +00:00
|
|
|
resourcePack.getContentKey(), "", header.getUuid().toString(), false, false));
|
2020-09-17 00:08:26 +00:00
|
|
|
}
|
2021-11-20 21:34:30 +00:00
|
|
|
resourcePacksInfo.setForcedToAccept(GeyserImpl.getInstance().getConfig().isForceResourcePacks());
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(resourcePacksInfo);
|
2021-11-13 16:03:55 +00:00
|
|
|
|
2022-01-16 21:09:53 +00:00
|
|
|
GeyserLocale.loadGeyserLocale(session.locale());
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-10-30 00:23:21 +00:00
|
|
|
public PacketSignal handle(ResourcePackClientResponsePacket packet) {
|
2019-11-02 21:14:28 +00:00
|
|
|
switch (packet.getStatus()) {
|
2019-07-10 06:34:10 +00:00
|
|
|
case COMPLETED:
|
2022-07-09 22:39:02 +00:00
|
|
|
if (geyser.getConfig().getRemote().authType() != AuthType.ONLINE) {
|
2021-11-21 18:36:42 +00:00
|
|
|
session.authenticate(session.getAuthData().name());
|
|
|
|
} else if (!couldLoginUserByName(session.getAuthData().name())) {
|
2021-11-13 16:03:55 +00:00
|
|
|
// We must spawn the white world
|
|
|
|
session.connect();
|
|
|
|
}
|
2021-11-21 18:36:42 +00:00
|
|
|
geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.network.connect", session.getAuthData().name()));
|
2019-07-10 06:34:10 +00:00
|
|
|
break;
|
2020-09-17 00:08:26 +00:00
|
|
|
|
|
|
|
case SEND_PACKS:
|
2022-11-24 20:19:55 +00:00
|
|
|
packsToSent.addAll(packet.getPackIds());
|
|
|
|
sendPackDataInfo(packsToSent.pop());
|
2020-09-17 00:08:26 +00:00
|
|
|
break;
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
case HAVE_ALL_PACKS:
|
2020-09-17 00:08:26 +00:00
|
|
|
ResourcePackStackPacket stackPacket = new ResourcePackStackPacket();
|
2020-10-08 22:33:36 +00:00
|
|
|
stackPacket.setExperimentsPreviouslyToggled(false);
|
2020-09-17 00:08:26 +00:00
|
|
|
stackPacket.setForcedToAccept(false); // Leaving this as false allows the player to choose to download or not
|
|
|
|
stackPacket.setGameVersion(session.getClientData().getGameVersion());
|
|
|
|
|
|
|
|
for (ResourcePack pack : ResourcePack.PACKS.values()) {
|
|
|
|
ResourcePackManifest.Header header = pack.getManifest().getHeader();
|
|
|
|
stackPacket.getResourcePacks().add(new ResourcePackStackPacket.Entry(header.getUuid().toString(), header.getVersionString(), ""));
|
|
|
|
}
|
|
|
|
|
2022-07-02 16:50:16 +00:00
|
|
|
if (GeyserImpl.getInstance().getConfig().isAddNonBedrockItems()) {
|
2021-03-31 18:06:05 +00:00
|
|
|
// Allow custom items to work
|
|
|
|
stackPacket.getExperiments().add(new ExperimentData("data_driven_items", true));
|
|
|
|
}
|
|
|
|
|
2020-09-17 00:08:26 +00:00
|
|
|
session.sendUpstreamPacket(stackPacket);
|
2019-07-10 06:34:10 +00:00
|
|
|
break;
|
2020-09-17 00:08:26 +00:00
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
default:
|
2020-04-25 22:53:35 +00:00
|
|
|
session.disconnect("disconnectionScreen.resourcePack");
|
2019-07-10 06:34:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-07-13 19:00:51 +00:00
|
|
|
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
2019-07-12 20:59:36 +00:00
|
|
|
|
|
|
|
@Override
|
2022-10-30 00:23:21 +00:00
|
|
|
public PacketSignal handle(ModalFormResponsePacket packet) {
|
2021-08-18 00:57:46 +00:00
|
|
|
session.executeInEventLoop(() -> session.getFormCache().handleResponse(packet));
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2019-07-12 20:59:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 20:15:32 +00:00
|
|
|
private boolean couldLoginUserByName(String bedrockUsername) {
|
2022-03-03 23:52:26 +00:00
|
|
|
if (geyser.getConfig().getSavedUserLogins().contains(bedrockUsername)) {
|
|
|
|
String refreshToken = geyser.refreshTokenFor(bedrockUsername);
|
|
|
|
if (refreshToken != null) {
|
|
|
|
geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.auth.stored_credentials", session.getAuthData().name()));
|
|
|
|
session.authenticateWithRefreshToken(refreshToken);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-11-20 21:34:30 +00:00
|
|
|
if (geyser.getConfig().getUserAuths() != null) {
|
|
|
|
GeyserConfiguration.IUserAuthenticationInfo info = geyser.getConfig().getUserAuths().get(bedrockUsername);
|
2019-08-02 20:15:32 +00:00
|
|
|
|
|
|
|
if (info != null) {
|
2021-11-21 18:36:42 +00:00
|
|
|
geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.auth.stored_credentials", session.getAuthData().name()));
|
2021-01-11 20:52:02 +00:00
|
|
|
session.setMicrosoftAccount(info.isMicrosoftAccount());
|
2019-12-01 21:16:52 +00:00
|
|
|
session.authenticate(info.getEmail(), info.getPassword());
|
2019-08-02 20:15:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 20:45:56 +00:00
|
|
|
PendingMicrosoftAuthentication.AuthenticationTask task = geyser.getPendingMicrosoftAuthentication().getTask(session.getAuthData().xuid());
|
|
|
|
if (task != null) {
|
|
|
|
if (task.getAuthentication().isDone() && session.onMicrosoftLoginComplete(task)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 20:15:32 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:12:03 +00:00
|
|
|
@Override
|
2022-10-30 00:23:21 +00:00
|
|
|
public PacketSignal handle(MovePlayerPacket packet) {
|
2019-09-25 21:52:28 +00:00
|
|
|
if (session.isLoggingIn()) {
|
2020-11-24 16:14:02 +00:00
|
|
|
SetTitlePacket titlePacket = new SetTitlePacket();
|
|
|
|
titlePacket.setType(SetTitlePacket.Type.ACTIONBAR);
|
2022-01-16 21:09:53 +00:00
|
|
|
titlePacket.setText(GeyserLocale.getPlayerLocaleString("geyser.auth.login.wait", session.locale()));
|
2020-11-24 16:14:02 +00:00
|
|
|
titlePacket.setFadeInTime(0);
|
|
|
|
titlePacket.setFadeOutTime(1);
|
|
|
|
titlePacket.setStayTime(2);
|
2021-07-13 01:19:40 +00:00
|
|
|
titlePacket.setXuid("");
|
|
|
|
titlePacket.setPlatformOnlineId("");
|
2020-11-24 16:14:02 +00:00
|
|
|
session.sendUpstreamPacket(titlePacket);
|
2019-07-24 06:29:54 +00:00
|
|
|
}
|
2019-09-14 00:37:06 +00:00
|
|
|
|
|
|
|
return translateAndDefault(packet);
|
2019-07-12 20:59:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 00:08:26 +00:00
|
|
|
@Override
|
2022-10-30 00:23:21 +00:00
|
|
|
public PacketSignal handle(ResourcePackChunkRequestPacket packet) {
|
2020-09-17 00:08:26 +00:00
|
|
|
ResourcePackChunkDataPacket data = new ResourcePackChunkDataPacket();
|
|
|
|
ResourcePack pack = ResourcePack.PACKS.get(packet.getPackId().toString());
|
|
|
|
|
|
|
|
data.setChunkIndex(packet.getChunkIndex());
|
|
|
|
data.setProgress(packet.getChunkIndex() * ResourcePack.CHUNK_SIZE);
|
|
|
|
data.setPackVersion(packet.getPackVersion());
|
|
|
|
data.setPackId(packet.getPackId());
|
|
|
|
|
|
|
|
int offset = packet.getChunkIndex() * ResourcePack.CHUNK_SIZE;
|
2022-11-24 20:19:55 +00:00
|
|
|
long remainingSize = pack.getFile().length() - offset;
|
|
|
|
byte[] packData = new byte[(int) MathUtils.constrain(remainingSize, 0, ResourcePack.CHUNK_SIZE)];
|
2020-09-17 00:08:26 +00:00
|
|
|
|
|
|
|
try (InputStream inputStream = new FileInputStream(pack.getFile())) {
|
|
|
|
inputStream.skip(offset);
|
|
|
|
inputStream.read(packData, 0, packData.length);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2022-10-30 00:23:21 +00:00
|
|
|
data.setData(Unpooled.wrappedBuffer(packData));
|
2020-09-17 00:08:26 +00:00
|
|
|
|
|
|
|
session.sendUpstreamPacket(data);
|
2022-11-24 20:19:55 +00:00
|
|
|
|
|
|
|
// Check if it is the last chunk and send next pack in queue when available.
|
|
|
|
if (remainingSize <= ResourcePack.CHUNK_SIZE && !packsToSent.isEmpty()) {
|
|
|
|
sendPackDataInfo(packsToSent.pop());
|
|
|
|
}
|
|
|
|
|
2022-10-30 00:23:21 +00:00
|
|
|
return PacketSignal.HANDLED;
|
2020-09-17 00:08:26 +00:00
|
|
|
}
|
2022-11-24 20:19:55 +00:00
|
|
|
|
|
|
|
private void sendPackDataInfo(String id) {
|
|
|
|
ResourcePackDataInfoPacket data = new ResourcePackDataInfoPacket();
|
|
|
|
String[] packID = id.split("_");
|
|
|
|
ResourcePack pack = ResourcePack.PACKS.get(packID[0]);
|
|
|
|
ResourcePackManifest.Header header = pack.getManifest().getHeader();
|
|
|
|
|
|
|
|
data.setPackId(header.getUuid());
|
|
|
|
int chunkCount = (int) Math.ceil((int) pack.getFile().length() / (double) ResourcePack.CHUNK_SIZE);
|
|
|
|
data.setChunkCount(chunkCount);
|
|
|
|
data.setCompressedPackSize(pack.getFile().length());
|
|
|
|
data.setMaxChunkSize(ResourcePack.CHUNK_SIZE);
|
|
|
|
data.setHash(pack.getSha256());
|
|
|
|
data.setPackVersion(packID[1]);
|
|
|
|
data.setPremium(false);
|
2022-12-21 00:47:45 +00:00
|
|
|
data.setType(ResourcePackType.RESOURCES);
|
2022-11-24 20:19:55 +00:00
|
|
|
|
|
|
|
session.sendUpstreamPacket(data);
|
|
|
|
}
|
2019-12-23 17:47:31 +00:00
|
|
|
}
|