+ Added FileMonitor feature for localization files to allow for active reloading
This commit is contained in:
@@ -12,6 +12,8 @@ import dataStructures.TaggedPairStore;
|
||||
import utils.FileUtils;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
import utils.io.FileMonitor;
|
||||
import utils.io.MonitoredFile;
|
||||
|
||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
||||
// generates/updates a file externally with all the stub values as keys that are localized.
|
||||
@@ -32,6 +34,9 @@ public abstract class LocBase
|
||||
private void Warn(String str) { GlobalLog.Warn(LogFilter.Strings, str); }
|
||||
private void Error(String str) { GlobalLog.Error(LogFilter.Strings, str); }
|
||||
|
||||
// File monitoring
|
||||
protected FileMonitor fileMonitor;
|
||||
|
||||
// Ok... so this is an array because if it's not an array, the parser will parse the string
|
||||
public LocBase(String filename, String functionName)
|
||||
{
|
||||
@@ -39,6 +44,22 @@ public abstract class LocBase
|
||||
this.functionName = functionName;
|
||||
}
|
||||
|
||||
// Checks the file specified for updates
|
||||
public void Update()
|
||||
{
|
||||
synchronized(stringStore)
|
||||
{
|
||||
fileMonitor.Update(this::OnFileChange);
|
||||
}
|
||||
}
|
||||
|
||||
// When the file is changed
|
||||
protected void OnFileChange(MonitoredFile file)
|
||||
{
|
||||
Log("Loc file was modified at path " + file.path);
|
||||
UpdateLocFromDisk();
|
||||
}
|
||||
|
||||
// Structure used for holding a pair of strings and any other info we need
|
||||
// about localized information that is being looked up.
|
||||
private class LocInfo
|
||||
|
||||
@@ -3,6 +3,7 @@ package core;
|
||||
import java.util.ArrayList;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
import utils.io.FileMonitor;
|
||||
import dataStructures.Pair;
|
||||
|
||||
// Performs the same localization for the strings associated with command names as
|
||||
@@ -27,6 +28,8 @@ public class LocCommands extends LocBase
|
||||
UpdateLocFromDisk();
|
||||
ScrapeAll();
|
||||
SaveLocToDisk();
|
||||
|
||||
fileMonitor = new FileMonitor(filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -47,4 +50,9 @@ public class LocCommands extends LocBase
|
||||
instance.stringStore.ForEach((pair) -> raw.add((String)((Pair<?, ?>)pair).First ));
|
||||
return raw;
|
||||
}
|
||||
|
||||
public static void Upkeep()
|
||||
{
|
||||
instance.Update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package core;
|
||||
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
import utils.io.FileMonitor;
|
||||
|
||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
||||
// generates/updates a file externally (phrases.config) with all the stub values as keys that
|
||||
@@ -26,6 +27,8 @@ public class LocStrings extends LocBase
|
||||
UpdateLocFromDisk();
|
||||
ScrapeAll();
|
||||
SaveLocToDisk();
|
||||
|
||||
fileMonitor = new FileMonitor(filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -43,4 +46,9 @@ public class LocStrings extends LocBase
|
||||
{
|
||||
return instance.GetKey(stubbedPreviously);
|
||||
}
|
||||
|
||||
public static void Upkeep()
|
||||
{
|
||||
instance.Update();
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ import java.util.List;
|
||||
|
||||
import dataStructures.Pair;
|
||||
import utils.FileUtils;
|
||||
import utils.directoryMonitor.DirectoryMonitor;
|
||||
import utils.directoryMonitor.MonitoredFile;
|
||||
import utils.io.DirectoryMonitor;
|
||||
import utils.io.MonitoredFile;
|
||||
|
||||
// All things considered, this doesn't need to be particularly efficient since anything
|
||||
// less than 10ms of search time won't be noticable to an end user really, not for
|
||||
|
||||
Reference in New Issue
Block a user