- Don't require configuring the packId, just the link instead

- Deprecate GeyserLoadResourcePacksEvent in favor of GeyserDefineResourcePacksEvent
- Load CDNentries properly
This commit is contained in:
onebeastchris 2023-10-10 21:45:42 +02:00
parent 02d6473dc5
commit dbfc153b04
10 changed files with 239 additions and 36 deletions

View file

@ -75,8 +75,8 @@ public abstract class SessionLoadResourcePacksEvent extends ConnectionEvent {
/**
* Unregisters a {@link ResourcePack} or {@link ResourcePackCDNEntry} from being sent to the client.
*
* @param uuid the UUID of the resource pack
* @return true whether the resource pack was removed from the list of resource packs.
* @param uuid the UUID of the resource pack/CDN entry to remove.
* @return true whether the resource pack/CDN entry was removed successfully.
*/
public abstract boolean unregister(@NonNull UUID uuid);
}

View file

@ -0,0 +1,79 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* 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:
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event.lifecycle;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.event.Event;
import org.geysermc.geyser.api.pack.ResourcePack;
import org.geysermc.geyser.api.pack.ResourcePackCDNEntry;
import java.util.List;
import java.util.UUID;
/**
* Called when {@link ResourcePack}'s and {@link ResourcePackCDNEntry}'s are loaded within Geyser.
*
*/
public abstract class GeyserDefineResourcePacksEvent implements Event {
/**
* Gets an unmodifiable list of {@link ResourcePack}s that will be sent to clients.
*
* @return an unmodifiable list of resource packs that will be sent to clients.
*/
public abstract @NonNull List<ResourcePack> resourcePacks();
/**
* Gets an unmodifiable list of {@link ResourcePackCDNEntry}s that will be sent to clients.
*
* @return an unmodifiable list of resource pack CDN entries that will be sent to clients.
*/
public abstract @NonNull List<ResourcePackCDNEntry> cdnEntries();
/**
* Registers a {@link ResourcePack} to be sent to clients.
*
* @param resourcePack a resource pack that will be sent to clients.
* @return true if the resource pack was added successfully,
* or false if already present
*/
public abstract boolean register(@NonNull ResourcePack resourcePack);
/**
* Registers a {@link ResourcePackCDNEntry} to be sent to clients.
*
* @param entry a resource pack CDN entry that will be sent to clients.
*/
public abstract boolean register(@NonNull ResourcePackCDNEntry entry);
/**
* Unregisters a {@link ResourcePack} or {@link ResourcePackCDNEntry} from being sent to clients.
*
* @param uuid the UUID of the resource pack/CDN entry to remove.
* @return true whether the resource pack/CDN entry was removed successfully.
*/
public abstract boolean unregister(@NonNull UUID uuid);
}

View file

@ -33,8 +33,11 @@ import java.util.List;
/**
* Called when resource packs are loaded within Geyser.
* @deprecated Use {@link GeyserDefineResourcePacksEvent} instead.
*
* @param resourcePacks a mutable list of the currently listed resource packs
*/
public record GeyserLoadResourcePacksEvent(@NonNull List<Path> resourcePacks) implements Event {
@Deprecated(forRemoval = true)
public record GeyserLoadResourcePacksEvent(@Deprecated @NonNull List<Path> resourcePacks) implements Event {
}

View file

@ -30,7 +30,6 @@ import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.api.network.AuthType;
import org.geysermc.geyser.api.network.BedrockListener;
import org.geysermc.geyser.api.network.RemoteServer;
import org.geysermc.geyser.api.pack.ResourcePackCDNEntry;
import org.geysermc.geyser.network.CIDRMatcher;
import org.geysermc.geyser.network.GameProtocol;
import org.geysermc.geyser.text.GeyserLocale;
@ -97,7 +96,7 @@ public interface GeyserConfiguration {
boolean isForceResourcePacks();
List<ResourcePackCDNEntry> getCDNResourcePacks();
List<String> getCdnResourcePacks();
boolean isXboxAchievementsEnabled();

View file

@ -36,14 +36,16 @@ import lombok.Getter;
import lombok.Setter;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.network.AuthType;
import org.geysermc.geyser.api.pack.ResourcePackCDNEntry;
import org.geysermc.geyser.network.CIDRMatcher;
import org.geysermc.geyser.text.AsteriskSerializer;
import org.geysermc.geyser.text.GeyserLocale;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Getter
@ -136,7 +138,7 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration
private boolean forceResourcePacks = true;
@JsonProperty("cdn-resource-packs")
private Map<UUID, String> cdnResourcePacks = new HashMap<>();
private List<String> cdnResourcePacks = new ArrayList<>();
@JsonProperty("xbox-achievements-enabled")
private boolean xboxAchievementsEnabled = false;
@ -345,13 +347,4 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration
return AuthType.getByName(p.getValueAsString());
}
}
@Override
public List<ResourcePackCDNEntry> getCDNResourcePacks() {
List<ResourcePackCDNEntry> entries = new ArrayList<>();
for (Map.Entry<UUID, String> entry : cdnResourcePacks.entrySet()) {
entries.add(new ResourcePackCDNEntry(entry.getValue(), entry.getKey()));
}
return entries;
}
}

