mirror of
https://github.com/TimeCrafters/CenterStage
synced 2025-12-15 08:42:35 +00:00
WIP: Experimental support for live reloading TCT Config variables
This commit is contained in:
@@ -172,6 +172,13 @@ public abstract class CyberarmState implements Runnable {
|
||||
return hasFinished;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets state as finished
|
||||
*/
|
||||
public void finished() {
|
||||
hasFinished = true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Get value of isRunning
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package org.timecrafters.TimeCraftersConfigurationTool.library;
|
||||
|
||||
import android.os.FileObserver;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
@@ -33,6 +37,8 @@ import java.io.FileReader;
|
||||
public class TimeCraftersConfiguration {
|
||||
private static final String TAG = "TCT|TCConfig";
|
||||
private Config config;
|
||||
private boolean liveReload = false;
|
||||
private FileObserver fileObserver;
|
||||
|
||||
public TimeCraftersConfiguration() {
|
||||
Settings settings = loadSettings();
|
||||
@@ -47,6 +53,10 @@ public class TimeCraftersConfiguration {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void enableLiveReload(boolean b) {
|
||||
liveReload = b;
|
||||
}
|
||||
|
||||
public Group group(String groupName) {
|
||||
for (final Group group : config.getGroups()) {
|
||||
if (group.name.trim().equals(groupName.trim())) {
|
||||
@@ -109,6 +119,21 @@ public class TimeCraftersConfiguration {
|
||||
Config config = gsonForConfig().fromJson(new FileReader(configFile), Config.class);
|
||||
config.setName(name);
|
||||
|
||||
final TimeCraftersConfiguration self = this;
|
||||
if (fileObserver == null) {
|
||||
fileObserver = new FileObserver(configFile) {
|
||||
@Override
|
||||
public void onEvent(int event, @Nullable String filePath) {
|
||||
if (self.liveReload && event == CLOSE_WRITE) {
|
||||
Config newConfig = loadConfig(path);
|
||||
|
||||
self.config.syncVariables(newConfig);
|
||||
}
|
||||
}
|
||||
};
|
||||
fileObserver.startWatching();
|
||||
}
|
||||
|
||||
return config;
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.timecrafters.TimeCraftersConfigurationTool.library.backend.config.Act
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.backend.config.Configuration;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.backend.config.Group;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.backend.config.Presets;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.backend.config.Variable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
@@ -41,4 +42,31 @@ public class Config {
|
||||
public ArrayList<Group> getGroups() {
|
||||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the values of Variables from reloaded config
|
||||
* @param newConfig
|
||||
*/
|
||||
public void syncVariables(Config newConfig) {
|
||||
for (Group group : groups) {
|
||||
for (Action action : group.getActions()) {
|
||||
|
||||
for (Group newGroup : newConfig.groups) {
|
||||
for (Action newAction : newGroup.getActions()) {
|
||||
|
||||
if (group.name.trim().equals(newGroup.name.trim()) && action.name.trim().equals(newAction.name.trim())) {
|
||||
for (Variable variable : action.getVariables()) {
|
||||
for (Variable newVariable : newAction.getVariables()) {
|
||||
|
||||
if (variable.name.trim().equals(newVariable.name.trim())) {
|
||||
variable.setValue(newVariable.rawValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user