Skip to content

Commit

Permalink
Allow finding schematic format by InputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Jan 15, 2023
1 parent 98611c2 commit 7f6b79c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
@@ -1,3 +1,4 @@

import org.gradle.api.Project

object Versions {
Expand Down
Expand Up @@ -20,8 +20,6 @@
package com.sk89q.worldedit.extent.clipboard.io;

import com.google.common.collect.ImmutableSet;
import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.jnbt.NBTOutputStream;
import com.sk89q.worldedit.extent.clipboard.io.sponge.SpongeSchematicV1Reader;
import com.sk89q.worldedit.extent.clipboard.io.sponge.SpongeSchematicV2Reader;
import com.sk89q.worldedit.extent.clipboard.io.sponge.SpongeSchematicV2Writer;
Expand All @@ -35,8 +33,6 @@

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -72,9 +68,9 @@ public ClipboardWriter getWriter(OutputStream outputStream) throws IOException {
}

@Override
public boolean isFormat(File file) {
public boolean isFormat(InputStream inputStream) {
LinRootEntry rootEntry;
try (var stream = new DataInputStream(new GZIPInputStream(new FileInputStream(file)))) {
try (var stream = new DataInputStream(new GZIPInputStream(inputStream))) {
rootEntry = LinBinaryIO.readUsing(stream, LinRootEntry::readFrom);
} catch (Exception e) {
return false;
Expand Down Expand Up @@ -105,8 +101,8 @@ public ClipboardWriter getWriter(OutputStream outputStream) throws IOException {
}

@Override
public boolean isFormat(File file) {
return detectOldSpongeSchematic(file, 1);
public boolean isFormat(InputStream inputStream) {
return detectOldSpongeSchematic(inputStream, 1);
}
},
SPONGE_V2_SCHEMATIC("sponge.2") {
Expand All @@ -129,8 +125,8 @@ public ClipboardWriter getWriter(OutputStream outputStream) throws IOException {
}

@Override
public boolean isFormat(File file) {
return detectOldSpongeSchematic(file, 2);
public boolean isFormat(InputStream inputStream) {
return detectOldSpongeSchematic(inputStream, 2);
}
},
SPONGE_V3_SCHEMATIC("sponge.3", "sponge", "schem") {
Expand All @@ -153,9 +149,9 @@ public ClipboardWriter getWriter(OutputStream outputStream) throws IOException {
}

@Override
public boolean isFormat(File file) {
public boolean isFormat(InputStream inputStream) {
LinCompoundTag root;
try (var stream = new DataInputStream(new GZIPInputStream(new FileInputStream(file)))) {
try (var stream = new DataInputStream(new GZIPInputStream(inputStream))) {
root = LinBinaryIO.readUsing(stream, LinRootEntry::readFrom).value();
} catch (Exception e) {
return false;
Expand All @@ -173,9 +169,9 @@ public boolean isFormat(File file) {
},
;

private static boolean detectOldSpongeSchematic(File file, int version) {
private static boolean detectOldSpongeSchematic(InputStream inputStream, int version) {
LinRootEntry rootEntry;
try (var stream = new DataInputStream(new GZIPInputStream(new FileInputStream(file)))) {
try (var stream = new DataInputStream(new GZIPInputStream(inputStream))) {
rootEntry = LinBinaryIO.readUsing(stream, LinRootEntry::readFrom);
} catch (Exception e) {
return false;
Expand Down
Expand Up @@ -20,6 +20,8 @@
package com.sk89q.worldedit.extent.clipboard.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -68,7 +70,23 @@ public interface ClipboardFormat {
* @param file the file
* @return true if the given file is of this format
*/
boolean isFormat(File file);
default boolean isFormat(File file) {
try {
return isFormat(new FileInputStream(file));
} catch (FileNotFoundException e) {
return false;
}
}

/**
* Return whether the given stream is of this format.
*
* @param inputStream The stream
* @return true if the given stream is of this format
*/
default boolean isFormat(InputStream inputStream) {
return false;
}

/**
* Get the file extension this format primarily uses.
Expand Down
Expand Up @@ -25,13 +25,15 @@
import com.sk89q.worldedit.WorldEdit;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -80,7 +82,7 @@ public static ClipboardFormat findByAlias(String alias) {
}

/**
* Detect the format of given a file.
* Detect the format of a given file.
*
* @param file
* the file
Expand All @@ -99,6 +101,25 @@ public static ClipboardFormat findByFile(File file) {
return null;
}

/**
* Detect the format of a given input stream.
*
* @param inputStreamSupplier The input stream supplier
* @return the format, otherwise null if one cannot be detected
*/
@Nullable
public static ClipboardFormat findByInputStream(Supplier<InputStream> inputStreamSupplier) {
checkNotNull(inputStreamSupplier);

for (ClipboardFormat format : registeredFormats) {
if (format.isFormat(inputStreamSupplier.get())) {
return format;
}
}

return null;
}

/**
* A mapping from extensions to formats.
*
Expand Down

0 comments on commit 7f6b79c

Please sign in to comment.