2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
|
|
|
* Copyright (C) 2016-2021 ViaVersion 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 21:16:10 +02:00
|
|
|
package com.viaversion.viaversion.protocol;
|
2016-03-17 22:24:25 +01:00
|
|
|
|
2021-03-21 20:25:52 +01:00
|
|
|
import com.google.common.base.Preconditions;
|
2016-05-19 19:56:42 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2018-07-03 14:58:00 +02:00
|
|
|
import com.google.common.collect.Range;
|
2020-07-21 09:10:16 +02:00
|
|
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
2021-04-26 22:54:43 +02:00
|
|
|
import com.viaversion.viaversion.api.Via;
|
|
|
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
|
|
|
import com.viaversion.viaversion.api.data.MappingDataLoader;
|
2021-04-26 21:16:10 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.ProtocolManager;
|
|
|
|
import com.viaversion.viaversion.api.protocol.ProtocolPathEntry;
|
|
|
|
import com.viaversion.viaversion.api.protocol.ProtocolPathKey;
|
2021-04-27 18:21:51 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.Protocol;
|
2021-04-27 13:41:39 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
2021-04-26 21:16:10 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
|
|
|
import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion;
|
2021-04-26 22:54:43 +02:00
|
|
|
import com.viaversion.viaversion.protocol.packet.PacketWrapperImpl;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.protocols.base.BaseProtocol;
|
|
|
|
import com.viaversion.viaversion.protocols.base.BaseProtocol1_16;
|
|
|
|
import com.viaversion.viaversion.protocols.base.BaseProtocol1_7;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_10to1_9_3.Protocol1_10To1_9_3_4;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_11_1to1_11.Protocol1_11_1To1_11;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_11to1_10.Protocol1_11To1_10;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.Protocol1_12_1To1_12;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_12_2to1_12_1.Protocol1_12_2To1_12_1;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.Protocol1_12To1_11_1;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_13_2to1_13_1.Protocol1_13_2To1_13_1;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_14_1to1_14.Protocol1_14_1To1_14;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_14_2to1_14_1.Protocol1_14_2To1_14_1;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_14_3to1_14_2.Protocol1_14_3To1_14_2;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_14_4to1_14_3.Protocol1_14_4To1_14_3;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_15_1to1_15.Protocol1_15_1To1_15;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_15_2to1_15_1.Protocol1_15_2To1_15_1;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_16_1to1_16.Protocol1_16_1To1_16;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_16_3to1_16_2.Protocol1_16_3To1_16_2;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_16_4to1_16_3.Protocol1_16_4To1_16_3;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.Protocol1_9_1_2To1_9_3_4;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9_1to1_9.Protocol1_9_1To1_9;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.Protocol1_9_3To1_9_1_2;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_9_1.Protocol1_9To1_9_1;
|
2021-04-26 22:54:43 +02:00
|
|
|
import com.viaversion.viaversion.util.Pair;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
2021-04-27 13:25:18 +02:00
|
|
|
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
|
2016-03-17 22:24:25 +01:00
|
|
|
|
2020-04-03 00:05:44 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.SortedSet;
|
|
|
|
import java.util.TreeSet;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.SynchronousQueue;
|
2020-07-21 09:10:16 +02:00
|
|
|
import java.util.concurrent.ThreadFactory;
|
2020-04-03 00:05:44 +02:00
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-03-21 20:25:52 +01:00
|
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
|
|
import java.util.function.Function;
|
2016-03-17 22:24:25 +01:00
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
public class ProtocolManagerImpl implements ProtocolManager {
|
2021-04-21 11:58:19 +02:00
|
|
|
private static final Protocol BASE_PROTOCOL = new BaseProtocol();
|
2021-03-24 15:21:18 +01:00
|
|
|
|
2016-03-17 22:24:25 +01:00
|
|
|
// Input Version -> Output Version & Protocol (Allows fast lookup)
|
2021-03-24 15:21:18 +01:00
|
|
|
private final Int2ObjectMap<Int2ObjectMap<Protocol>> registryMap = new Int2ObjectOpenHashMap<>(32);
|
|
|
|
private final Map<Class<? extends Protocol>, Protocol> protocols = new HashMap<>();
|
2021-03-25 22:34:30 +01:00
|
|
|
private final Map<ProtocolPathKey, List<ProtocolPathEntry>> pathCache = new ConcurrentHashMap<>();
|
2021-03-24 15:21:18 +01:00
|
|
|
private final Set<Integer> supportedVersions = new HashSet<>();
|
|
|
|
private final List<Pair<Range<Integer>, Protocol>> baseProtocols = Lists.newCopyOnWriteArrayList();
|
|
|
|
private final List<Protocol> registerList = new ArrayList<>();
|
|
|
|
|
|
|
|
private final ReadWriteLock mappingLoaderLock = new ReentrantReadWriteLock();
|
|
|
|
private Map<Class<? extends Protocol>, CompletableFuture<Void>> mappingLoaderFutures = new HashMap<>();
|
|
|
|
private ThreadPoolExecutor mappingLoaderExecutor;
|
|
|
|
private boolean mappingsLoaded;
|
|
|
|
|
2021-03-26 12:51:38 +01:00
|
|
|
private ServerProtocolVersion serverProtocolVersion = new ServerProtocolVersionSingleton(-1);
|
2021-03-24 15:21:18 +01:00
|
|
|
private int maxProtocolPathSize = 50;
|
|
|
|
|
|
|
|
public ProtocolManagerImpl() {
|
2020-07-21 09:10:16 +02:00
|
|
|
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Via-Mappingloader-%d").build();
|
|
|
|
mappingLoaderExecutor = new ThreadPoolExecutor(5, 16, 45L, TimeUnit.SECONDS, new SynchronousQueue<>(), threadFactory);
|
2020-03-23 01:23:17 +01:00
|
|
|
mappingLoaderExecutor.allowCoreThreadTimeOut(true);
|
2021-03-24 15:21:18 +01:00
|
|
|
}
|
2020-03-23 01:23:17 +01:00
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
public void registerProtocols() {
|
2016-04-22 14:28:54 +02:00
|
|
|
// Base Protocol
|
2018-07-03 14:58:00 +02:00
|
|
|
registerBaseProtocol(BASE_PROTOCOL, Range.lessThan(Integer.MIN_VALUE));
|
2020-10-16 18:21:45 +02:00
|
|
|
registerBaseProtocol(new BaseProtocol1_7(), Range.lessThan(ProtocolVersion.v1_16.getVersion()));
|
|
|
|
registerBaseProtocol(new BaseProtocol1_16(), Range.atLeast(ProtocolVersion.v1_16.getVersion()));
|
2018-07-03 14:58:00 +02:00
|
|
|
|
2019-08-23 22:14:32 +02:00
|
|
|
registerProtocol(new Protocol1_9To1_8(), ProtocolVersion.v1_9, ProtocolVersion.v1_8);
|
2020-10-16 18:21:45 +02:00
|
|
|
registerProtocol(new Protocol1_9_1To1_9(), Arrays.asList(ProtocolVersion.v1_9_1.getVersion(), ProtocolVersion.v1_9_2.getVersion()), ProtocolVersion.v1_9.getVersion());
|
2019-08-23 22:14:32 +02:00
|
|
|
registerProtocol(new Protocol1_9_3To1_9_1_2(), ProtocolVersion.v1_9_3, ProtocolVersion.v1_9_2);
|
2020-04-24 13:27:29 +02:00
|
|
|
|
2021-02-15 20:02:31 +01:00
|
|
|
registerProtocol(new Protocol1_9To1_9_1(), ProtocolVersion.v1_9, ProtocolVersion.v1_9_1);
|
2020-10-16 18:21:45 +02:00
|
|
|
registerProtocol(new Protocol1_9_1_2To1_9_3_4(), Arrays.asList(ProtocolVersion.v1_9_1.getVersion(), ProtocolVersion.v1_9_2.getVersion()), ProtocolVersion.v1_9_3.getVersion());
|
2019-08-23 22:14:32 +02:00
|
|
|
registerProtocol(new Protocol1_10To1_9_3_4(), ProtocolVersion.v1_10, ProtocolVersion.v1_9_3);
|
2016-05-17 23:48:56 +02:00
|
|
|
|
2019-08-23 22:14:32 +02:00
|
|
|
registerProtocol(new Protocol1_11To1_10(), ProtocolVersion.v1_11, ProtocolVersion.v1_10);
|
|
|
|
registerProtocol(new Protocol1_11_1To1_11(), ProtocolVersion.v1_11_1, ProtocolVersion.v1_11);
|
2017-06-07 17:50:22 +02:00
|
|
|
|
2019-08-23 22:14:32 +02:00
|
|
|
registerProtocol(new Protocol1_12To1_11_1(), ProtocolVersion.v1_12, ProtocolVersion.v1_11_1);
|
|
|
|
registerProtocol(new Protocol1_12_1To1_12(), ProtocolVersion.v1_12_1, ProtocolVersion.v1_12);
|
|
|
|
registerProtocol(new Protocol1_12_2To1_12_1(), ProtocolVersion.v1_12_2, ProtocolVersion.v1_12_1);
|
2017-10-26 15:39:57 +02:00
|
|
|
|
2019-08-23 22:14:32 +02:00
|
|
|
registerProtocol(new Protocol1_13To1_12_2(), ProtocolVersion.v1_13, ProtocolVersion.v1_12_2);
|
|
|
|
registerProtocol(new Protocol1_13_1To1_13(), ProtocolVersion.v1_13_1, ProtocolVersion.v1_13);
|
|
|
|
registerProtocol(new Protocol1_13_2To1_13_1(), ProtocolVersion.v1_13_2, ProtocolVersion.v1_13_1);
|
2019-04-23 17:02:18 +02:00
|
|
|
|
2019-08-23 22:14:32 +02:00
|
|
|
registerProtocol(new Protocol1_14To1_13_2(), ProtocolVersion.v1_14, ProtocolVersion.v1_13_2);
|
|
|
|
registerProtocol(new Protocol1_14_1To1_14(), ProtocolVersion.v1_14_1, ProtocolVersion.v1_14);
|
|
|
|
registerProtocol(new Protocol1_14_2To1_14_1(), ProtocolVersion.v1_14_2, ProtocolVersion.v1_14_1);
|
|
|
|
registerProtocol(new Protocol1_14_3To1_14_2(), ProtocolVersion.v1_14_3, ProtocolVersion.v1_14_2);
|
|
|
|
registerProtocol(new Protocol1_14_4To1_14_3(), ProtocolVersion.v1_14_4, ProtocolVersion.v1_14_3);
|
2019-09-19 10:27:48 +02:00
|
|
|
|
|
|
|
registerProtocol(new Protocol1_15To1_14_4(), ProtocolVersion.v1_15, ProtocolVersion.v1_14_4);
|
2019-12-17 16:28:43 +01:00
|
|
|
registerProtocol(new Protocol1_15_1To1_15(), ProtocolVersion.v1_15_1, ProtocolVersion.v1_15);
|
2020-01-19 19:09:57 +01:00
|
|
|
registerProtocol(new Protocol1_15_2To1_15_1(), ProtocolVersion.v1_15_2, ProtocolVersion.v1_15_1);
|
2020-02-05 20:09:06 +01:00
|
|
|
|
|
|
|
registerProtocol(new Protocol1_16To1_15_2(), ProtocolVersion.v1_16, ProtocolVersion.v1_15_2);
|
2020-06-24 14:10:33 +02:00
|
|
|
registerProtocol(new Protocol1_16_1To1_16(), ProtocolVersion.v1_16_1, ProtocolVersion.v1_16);
|
2020-07-01 17:20:59 +02:00
|
|
|
registerProtocol(new Protocol1_16_2To1_16_1(), ProtocolVersion.v1_16_2, ProtocolVersion.v1_16_1);
|
2020-09-07 15:11:11 +02:00
|
|
|
registerProtocol(new Protocol1_16_3To1_16_2(), ProtocolVersion.v1_16_3, ProtocolVersion.v1_16_2);
|
2020-10-07 12:32:52 +02:00
|
|
|
registerProtocol(new Protocol1_16_4To1_16_3(), ProtocolVersion.v1_16_4, ProtocolVersion.v1_16_3);
|
2020-11-04 21:52:38 +01:00
|
|
|
|
|
|
|
registerProtocol(new Protocol1_17To1_16_4(), ProtocolVersion.v1_17, ProtocolVersion.v1_16_4);
|
2019-08-23 22:14:32 +02:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
|
|
|
public void registerProtocol(Protocol protocol, ProtocolVersion supported, ProtocolVersion output) {
|
2020-10-16 18:21:45 +02:00
|
|
|
registerProtocol(protocol, Collections.singletonList(supported.getVersion()), output.getVersion());
|
2016-03-17 22:24:25 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
|
|
|
public void registerProtocol(Protocol protocol, List<Integer> supported, int output) {
|
2016-03-23 15:00:48 +01:00
|
|
|
// Clear cache as this may make new routes.
|
2020-03-23 01:23:17 +01:00
|
|
|
if (!pathCache.isEmpty()) {
|
2016-03-23 15:00:48 +01:00
|
|
|
pathCache.clear();
|
2020-03-23 01:23:17 +01:00
|
|
|
}
|
2016-03-23 15:00:48 +01:00
|
|
|
|
2020-06-07 10:14:18 +02:00
|
|
|
protocols.put(protocol.getClass(), protocol);
|
|
|
|
|
|
|
|
for (int version : supported) {
|
2020-06-09 08:38:22 +02:00
|
|
|
Int2ObjectMap<Protocol> protocolMap = registryMap.computeIfAbsent(version, s -> new Int2ObjectOpenHashMap<>(2));
|
2020-03-23 01:23:17 +01:00
|
|
|
protocolMap.put(output, protocol);
|
2016-03-17 22:24:25 +01:00
|
|
|
}
|
2016-04-04 00:30:10 +02:00
|
|
|
|
2016-09-25 15:39:37 +02:00
|
|
|
if (Via.getPlatform().isPluginEnabled()) {
|
2016-09-25 21:05:58 +02:00
|
|
|
protocol.register(Via.getManager().getProviders());
|
2016-05-19 19:56:42 +02:00
|
|
|
refreshVersions();
|
2016-04-04 00:30:10 +02:00
|
|
|
} else {
|
|
|
|
registerList.add(protocol);
|
|
|
|
}
|
2020-03-23 01:23:17 +01:00
|
|
|
|
|
|
|
if (protocol.hasMappingDataToLoad()) {
|
|
|
|
if (mappingLoaderExecutor != null) {
|
|
|
|
// Submit mapping data loading
|
2020-03-23 12:22:34 +01:00
|
|
|
addMappingLoaderFuture(protocol.getClass(), protocol::loadMappingData);
|
2020-03-23 01:23:17 +01:00
|
|
|
} else {
|
|
|
|
// Late protocol adding - just do it on the current thread
|
|
|
|
protocol.loadMappingData();
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 22:24:25 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
|
|
|
public void registerBaseProtocol(Protocol baseProtocol, Range<Integer> supportedProtocols) {
|
2021-04-21 11:58:19 +02:00
|
|
|
Preconditions.checkArgument(baseProtocol.isBaseProtocol(), "Protocol is not a base protocol");
|
2018-07-03 14:58:00 +02:00
|
|
|
baseProtocols.add(new Pair<>(supportedProtocols, baseProtocol));
|
|
|
|
if (Via.getPlatform().isPluginEnabled()) {
|
|
|
|
baseProtocol.register(Via.getManager().getProviders());
|
|
|
|
refreshVersions();
|
|
|
|
} else {
|
|
|
|
registerList.add(baseProtocol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
public void refreshVersions() {
|
2016-05-19 19:56:42 +02:00
|
|
|
supportedVersions.clear();
|
|
|
|
|
2021-03-26 12:51:38 +01:00
|
|
|
supportedVersions.add(serverProtocolVersion.lowestSupportedVersion());
|
2016-05-19 19:56:42 +02:00
|
|
|
for (ProtocolVersion versions : ProtocolVersion.getProtocols()) {
|
2021-03-26 12:51:38 +01:00
|
|
|
List<ProtocolPathEntry> paths = getProtocolPath(versions.getVersion(), serverProtocolVersion.lowestSupportedVersion());
|
2016-05-19 19:56:42 +02:00
|
|
|
if (paths == null) continue;
|
2020-10-16 18:21:45 +02:00
|
|
|
supportedVersions.add(versions.getVersion());
|
2021-03-25 22:34:30 +01:00
|
|
|
for (ProtocolPathEntry path : paths) {
|
|
|
|
supportedVersions.add(path.getOutputProtocolVersion());
|
2020-04-03 00:05:44 +02:00
|
|
|
}
|
2016-05-19 19:56:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
2021-04-02 14:15:30 +02:00
|
|
|
public @Nullable List<ProtocolPathEntry> getProtocolPath(int clientVersion, int serverVersion) {
|
2021-03-25 22:34:30 +01:00
|
|
|
ProtocolPathKey protocolKey = new ProtocolPathKeyImpl(clientVersion, serverVersion);
|
2021-03-24 15:21:18 +01:00
|
|
|
// Check cache
|
2021-03-25 22:34:30 +01:00
|
|
|
List<ProtocolPathEntry> protocolList = pathCache.get(protocolKey);
|
2021-03-24 15:21:18 +01:00
|
|
|
if (protocolList != null) {
|
|
|
|
return protocolList;
|
2016-03-17 22:24:25 +01:00
|
|
|
}
|
2021-03-25 22:34:30 +01:00
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
// Generate path
|
2021-03-25 22:34:30 +01:00
|
|
|
List<ProtocolPathEntry> outputPath = getProtocolPath(new ArrayList<>(), clientVersion, serverVersion);
|
2021-03-24 15:21:18 +01:00
|
|
|
// If it found a path, cache it.
|
|
|
|
if (outputPath != null) {
|
|
|
|
pathCache.put(protocolKey, outputPath);
|
2016-04-04 00:30:10 +02:00
|
|
|
}
|
2021-03-24 15:21:18 +01:00
|
|
|
return outputPath;
|
2016-04-04 00:30:10 +02:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:02:46 +01:00
|
|
|
/**
|
2021-03-24 15:21:18 +01:00
|
|
|
* Calculates a path to get from an input protocol to the server's protocol.
|
2016-03-22 19:02:46 +01:00
|
|
|
*
|
2021-03-24 15:21:18 +01:00
|
|
|
* @param current current items in the path
|
|
|
|
* @param clientVersion current input version
|
|
|
|
* @param serverVersion desired output version
|
|
|
|
* @return path that has been generated, null if failed
|
2016-03-22 19:02:46 +01:00
|
|
|
*/
|
2021-04-02 14:15:30 +02:00
|
|
|
private @Nullable List<ProtocolPathEntry> getProtocolPath(List<ProtocolPathEntry> current, int clientVersion, int serverVersion) {
|
2016-05-19 19:56:42 +02:00
|
|
|
if (clientVersion == serverVersion) return null; // We're already there
|
2020-09-08 09:06:54 +02:00
|
|
|
if (current.size() > maxProtocolPathSize) return null; // Fail safe, protocol too complicated.
|
2016-03-17 22:24:25 +01:00
|
|
|
|
|
|
|
// First check if there is any protocols for this
|
2020-06-08 22:23:24 +02:00
|
|
|
Int2ObjectMap<Protocol> inputMap = registryMap.get(clientVersion);
|
2018-09-30 20:48:23 +02:00
|
|
|
if (inputMap == null) {
|
2016-03-17 22:24:25 +01:00
|
|
|
return null; // Not supported
|
|
|
|
}
|
2020-06-08 13:49:26 +02:00
|
|
|
|
2016-03-17 22:24:25 +01:00
|
|
|
// Next check there isn't an obvious path
|
2018-09-30 20:48:23 +02:00
|
|
|
Protocol protocol = inputMap.get(serverVersion);
|
|
|
|
if (protocol != null) {
|
2021-03-25 22:34:30 +01:00
|
|
|
current.add(new ProtocolPathEntryImpl(serverVersion, protocol));
|
2016-03-17 22:24:25 +01:00
|
|
|
return current; // Easy solution
|
|
|
|
}
|
2020-06-08 13:49:26 +02:00
|
|
|
|
2016-03-17 22:24:25 +01:00
|
|
|
// There might be a more advanced solution... So we'll see if any of the others can get us there
|
2021-03-25 22:34:30 +01:00
|
|
|
List<ProtocolPathEntry> shortest = null;
|
2020-06-08 22:23:24 +02:00
|
|
|
for (Int2ObjectMap.Entry<Protocol> entry : inputMap.int2ObjectEntrySet()) {
|
2016-03-17 22:24:25 +01:00
|
|
|
// Ensure it wasn't caught by the other loop
|
2021-03-25 22:34:30 +01:00
|
|
|
if (entry.getIntKey() == serverVersion) continue;
|
2020-06-08 22:23:24 +02:00
|
|
|
|
2021-03-25 22:34:30 +01:00
|
|
|
ProtocolPathEntry pathEntry = new ProtocolPathEntryImpl(entry.getIntKey(), entry.getValue());
|
2020-06-08 22:23:24 +02:00
|
|
|
// Ensure no recursion
|
2021-03-25 22:34:30 +01:00
|
|
|
if (current.contains(pathEntry)) continue;
|
2020-06-08 22:23:24 +02:00
|
|
|
|
|
|
|
// Create a copy
|
2021-03-25 22:34:30 +01:00
|
|
|
List<ProtocolPathEntry> newCurrent = new ArrayList<>(current);
|
|
|
|
newCurrent.add(pathEntry);
|
|
|
|
|
|
|
|
// Calculate the rest of the protocol using the current path entry
|
|
|
|
newCurrent = getProtocolPath(newCurrent, entry.getIntKey(), serverVersion);
|
|
|
|
|
|
|
|
// If it's shorter then choose it
|
|
|
|
if (newCurrent != null
|
|
|
|
&& (shortest == null || shortest.size() > newCurrent.size())) {
|
|
|
|
shortest = newCurrent;
|
2016-03-17 22:24:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:48:56 +02:00
|
|
|
return shortest; // null if none found
|
2016-03-17 22:24:25 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
2021-04-26 22:54:43 +02:00
|
|
|
public @Nullable <T extends Protocol> T getProtocol(Class<T> protocolClass) {
|
|
|
|
return (T) protocols.get(protocolClass);
|
2020-06-07 10:14:18 +02:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
|
|
|
public Protocol getBaseProtocol(int serverVersion) {
|
2018-07-03 16:26:00 +02:00
|
|
|
for (Pair<Range<Integer>, Protocol> rangeProtocol : Lists.reverse(baseProtocols)) {
|
|
|
|
if (rangeProtocol.getKey().contains(serverVersion)) {
|
2018-07-03 14:58:00 +02:00
|
|
|
return rangeProtocol.getValue();
|
2018-07-03 16:26:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 14:58:00 +02:00
|
|
|
throw new IllegalStateException("No Base Protocol for " + serverVersion);
|
|
|
|
}
|
2018-07-03 16:19:32 +02:00
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
|
|
|
public boolean isBaseProtocol(Protocol protocol) {
|
2021-04-21 11:58:19 +02:00
|
|
|
return protocol.isBaseProtocol();
|
2018-07-03 16:19:32 +02:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
2021-03-26 12:51:38 +01:00
|
|
|
public ServerProtocolVersion getServerProtocolVersion() {
|
|
|
|
return serverProtocolVersion;
|
2021-03-24 15:21:18 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 12:51:38 +01:00
|
|
|
public void setServerProtocol(ServerProtocolVersion serverProtocolVersion) {
|
|
|
|
this.serverProtocolVersion = serverProtocolVersion;
|
2021-04-27 13:25:18 +02:00
|
|
|
//noinspection deprecation
|
|
|
|
ProtocolRegistry.SERVER_PROTOCOL = serverProtocolVersion.lowestSupportedVersion();
|
2021-03-24 15:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isWorkingPipe() {
|
|
|
|
for (Int2ObjectMap<Protocol> map : registryMap.values()) {
|
2021-03-26 12:51:38 +01:00
|
|
|
for (int protocolVersion : serverProtocolVersion.supportedVersions()) {
|
|
|
|
if (map.containsKey(protocolVersion)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 15:21:18 +01:00
|
|
|
}
|
|
|
|
return false; // No destination for protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedSet<Integer> getSupportedVersions() {
|
|
|
|
return Collections.unmodifiableSortedSet(new TreeSet<>(supportedVersions));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxProtocolPathSize() {
|
|
|
|
return maxProtocolPathSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setMaxProtocolPathSize(int maxProtocolPathSize) {
|
|
|
|
this.maxProtocolPathSize = maxProtocolPathSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Protocol getBaseProtocol() {
|
|
|
|
return BASE_PROTOCOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void completeMappingDataLoading(Class<? extends Protocol> protocolClass) throws Exception {
|
2020-04-03 00:05:44 +02:00
|
|
|
if (mappingsLoaded) return;
|
2020-03-23 01:23:17 +01:00
|
|
|
|
2020-04-03 00:05:44 +02:00
|
|
|
CompletableFuture<Void> future = getMappingLoaderFuture(protocolClass);
|
2021-03-21 20:25:52 +01:00
|
|
|
if (future != null) {
|
|
|
|
// Wait for completion
|
|
|
|
future.get();
|
|
|
|
}
|
2020-04-24 13:27:29 +02:00
|
|
|
}
|
2020-03-23 01:23:17 +01:00
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
|
|
|
public boolean checkForMappingCompletion() {
|
|
|
|
mappingLoaderLock.readLock().lock();
|
2021-03-21 20:25:52 +01:00
|
|
|
try {
|
2020-04-24 13:48:22 +02:00
|
|
|
if (mappingsLoaded) return false;
|
2020-03-23 01:23:17 +01:00
|
|
|
|
2020-04-24 13:27:29 +02:00
|
|
|
for (CompletableFuture<Void> future : mappingLoaderFutures.values()) {
|
|
|
|
// Return if any future hasn't completed yet
|
|
|
|
if (!future.isDone()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-03 00:05:44 +02:00
|
|
|
}
|
2020-04-24 13:27:29 +02:00
|
|
|
|
|
|
|
shutdownLoaderExecutor();
|
|
|
|
return true;
|
2021-03-21 20:25:52 +01:00
|
|
|
} finally {
|
2021-03-24 15:21:18 +01:00
|
|
|
mappingLoaderLock.readLock().unlock();
|
2020-03-23 01:23:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
|
|
|
public void addMappingLoaderFuture(Class<? extends Protocol> protocolClass, Runnable runnable) {
|
2021-03-21 20:25:52 +01:00
|
|
|
CompletableFuture<Void> future = CompletableFuture.runAsync(runnable, mappingLoaderExecutor).exceptionally(mappingLoaderThrowable(protocolClass));
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
mappingLoaderLock.writeLock().lock();
|
2021-03-21 20:25:52 +01:00
|
|
|
try {
|
2020-04-03 00:05:44 +02:00
|
|
|
mappingLoaderFutures.put(protocolClass, future);
|
2021-03-21 20:25:52 +01:00
|
|
|
} finally {
|
2021-03-24 15:21:18 +01:00
|
|
|
mappingLoaderLock.writeLock().unlock();
|
2020-04-03 00:05:44 +02:00
|
|
|
}
|
2020-03-23 10:40:57 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
|
|
|
public void addMappingLoaderFuture(Class<? extends Protocol> protocolClass, Class<? extends Protocol> dependsOn, Runnable runnable) {
|
2021-03-21 20:25:52 +01:00
|
|
|
CompletableFuture<Void> future = getMappingLoaderFuture(dependsOn)
|
|
|
|
.whenCompleteAsync((v, throwable) -> runnable.run(), mappingLoaderExecutor).exceptionally(mappingLoaderThrowable(protocolClass));
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
mappingLoaderLock.writeLock().lock();
|
2021-03-21 20:25:52 +01:00
|
|
|
try {
|
2020-04-24 13:48:22 +02:00
|
|
|
mappingLoaderFutures.put(protocolClass, future);
|
2021-03-21 20:25:52 +01:00
|
|
|
} finally {
|
2021-03-24 15:21:18 +01:00
|
|
|
mappingLoaderLock.writeLock().unlock();
|
2020-04-24 13:48:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
@Override
|
2021-04-02 14:15:30 +02:00
|
|
|
public @Nullable CompletableFuture<Void> getMappingLoaderFuture(Class<? extends Protocol> protocolClass) {
|
2021-03-24 15:21:18 +01:00
|
|
|
mappingLoaderLock.readLock().lock();
|
2021-03-21 20:25:52 +01:00
|
|
|
try {
|
|
|
|
return mappingsLoaded ? null : mappingLoaderFutures.get(protocolClass);
|
|
|
|
} finally {
|
2021-03-24 15:21:18 +01:00
|
|
|
mappingLoaderLock.readLock().unlock();
|
2020-04-03 00:05:44 +02:00
|
|
|
}
|
2020-03-23 01:23:17 +01:00
|
|
|
}
|
2021-03-21 20:25:52 +01:00
|
|
|
|
2021-04-26 22:54:43 +02:00
|
|
|
@Override
|
|
|
|
public PacketWrapper createPacketWrapper(int packetId, ByteBuf buf, UserConnection connection) {
|
|
|
|
return new PacketWrapperImpl(packetId, buf, connection);
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
/**
|
|
|
|
* Called when the server is enabled, to register any non-registered listeners.
|
|
|
|
*/
|
|
|
|
public void onServerLoaded() {
|
|
|
|
for (Protocol protocol : registerList) {
|
|
|
|
protocol.register(Via.getManager().getProviders());
|
|
|
|
}
|
|
|
|
registerList.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void shutdownLoaderExecutor() {
|
2021-03-21 20:25:52 +01:00
|
|
|
Preconditions.checkArgument(!mappingsLoaded);
|
|
|
|
|
|
|
|
Via.getPlatform().getLogger().info("Finished mapping loading, shutting down loader executor!");
|
|
|
|
mappingsLoaded = true;
|
|
|
|
mappingLoaderExecutor.shutdown();
|
|
|
|
mappingLoaderExecutor = null;
|
|
|
|
mappingLoaderFutures.clear();
|
|
|
|
mappingLoaderFutures = null;
|
|
|
|
if (MappingDataLoader.isCacheJsonMappings()) {
|
|
|
|
MappingDataLoader.getMappingsCache().clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:21:18 +01:00
|
|
|
private Function<Throwable, Void> mappingLoaderThrowable(Class<? extends Protocol> protocolClass) {
|
2021-03-21 20:25:52 +01:00
|
|
|
return throwable -> {
|
|
|
|
Via.getPlatform().getLogger().severe("Error during mapping loading of " + protocolClass.getSimpleName());
|
|
|
|
throwable.printStackTrace();
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
}
|
2016-03-17 22:24:25 +01:00
|
|
|
}
|