Remove ZSTD Compression

This commit is contained in:
KAMKEEL committed 2022-04-20 17:49:20 -04:00
1 parent a3414d8d75
commit d6cd41946c
9 files changed
+78 -462

No files matched your search

+16 -48
View File
@@ -29,27 +29,12 @@ minecraft {
sourceCompatibility = 1.8
targetCompatibility = 1.8
configurations {
shade
compile.extendsFrom shade
}
// List of all supported OS for zstd compression (This is include only for anyone which is not using the default compression)
def supportedOS = [
"linux_amd64",
"linux_i386",
"win_amd64",
"win_x86",
"darwin_x86_64",
"darwin_aarch64"
]
dependencies {
// you may put jars on which you depend on in ./libs
// or you may define them like so..
//compile "some.group:artifact:version:classifier"
//compile "some.group:artifact:version"
// real examples
//compile 'com.mod-buildcraft:buildcraft:6.0.8:dev' // adds buildcraft to the dev env
//compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env
@@ -57,44 +42,27 @@ dependencies {
// for more info...
// http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
// http://www.gradle.org/docs/current/userguide/dependency_management.html
// shade 'com.github.luben:zstd-jni:1.5.2-2' - Remove almost implementations of zstd
for (os in supportedOS) {
shade ("com.github.luben:zstd-jni:1.5.2-2:${os}")
}
}
processResources
{
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
{
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
// replace version and mcversion
expand 'version':project.version, 'mcversion':project.minecraft.version
}
// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
// replace version and mcversion
expand 'version':project.version, 'mcversion':project.minecraft.version
}
jar {
configurations.shade.each { dep ->
from(project.zipTree(dep)) {
exclude 'META-INF', 'META-INF/**'
// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
}
}
sourceSets {main { output.resourcesDir = output.classesDir }}
sourceSets {main { output.resourcesDir = output.classesDir }}
@@ -1,187 +0,0 @@
package foxz.command;
import foxz.commandhelper.ChMcLogger;
import foxz.commandhelper.annotations.Command;
import foxz.commandhelper.annotations.SubCommand;
import foxz.commandhelper.permissions.OpOnly;
import luizotavio.compressor.ZstdCompressor;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.controllers.PlayerDataController;
import noppes.npcs.util.NBTJsonUtil;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static luizotavio.compressor.executor.IOExecutor.IO_EXECUTOR;
@Command(
name = "compressor",
desc = "Convert a file to a compressed file or back to json",
usage = "update [file|all] [json|zstd]"
)
public class CmdCompressor extends ChMcLogger {
public CmdCompressor(Object sender) {
super(sender);
}
@SubCommand(
name = "update",
desc = "Convert all files in the current directory to compressed files",
permissions = {OpOnly.class}
)
public boolean onConvert(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
if (args.length == 0) {
sendmessage("Usage: /noppes compressor update [<filename>|all] [json|zstd]");
return true;
}
if (args.length == 1) {
sendmessage("Please specify the type of compression to use");
return true;
}
String type;
if (args[1].equalsIgnoreCase("json")) {
type = "json";
} else if (args[1].equalsIgnoreCase("zstd")) {
type = "zstd";
} else {
sendmessage("Please specify the type of compression to use");
return true;
}
File directory = PlayerDataController.instance.getSaveDir();
if (args[0].equalsIgnoreCase("all")) {
sendmessage("Converting all files in the current directory to " + type + " files");
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
continue;
}
if (file.getName().endsWith(".json") && type.equalsIgnoreCase("zstd")) {
submit(() -> {
try {
NBTTagCompound compound = NBTJsonUtil.LoadFile(file);
File compressed = new File(file.getParentFile(), file.getName().replace(".json", ".zstd"));
byte[] bytes = ZstdCompressor.writeCompound(compound);
Files.write(
compressed.toPath(),
bytes,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE
);
} catch (Exception exception) {
exception.printStackTrace();
}
});
}
if (file.getName().endsWith(".zstd") && type.equalsIgnoreCase("json")) {
submit(() -> {
try {
NBTTagCompound compound = ZstdCompressor.readCompressCompound(
new RandomAccessFile(file, "r")
);
File compressed = new File(file.getParentFile(), file.getName().replace(".zstd", ".json"));
NBTJsonUtil.SaveFile(compressed, compound);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
sendmessage("Done! All files were converted.");
} else {
File file = new File(directory, args[0] + ".json");
if (!file.exists()) {
file = new File(directory, args[0] + ".zstd");
}
if (!file.exists()) {
sendmessage("File not found");
return true;
}
File finalFile = file;
if (file.getName().endsWith(".json")) {
if (type.equalsIgnoreCase("json")) {
sendmessage("File is already in json format");
return true;
}
submit(() -> {
try {
NBTTagCompound compound = NBTJsonUtil.LoadFile(finalFile);
File compressed = new File(finalFile.getParentFile(), finalFile.getName().replace(".json", ".zstd"));
byte[] bytes = ZstdCompressor.writeCompound(compound);
Files.write(
compressed.toPath(),
bytes,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE
);
sendmessage("File compressed to zstd!");
} catch (Exception exception) {
exception.printStackTrace();
}
});
}
if (file.getName().endsWith(".zstd")) {
if (type.equalsIgnoreCase("zstd")) {
sendmessage("File is already in zstd format");
return true;
}
submit(() -> {
try {
NBTTagCompound compound = ZstdCompressor.readCompressCompound(
new RandomAccessFile(finalFile, "r")
);
if (compound == null) {
return;
}
File compressed = new File(finalFile.getParentFile(), finalFile.getName().replace(".zstd", ".json"));
NBTJsonUtil.SaveFile(compressed, compound);
sendmessage("File converted to json!");
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
return true;
}
private void submit(Runnable runnable) throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture.runAsync(runnable, IO_EXECUTOR).get(5, TimeUnit.MINUTES);
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ import foxz.commandhelper.permissions.PlayerOnly;
@Command(
name = "noppes",
desc = "noppes root command",
sub = {CmdClone.class, CmdScript.class, CmdQuest.class, CmdDialog.class, CmdConfig.class, CmdCompressor.class}
sub = {CmdClone.class, CmdScript.class, CmdQuest.class, CmdDialog.class, CmdConfig.class}
)
public class CmdNoppes extends ChMcLogger {
@@ -1,61 +0,0 @@
package luizotavio.compressor;
import com.github.luben.zstd.Zstd;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTSizeTracker;
import net.minecraft.nbt.NBTTagCompound;
import java.io.*;
public class ZstdCompressor {
public static byte[] readCompressed(DataInput inputStream) throws IOException {
int compressedLength = inputStream.readInt();
int uncompressedLength = inputStream.readInt();
if (compressedLength < 0 || uncompressedLength < 0) {
throw new IllegalArgumentException("Invalid length");
}
byte[] compressed = new byte[compressedLength];
inputStream.readFully(compressed);
return Zstd.decompress(compressed, uncompressedLength);
}
public static NBTTagCompound readCompressCompound(DataInput inputStream) throws IOException {
NBTTagCompound compound;
byte[] compressed = readCompressed(inputStream);
try (DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(compressed))) {
compound = CompressedStreamTools.func_152456_a(dataInputStream, NBTSizeTracker.field_152451_a);
}
return compound;
}
public static byte[] writeCompound(NBTTagCompound compound) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(),
copy = new ByteArrayOutputStream();
try (DataOutputStream dataOutputStream = new DataOutputStream(outputStream)) {
CompressedStreamTools.write(compound, dataOutputStream);
}
byte[] uncompressed = outputStream.toByteArray();
byte[] compress = Zstd.compress(uncompressed);
try (DataOutputStream dataOutputStream = new DataOutputStream(copy)) {
dataOutputStream.writeInt(compress.length);
dataOutputStream.writeInt(uncompressed.length);
dataOutputStream.write(compress);
return copy.toByteArray();
}
}
}
@@ -1,16 +0,0 @@
package luizotavio.compressor.executor;
import java.util.concurrent.ExecutorService;
public class IOExecutor {
/**
* Please, do not change this value unless you know what you are doing.
* This value is used to define the number of threads that will be used to
* compress the files.
*
* If you want to change this value, please, do it with care.
*/
public static ExecutorService IO_EXECUTOR;
}
-15
View File
@@ -10,7 +10,6 @@ import cpw.mods.fml.common.network.FMLEventChannel;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.EntityRegistry;
import foxz.command.CommandNoppes;
import luizotavio.compressor.executor.IOExecutor;
import net.minecraft.block.Block;
import net.minecraft.block.BlockIce;
import net.minecraft.block.BlockLeavesBase;
@@ -31,7 +30,6 @@ import noppes.npcs.entity.old.*;
import noppes.npcs.scripted.wrapper.WrapperNpcAPI;
import java.io.File;
import java.util.Set;
import java.util.concurrent.Executors;
@Mod(modid = "customnpcs", name = "CustomNpcs", version = "1.6-beta")
public class CustomNpcs {
@@ -41,12 +39,6 @@ public class CustomNpcs {
private static int NewEntityStartId = 0;
@ConfigProp(info = "*BACKUP BEFORE CHANGING* File Type used with PlayerData. Converting files may reduce lag. To convert existing files: /noppes compressor update [<filename>|all] [json|zstd]")
public static String CompressorType = "json";
@ConfigProp(info = "The size of IO executors used for compression and decompression. This is the number of threads used for compression and decompression. If you are experiencing issues with the server lagging/crashing, try changing this to a higher number.")
public static int CompressorThreads = 2;
@ConfigProp(info = "Enables/Disables ALL scripting. You can still see and write code in the scripter, but these scripts won't run. True by default")
public static boolean ScriptingEnabled = true;
@@ -217,13 +209,6 @@ public class CustomNpcs {
registerNpc(EntityNPCGolem.class, "npcGolem");
registerNpc(EntityCustomNpc.class, "CustomNpc");
IOExecutor.IO_EXECUTOR = Executors.newFixedThreadPool(
CompressorThreads,
new ThreadFactoryBuilder()
.setNameFormat("CustomNpcs-IO-%d")
.build()
);
registerNewEntity(EntityChairMount.class, "CustomNpcChairMount", 64, 10, false);
registerNewEntity(EntityProjectile.class, "throwableitem", 64, 3, true);
registerNewEntity(EntityMagicProjectile.class, "magicprojectile", 64, 3, true);
@@ -4,13 +4,11 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.constants.EnumRoleType;
import noppes.npcs.controllers.data.PlayerDataScript;
import noppes.npcs.entity.EntityCustomNpc;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.entity.data.DataTimers;
@@ -1,27 +1,24 @@
package noppes.npcs.controllers;
import luizotavio.compressor.ZstdCompressor;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTSizeTracker;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.util.JsonException;
import noppes.npcs.util.NBTJsonUtil;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import static luizotavio.compressor.executor.IOExecutor.IO_EXECUTOR;
public class PlayerDataController {
public class PlayerDataController {
public static PlayerDataController instance;
public PlayerDataController(){
instance = this;
}
@@ -45,24 +42,24 @@ public class PlayerDataController {
filename = "noplayername";
filename += ".dat";
try {
File file = new File(saveDir, filename);
if(file.exists()){
NBTTagCompound comp = CompressedStreamTools.readCompressed(new FileInputStream(file));
file.delete();
file = new File(saveDir, filename+"_old");
if(file.exists())
file.delete();
return comp;
}
File file = new File(saveDir, filename);
if(file.exists()){
NBTTagCompound comp = CompressedStreamTools.readCompressed(new FileInputStream(file));
file.delete();
file = new File(saveDir, filename+"_old");
if(file.exists())
file.delete();
return comp;
}
} catch (Exception e) {
LogWriter.except(e);
}
try {
File file = new File(saveDir, filename+"_old");
if(file.exists()){
return CompressedStreamTools.readCompressed(new FileInputStream(file));
}
File file = new File(saveDir, filename+"_old");
if(file.exists()){
return CompressedStreamTools.readCompressed(new FileInputStream(file));
}
} catch (Exception e) {
LogWriter.except(e);
}
@@ -73,52 +70,38 @@ public class PlayerDataController {
public NBTTagCompound loadPlayerData(String player){
File saveDir = getSaveDir();
String filename = player;
if(filename.isEmpty()) {
return new NBTTagCompound();
}
if(CustomNpcs.CompressorType.equalsIgnoreCase("zstd")) {
filename += ".zstd";
} else {
filename += ".json";
}
if(filename.isEmpty())
filename = "noplayername";
filename += ".json";
try {
return ensureCompressor(
new File(saveDir, filename)
);
File file = new File(saveDir, filename);
if(file.exists()){
return NBTJsonUtil.LoadFile(file);
}
} catch (Exception e) {
LogWriter.error("Error loading: " + filename, e);
}
return new NBTTagCompound();
}
public void savePlayerData(PlayerData data){
NBTTagCompound compound = data.getNBT();
String filename;
if (CustomNpcs.CompressorType.equalsIgnoreCase("zstd")) {
filename = data.uuid + ".zstd";
} else {
filename = data.uuid + ".json";
}
String filename = data.uuid + ".json";
try {
File saveDir = getSaveDir();
File file = new File(saveDir, filename);
if (file.exists()) {
file.delete();
File file = new File(saveDir, filename+"_new");
File file1 = new File(saveDir, filename);
NBTJsonUtil.SaveFile(file, compound);
if(file1.exists()){
file1.delete();
}
writeCompressor(file, compound);
file.renameTo(file1);
} catch (Exception e) {
LogWriter.except(e);
}
}
public PlayerBankData getBankData(EntityPlayer player, int bankId) {
Bank bank = BankController.getInstance().getBank(bankId);
PlayerBankData data = getPlayerData(player).bankData;
@@ -127,7 +110,7 @@ public class PlayerDataController {
}
return data;
}
public PlayerData getPlayerData(EntityPlayer player){
PlayerData data = (PlayerData) player.getExtendedProperties("CustomNpcsData");
if(data == null){
@@ -139,14 +122,14 @@ public class PlayerDataController {
return data;
}
public String hasPlayer(String username) {
for(String name : getUsernameData().keySet()){
if(name.equalsIgnoreCase(username))
return name;
}
for(String name : getUsernameData().keySet()){
if(name.equalsIgnoreCase(username))
return name;
}
return "";
}
public PlayerData getDataFromUsername(String username){
EntityPlayer player = MinecraftServer.getServer().getConfigurationManager().func_152612_a(username);
PlayerData data = null;
@@ -162,91 +145,37 @@ public class PlayerDataController {
}
else
data = getPlayerData(player);
return data;
}
public void addPlayerMessage(String username, PlayerMail mail) {
mail.time = System.currentTimeMillis();
EntityPlayer player = MinecraftServer.getServer().getConfigurationManager().func_152612_a(username);
PlayerData data = getDataFromUsername(username);
data.mailData.playermail.add(mail.copy());
savePlayerData(data);
}
public Map<String, NBTTagCompound> getUsernameData(){
Map<String, NBTTagCompound> map = new HashMap<String, NBTTagCompound>();
for(File file : getSaveDir().listFiles()){
if(file.isDirectory() || !file.getName().endsWith(".json"))
continue;
for(File file : getSaveDir().listFiles()){
if(file.isDirectory() || !file.getName().endsWith(".json"))
continue;
try {
NBTTagCompound compound = ensureCompressor(file);
if(compound.hasKey("PlayerName")){
map.put(compound.getString("PlayerName"), compound);
}
NBTTagCompound compound = NBTJsonUtil.LoadFile(file);
if(compound.hasKey("PlayerName")){
map.put(compound.getString("PlayerName"), compound);
}
} catch (Exception e) {
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
}
}
}
return map;
}
public boolean hasMail(EntityPlayer player) {
return getPlayerData(player).mailData.hasMail();
}
private void writeCompressor(File file, NBTTagCompound compound) {
if (compound == null) {
throw new IllegalArgumentException("Compound cannot be null!");
}
if (CustomNpcs.CompressorType.equalsIgnoreCase("json")) {
IO_EXECUTOR.submit(() -> {
try {
NBTJsonUtil.SaveFile(file, compound);
} catch (IOException | JsonException e) {
e.printStackTrace();
}
});
} else if (CustomNpcs.CompressorType.equalsIgnoreCase("zstd")) {
IO_EXECUTOR.submit(() -> {
try {
byte[] compressed = ZstdCompressor.writeCompound(compound);
Files.write(
file.toPath(),
compressed,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE
);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
private NBTTagCompound ensureCompressor(File file) throws JsonException, IOException {
if (!file.exists()) {
return new NBTTagCompound();
}
if (CustomNpcs.CompressorType.equalsIgnoreCase("json")) {
return NBTJsonUtil.LoadFile(file);
}
if (CustomNpcs.CompressorType.equalsIgnoreCase("zstd")) {
byte[] readCompressed = ZstdCompressor.readCompressed(
new RandomAccessFile(file, "r")
);
try (DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(readCompressed))) {
return CompressedStreamTools.func_152456_a(dataInputStream, NBTSizeTracker.field_152451_a);
}
}
throw new IOException("Unknown compressor type: " + CustomNpcs.CompressorType);
}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 345 B