Move archive unpack into WE working dir

This commit is contained in:
Octavia Togami 2020-10-29 13:35:12 -07:00
parent c0a1e318c4
commit d742c0fe0c
3 changed files with 24 additions and 28 deletions

View File

@ -23,6 +23,7 @@
import com.sk89q.worldedit.util.concurrency.LazyReference;
import com.sk89q.worldedit.util.io.ResourceLoader;
import com.sk89q.worldedit.util.io.WorldEditResourceLoader;
import com.sk89q.worldedit.util.io.file.ArchiveUnpacker;
import com.sk89q.worldedit.util.translation.TranslationManager;
import com.sk89q.worldedit.world.DataFixer;
import com.sk89q.worldedit.world.World;
@ -38,9 +39,16 @@
public abstract class AbstractPlatform implements Platform {
private final ResourceLoader resourceLoader = new WorldEditResourceLoader(WorldEdit.getInstance());
private final LazyReference<ArchiveUnpacker> archiveUnpacker = LazyReference.from(() -> {
try {
return new ArchiveUnpacker(getConfiguration().getWorkingDirectoryPath().resolve(".archive-unpack"));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
private final LazyReference<TranslationManager> translationManager = LazyReference.from(() -> {
try {
return new TranslationManager(getResourceLoader());
return new TranslationManager(archiveUnpacker.getValue(), getResourceLoader());
} catch (IOException e) {
throw new UncheckedIOException(e);
}

View File

@ -27,42 +27,31 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ArchiveUnpacker {
public final class ArchiveUnpacker {
private static final String UNPACK_FINISHED = ".unpack_finished";
private static final Path TEMP_DIR;
static {
try {
TEMP_DIR = Paths.get(
System.getProperty("java.io.tmpdir"),
"worldedit-unpack-dir-for-" + System.getProperty("user.name")
);
Files.createDirectories(
TEMP_DIR,
SafeFiles.getOwnerOnlyFileAttributes(AttributeTarget.DIRECTORY)
);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static final Lock lock = new ReentrantLock();
public static Path unpackArchive(URL archiveUrl) throws IOException {
private final Path unpackDir;
public ArchiveUnpacker(Path unpackDir) throws IOException {
this.unpackDir = unpackDir;
Files.createDirectories(unpackDir);
}
public Path unpackArchive(URL archiveUrl) throws IOException {
String hash;
try (InputStream data = archiveUrl.openStream()) {
hash = ByteStreams.readBytes(data, new ByteProcessor<String>() {
@ -80,7 +69,7 @@ public String getResult() {
}
});
}
Path dest = TEMP_DIR.resolve(hash);
Path dest = unpackDir.resolve(hash);
if (Files.exists(dest.resolve(UNPACK_FINISHED))) {
// trust this, no other option :)
return dest;
@ -130,7 +119,4 @@ public String getResult() {
}
}
private ArchiveUnpacker() {
}
}

View File

@ -96,17 +96,19 @@ public static String makeTranslationKey(String type, String id) {
private final Lock loadLock = new ReentrantLock();
private Locale defaultLocale = Locale.ENGLISH;
private final ArchiveUnpacker archiveUnpacker;
private final ResourceLoader resourceLoader;
private final Path userProvidedFlatRoot;
private final Path internalZipRoot;
@Nullable
private Path userProvidedZipRoot;
public TranslationManager(ResourceLoader resourceLoader) throws IOException {
public TranslationManager(ArchiveUnpacker archiveUnpacker, ResourceLoader resourceLoader) throws IOException {
this.archiveUnpacker = archiveUnpacker;
this.resourceLoader = resourceLoader;
checkNotNull(resourceLoader);
this.userProvidedFlatRoot = resourceLoader.getLocalResource("lang");
this.internalZipRoot = ArchiveUnpacker.unpackArchive(checkNotNull(
this.internalZipRoot = archiveUnpacker.unpackArchive(checkNotNull(
resourceLoader.getRootResource("lang/i18n.zip"),
"Missing internal i18n.zip!"
));
@ -116,7 +118,7 @@ private void load() throws IOException {
Path userZip = resourceLoader.getLocalResource("lang/i18n.zip");
Path result = null;
if (Files.exists(userZip)) {
result = ArchiveUnpacker.unpackArchive(userZip.toUri().toURL());
result = archiveUnpacker.unpackArchive(userZip.toUri().toURL());
}
this.userProvidedZipRoot = result;
}