-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cached file tree to emulate case insensitive access.
- Loading branch information
1 parent
30ac63f
commit 0599564
Showing
3 changed files
with
107 additions
and
17 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...edDungeonDesktop/src/libgdx/java/com/nyrds/platform/storage/CaseInsensitiveFileCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters