Skip to content

Commit

Permalink
ref: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Sep 15, 2024
1 parent 92d5e12 commit 9438ecf
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,97 +53,105 @@ public static void loadChangeConfigs() {
private static void loadConfigForPair(final String fileName, final String currentSignal,
final String nextSignal, final Map<String, String> savedPredicates,
final Map<String, List<String>> values) {
final Signal start = Signal.SIGNALS.get(currentSignal.toLowerCase());
final Signal end = Signal.SIGNALS.get(nextSignal.toLowerCase());
if (start == null || end == null) {
OpenSignalsMain.getLogger()
.warn("The signal '" + nextSignal + "' or the signal '" + nextSignal
+ "' doen't exists! " + "This config with filename '" + fileName
+ "' will be skiped!");
return;
}
final Map.Entry<Signal, Signal> pair = Maps.immutableEntry(start, end);
if (CHANGECONFIGS.containsKey(pair)) {
throw new LogicalParserException(
"A signalconfig with the signals [" + start.getSignalTypeName() + ", "
+ end.getSignalTypeName() + "] does alredy exists! '" + fileName
+ "' tried to register a chaneconfig for the same signalpair!");
}
final FunctionParsingInfo startInfo = new FunctionParsingInfo(start);
final FunctionParsingInfo endInfo = new FunctionParsingInfo(
LogicParser.UNIVERSAL_TRANSLATION_TABLE, end);
final List<ConfigProperty> properties = new ArrayList<>();

for (final Map.Entry<String, List<String>> entry : values.entrySet()) {

String valueToParse = entry.getKey().toLowerCase();
Predicate<Map<Class<?>, Object>> predicate = t -> true;

if (valueToParse.contains("map(") && savedPredicates != null
&& !savedPredicates.isEmpty()) {
final char[] chars = entry.getKey().toCharArray();
String names = "";
boolean readKey = false;
final StringBuilder builder = new StringBuilder();
String mapKey = "";
for (final char letter : chars) {

final String current = builder.append(letter).toString();
final boolean isOpenBracket = current.equals("(");
final boolean isCloseBracket = current.equals(")");
builder.setLength(0);

if (readKey) {
try {
if (isCloseBracket) {
valueToParse = valueToParse.replace("map(" + mapKey + ")",
"(" + savedPredicates.get(mapKey).toLowerCase() + ")");
names = "";
mapKey = "";
readKey = false;
try {

final Signal start = Signal.SIGNALS.get(currentSignal.toLowerCase());
final Signal end = Signal.SIGNALS.get(nextSignal.toLowerCase());
if (start == null || end == null) {
OpenSignalsMain.getLogger()
.warn("The signal '" + nextSignal + "' or the signal '" + nextSignal
+ "' doen't exists! " + "This config with filename '" + fileName
+ "' will be skiped!");
return;
}
final Map.Entry<Signal, Signal> pair = Maps.immutableEntry(start, end);
if (CHANGECONFIGS.containsKey(pair)) {
throw new LogicalParserException(
"A signalconfig with the signals [" + start.getSignalTypeName() + ", "
+ end.getSignalTypeName() + "] does alredy exists! '" + fileName
+ "' tried to register a chaneconfig for the same signalpair!");
}
final FunctionParsingInfo startInfo = new FunctionParsingInfo(start);
final FunctionParsingInfo endInfo = new FunctionParsingInfo(
LogicParser.UNIVERSAL_TRANSLATION_TABLE, end);
final List<ConfigProperty> properties = new ArrayList<>();

for (final Map.Entry<String, List<String>> entry : values.entrySet()) {

String valueToParse = entry.getKey().toLowerCase();
Predicate<Map<Class<?>, Object>> predicate = t -> true;

if (valueToParse.contains("map(") && savedPredicates != null
&& !savedPredicates.isEmpty()) {
final char[] chars = entry.getKey().toCharArray();
String names = "";
boolean readKey = false;
final StringBuilder builder = new StringBuilder();
String mapKey = "";
for (final char letter : chars) {

final String current = builder.append(letter).toString();
final boolean isOpenBracket = current.equals("(");
final boolean isCloseBracket = current.equals(")");
builder.setLength(0);

if (readKey) {
try {
if (isCloseBracket) {
valueToParse = valueToParse.replace("map(" + mapKey + ")",
"(" + savedPredicates.get(mapKey).toLowerCase() + ")");
names = "";
mapKey = "";
readKey = false;
continue;
}
mapKey += current;
continue;
} catch (final Exception e) {
OpenSignalsMain.exitMinecraftWithMessage(
"Something went wrong with the predicate saver in "
+ fileName + "! Did you used it correctly?");
}
mapKey += current;
}
if (current.equals("(") && names.equals("map")) {
readKey = true;
mapKey = "";
continue;
} catch (final Exception e) {
OpenSignalsMain.exitMinecraftWithMessage(
"Something went wrong with the predicate saver in " + fileName
+ "! Did you used it correctly?");
}
final boolean isBracket = isCloseBracket || isOpenBracket;
if (Character.isWhitespace(letter) || current.equals("!") || isBracket) {
names = "";
mapKey = "";
continue;
}
names += current;
}
if (current.equals("(") && names.equals("map")) {
readKey = true;
mapKey = "";
continue;
}
final boolean isBracket = isCloseBracket || isOpenBracket;
if (Character.isWhitespace(letter) || current.equals("!") || isBracket) {
names = "";
mapKey = "";
continue;
}
names += current;
}
}

if (valueToParse != null && !valueToParse.isEmpty()
&& !valueToParse.equalsIgnoreCase("true")) {
predicate = LogicParser.predicate(valueToParse, endInfo);
}
if (valueToParse != null && !valueToParse.isEmpty()
&& !valueToParse.equalsIgnoreCase("true")) {
predicate = LogicParser.predicate(valueToParse, endInfo);
}

final Map<SEProperty, String> propertiesToSet = new HashMap<>();
final Map<SEProperty, String> propertiesToSet = new HashMap<>();

for (final String value : entry.getValue()) {
for (final String value : entry.getValue()) {

final String[] valuetoChange = value.split("\\.");
final SEProperty property = (SEProperty) startInfo.getProperty(valuetoChange[0]);
propertiesToSet.put(property, valuetoChange[1]);
}
final String[] valuetoChange = value.split("\\.");
final SEProperty property = (SEProperty) startInfo
.getProperty(valuetoChange[0]);
propertiesToSet.put(property, valuetoChange[1]);
}

properties.add(new ConfigProperty(predicate, propertiesToSet));
properties.add(new ConfigProperty(predicate, propertiesToSet));

}
CHANGECONFIGS.put(pair, properties);
} catch (final Exception e) {
OpenSignalsMain.getLogger().error("There was a problem loading the ChangeConfig ["
+ fileName + "]! Please check the file!");
e.printStackTrace();
}
CHANGECONFIGS.put(pair, properties);
}

private static class ChangeConfigParserV2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,45 @@ public static void loadOneSignalNonPredicateConfig(final Map<Signal, List<Config
final OneSignalNonPredicateConfigParserV2 parser = GSON.fromJson(files.getValue(),
OneSignalNonPredicateConfigParserV2.class);
for (final String currentSignal : parser.currentSignals) {
loadConfig(map, files.getKey(), currentSignal, parser.values);
loadConfig(map, files.getKey(), currentSignal, parser.values, path);
}
} catch (final Exception e) {
OpenSignalsMain.getLogger().error("Please update your config [" + files.getKey()
+ "] located in [" + path + "]!");
final OneSignalNonPredicateConfigParser parser = GSON.fromJson(files.getValue(),
OneSignalNonPredicateConfigParser.class);
loadConfig(map, files.getKey(), parser.currentSignal, parser.values);
loadConfig(map, files.getKey(), parser.currentSignal, parser.values, path);
}

}

private static void loadConfig(final Map<Signal, List<ConfigProperty>> map,
final String fileName, final String currentSignal, final List<String> values) {
final Signal signal = checkSignal(currentSignal, fileName);
if (signal == null)
return;

if (map.containsKey(signal)) {
throw new LogicalParserException("A signalconfig with the signals ["
+ signal.getSignalTypeName() + "] does alredy exists! '" + fileName
+ "' tried to register the same signalconfig!");
}
final String fileName, final String currentSignal, final List<String> values,
final String path) {
try {
final Signal signal = checkSignal(currentSignal, fileName);
if (signal == null)
return;

if (map.containsKey(signal)) {
throw new LogicalParserException("A signalconfig with the signals ["
+ signal.getSignalTypeName() + "] does alredy exists! '" + fileName
+ "' tried to register the same signalconfig!");
}

final FunctionParsingInfo info = new FunctionParsingInfo(signal);
final List<ConfigProperty> propertes = new ArrayList<>();
for (final String property : values) {
final String[] value = property.split("\\.");
propertes.add(new ConfigProperty(t -> true,
ImmutableMap.of((SEProperty) info.getProperty(value[0]), value[1])));
final FunctionParsingInfo info = new FunctionParsingInfo(signal);
final List<ConfigProperty> propertes = new ArrayList<>();
for (final String property : values) {
final String[] value = property.split("\\.");
propertes.add(new ConfigProperty(t -> true,
ImmutableMap.of((SEProperty) info.getProperty(value[0]), value[1])));
}
map.put(signal, propertes);
} catch (final Exception e) {
OpenSignalsMain.getLogger().error("There was a problem loading the config [" + fileName
+ "] located in [" + path + "]! Please check the file!");
e.printStackTrace();
}
map.put(signal, propertes);
}

private static Signal checkSignal(final String signalName, final String filename) {
Expand Down
Loading

0 comments on commit 9438ecf

Please sign in to comment.