compound tags from scarpet are saved compressed and allowing to read compressed compounds as well.

This commit is contained in:
gnembon committed 2020-12-03 00:44:27 -04:00
1 parent 6a687177ab
commit 5a15c115ac
2 files changed
+39 -8

No files matched your search

@@ -88,12 +88,12 @@ public class Auxiliary {
public static final String MARKER_STRING = "__scarpet_marker"; public static final String MARKER_STRING = "__scarpet_marker";
private static final Map<String, SoundCategory> mixerMap = Arrays.stream(SoundCategory.values()).collect(Collectors.toMap(SoundCategory::getName, k -> k)); private static final Map<String, SoundCategory> mixerMap = Arrays.stream(SoundCategory.values()).collect(Collectors.toMap(SoundCategory::getName, k -> k));
public static String recognizeResource(Value value) public static String recognizeResource(Value value, boolean isFloder)
{ {
String origfile = value.getString(); String origfile = value.getString();
String file = origfile.toLowerCase(Locale.ROOT).replaceAll("[^A-Za-z0-9\\-+_/]", ""); String file = origfile.toLowerCase(Locale.ROOT).replaceAll("[^A-Za-z0-9\\-+_/]", "");
file = Arrays.stream(file.split("/+")).filter(s -> !s.isEmpty()).collect(Collectors.joining("/")); file = Arrays.stream(file.split("/+")).filter(s -> !s.isEmpty()).collect(Collectors.joining("/"));
if (file.isEmpty()) if (file.isEmpty() && !isFloder)
{ {
throw new InternalExpressionException("Cannot use "+origfile+" as resource name - must have some letters and numbers"); throw new InternalExpressionException("Cannot use "+origfile+" as resource name - must have some letters and numbers");
} }
@@ -786,7 +786,7 @@ public class Auxiliary {
}); });
expression.addLazyFunction("read_file", 2, (c, t, lv) -> { expression.addLazyFunction("read_file", 2, (c, t, lv) -> {
String resource = recognizeResource(lv.get(0).evalValue(c)); String resource = recognizeResource(lv.get(0).evalValue(c), false);
String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT); String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT);
boolean shared = origtype.startsWith("shared_"); boolean shared = origtype.startsWith("shared_");
String type = shared ? origtype.substring(7) : origtype; //len(shared_) String type = shared ? origtype.substring(7) : origtype; //len(shared_)
@@ -809,7 +809,7 @@ public class Auxiliary {
}); });
expression.addLazyFunction("delete_file", 2, (c, t, lv) -> { expression.addLazyFunction("delete_file", 2, (c, t, lv) -> {
String resource = recognizeResource(lv.get(0).evalValue(c)); String resource = recognizeResource(lv.get(0).evalValue(c), false);
String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT); String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT);
boolean shared = origtype.startsWith("shared_"); boolean shared = origtype.startsWith("shared_");
String type = shared ? origtype.substring(7) : origtype; //len(shared_) String type = shared ? origtype.substring(7) : origtype; //len(shared_)
@@ -821,7 +821,7 @@ public class Auxiliary {
expression.addLazyFunction("write_file", -1, (c, t, lv) -> { expression.addLazyFunction("write_file", -1, (c, t, lv) -> {
if (lv.size() < 3) throw new InternalExpressionException("'write_file' requires three or more arguments"); if (lv.size() < 3) throw new InternalExpressionException("'write_file' requires three or more arguments");
String resource = recognizeResource(lv.get(0).evalValue(c)); String resource = recognizeResource(lv.get(0).evalValue(c), false);
String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT); String origtype = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT);
boolean shared = origtype.startsWith("shared_"); boolean shared = origtype.startsWith("shared_");
String type = shared ? origtype.substring(7) : origtype; //len(shared_) String type = shared ? origtype.substring(7) : origtype; //len(shared_)
@@ -874,7 +874,7 @@ public class Auxiliary {
if (lv.size()>0) if (lv.size()>0)
{ {
c.host.issueDeprecation("load_app_data(...) with arguments"); c.host.issueDeprecation("load_app_data(...) with arguments");
file = recognizeResource(lv.get(0).evalValue(c)); file = recognizeResource(lv.get(0).evalValue(c), false);
if (lv.size() > 1) if (lv.size() > 1)
{ {
shared = lv.get(1).evalValue(c).getBoolean(); shared = lv.get(1).evalValue(c).getBoolean();
@@ -897,7 +897,7 @@ public class Auxiliary {
if (lv.size()>1) if (lv.size()>1)
{ {
c.host.issueDeprecation("store_app_data(...) with more than one argument"); c.host.issueDeprecation("store_app_data(...) with more than one argument");
file = recognizeResource(lv.get(1).evalValue(c)); file = recognizeResource(lv.get(1).evalValue(c), false);
if (lv.size() > 2) if (lv.size() > 2)
{ {
shared = lv.get(2).evalValue(c).getBoolean(); shared = lv.get(2).evalValue(c).getBoolean();
@@ -1,5 +1,7 @@
package carpet.script.bundled; package carpet.script.bundled;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.PositionTracker; import net.minecraft.nbt.PositionTracker;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
import net.minecraft.nbt.TagReaders; import net.minecraft.nbt.TagReaders;
@@ -60,7 +62,20 @@ public class FileModule extends Module
} }
//copied private method from net.minecraft.nbt.NbtIo.read() //copied private method from net.minecraft.nbt.NbtIo.read()
// to read non-compound tags - these won't be compressed
public static Tag read(File file) public static Tag read(File file)
{
try
{
return NbtIo.readCompressed(file);
}
catch (IOException e)
{
try
{
return NbtIo.read(file);
}
catch (IOException ioException)
{ {
try (DataInputStream dataInput_1 = new DataInputStream(new FileInputStream(file))) try (DataInputStream dataInput_1 = new DataInputStream(new FileInputStream(file)))
{ {
@@ -80,6 +95,8 @@ public class FileModule extends Module
} }
return null; return null;
} }
}
}
//copied private method from net.minecraft.nbt.NbtIo.write() and client method safe_write //copied private method from net.minecraft.nbt.NbtIo.write() and client method safe_write
public static boolean write(Tag tag_1, File file) public static boolean write(Tag tag_1, File file)
@@ -87,7 +104,20 @@ public class FileModule extends Module
File file_2 = new File(file.getAbsolutePath() + "_tmp"); File file_2 = new File(file.getAbsolutePath() + "_tmp");
if (file_2.exists()) file_2.delete(); if (file_2.exists()) file_2.delete();
try(DataOutputStream dataOutputStream_1 = new DataOutputStream(new FileOutputStream(file_2))) if (tag_1 instanceof CompoundTag)
{
try
{
NbtIo.writeCompressed((CompoundTag) tag_1, file_2);
}
catch (IOException e)
{
return false;
}
}
else
{
try (DataOutputStream dataOutputStream_1 = new DataOutputStream(new FileOutputStream(file_2)))
{ {
dataOutputStream_1.writeByte(tag_1.getType()); dataOutputStream_1.writeByte(tag_1.getType());
if (tag_1.getType() != 0) if (tag_1.getType() != 0)
@@ -100,6 +130,7 @@ public class FileModule extends Module
{ {
return false; return false;
} }
}
if (file.exists()) file.delete(); if (file.exists()) file.delete();
if (!file.exists()) file_2.renameTo(file); if (!file.exists()) file_2.renameTo(file);
return true; return true;