From d742c0fe0c5b36c46163031b674c2c58ee151cf9 Mon Sep 17 00:00:00 2001 From: Octavia Togami Date: Thu, 29 Oct 2020 13:35:12 -0700 Subject: [PATCH] Move archive unpack into WE working dir --- .../extension/platform/AbstractPlatform.java | 10 +++++- .../util/io/file/ArchiveUnpacker.java | 34 ++++++------------- .../util/translation/TranslationManager.java | 8 +++-- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlatform.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlatform.java index fcff0787d..6f93277b8 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlatform.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlatform.java @@ -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 = LazyReference.from(() -> { + try { + return new ArchiveUnpacker(getConfiguration().getWorkingDirectoryPath().resolve(".archive-unpack")); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); private final LazyReference translationManager = LazyReference.from(() -> { try { - return new TranslationManager(getResourceLoader()); + return new TranslationManager(archiveUnpacker.getValue(), getResourceLoader()); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/io/file/ArchiveUnpacker.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/io/file/ArchiveUnpacker.java index 44982e2ab..16f37eb68 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/io/file/ArchiveUnpacker.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/io/file/ArchiveUnpacker.java @@ -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() { @@ -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() { - } - } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java index f4b99d991..7ea8c00ba 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java @@ -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; }