9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

添加自定义跳过优化

This commit is contained in:
XiaoMoMi
2025-10-25 22:07:08 +08:00
parent a1fd3650bf
commit 2c6c902fae
7 changed files with 109 additions and 7 deletions

View File

@@ -6,7 +6,9 @@ import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.PacketFlow;
import net.momirealms.craftengine.core.util.VersionHelper;
import java.util.*;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
public class PlayPacketIdHelper {
// 1.20.5-latest

View File

@@ -5,7 +5,6 @@ import com.google.common.collect.Multimap;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.google.gson.*;
import net.kyori.adventure.text.Component;
import net.momirealms.craftengine.core.font.BitmapImage;
import net.momirealms.craftengine.core.font.Font;
import net.momirealms.craftengine.core.item.equipment.ComponentBasedEquipment;
@@ -28,6 +27,7 @@ import net.momirealms.craftengine.core.pack.revision.Revisions;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.config.ConfigParser;
import net.momirealms.craftengine.core.plugin.config.SectionConfigParser;
import net.momirealms.craftengine.core.plugin.config.StringKeyConstructor;
import net.momirealms.craftengine.core.plugin.locale.LangData;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
@@ -60,7 +60,6 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static net.momirealms.craftengine.core.util.MiscUtils.castToMap;
@@ -105,6 +104,7 @@ public abstract class AbstractPackManager implements PackManager {
private Map<Path, CachedAssetFile> cachedAssetFiles = Collections.emptyMap();
protected BiConsumer<Path, Path> zipGenerator;
protected ResourcePackHost resourcePackHost;
private final SkipOptimizationParser parser = new SkipOptimizationParser();
public AbstractPackManager(CraftEngine plugin, Consumer<PackCacheData> cacheEventDispatcher, BiConsumer<Path, Path> generationEventDispatcher) {
this.plugin = plugin;
@@ -242,6 +242,7 @@ public abstract class AbstractPackManager implements PackManager {
@Override
public void unload() {
this.parser.clearCache();
this.loadedPacks.clear();
}
@@ -858,8 +859,10 @@ public abstract class AbstractPackManager implements PackManager {
List<Path> imagesToOptimize = new ArrayList<>();
List<Path> commonJsonToOptimize = new ArrayList<>();
List<Path> modelJsonToOptimize = new ArrayList<>();
Set<String> excludeTexture = Config.optimizeTextureExclude();
Set<String> excludeJson = Config.optimizeJsonExclude();
Set<String> excludeTexture = new HashSet<>(Config.optimizeTextureExclude());
Set<String> excludeJson = new HashSet<>(Config.optimizeJsonExclude());
excludeTexture.addAll(this.parser.excludeTexture());
excludeJson.addAll(this.parser.excludeJson());
Predicate<Path> texturePathPredicate = p -> !excludeTexture.contains(CharacterUtils.replaceBackslashWithSlash(path.relativize(p).toString()));
Predicate<Path> jsonPathPredicate = p -> !excludeJson.contains(CharacterUtils.replaceBackslashWithSlash(path.relativize(p).toString()));
@@ -2755,4 +2758,66 @@ public abstract class AbstractPackManager implements PackManager {
}
}
}
@Override
public ConfigParser parser() {
return this.parser;
}
public static class SkipOptimizationParser extends SectionConfigParser {
private static final String[] SECTION_ID = new String[] {"skip-optimization"};
private final Set<String> excludeTexture = new HashSet<>();
private final Set<String> excludeJson = new HashSet<>();
public SkipOptimizationParser() {
}
public void clearCache() {
this.excludeTexture.clear();
this.excludeJson.clear();
}
public Set<String> excludeTexture() {
return excludeTexture;
}
public Set<String> excludeJson() {
return excludeJson;
}
@Override
protected void parseSection(Pack pack, Path path, Map<String, Object> section) throws LocalizedException {
if (!Config.optimizeResourcePack()) return;
List<String> textures = MiscUtils.getAsStringList(section.get("texture"));
if (!textures.isEmpty()) {
for (String texture : textures) {
if (texture.endsWith(".png")) {
this.excludeTexture.add(texture);
} else {
this.excludeTexture.add(texture + ".png");
}
}
}
List<String> jsons = MiscUtils.getAsStringList(section.get("json"));
if (!jsons.isEmpty()) {
for (String json : jsons) {
if (json.endsWith(".json") || json.endsWith(".mcmeta")) {
this.excludeJson.add(json);
} else {
this.excludeJson.add(json + ".json");
}
}
}
}
@Override
public String[] sectionId() {
return SECTION_ID;
}
@Override
public int loadingSequence() {
return LoadingSequence.SKIP_OPTIMIZATION;
}
}
}

View File

@@ -21,4 +21,5 @@ public final class LoadingSequence {
public static final int EMOJI = 140;
public static final int ADVANCEMENT = 150;
public static final int LANG = 160;
public static final int SKIP_OPTIMIZATION = 170;
}

View File

@@ -12,6 +12,8 @@ import java.util.Collection;
public interface PackManager extends Manageable {
ConfigParser parser();
void loadResources(boolean recipe);
void initCachedAssets();

View File

@@ -298,6 +298,8 @@ public abstract class CraftEngine implements Plugin {
this.packManager.registerConfigSectionParser(this.vanillaLootManager.parser());
// register advancement parser
this.packManager.registerConfigSectionParser(this.advancementManager.parser());
// register skip-optimization parser
this.packManager.registerConfigSectionParser(this.packManager.parser());
}
public void applyDependencies() {

View File

@@ -333,9 +333,15 @@ public class Config {
resource_pack$protection$obfuscation$resource_location$bypass_equipments = config.getStringList("resource-pack.protection.obfuscation.resource-location.bypass-equipments");
resource_pack$optimization$enable = config.getBoolean("resource-pack.optimization.enable", false);
resource_pack$optimization$texture$enable = config.getBoolean("resource-pack.optimization.texture.enable", true);
resource_pack$optimization$texture$exlude = new HashSet<>(config.getStringList("resource-pack.optimization.texture.exclude"));
resource_pack$optimization$texture$exlude = config.getStringList("resource-pack.optimization.texture.exclude").stream().map(p -> {
if (!p.endsWith(".png")) return p + ".png";
return p;
}).collect(Collectors.toSet());
resource_pack$optimization$json$enable = config.getBoolean("resource-pack.optimization.json.enable", true);
resource_pack$optimization$json$exclude = new HashSet<>(config.getStringList("resource-pack.optimization.json.exclude"));
resource_pack$optimization$json$exclude = config.getStringList("resource-pack.optimization.json.exclude").stream().map(p -> {
if (!p.endsWith(".json") && !p.endsWith(".mcmeta")) return p + ".json";
return p;
}).collect(Collectors.toSet());
resource_pack$validation$enable = config.getBoolean("resource-pack.validation.enable", true);
resource_pack$validation$fix_atlas = VersionHelper.PREMIUM && config.getBoolean("resource-pack.validation.fix-atlas", true);
resource_pack$exclude_core_shaders = config.getBoolean("resource-pack.exclude-core-shaders", false);

View File

@@ -29,6 +29,7 @@ import org.apache.commons.imaging.formats.png.PngConstants;
import org.apache.commons.imaging.palette.Palette;
import org.apache.commons.imaging.palette.PaletteFactory;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -64,6 +65,8 @@ public class PngWriter {
public void write(BufferedImage src, OutputStream os, PaletteFactory paletteFactory) throws IOException {
final int width = src.getWidth();
final int height = src.getHeight();
src = convertTo8BitRGB(src);
final boolean hasAlpha = paletteFactory.hasTransparency(src);
boolean isGrayscale = paletteFactory.isGrayscale(src);
@@ -86,6 +89,27 @@ public class PngWriter {
os.close();
}
public static BufferedImage convertTo8BitRGB(BufferedImage sourceImage) {
int type = sourceImage.getType();
if (type == BufferedImage.TYPE_INT_ARGB ||
type == BufferedImage.TYPE_INT_RGB ||
type == BufferedImage.TYPE_BYTE_INDEXED) {
return sourceImage;
}
BufferedImage eightBitImage = new BufferedImage(
sourceImage.getWidth(),
sourceImage.getHeight(),
sourceImage.getColorModel().hasAlpha() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB
);
Graphics2D g2d = eightBitImage.createGraphics();
g2d.drawImage(sourceImage, 0, 0, null);
g2d.dispose();
return eightBitImage;
}
private Pair<PngColorType, byte[]> findBestCompressMethod(BufferedImage src, PaletteFactory paletteFactory, boolean hasAlpha, boolean isGrayscale) throws IOException {
byte[] paletteSize = tryPalette(src, paletteFactory, hasAlpha);
byte[] normalSize = tryNormal(src, hasAlpha, isGrayscale);