Skip to content

Commit

Permalink
Prepared fix for issue #1039.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsschmidt1337 committed Aug 4, 2024
1 parent fdcb0fe commit ff6db7e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/org/nschmidt/ldparteditor/text/DatParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -678,16 +679,18 @@ private static List<ParsingResult> 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$
Expand Down Expand Up @@ -744,6 +747,13 @@ private static List<ParsingResult> 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()) {
Expand Down
36 changes: 35 additions & 1 deletion src/org/nschmidt/ldparteditor/text/PrimitiveReplacer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> GENERATED_PRIMITIVES_CACHE = new HashMap<>();
private static final Map<PrimitiveKey, List<String>> GENERATED_PRIMITIVES_CACHE = new HashMap<>();

public static void clearCache() {
GENERATED_PRIMITIVES_CACHE.clear();
}

public static List<String> substitutePrimitives(String shortFilename, int primitiveSubstitutionQuality) {
final PrimitiveKey key = new PrimitiveKey(shortFilename, primitiveSubstitutionQuality);
final List<String> 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<String> substitutePrimitives(String shortFilename, int primitiveSubstitutionQuality, boolean isHiQuality) {
// TODO Needs implementation!
return List.of();
}

private record PrimitiveKey(String shortFilename, int primitiveSubstitutionQuality) {}
}

0 comments on commit ff6db7e

Please sign in to comment.