Skip to content

Commit

Permalink
LibGDX:
Browse files Browse the repository at this point in the history
* cached file tree to emulate case insensitive access.
  • Loading branch information
Mikhael-Danilov committed Dec 21, 2024
1 parent 30ac63f commit 0599564
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.nyrds.platform.storage;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.nyrds.platform.util.PUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CaseInsensitiveFileCache {

private final Map<String, FileHandle> fileCache;


public CaseInsensitiveFileCache(String... rootPaths) {
this.fileCache = new HashMap<>();

for (int i = rootPaths.length - 1; i >= 0; --i) {
cacheFiles(rootPaths[i]);
}

for (String key : fileCache.keySet()) {
PUtil.slog("file", "Cached file: " + key);
}
}

private void cacheFiles(String... rootPaths) {
for (String rootPath : rootPaths) {
FileHandle rootDir = Gdx.files.internal(rootPath);
if (rootDir.exists() && rootDir.isDirectory()) {
cacheFilesRecursive(rootDir, rootDir.path());
} else {
System.err.println("Root directory does not exist or is not a directory: " + rootPath);
}
}
}

private void cacheFilesRecursive(FileHandle directory, String prefix) {
for (FileHandle file : directory.list()) {
String relativePath = file.path().substring(prefix.length() + 1);
String lowerCaseRelativePath = relativePath.toLowerCase();

fileCache.put(lowerCaseRelativePath, file);
if (file.isDirectory()) {
cacheFilesRecursive(file, prefix);
}
}
}

public FileHandle getFile(String fileName) {
String lowerCaseName = fileName.toLowerCase();
FileHandle file = fileCache.get(lowerCaseName);
if (file == null) {
throw new IllegalArgumentException("File not found: " + fileName);
}
return file;
}

public boolean exists(String fileName) {
String lowerCaseName = fileName.toLowerCase();
return fileCache.containsKey(lowerCaseName);
}

public List<FileHandle> getAllFiles() {
return new ArrayList<>(fileCache.values());
}

public List<String> getAllCachedPaths() {
return new ArrayList<>(fileCache.keySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@

public class FileSystem {

static CaseInsensitiveFileCache fileCache = new CaseInsensitiveFileCache(getAllResPaths());

static public FileHandle getInternalStorageFileHandle(String fileName) {
FileHandle fileHandle = null;
for(String path : getAllResPaths()) {
fileHandle = Gdx.files.internal(path+fileName);
if(fileHandle.exists()) {
return fileHandle;
}
}
PUtil.slog("file", "File not found: " + fileName);
return fileHandle;
PUtil.slog("file", "getInternalStorageFileHandle: " + fileName);
return fileCache.getFile(fileName);
}

private static String[] getAllResPaths() {
Expand All @@ -51,6 +45,7 @@ private static String[] getAllResPaths() {
};
}


static public String[] listResources(String resName) {
Set<String> resList = new HashSet<>();
for (String path : getAllResPaths()) {
Expand All @@ -63,6 +58,8 @@ static public String[] listResources(String resName) {
return resList.toArray(new String[0]);
}

static public boolean exists(String fileName) { return fileCache.exists(fileName); } // exists

static public @NotNull FileHandle getInternalStorageFileHandleBase(String fileName) {
FileHandle fileHandle = null;
for(String path : new String[] {
Expand Down Expand Up @@ -228,4 +225,8 @@ public static void ensureDir(String dir) {
throw new ModError("Can't create directory:"+dir);
}
}

public static void invalidateCache() {
fileCache = new CaseInsensitiveFileCache(getAllResPaths());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@
import com.nyrds.platform.storage.FileSystem;
import com.watabou.pixeldungeon.utils.GLog;
import com.watabou.pixeldungeon.utils.Utils;
import lombok.SneakyThrows;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import lombok.SneakyThrows;


public class ModdingMode {
Expand Down Expand Up @@ -92,6 +109,8 @@ public static void selectMod(String mod) {
} catch (Exception e) {
EventCollector.logException(e);
mActiveMod = ModdingMode.REMIXED;
} finally {
FileSystem.invalidateCache();
}
}

Expand Down Expand Up @@ -173,14 +192,11 @@ public static boolean inRemixed() {
}

public static boolean isResourceExists(String resName) {
return isAssetExist(resName) || isResourceExistInMod(resName);
return FileSystem.exists(resName);
}

public static boolean isResourceExistInMod(String resName) {
if (!mActiveMod.equals(REMIXED)) {
return FileSystem.getExternalStorageFile(mActiveMod + "/" + resName).exists();
}
return false;
return FileSystem.exists(resName);
}

@NotNull
Expand Down

0 comments on commit 0599564

Please sign in to comment.