mirror of
https://github.com/EngineHub/WorldEdit.git
synced 2024-12-15 04:41:37 +08:00
Move archive unpack into WE working dir
This commit is contained in:
parent
c0a1e318c4
commit
d742c0fe0c
@ -23,6 +23,7 @@
|
|||||||
import com.sk89q.worldedit.util.concurrency.LazyReference;
|
import com.sk89q.worldedit.util.concurrency.LazyReference;
|
||||||
import com.sk89q.worldedit.util.io.ResourceLoader;
|
import com.sk89q.worldedit.util.io.ResourceLoader;
|
||||||
import com.sk89q.worldedit.util.io.WorldEditResourceLoader;
|
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.util.translation.TranslationManager;
|
||||||
import com.sk89q.worldedit.world.DataFixer;
|
import com.sk89q.worldedit.world.DataFixer;
|
||||||
import com.sk89q.worldedit.world.World;
|
import com.sk89q.worldedit.world.World;
|
||||||
@ -38,9 +39,16 @@
|
|||||||
public abstract class AbstractPlatform implements Platform {
|
public abstract class AbstractPlatform implements Platform {
|
||||||
|
|
||||||
private final ResourceLoader resourceLoader = new WorldEditResourceLoader(WorldEdit.getInstance());
|
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(() -> {
|
private final LazyReference<TranslationManager> translationManager = LazyReference.from(() -> {
|
||||||
try {
|
try {
|
||||||
return new TranslationManager(getResourceLoader());
|
return new TranslationManager(archiveUnpacker.getValue(), getResourceLoader());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
}
|
}
|
||||||
|
@ -27,42 +27,31 @@
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.UncheckedIOException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.channels.Channels;
|
import java.nio.channels.Channels;
|
||||||
import java.nio.channels.SeekableByteChannel;
|
import java.nio.channels.SeekableByteChannel;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
public class ArchiveUnpacker {
|
public final class ArchiveUnpacker {
|
||||||
|
|
||||||
private static final String UNPACK_FINISHED = ".unpack_finished";
|
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();
|
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;
|
String hash;
|
||||||
try (InputStream data = archiveUrl.openStream()) {
|
try (InputStream data = archiveUrl.openStream()) {
|
||||||
hash = ByteStreams.readBytes(data, new ByteProcessor<String>() {
|
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))) {
|
if (Files.exists(dest.resolve(UNPACK_FINISHED))) {
|
||||||
// trust this, no other option :)
|
// trust this, no other option :)
|
||||||
return dest;
|
return dest;
|
||||||
@ -130,7 +119,4 @@ public String getResult() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArchiveUnpacker() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -96,17 +96,19 @@ public static String makeTranslationKey(String type, String id) {
|
|||||||
private final Lock loadLock = new ReentrantLock();
|
private final Lock loadLock = new ReentrantLock();
|
||||||
private Locale defaultLocale = Locale.ENGLISH;
|
private Locale defaultLocale = Locale.ENGLISH;
|
||||||
|
|
||||||
|
private final ArchiveUnpacker archiveUnpacker;
|
||||||
private final ResourceLoader resourceLoader;
|
private final ResourceLoader resourceLoader;
|
||||||
private final Path userProvidedFlatRoot;
|
private final Path userProvidedFlatRoot;
|
||||||
private final Path internalZipRoot;
|
private final Path internalZipRoot;
|
||||||
@Nullable
|
@Nullable
|
||||||
private Path userProvidedZipRoot;
|
private Path userProvidedZipRoot;
|
||||||
|
|
||||||
public TranslationManager(ResourceLoader resourceLoader) throws IOException {
|
public TranslationManager(ArchiveUnpacker archiveUnpacker, ResourceLoader resourceLoader) throws IOException {
|
||||||
|
this.archiveUnpacker = archiveUnpacker;
|
||||||
this.resourceLoader = resourceLoader;
|
this.resourceLoader = resourceLoader;
|
||||||
checkNotNull(resourceLoader);
|
checkNotNull(resourceLoader);
|
||||||
this.userProvidedFlatRoot = resourceLoader.getLocalResource("lang");
|
this.userProvidedFlatRoot = resourceLoader.getLocalResource("lang");
|
||||||
this.internalZipRoot = ArchiveUnpacker.unpackArchive(checkNotNull(
|
this.internalZipRoot = archiveUnpacker.unpackArchive(checkNotNull(
|
||||||
resourceLoader.getRootResource("lang/i18n.zip"),
|
resourceLoader.getRootResource("lang/i18n.zip"),
|
||||||
"Missing internal i18n.zip!"
|
"Missing internal i18n.zip!"
|
||||||
));
|
));
|
||||||
@ -116,7 +118,7 @@ private void load() throws IOException {
|
|||||||
Path userZip = resourceLoader.getLocalResource("lang/i18n.zip");
|
Path userZip = resourceLoader.getLocalResource("lang/i18n.zip");
|
||||||
Path result = null;
|
Path result = null;
|
||||||
if (Files.exists(userZip)) {
|
if (Files.exists(userZip)) {
|
||||||
result = ArchiveUnpacker.unpackArchive(userZip.toUri().toURL());
|
result = archiveUnpacker.unpackArchive(userZip.toUri().toURL());
|
||||||
}
|
}
|
||||||
this.userProvidedZipRoot = result;
|
this.userProvidedZipRoot = result;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user