Re-generate textmap cache if an error occurs

This commit is contained in:
KingRainbow44 2023-05-04 11:26:19 -04:00
parent 8bb7ae22d0
commit 8ee3dec5df
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
2 changed files with 38 additions and 22 deletions

View File

@ -85,8 +85,13 @@ public final class Tools {
void newTranslatedLine(String template, TextStrings... textstrings) { void newTranslatedLine(String template, TextStrings... textstrings) {
for (int i = 0; i < TextStrings.NUM_LANGUAGES; i++) { for (int i = 0; i < TextStrings.NUM_LANGUAGES; i++) {
String s = template; String s = template;
for (int j = 0; j < textstrings.length; j++) for (int j = 0; j < textstrings.length; j++) try {
s = s.replace("{" + j + "}", textstrings[j].strings[i]); s = s.replace("{" + j + "}", textstrings[j].strings[i]);
} catch (NullPointerException ignored) {
// TextMap cache is outdated.
j--; // Retry the action.
Language.loadTextMaps(true);
}
handbookBuilders.get(i).append(s + "\n"); handbookBuilders.get(i).append(s + "\n");
} }
} }

View File

@ -314,38 +314,49 @@ public final class Language {
return getTextMapKey((int) hash); return getTextMapKey((int) hash);
} }
/**
* Loads game text maps with caching.
*/
public static void loadTextMaps() { public static void loadTextMaps() {
// Check system timestamps on cache and resources Language.loadTextMaps(false);
try { }
long cacheModified = Files.getLastModifiedTime(TEXTMAP_CACHE_PATH).toMillis();
/**
* Loads game language data (text maps).
*
* @param bypassCache Should the cache be bypassed?
*/
public static void loadTextMaps(boolean bypassCache) {
// Check system timestamps on cache and resources
if (!bypassCache) try {
long cacheModified = Files.getLastModifiedTime(TEXTMAP_CACHE_PATH).toMillis();
long textmapsModified = long textmapsModified =
Files.list(getResourcePath("TextMap")) Files.list(getResourcePath("TextMap"))
.filter(path -> path.toString().endsWith(".json")) .filter(path -> path.toString().endsWith(".json"))
.map( .map(
path -> { path -> {
try { try {
return Files.getLastModifiedTime(path).toMillis(); return Files.getLastModifiedTime(path).toMillis();
} catch (Exception ignored) { } catch (Exception ignored) {
Grasscutter.getLogger() Grasscutter.getLogger()
.debug("Exception while checking modified time: ", path); .debug("Exception while checking modified time: ", path);
return Long.MAX_VALUE; // Don't use cache, something has gone wrong return Long.MAX_VALUE; // Don't use cache, something has gone wrong
} }
}) })
.max(Long::compare) .max(Long::compare)
.get(); .get();
Grasscutter.getLogger() Grasscutter.getLogger()
.debug( .debug(
"Cache modified %d, textmap modified %d".formatted(cacheModified, textmapsModified)); "Cache modified %d, textmap modified %d".formatted(cacheModified, textmapsModified));
if (textmapsModified < cacheModified) { if (textmapsModified < cacheModified) {
// Try loading from cache // Try loading from cache
Grasscutter.getLogger().debug("Loading cached 'TextMaps'..."); Grasscutter.getLogger().debug("Loading cached 'TextMaps'...");
textMapStrings = loadTextMapsCache(); textMapStrings = loadTextMapsCache();
return; return;
} }
} catch (Exception e) { } catch (Exception exception) {
Grasscutter.getLogger().debug("Exception while checking cache: ", e); Grasscutter.getLogger().error("Error loading textmaps cache: " + exception.toString());
} }
// Regenerate cache // Regenerate cache