Skip to content

Commit

Permalink
Prepared fix for issue #980.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsschmidt1337 committed Sep 6, 2023
1 parent d11826b commit b52b752
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
import org.nschmidt.ldparteditor.helper.composite3d.GuiStatusManager;
import org.nschmidt.ldparteditor.helper.composite3d.SelectorSettings;
import org.nschmidt.ldparteditor.helper.composite3d.ViewIdleManager;
import org.nschmidt.ldparteditor.helper.compositetext.DataMetacommandExporter;
import org.nschmidt.ldparteditor.helper.compositetext.Inliner;
import org.nschmidt.ldparteditor.helper.compositetext.Inspector;
import org.nschmidt.ldparteditor.helper.compositetext.Issue2ClipboardCopier;
Expand Down Expand Up @@ -1833,6 +1834,28 @@ public void mouseDown(MouseEvent e) {
widgetUtil(mntmCopyPtr[0]).addSelectionListener(e -> tabState.folder[0].copy());
widgetUtil(mntmCutPtr[0]).addSelectionListener(e -> tabState.folder[0].cut());
widgetUtil(mntmPastePtr[0]).addSelectionListener(e -> tabState.folder[0].paste());

widgetUtil(mntmImportPngDataPtr[0]).addSelectionListener(e -> {
if (!tabState.getFileNameObj().getVertexManager().isUpdated()){
return;
}

final DatFile df = tabState.getFileNameObj();
final VertexManager vm = df.getVertexManager();
vm.addSnapshot();
// TODO Needs implementation!
});
widgetUtil(mntmExportPngDataPtr[0]).addSelectionListener(e -> {
final DatFile df = tabState.getFileNameObj();
final StyledText st = getTextComposite();
int s1 = st.getSelectionRange().x;
int s2 = s1 + st.getSelectionRange().y;
int fromLine = s1 > -1 ? st.getLineAtOffset(s1) : s1 * -1;
int toLine = s2 > -1 ? st.getLineAtOffset(s2) : s2 * -1;
fromLine++;
toLine++;
DataMetacommandExporter.export(fromLine, toLine, df);
});

widgetUtil(mntmDrawSelectionPtr[0]).addSelectionListener(e -> {
if (!tabState.getFileNameObj().getVertexManager().isUpdated()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class CompositeTabDesign extends CTabItem {
final MenuItem[] mntmDeletePtr = new MenuItem[1];
final MenuItem[] mntmPastePtr = new MenuItem[1];

final MenuItem[] mntmImportPngDataPtr = new MenuItem[1];
final MenuItem[] mntmExportPngDataPtr = new MenuItem[1];

final MenuItem[] mntmQuickFixPtr = new MenuItem[1];
final MenuItem[] mntmQuickFixSamePtr = new MenuItem[1];
final NButton[] btnQuickFixPtr = new NButton[1];
Expand Down Expand Up @@ -333,6 +336,18 @@ private final void createContents(CTabFolder tabFolder) {
mntmDelete.setText(I18n.COPYNPASTE_DELETE);
mntmDelete.setImage(ResourceManager.getImage("icon16_delete.png")); //$NON-NLS-1$
mntmDeletePtr[0] = mntmDelete;

new MenuItem(menu[0], SWT.SEPARATOR);

MenuItem mntmImportPngData = new MenuItem(menu[0], I18n.rightToLeftStyle());
mntmImportPngData.setText(I18n.EDITORTEXT_DATA_IMPORT);
mntmImportPngData.setImage(ResourceManager.getImage("icon16_bgpic.png")); //$NON-NLS-1$
mntmImportPngDataPtr[0] = mntmImportPngData;

MenuItem mntmExportPngData = new MenuItem(menu[0], I18n.rightToLeftStyle());
mntmExportPngData.setText(I18n.EDITORTEXT_DATA_EXPORT);
mntmExportPngData.setImage(ResourceManager.getImage("icon16_bgpic.png")); //$NON-NLS-1$
mntmExportPngDataPtr[0] = mntmExportPngData;
}
}
}
54 changes: 54 additions & 0 deletions src/org/nschmidt/ldparteditor/data/BinaryDataRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* MIT - License
Copyright (c) 2012 - this year, Nils Schmidt
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. */
package org.nschmidt.ldparteditor.data;

import java.util.HashMap;
import java.util.Map;

import org.nschmidt.ldparteditor.logger.NLogger;

class BinaryDataRegistry {

private final Map<String, GDataBinary> fileNameToMetaTagMap = new HashMap<>();
private final Map<String, byte[]> fileNameToBinaryMap = new HashMap<>();

public boolean hasFile(String filename) {
return fileNameToMetaTagMap.containsKey(filename)
|| fileNameToBinaryMap.containsKey(filename);
}

public byte[] getFileBytes(String filename) {
if (fileNameToBinaryMap.containsKey(filename)) {
return fileNameToBinaryMap.getOrDefault(filename, new byte[0]);
}

final GDataBinary dataMetaTag = fileNameToMetaTagMap.get(filename);
final byte[] binary = dataMetaTag.loadBinary();
fileNameToBinaryMap.put(filename, binary);
fileNameToMetaTagMap.remove(filename);

return binary;
}

public void addData(GDataBinary dataMetaTag) {
NLogger.debug(BinaryDataRegistry.class, "Register binary !DATA: {0}", dataMetaTag); //$NON-NLS-1$
// "0 !DATA " has a length of 8
final String filename = dataMetaTag.toString().substring(8);
NLogger.debug(BinaryDataRegistry.class, "The filename is: {0}", filename); //$NON-NLS-1$
fileNameToMetaTagMap.put(filename, dataMetaTag);
fileNameToBinaryMap.remove(filename);
}
}
5 changes: 5 additions & 0 deletions src/org/nschmidt/ldparteditor/data/DatFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public final class DatFile {
private HistoryManager history = new HistoryManager(this);
private DuplicateManager duplicate = new DuplicateManager(this);
private DatHeaderManager datHeader = new DatHeaderManager(this);
private final BinaryDataRegistry binaryData = new BinaryDataRegistry();

public static DatFile createDatFileForReview(String path) {
return new DatFile(path, true);
Expand Down Expand Up @@ -2050,4 +2051,8 @@ public void setOptimizingCSG(boolean optimizingCSG) {
public boolean isFromPartReview() {
return fromPartReview;
}

public BinaryDataRegistry getBinaryData() {
return binaryData;
}
}
168 changes: 168 additions & 0 deletions src/org/nschmidt/ldparteditor/data/GDataBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/* MIT - License
Copyright (c) 2012 - this year, Nils Schmidt
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. */
package org.nschmidt.ldparteditor.data;

import java.util.Base64;
import java.util.Map;
import java.util.regex.Pattern;

import org.nschmidt.ldparteditor.composite.Composite3D;
import org.nschmidt.ldparteditor.helper.math.ThreadsafeHashMap;
import org.nschmidt.ldparteditor.helper.math.ThreadsafeSortedMap;
import org.nschmidt.ldparteditor.logger.NLogger;

/**
* Holds binary base64 encoded data
*/
public final class GDataBinary extends GData {

private final DatFile df;

public GDataBinary(String text, DatFile df, GData1 parent) {
super(parent);
this.df = df;
this.text = text;
this.df.getBinaryData().addData(this);
}

public byte[] loadBinary() {
final Pattern whitespace = Pattern.compile("\\s+"); //$NON-NLS-1$
final StringBuilder base64Sb = new StringBuilder();

GData gd = this.next;
while (gd != null) {
final String line = whitespace.matcher(gd.toString()).replaceAll(" ").trim(); //$NON-NLS-1$
if (line.startsWith("0 !: ")) { //$NON-NLS-1$
final String encodedSubstring = line.substring(5);
base64Sb.append(encodedSubstring);
} else if (line.length() > 0) {
break;
}

gd = gd.next;
}

final String encodedString = base64Sb.toString();

try {
return Base64.getDecoder().decode(encodedString);
} catch (IllegalArgumentException iae) {
NLogger.debug(GDataBinary.class, iae);
}

return new byte[0];
}

@Override
public void drawGL20(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20RandomColours(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20BFC(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20BFCuncertified(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20BFCbackOnly(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20BFCcolour(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20WhileAddCondlines(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20CoplanarityHeatmap(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20Wireframe(Composite3D c3d) {
// Implementation is not required.
}

@Override
public void drawGL20BFCtextured(Composite3D c3d) {
// Implementation is not required.
}

@Override
public int type() {
return 11;
}

@Override
String getNiceString() {
return text;
}

@Override
public String inlinedString(BFC bfc, GColour colour) {
return text;
}

@Override
public String transformAndColourReplace(String colour, Matrix matrix) {
return text;
}

@Override
public void getBFCorientationMap(Map<GData,BFC> map) {
// Implementation is not required.
}

@Override
public void getBFCorientationMapNOCERTIFY(Map<GData, BFC> map) {
// Implementation is not required.
}

@Override
public void getBFCorientationMapNOCLIP(Map<GData, BFC> map) {
// Implementation is not required.
}

@Override
public void getVertexNormalMap(GDataState state, ThreadsafeSortedMap<Vertex, float[]> vertexLinkedToNormalCACHE, ThreadsafeHashMap<GData, float[]> dataLinkedToNormalCACHE, VM00Base vm) {
// Implementation is not required.
}

@Override
public void getVertexNormalMapNOCERTIFY(GDataState state, ThreadsafeSortedMap<Vertex, float[]> vertexLinkedToNormalCACHE, ThreadsafeHashMap<GData, float[]> dataLinkedToNormalCACHE, VM00Base vm) {
// Implementation is not required.
}

@Override
public void getVertexNormalMapNOCLIP(GDataState state, ThreadsafeSortedMap<Vertex, float[]> vertexLinkedToNormalCACHE, ThreadsafeHashMap<GData, float[]> dataLinkedToNormalCACHE, VM00Base vm) {
// Implementation is not required.
}
}
6 changes: 5 additions & 1 deletion src/org/nschmidt/ldparteditor/data/GTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
package org.nschmidt.ldparteditor.data;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -763,7 +764,10 @@ private int loadPNGTexture(String filename, int textureUnit, DatFile datFile) {
}

if (in == null) {
if (fileExists) {
if (datFile.getBinaryData().hasFile(filename)) {
final byte[] source = datFile.getBinaryData().getFileBytes(filename);
in = new ByteArrayInputStream(source);
} else if (fileExists) {
// Open the PNG file as an FileInputStream
filename = fileToOpen.getAbsolutePath();
in = new FileInputStream(filename);
Expand Down
1 change: 1 addition & 0 deletions src/org/nschmidt/ldparteditor/enumtype/DatKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum DatKeyword {
keywords.add("!CATEGORY"); //$NON-NLS-1$
keywords.add("!CMDLINE"); //$NON-NLS-1$
keywords.add("!COLOUR"); //$NON-NLS-1$
keywords.add("!DATA"); //$NON-NLS-1$
keywords.add("!HELP"); //$NON-NLS-1$
keywords.add("!HISTORY"); //$NON-NLS-1$
keywords.add("!KEYWORDS"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.nschmidt.ldparteditor.helper.compositetext;

/* MIT - License
Copyright (c) 2012 - this year, Nils Schmidt
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. */

import org.nschmidt.ldparteditor.data.DatFile;
import org.nschmidt.ldparteditor.data.GData;
import org.nschmidt.ldparteditor.data.GDataBinary;
import org.nschmidt.ldparteditor.helper.math.HashBiMap;

/**
* Exports !DATA meta-commands to a file (PNG only)
*/
public enum DataMetacommandExporter {
INSTANCE;

/**
* Exports selected !DATA meta-commands to a file (PNG only)
*
* @param lineStart
* start line number to export
* @param lineEnd
* end line number to export
* @param datFile
*/
public static void export(int lineStart, int lineEnd, DatFile datFile) {
HashBiMap<Integer, GData> dpl = datFile.getDrawPerLineNoClone();
lineEnd++;
for (int line = lineStart; line < lineEnd; line++) {
final GData data = dpl.getValue(line);
if (data instanceof GDataBinary gd) {
// TODO Needs implementation!
System.out.println(data);
}
}
}
}
4 changes: 4 additions & 0 deletions src/org/nschmidt/ldparteditor/i18n/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,10 @@ private static void adjust() { // Calculate line offset
public static final String EDITORTEXT_COMMENT = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_COMPILE = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_COMPILE_CSG = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_DATA_ERROR = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_DATA_EXPORT = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_DATA_IMPORT = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_DATA_NO_SELECTION = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_DRAW_SELECTION = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_DRAW_UNTIL_SELECTION = EDITORTEXT.getString(getProperty());
public static final String EDITORTEXT_DUPLICATE = EDITORTEXT.getString(getProperty());
Expand Down
Loading

0 comments on commit b52b752

Please sign in to comment.