View file

@ -0,0 +1,93 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* 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:
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.event.type;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.event.lifecycle.GeyserDefineResourcePacksEvent;
import org.geysermc.geyser.api.pack.ResourcePack;
import org.geysermc.geyser.api.pack.ResourcePackCDNEntry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class GeyserDefineResourcePacksEventImpl extends GeyserDefineResourcePacksEvent {
private final Map<String, ResourcePack> packs;
private final Map<String, ResourcePackCDNEntry> cdnEntries;
public GeyserDefineResourcePacksEventImpl(Map<String, ResourcePack> packMap, List<ResourcePackCDNEntry> cdnEntries) {
this.packs = packMap;
this.cdnEntries = new HashMap<>();
cdnEntries.forEach(entry -> this.cdnEntries.put(entry.uuid().toString(), entry));
}
public @NonNull Map<String, ResourcePack> getPacks() {
return packs;
}
@Override
public @NonNull List<ResourcePack> resourcePacks() {
return List.copyOf(packs.values());
}
@Override
public @NonNull List<ResourcePackCDNEntry> cdnEntries() {
return List.copyOf(cdnEntries.values());
}
@Override
public boolean register(@NonNull ResourcePack resourcePack) {
String packID = resourcePack.manifest().header().uuid().toString();
if (packs.containsValue(resourcePack) || packs.containsKey(packID) || cdnEntries.containsKey(packID)) {
return false;
}
packs.put(resourcePack.manifest().header().uuid().toString(), resourcePack);
return true;
}
@Override
public boolean register(@NonNull ResourcePackCDNEntry entry) {
String packID = entry.uuid().toString();
if (packs.containsKey(packID) || cdnEntries.containsValue(entry) || cdnEntries.containsKey(packID)) {
return false;
}
cdnEntries.put(packID, entry);
return true;
}
@Override
public boolean unregister(@NonNull UUID uuid) {
if (packs.containsKey(uuid.toString())) {
return packs.remove(uuid.toString()) != null;
} else if (cdnEntries.containsKey(uuid.toString())) {
return cdnEntries.remove(uuid.toString()) != null;
} else {
return false;
}
}
}

View file

