Skip to content

Commit

Permalink
Fix emotes loading if symbolic link
Browse files Browse the repository at this point in the history
Fixes #504
Also reduces the load time from 4 sec. to 1 sec.
  • Loading branch information
dima-dencep committed Nov 29, 2024
1 parent f850f1d commit 038c30f
Showing 1 changed file with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,29 @@
import io.github.kosmx.emotes.executor.EmoteInstance;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Stream;

/**
* Serializing emotes.
*/
public class EmoteSerializer {
public static void serializeEmotes(UUIDMap<KeyframeAnimation> emotes, Path externalEmotes) {
try {
if (!Files.isDirectory(externalEmotes)) {
return; // Just skip
}

Files.walkFileTree(externalEmotes, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (AnimationFormat.byFileName(file.getFileName().toString()).getExtension() != null) {
emotes.addAll(serializeExternalEmote(file));
}

return FileVisitResult.CONTINUE;
}
if (!Files.isDirectory(externalEmotes)) {
return; // Just skip
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return Files.isSameFile(externalEmotes, dir) ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
}
});
try (Stream<Path> paths = Files.walk(externalEmotes, 1, FileVisitOption.FOLLOW_LINKS)) {
paths.filter(
file -> AnimationFormat.byFileName(file.getFileName().toString()).getExtension() != null
).parallel().forEach(file -> emotes.addAll(serializeExternalEmote(file)));
} catch (Throwable e) {
EmoteInstance.instance.getLogger().log(Level.WARNING, "Failed to walk emotes!", e);
}
Expand Down

0 comments on commit 038c30f

Please sign in to comment.