Skip to content

Commit

Permalink
Ignore file changes in hidden folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Avatcher committed Apr 1, 2024
1 parent 723cacc commit 05bdd5b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import org.figuramc.figura.utils.FiguraText;
import org.figuramc.figura.utils.IOUtils;

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -310,7 +313,7 @@ public static void tick() {
Path path = entry.getKey().resolve((Path) event.context());
String name = IOUtils.getFileNameOrEmpty(path);

if (IOUtils.isHidden(path) || !(Files.isDirectory(path) || name.matches("(.*(\\.lua|\\.bbmodel|\\.ogg|\\.png)$|avatar\\.json)")))
if (IOUtils.isHiddenAvatarResource(path) || !(Files.isDirectory(path) || name.matches("(.*(\\.lua|\\.bbmodel|\\.ogg|\\.png)$|avatar\\.json)")))
continue;

if (kind == StandardWatchEventKinds.ENTRY_CREATE && !IS_WINDOWS)
Expand Down
34 changes: 34 additions & 0 deletions common/src/main/java/org/figuramc/figura/utils/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import org.figuramc.figura.FiguraMod;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -166,6 +167,39 @@ public static boolean isHidden(Path path) {
return hidden || getFileNameOrEmpty(path).startsWith(".");
}

/**
* Checks, if given file is a hidden avatar resource.
* Avatar resource is hidden, if it is contained within
* a hidden folder. Folder is considered "hidden" if either
* it is marked as "hidden" by the OS or folder's name starts
* with a `{@code .}` (dot), or if it is contained within
* another hidden folder.
*
* @param path Path of the file to check
* @return {@code true} if given file is hidden
*/
public static boolean isHiddenAvatarResource(@NotNull Path path) {
final Path avatarsDirectory = FiguraMod.getFiguraDirectory().resolve("avatars/");
if (!path.toAbsolutePath().startsWith(avatarsDirectory.toAbsolutePath())) {
throw new IllegalArgumentException("A path to a file within avatars folder was expected");
}
try {
// Iterate through all parent folders of the avatars
// resource to find a hidden one (if any)
for (Path parent = path;
!Files.isSameFile(parent, avatarsDirectory);
parent = parent.resolve("..").normalize()) {
if (Files.isHidden(parent) || parent.getFileName().toString().startsWith(".")) {
return true;
}
}
return false;
} catch (IOException e) {
FiguraMod.LOGGER.error("Failed to get if \"" + path + "\" is hidden", e);
return false;
}
}

public static class DirWrapper {
private final Path path;

Expand Down

0 comments on commit 05bdd5b

Please sign in to comment.