Skip to content

Commit

Permalink
Timeline translation and export fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xpdota committed Dec 14, 2023
1 parent fdcc1ce commit 0525ae2
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.xp.xivsupport.timelines;

import gg.xp.reevent.events.Event;
import gg.xp.xivsupport.timelines.intl.LanguageReplacements;

public interface EventSyncController {
boolean shouldSync(Event event);
Expand All @@ -10,4 +11,8 @@ public interface EventSyncController {
default boolean isEditable() {
return false;
};

EventSyncController translateWith(LanguageReplacements replacements);

String toTextFormat();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gg.xp.xivsupport.timelines;

import gg.xp.reevent.events.Event;
import gg.xp.xivsupport.timelines.cbevents.CbEventFmt;
import gg.xp.xivsupport.timelines.intl.LanguageReplacements;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -10,11 +12,13 @@ public class FileEventSyncController implements EventSyncController {

private final Class<? extends Event> eventType;
private final Predicate<Event> predicate;
private final String originalType;
private final Map<String, String> original;

public FileEventSyncController(Class<? extends Event> eventType, Predicate<Event> predicate, Map<String, String> original) {
public FileEventSyncController(Class<? extends Event> eventType, Predicate<Event> predicate, String originalType, Map<String, String> original) {
this.eventType = eventType;
this.predicate = predicate;
this.originalType = originalType;
this.original = new HashMap<>(original);
}

Expand All @@ -28,6 +32,26 @@ public Class<? extends Event> eventType() {
return eventType;
}

@Override
public EventSyncController translateWith(LanguageReplacements replacements) {
Map<String, String> modified = new HashMap<>(original);
modified.replaceAll((key, val) -> {
for (var syncReplacement : replacements.replaceSync().entrySet()) {
val = syncReplacement.getKey().matcher(val).replaceAll(syncReplacement.getValue());
}
return val;
});
if (modified.equals(original)) {
return this;
}
return CbEventFmt.parse(originalType, modified);
}

@Override
public String toTextFormat() {
return CbEventFmt.format(this.originalType, this.original);
}

@Override
public String toString() {
return eventType.getSimpleName() + original;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ default String toTextFormat() {
if (sync() != null) {
sb.append("sync /").append(sync().pattern()).append('/').append(' ');
}
if (eventSyncController() != null) {
sb.append(eventSyncController().toTextFormat()).append(' ');
}
TimelineWindow window = timelineWindow();
if (!TimelineWindow.DEFAULT.equals(window)) {
sb.append("window ").append(fmtDouble(window.start())).append(',').append(fmtDouble(window.end())).append(' ');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package gg.xp.xivsupport.timelines;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import gg.xp.reevent.events.Event;
import gg.xp.xivsupport.timelines.cbevents.CbEventTypes;
import gg.xp.xivsupport.timelines.cbevents.CbEventFmt;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -38,16 +30,6 @@ public final class TimelineParser {
);

private static final Pattern timelineLabelPattern = Pattern.compile("^(?<time>\\d+\\.?\\d*) label \"(?<label>[^\"]*)\"");
private static final ObjectMapper mapper = JsonMapper.builder()
// get as close as possible to json5
.configure(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonReadFeature.ALLOW_TRAILING_COMMA, true)
.configure(JsonReadFeature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true)
.configure(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS, true)
.configure(JsonReadFeature.ALLOW_JAVA_COMMENTS, true)
.configure(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS, true)
.build();

private TimelineParser() {
}
Expand Down Expand Up @@ -96,19 +78,7 @@ public static List<TimelineEntry> parseMultiple(Collection<String> line) {
if (syncRaw == null) {
sync = null;
if (eventTypeRaw != null) {
CbEventTypes eventDef = CbEventTypes.valueOf(eventTypeRaw);
String eventCondRaw = matcher.group("eventCond");
// TODO: support translation for this
Map<String, String> conditions;
try {
conditions = mapper.readValue(eventCondRaw, new TypeReference<>() {
});
}
catch (JsonProcessingException e) {
throw new RuntimeException("Error reading JSON: " + eventCondRaw, e);
}
Predicate<Event> condition = eventDef.make(conditions);
esc = new FileEventSyncController(eventDef.eventType(), condition, conditions);
esc = CbEventFmt.parseRaw(eventTypeRaw, matcher.group("eventCond"));
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,16 @@ public static TimelineProcessor of(TimelineManager manager, InputStream file, Li
TimelineEntry currentItem = all.get(i);
final String originalName = currentItem.name();
final String originalSync = currentItem.sync() == null ? null : currentItem.sync().pattern();
String newName = originalName;
String newSync = originalSync;
if (originalName != null) {
for (var textReplacement : replacements.replaceText().entrySet()) {
newName = textReplacement.getKey().matcher(newName).replaceAll(textReplacement.getValue());
}
}
if (originalSync != null) {
for (var syncReplacement : replacements.replaceSync().entrySet()) {
newSync = syncReplacement.getKey().matcher(newSync).replaceAll(syncReplacement.getValue());
}
final EventSyncController originalEsc = currentItem.eventSyncController();
final String newName = originalName == null ? null : replacements.doNameReplacement(originalName);
final String newSync = originalSync == null ? null : replacements.doSyncReplacement(originalSync);
EventSyncController newEsc = originalEsc;
if (originalEsc != null) {
newEsc = originalEsc.translateWith(replacements);
}
if (!Objects.equals(originalName, newName) || !Objects.equals(originalSync, newSync)) {
if (!Objects.equals(originalName, newName) || !Objects.equals(originalSync, newSync) || !Objects.equals(originalEsc, newEsc)) {
Pattern newSyncFinal = newSync == null ? null : Pattern.compile(newSync);
all.set(i, new TranslatedTextFileEntry(currentItem, newName, newSyncFinal));
all.set(i, new TranslatedTextFileEntry(currentItem, newName, newSyncFinal, newEsc));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
public class TranslatedTextFileEntry implements TimelineEntry {

private final TimelineEntry untranslated;
private final String nameOverride;
private final Pattern syncOverride;
private final @Nullable String nameOverride;
private final @Nullable Pattern syncOverride;
private final @Nullable EventSyncController esc;

public TranslatedTextFileEntry(TimelineEntry untranslated, @Nullable String nameOverride, @Nullable Pattern syncOverride) {
public TranslatedTextFileEntry(TimelineEntry untranslated, @Nullable String nameOverride, @Nullable Pattern syncOverride, @Nullable EventSyncController esc) {
this.untranslated = untranslated;
this.nameOverride = nameOverride;
this.syncOverride = syncOverride;
this.esc = esc;
}

@Override
Expand All @@ -34,7 +36,7 @@ public double getMaxTime() {

@Override
public @Nullable EventSyncController eventSyncController() {
return untranslated().eventSyncController();
return esc ;
}

@Override
Expand All @@ -57,10 +59,11 @@ public boolean forceJump() {
@Override
public String toString() {
return "TranslatedTextFileEntry{" +
"nameOverride='" + nameOverride +
"', syncOverride='" + syncOverride +
"', untranslated='" + untranslated +
"'}";
"nameOverride='" + nameOverride +
"', syncOverride='" + syncOverride +
"', untranslated='" + untranslated +
"', syncControl='" + esc +
"'}";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gg.xp.xivsupport.timelines.cbevents;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import gg.xp.reevent.events.Event;
import gg.xp.xivsupport.timelines.EventSyncController;
import gg.xp.xivsupport.timelines.FileEventSyncController;

import java.util.Map;
import java.util.function.Predicate;

public final class CbEventFmt {

private static final ObjectMapper mapper = JsonMapper.builder()
// get as close as possible to json5
.configure(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonReadFeature.ALLOW_TRAILING_COMMA, true)
.configure(JsonReadFeature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true)
.configure(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS, true)
.configure(JsonReadFeature.ALLOW_JAVA_COMMENTS, true)
.configure(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS, true)
.configure(JsonWriteFeature.QUOTE_FIELD_NAMES, false)
.build();

private CbEventFmt() {
}

public static EventSyncController parseRaw(String type, String conditionsRaw) {
CbEventTypes eventDef = CbEventTypes.valueOf(type);
Map<String, String> conditions;
try {
conditions = mapper.readValue(conditionsRaw, new TypeReference<>() {
});
}
catch (JsonProcessingException e) {
throw new RuntimeException("Error reading JSON: " + conditionsRaw, e);
}
Predicate<Event> condition = eventDef.make(conditions);
return new FileEventSyncController(eventDef.eventType(), condition, type, conditions);
}

public static EventSyncController parse(String type, Map<String, String> conditions) {
CbEventTypes eventDef = CbEventTypes.valueOf(type);
Predicate<Event> condition = eventDef.make(conditions);
return new FileEventSyncController(eventDef.eventType(), condition, type, conditions);
}

public static String format(String originalType, Map<String, String> originalValues) {
try {
return String.format("%s %s", originalType, mapper.writeValueAsString(originalValues));
}
catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ public class TimelinesTab extends TitleBorderFullsizePanel implements PluginTab
private TimelineProcessor currentTimeline;
private TimelineCustomizations currentCust;

private TableCellEditor noLabelEdit(TableCellEditor wrapped) {
return new RowConditionalTableCellEditor<TimelineEntry>(wrapped, item -> !item.isLabel());
}

public TimelinesTab(TimelineManager backend, TimelineOverlay overlay, XivState state, ActionTableFactory actionTableFactory, TimelineBarColorProviderImpl tbcp) {
super("Timelines");
// TODO: searching
Expand Down Expand Up @@ -162,8 +158,16 @@ public TimelinesTab(TimelineManager backend, TimelineOverlay overlay, XivState s
.addColumn(new CustomColumn<>("Text/Name", TimelineEntry::name, col -> {
col.setCellEditor(StandardColumns.stringEditorEmptyToNull(safeEditTimelineEntry(false, (item, value) -> item.name = value, (item, value) -> item.name = value)));
}))
.addColumn(new CustomColumn<>("Pattern", TimelineEntry::sync, col -> {
col.setCellEditor(noLabelEdit(StandardColumns.regexEditorEmptyToNull(safeEditTimelineEntry(false, (item, value) -> item.sync = value), Pattern.CASE_INSENSITIVE)));
.addColumn(new CustomColumn<>("Sync", e -> {
if (e.sync() != null) {
return e.sync();
}
if (e.eventSyncController() != null) {
return e.eventSyncController().toString();
}
return null;
}, col -> {
col.setCellEditor(noLabelNoNewSyncEdit(StandardColumns.regexEditorEmptyToNull(safeEditTimelineEntry(false, (item, value) -> item.sync = value), Pattern.CASE_INSENSITIVE)));
}))
.addColumn(new CustomColumn<>("Duration", TimelineEntry::duration, col -> {
col.setCellEditor(noLabelEdit(StandardColumns.doubleEditorEmptyToNull(safeEditTimelineEntry(false, (item, value) -> item.duration = value))));
Expand Down Expand Up @@ -972,4 +976,13 @@ public boolean isCellEditable(EventObject anEvent) {
}

}

private TableCellEditor noLabelNoNewSyncEdit(TableCellEditor wrapped) {
return new RowConditionalTableCellEditor<TimelineEntry>(wrapped, item -> !item.isLabel() && !item.hasEventSync());
}

private TableCellEditor noLabelEdit(TableCellEditor wrapped) {
return new RowConditionalTableCellEditor<TimelineEntry>(wrapped, item -> !item.isLabel());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -36,6 +37,20 @@ public Map<String, String> sortedReplaceText() {
return replaceText.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().pattern(), e -> e.getValue()));
}

public @NotNull String doNameReplacement(@NotNull String input) {
for (var nameReplacement : replaceText().entrySet()) {
input = nameReplacement.getKey().matcher(input).replaceAll(nameReplacement.getValue());
}
return input;
}

public @NotNull String doSyncReplacement(@NotNull String input) {
for (var syncReplacement : replaceSync().entrySet()) {
input = syncReplacement.getKey().matcher(input).replaceAll(syncReplacement.getValue());
}
return input;
}

public static LanguageReplacements empty() {
return new LanguageReplacements(Collections.emptyMap(), Collections.emptyMap());
}
Expand Down

0 comments on commit 0525ae2

Please sign in to comment.