@ -31,6 +31,7 @@ import org.geysermc.geyser.api.pack.ResourcePack;
import org.geysermc.geyser.api.pack.ResourcePackCDNEntry;
import org.geysermc.geyser.session.GeyserSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -38,13 +39,13 @@ import java.util.UUID;
public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksEvent {
private final Map<String, ResourcePack> packs;
private final List<ResourcePackCDNEntry> cdnEntries;
private final Map<String, ResourcePackCDNEntry> cdnEntries;
public SessionLoadResourcePacksEventImpl(GeyserSession session, Map<String, ResourcePack> packMap, List<ResourcePackCDNEntry> cdnEntries) {
super(session);
this.packs = packMap;
this.cdnEntries = cdnEntries;
this.cdnEntries = new HashMap<>();
cdnEntries.forEach(entry -> this.cdnEntries.put(entry.uuid().toString(), entry));
}
public @NonNull Map<String, ResourcePack> getPacks() {
@ -58,14 +59,13 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
@Override
public @NonNull List<ResourcePackCDNEntry> cdnEntries() {
return List.copyOf(cdnEntries);
return List.copyOf(cdnEntries.values());
}
@Override
public boolean register(@NonNull ResourcePack resourcePack) {
String packID = resourcePack.manifest().header().uuid().toString();
if (packs.containsValue(resourcePack) || packs.containsKey(packID)
|| !cdnEntries.isEmpty() && cdnEntries.stream().anyMatch(entry -> entry.uuid().toString().equals(packID))) {
if (packs.containsValue(resourcePack) || packs.containsKey(packID) || cdnEntries.containsKey(packID)) {
return false;
}
packs.put(resourcePack.manifest().header().uuid().toString(), resourcePack);
@ -74,12 +74,11 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
@Override
public boolean register(@NonNull ResourcePackCDNEntry entry) {
UUID packID = entry.uuid();
if (packs.containsKey(packID.toString()) || cdnEntries.contains(entry)
|| !cdnEntries.isEmpty() && cdnEntries.stream().anyMatch(cdnEntry -> cdnEntry.uuid().equals(packID))) {
String packID = entry.uuid().toString();
if (packs.containsKey(packID) || cdnEntries.containsKey(packID) || cdnEntries.containsValue(entry)) {
return false;
}
cdnEntries.add(entry);
cdnEntries.put(packID, entry);
return true;
}
@ -87,8 +86,8 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
public boolean unregister(@NonNull UUID uuid) {
if (packs.containsKey(uuid.toString())) {
return packs.remove(uuid.toString()) != null;
} else if (!cdnEntries.isEmpty() && cdnEntries.stream().anyMatch(entry -> entry.uuid().equals(uuid))) {
return cdnEntries.removeIf(entry -> entry.uuid().equals(uuid));
} else if (cdnEntries.containsKey(uuid.toString())) {
return cdnEntries.remove(uuid.toString()) != null;
} else {
return false;
}

View file

@ -57,6 +57,7 @@ import org.geysermc.geyser.event.type.SessionLoadResourcePacksEventImpl;
import org.geysermc.geyser.pack.GeyserResourcePack;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.registry.loader.ResourcePackLoader;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.PendingMicrosoftAuthentication;
import org.geysermc.geyser.text.GeyserLocale;
@ -179,8 +180,7 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
geyser.getSessionManager().addPendingSession(session);
GeyserImpl.getInstance().getLogger().error(geyser.getConfig().getCDNResourcePacks().toString());
this.resourcePackLoadEvent = new SessionLoadResourcePacksEventImpl(session, new HashMap<>(Registries.RESOURCE_PACKS.get()), geyser.getConfig().getCDNResourcePacks());
this.resourcePackLoadEvent = new SessionLoadResourcePacksEventImpl(session, new HashMap<>(Registries.RESOURCE_PACKS.get()), ResourcePackLoader.RESOURCE_PACK_CDN_ENTRY_LIST);
this.geyser.eventBus().fire(this.resourcePackLoadEvent);
ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket();

View file

@ -28,12 +28,15 @@ package org.geysermc.geyser.registry.loader;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.event.lifecycle.GeyserLoadResourcePacksEvent;
import org.geysermc.geyser.api.pack.ResourcePack;
import org.geysermc.geyser.api.pack.ResourcePackCDNEntry;
import org.geysermc.geyser.event.type.GeyserDefineResourcePacksEventImpl;
import org.geysermc.geyser.pack.GeyserResourcePack;
import org.geysermc.geyser.pack.GeyserResourcePackManifest;
import org.geysermc.geyser.pack.SkullResourcePackManager;
import org.geysermc.geyser.pack.path.GeyserPathPackCodec;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.util.FileUtils;
import org.geysermc.geyser.util.WebUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@ -45,6 +48,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -58,8 +62,12 @@ public class ResourcePackLoader implements RegistryLoader<Path, Map<String, Reso
static final PathMatcher PACK_MATCHER = FileSystems.getDefault().getPathMatcher("glob:**.{zip,mcpack}");
private static final Path CACHED_CDN_PACKS_DIRECTORY = GeyserImpl.getInstance().getBootstrap().getConfigFolder().resolve("cache").resolve("cdn-packs");
private static final boolean SHOW_RESOURCE_PACK_LENGTH_WARNING = Boolean.parseBoolean(System.getProperty("Geyser.ShowResourcePackLengthWarning", "true"));
public static List<ResourcePackCDNEntry> RESOURCE_PACK_CDN_ENTRY_LIST = new ArrayList<>();
/**
* Loop through the packs directory and locate valid resource pack files
*/
@ -94,6 +102,33 @@ public class ResourcePackLoader implements RegistryLoader<Path, Map<String, Reso
resourcePacks.add(skullResourcePack);
}
// Download CDN packs to get the pack uuid's
if (!Files.exists(CACHED_CDN_PACKS_DIRECTORY)) {
try {
Files.createDirectories(CACHED_CDN_PACKS_DIRECTORY);
} catch (IOException e) {
GeyserImpl.getInstance().getLogger().error("Could not create cached packs directory", e);
}
}
List<String> cdnPacks = GeyserImpl.getInstance().getConfig().getCdnResourcePacks();
for (String url: cdnPacks) {
int packHash = url.hashCode();
Path cachedPath = CACHED_CDN_PACKS_DIRECTORY.resolve(packHash + ".zip");
WebUtils.downloadFile(url, cachedPath.toString());
ResourcePack cdnpack = readPack(cachedPath);
UUID uuid = cdnpack.manifest().header().uuid();
RESOURCE_PACK_CDN_ENTRY_LIST.add(new ResourcePackCDNEntry(url, uuid));
try {
Files.delete(cachedPath);
} catch (IOException e) {
GeyserImpl.getInstance().getLogger().error("Could not delete cached pack", e);
}
}
GeyserLoadResourcePacksEvent event = new GeyserLoadResourcePacksEvent(resourcePacks);
GeyserImpl.getInstance().eventBus().fire(event);
@ -105,6 +140,11 @@ public class ResourcePackLoader implements RegistryLoader<Path, Map<String, Reso
e.printStackTrace();
}
}
GeyserDefineResourcePacksEventImpl defineEvent = new GeyserDefineResourcePacksEventImpl(packMap, RESOURCE_PACK_CDN_ENTRY_LIST);
packMap = defineEvent.getPacks();
RESOURCE_PACK_CDN_ENTRY_LIST = defineEvent.cdnEntries();
return packMap;
}

View file

@ -176,13 +176,10 @@ above-bedrock-nether-building: false
force-resource-packs: true
# A list of links to send to the client to download resource packs from.
# These must be direct links, and you need to include the resource pack uuid
# You can find the uuid in the manifest.json file inside the resource pack zip.
# These must be direct links to the resource pack, not a link to a page containing the resource pack.
cdn-resource-packs:
# Example: GeyserOptionalPack
{
e5f5c938-a701-11eb-b2a3-047d7bb283ba : "https://ci.opencollab.dev/job/GeyserMC/job/GeyserOptionalPack/job/master/lastSuccessfulBuild/artifact/GeyserOptionalPack.mcpack"
}
- "https://ci.opencollab.dev/job/GeyserMC/job/GeyserOptionalPack/job/master/lastSuccessfulBuild/artifact/GeyserOptionalPack.mcpack"
# Allows Xbox achievements to be unlocked.
# THIS DISABLES ALL COMMANDS FROM SUCCESSFULLY RUNNING FOR BEDROCK IN-GAME, as otherwise Bedrock thinks you are cheating.