Skip to content

Commit c33ccea

Browse files
committed
Cleanup FileNameUtils and FileUtils a bit
1 parent 8ab35e5 commit c33ccea

9 files changed

+82
-80
lines changed

src/main/java/fi/dy/masa/malilib/gui/interfaces/IDirectoryCache.java

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public interface IDirectoryCache
77
{
8+
// TODO -- Remove the file system; needs to deal with the FileFilter mechanism to make it compat with Path
89
@Nullable
910
File getCurrentDirectoryForContext(String context);
1011

src/main/java/fi/dy/masa/malilib/gui/interfaces/IDirectoryNavigator.java

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public interface IDirectoryNavigator
66
{
7+
// TODO -- Remove the file system; needs to deal with the FileFilter mechanism to make it compat with Path
78
File getCurrentDirectory();
89

910
void switchToDirectory(File dir);

src/main/java/fi/dy/masa/malilib/gui/widgets/WidgetDirectoryNavigation.java

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
public class WidgetDirectoryNavigation extends WidgetSearchBar
2121
{
22+
// TODO -- Remove the file system; needs to deal with the FileFilter mechanism to make it compat with Path
2223
protected final File currentDir;
2324
protected final File rootDir;
2425
protected final IDirectoryNavigator navigator;

src/main/java/fi/dy/masa/malilib/gui/widgets/WidgetFileBrowserBase.java

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
public abstract class WidgetFileBrowserBase extends WidgetListBase<DirectoryEntry, WidgetDirectoryEntry> implements IDirectoryNavigator
2424
{
25+
// TODO -- Remove the file system; needs to deal with the FileFilter mechanism to make it compat with Path
2526
protected static final FileFilter DIRECTORY_FILTER = new FileFilterDirectories();
2627
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2728

src/main/java/fi/dy/masa/malilib/util/DirectoryCreator.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public class DirectoryCreator implements IStringConsumerFeedback
1010
{
11+
// TODO -- Remove the file system; needs to deal with the FileFilter mechanism to make it compat with Path
1112
protected final File dir;
1213
@Nullable protected final IDirectoryNavigator navigator;
1314

src/main/java/fi/dy/masa/malilib/util/FileNameUtils.java

-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
import java.util.Locale;
66
import com.google.common.collect.ImmutableSet;
77
import org.apache.commons.lang3.StringUtils;
8-
import org.jetbrains.annotations.ApiStatus;
98

109
/**
1110
* Post-ReWrite code
1211
*/
13-
@ApiStatus.Experimental
1412
public class FileNameUtils
1513
{
1614
public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");

src/main/java/fi/dy/masa/malilib/util/FileUtils.java

+47-49
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package fi.dy.masa.malilib.util;
22

3-
import javax.annotation.Nullable;
43
import java.io.BufferedWriter;
54
import java.io.File;
6-
import java.io.FileInputStream;
75
import java.io.IOException;
86
import java.nio.charset.StandardCharsets;
97
import java.nio.file.*;
108
import java.util.*;
119
import java.util.function.Consumer;
1210
import java.util.function.Predicate;
13-
import com.google.common.collect.ImmutableSet;
14-
import net.minecraft.nbt.NbtCompound;
15-
import net.minecraft.nbt.NbtIo;
16-
import net.minecraft.nbt.NbtSizeTracker;
11+
import javax.annotation.Nullable;
12+
1713
import fi.dy.masa.malilib.MaLiLib;
1814
import fi.dy.masa.malilib.config.value.FileWriteType;
1915
import fi.dy.masa.malilib.util.game.wrap.GameWrap;
@@ -27,29 +23,34 @@ public class FileUtils
2723
public static final Predicate<Path> ALWAYS_FALSE_FILEFILTER = p -> false;
2824
public static final Predicate<Path> ANY_FILE_FILEFILTER = Files::isRegularFile;
2925
public static final Predicate<Path> JSON_FILEFILTER = (f) -> Files.isRegularFile(f) && f.getFileName().toString().endsWith(".json");
30-
private static final Set<Character> ILLEGAL_CHARACTERS = ImmutableSet.of( '/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':' );
3126

27+
/**
28+
* Please stop using the File object
29+
* @return ()
30+
*/
31+
@Deprecated(forRemoval = true)
3232
public static File getConfigDirectory()
3333
{
34-
//return new File(MinecraftClient.getInstance().runDirectory, "config");
3534
return new File(GameWrap.getClient().runDirectory, "config");
3635
}
3736

38-
public static Path getConfigDirectoryAsPath()
37+
/**
38+
* Please stop using the File object
39+
* @return ()
40+
*/
41+
@Deprecated(forRemoval = true)
42+
public static File getMinecraftDirectory()
3943
{
40-
//return new File(MinecraftClient.getInstance().runDirectory, "config");
41-
return GameWrap.getClient().runDirectory.toPath().resolve("config");
44+
return GameWrap.getClient().runDirectory;
4245
}
4346

44-
public static File getMinecraftDirectory()
47+
public static Path getConfigDirectoryAsPath()
4548
{
46-
//return MinecraftClient.getInstance().runDirectory;
47-
return GameWrap.getClient().runDirectory;
49+
return GameWrap.getClient().runDirectory.toPath().resolve("config");
4850
}
4951

50-
public static Path getMinecraftDirectoryPath()
52+
public static Path getMinecraftDirectoryAsPath()
5153
{
52-
//return MinecraftClient.getInstance().runDirectory;
5354
return GameWrap.getClient().runDirectory.toPath();
5455
}
5556

@@ -365,54 +366,51 @@ public static String getJoinedTrailingPathElements(File file, File rootPath, int
365366
return path;
366367
}
367368

368-
public static String getNameWithoutExtension(String name)
369+
public static String getJoinedTrailingPathElements(Path file, Path rootPath, int maxStringLength, String separator)
369370
{
370-
int i = name.lastIndexOf(".");
371-
return i != -1 ? name.substring(0, i) : name;
372-
}
371+
StringBuilder path = new StringBuilder();
373372

374-
public static String generateSimpleSafeFileName(String name)
375-
{
376-
return name.toLowerCase(Locale.US).replaceAll("\\W", "_");
377-
}
378-
379-
public static String generateSafeFileName(String name)
380-
{
381-
StringBuilder sb = new StringBuilder(name.length());
373+
if (maxStringLength <= 0)
374+
{
375+
return "...";
376+
}
382377

383-
for (int i = 0; i < name.length(); ++i)
378+
while (file != null)
384379
{
385-
char c = name.charAt(i);
380+
String name = file.getFileName().toString();
386381

387-
if (ILLEGAL_CHARACTERS.contains(c) == false)
382+
if ((path.length() == 0) == false)
388383
{
389-
sb.append(c);
384+
path.insert(0, name + separator);
385+
}
386+
else
387+
{
388+
path = new StringBuilder(name);
390389
}
391-
}
392390

393-
// Some weird reserved windows keywords apparently... FFS >_>
394-
return sb.toString().replaceAll("COM", "").replaceAll("PRN", "");
395-
}
391+
int len = path.length();
396392

397-
@Nullable
398-
public static NbtCompound readNBTFile(File file)
399-
{
400-
if (file.exists() && file.isFile() && file.canRead())
401-
{
402-
try
393+
if (len > maxStringLength)
403394
{
404-
FileInputStream is = new FileInputStream(file);
405-
NbtCompound nbt = NbtIo.readCompressed(is, NbtSizeTracker.ofUnlimitedBytes());
406-
is.close();
407-
return nbt;
395+
path = new StringBuilder("... " + path.substring(len - maxStringLength, len));
396+
break;
408397
}
409-
catch (Exception e)
398+
399+
if (file.equals(rootPath))
410400
{
411-
MaLiLib.LOGGER.warn("Failed to read NBT data from file '{}'", file.getAbsolutePath());
401+
break;
412402
}
403+
404+
file = file.getParent();
413405
}
414406

415-
return null;
407+
return path.toString();
408+
}
409+
410+
public static String getNameWithoutExtension(String name)
411+
{
412+
int i = name.lastIndexOf(".");
413+
return i != -1 ? name.substring(0, i) : name;
416414
}
417415

418416
public static boolean writeDataToFile(final Path file, Consumer<BufferedWriter> dataWriter, FileWriteType writeType)

src/main/java/fi/dy/masa/malilib/util/StringUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,9 @@ public static String getWorldOrServerName()
587587
// This used to be just MinecraftServer::getLevelName().
588588
// Getting the name would now require an @Accessor for MinecraftServer.field_23784
589589
String name = server.getSaveProperties().getLevelName();
590-
// todo this was probably breaking non-US Locale file names
590+
// this was breaking non-US Locale file names
591591
//return FileUtils.generateSimpleSafeFileName(name);
592-
return FileUtils.generateSafeFileName(name);
592+
return FileNameUtils.generateSafeFileName(name);
593593
}
594594
}
595595
else
@@ -659,7 +659,7 @@ public static String getStorageFileName(boolean globalData, String prefix, Strin
659659
name = prefix + defaultName + suffix;
660660
}
661661

662-
return FileNameUtils.generateSimpleSafeFileName(name) + suffix;
662+
return FileNameUtils.generateSafeFileName(name) + suffix;
663663
}
664664

665665
public static String stringifyAddress(SocketAddress address)

0 commit comments

Comments
 (0)