From ff6db7ed6b7a5c6f578b632fd0d1bd04f6795733 Mon Sep 17 00:00:00 2001 From: Nils Schmidt Date: Sun, 4 Aug 2024 20:58:38 +0200 Subject: [PATCH] Prepared fix for issue #1039. --- .../nschmidt/ldparteditor/text/DatParser.java | 16 +++++++-- .../ldparteditor/text/PrimitiveReplacer.java | 36 ++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/org/nschmidt/ldparteditor/text/DatParser.java b/src/org/nschmidt/ldparteditor/text/DatParser.java index 6575bc7ed..278fb5268 100644 --- a/src/org/nschmidt/ldparteditor/text/DatParser.java +++ b/src/org/nschmidt/ldparteditor/text/DatParser.java @@ -64,6 +64,7 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of import org.nschmidt.ldparteditor.logger.NLogger; import org.nschmidt.ldparteditor.project.Project; import org.nschmidt.ldparteditor.shell.editor3d.Editor3DWindow; +import org.nschmidt.ldparteditor.workbench.UserSettingState; import org.nschmidt.ldparteditor.workbench.WorkbenchManager; /** @@ -678,16 +679,18 @@ private static List parseReference(String[] dataSegments, int dep String[] prefix ; int readyOnlyAt = 2; + final UserSettingState userSettings = WorkbenchManager.getUserSettingState(); + final boolean isSubstitutingPrimitives = userSettings.isSubstitutingPrimitives(); if (datFile != null && !datFile.isProjectFile() && !View.DUMMY_DATFILE.equals(datFile)) { File dff = new File(datFile.getOldName()).getParentFile(); if (dff != null && dff.exists() && dff.isDirectory()) { - prefix = new String[]{dff.getAbsolutePath(), Project.getProjectPath(), WorkbenchManager.getUserSettingState().getUnofficialFolderPath(), WorkbenchManager.getUserSettingState().getLdrawFolderPath()}; + prefix = new String[]{dff.getAbsolutePath(), Project.getProjectPath(), userSettings.getUnofficialFolderPath(), userSettings.getLdrawFolderPath()}; readyOnlyAt = 3; } else { - prefix = new String[]{Project.getProjectPath(), WorkbenchManager.getUserSettingState().getUnofficialFolderPath(), WorkbenchManager.getUserSettingState().getLdrawFolderPath()}; + prefix = new String[]{Project.getProjectPath(), userSettings.getUnofficialFolderPath(), userSettings.getLdrawFolderPath()}; } } else { - prefix = new String[]{Project.getProjectPath(), WorkbenchManager.getUserSettingState().getUnofficialFolderPath(), WorkbenchManager.getUserSettingState().getLdrawFolderPath()}; + prefix = new String[]{Project.getProjectPath(), userSettings.getUnofficialFolderPath(), userSettings.getLdrawFolderPath()}; } String[] middle = new String[]{"", File.separator + "PARTS", File.separator + "parts", File.separator + "P", File.separator + "p"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ @@ -744,6 +747,13 @@ private static List parseReference(String[] dataSegments, int dep if (isVirtual) break; } + if (!isVirtual && isSubstitutingPrimitives) { + lines = PrimitiveReplacer.substitutePrimitives(shortFilename, userSettings.getPrimitiveSubstitutionQuality()); + if (!lines.isEmpty()) { + isVirtual = true; + } + } + if (isVirtual) { if (result.isEmpty()) { diff --git a/src/org/nschmidt/ldparteditor/text/PrimitiveReplacer.java b/src/org/nschmidt/ldparteditor/text/PrimitiveReplacer.java index 177543022..df933035c 100644 --- a/src/org/nschmidt/ldparteditor/text/PrimitiveReplacer.java +++ b/src/org/nschmidt/ldparteditor/text/PrimitiveReplacer.java @@ -16,14 +16,48 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of package org.nschmidt.ldparteditor.text; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.nschmidt.ldparteditor.logger.NLogger; + public enum PrimitiveReplacer { INSTANCE; - private static final Map GENERATED_PRIMITIVES_CACHE = new HashMap<>(); + private static final Map> GENERATED_PRIMITIVES_CACHE = new HashMap<>(); public static void clearCache() { GENERATED_PRIMITIVES_CACHE.clear(); } + + public static List substitutePrimitives(String shortFilename, int primitiveSubstitutionQuality) { + final PrimitiveKey key = new PrimitiveKey(shortFilename, primitiveSubstitutionQuality); + final List cachedResult = GENERATED_PRIMITIVES_CACHE.get(key); + if (cachedResult != null) { + NLogger.debug(PrimitiveReplacer.class, "Cache hit for : {0}", shortFilename); //$NON-NLS-1$ + return cachedResult; + } + + NLogger.debug(PrimitiveReplacer.class, "Checking potential primitive for substitution (quality {0}) : {1}", primitiveSubstitutionQuality, shortFilename); //$NON-NLS-1$ + + final boolean isHiQuality = shortFilename.startsWith("48\\"); //$NON-NLS-1$ + + if (shortFilename.startsWith("8\\") || primitiveSubstitutionQuality <= 48 && isHiQuality) { //$NON-NLS-1$ + NLogger.debug(PrimitiveReplacer.class, "Skipping primitive (low-quality or reduced hi-quality) : {0}", shortFilename); //$NON-NLS-1$ + return List.of(); + } + + if (isHiQuality) { + shortFilename = shortFilename.substring(3); + } + + return substitutePrimitives(shortFilename, primitiveSubstitutionQuality, isHiQuality); + } + + private static List substitutePrimitives(String shortFilename, int primitiveSubstitutionQuality, boolean isHiQuality) { + // TODO Needs implementation! + return List.of(); + } + + private record PrimitiveKey(String shortFilename, int primitiveSubstitutionQuality) {} }