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;
|
2023-06-17 01:39:53 +00:00
|
|
|
import org.geysermc.geyser.api.pack.PackCodec;
|
|
|
|
import org.geysermc.geyser.api.pack.ResourcePack;
|
|
|
|
import org.geysermc.geyser.api.pack.ResourcePackManifest;
|
|
|
|
import org.geysermc.geyser.event.type.SessionLoadResourcePacksEventImpl;
|
|
|
|
import org.geysermc.geyser.pack.GeyserResourcePack;
|
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
|
|
|
|
2023-06-17 01:39:53 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.channels.SeekableByteChannel;
|
2022-11-24 20:19:55 +00:00
|
|
|
import java.util.ArrayDeque;
|
|
|
|
import java.util.Deque;
|
2023-06-17 01:39:53 +00:00
|
|
|
import java.util.HashMap;
|
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
|
|
|
|
2023-06-09 16:10:59 +00:00
|
|
|
private boolean networkSettingsRequested = false;
|
2023-06-17 01:39:53 +00:00
|
|
|
private final Deque<String> packsToSent = new ArrayDeque<>();
|
|
|
|
|
|
|
|
private SessionLoadResourcePacksEventImpl resourcePackLoadEvent;
|
2022-11-24 20:19:55 +00:00
|
|
|
|
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 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;
|
2023-10-19 05:11:59 +00:00
|
|
|
} else {
|
|
|
|
throw new IllegalStateException("Default codec of protocol version " + protocolVersion + " should have been found");
|
2022-09-18 19:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-06-08 14:23:27 +00:00
|
|
|
if (!setCorrectCodec(packet.getProtocolVersion())) {
|
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());
|
2023-06-09 16:10:59 +00:00
|
|
|
networkSettingsRequested = true;
|
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
|
|
|
}
|
|
|
|
|
2023-06-09 16:10:59 +00:00
|
|
|
if (!networkSettingsRequested) {
|
|
|
|
session.disconnect(GeyserLocale.getLocaleStringLog("geyser.network.outdated.client", GameProtocol.getAllSupportedBedrockVersions()));
|
|
|
|
return PacketSignal.HANDLED;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-06-17 01:39:53 +00:00
|
|
|
this.resourcePackLoadEvent = new SessionLoadResourcePacksEventImpl(session, new HashMap<>(Registries.RESOURCE_PACKS.get()));
|
|
|
|
this.geyser.eventBus().fire(this.resourcePackLoadEvent);
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket();
|
2023-06-17 01:39:53 +00:00
|
|
|
for (ResourcePack pack : this.resourcePackLoadEvent.resourcePacks()) {
|
|
|
|
PackCodec codec = pack.codec();
|
|
|
|
ResourcePackManifest.Header header = pack.manifest().header();
|
2020-12-09 16:30:59 +00:00
|
|
|
resourcePacksInfo.getResourcePackInfos().add(new ResourcePacksInfoPacket.Entry(
|
2023-06-17 01:39:53 +00:00
|
|
|
header.uuid().toString(), header.version().toString(), codec.size(), pack.contentKey(),
|
|
|
|
"", header.uuid().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());
|
|
|
|
|
2023-06-17 01:39:53 +00:00
|
|
|
for (ResourcePack pack : this.resourcePackLoadEvent.resourcePacks()) {
|
|
|
|
ResourcePackManifest.Header header = pack.manifest().header();
|
|
|
|
stackPacket.getResourcePacks().add(new ResourcePackStackPacket.Entry(header.uuid().toString(), header.version().toString(), ""));
|
2020-09-17 00:08:26 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 20:45:56 +00:00
|
|
|
PendingMicrosoftAuthentication.AuthenticationTask task = geyser.getPendingMicrosoftAuthentication().getTask(session.getAuthData().xuid());
|
|
|
|
if (task != null) {
|
2023-10-02 23:12:54 +00:00
|
|
|
return task.getAuthentication().isDone() && session.onMicrosoftLoginComplete(task);
|
2022-02-26 20:45:56 +00:00
|
|
|
}
|
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();
|
2023-06-17 01:39:53 +00:00
|
|
|
ResourcePack pack = this.resourcePackLoadEvent.getPacks().get(packet.getPackId().toString());
|
|
|
|
PackCodec codec = pack.codec();
|
2020-09-17 00:08:26 +00:00
|
|
|
|
|
|
|
data.setChunkIndex(packet.getChunkIndex());
|
2023-06-17 01:39:53 +00:00
|
|
|
data.setProgress((long) packet.getChunkIndex() * GeyserResourcePack.CHUNK_SIZE);
|
2020-09-17 00:08:26 +00:00
|
|
|
data.setPackVersion(packet.getPackVersion());
|
|
|
|
data.setPackId(packet.getPackId());
|
|
|
|
|
2023-06-17 01:39:53 +00:00
|
|
|
int offset = packet.getChunkIndex() * GeyserResourcePack.CHUNK_SIZE;
|
|
|
|
long remainingSize = codec.size() - offset;
|
|
|
|
byte[] packData = new byte[(int) MathUtils.constrain(remainingSize, 0, GeyserResourcePack.CHUNK_SIZE)];
|
2020-09-17 00:08:26 +00:00
|
|
|
|
2023-06-17 01:39:53 +00:00
|
|
|
try (SeekableByteChannel channel = codec.serialize(pack)) {
|
|
|
|
channel.position(offset);
|
|
|
|
channel.read(ByteBuffer.wrap(packData, 0, packData.length));
|
|
|
|
} catch (IOException e) {
|
2020-09-17 00:08:26 +00:00
|
|
|
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.
|
2023-06-17 01:39:53 +00:00
|
|
|
if (remainingSize <= GeyserResourcePack.CHUNK_SIZE && !packsToSent.isEmpty()) {
|
2022-11-24 20:19:55 +00:00
|
|
|
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("_");
|
2023-06-17 01:39:53 +00:00
|
|
|
ResourcePack pack = this.resourcePackLoadEvent.getPacks().get(packID[0]);
|
|
|
|
PackCodec codec = pack.codec();
|
|
|
|
ResourcePackManifest.Header header = pack.manifest().header();
|
2022-11-24 20:19:55 +00:00
|
|
|
|
2023-06-17 01:39:53 +00:00
|
|
|
data.setPackId(header.uuid());
|
|
|
|
int chunkCount = (int) Math.ceil(codec.size() / (double) GeyserResourcePack.CHUNK_SIZE);
|
2022-11-24 20:19:55 +00:00
|
|
|
data.setChunkCount(chunkCount);
|
2023-06-17 01:39:53 +00:00
|
|
|
data.setCompressedPackSize(codec.size());
|
|
|
|
data.setMaxChunkSize(GeyserResourcePack.CHUNK_SIZE);
|
|
|
|
data.setHash(codec.sha256());
|
2022-11-24 20:19:55 +00:00
|
|
|
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
|
|
|
}
|