mirror of
https://github.com/darksaid98/Flashback.git
synced 2026-12-01 22:16:01 +00:00
Stuff
This commit is contained in:
1 parent
9c4a30d224
commit
666398e480
174 files changed
+8694
-2942
No files matched your search
@@ -0,0 +1,112 @@
|
||||
package com.moulberry.flashback.state;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.moulberry.flashback.FlashbackGson;
|
||||
import com.moulberry.flashback.keyframe.Keyframe;
|
||||
import com.moulberry.flashback.keyframe.KeyframeType;
|
||||
import com.moulberry.flashback.keyframe.impl.CameraKeyframe;
|
||||
import com.moulberry.flashback.keyframe.impl.FOVKeyframe;
|
||||
import com.moulberry.flashback.keyframe.impl.TickrateKeyframe;
|
||||
import com.moulberry.flashback.keyframe.impl.TimeOfDayKeyframe;
|
||||
import com.moulberry.flashback.serialization.QuaternionTypeAdapater;
|
||||
import com.moulberry.flashback.serialization.Vector3fTypeAdapater;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.*;
|
||||
|
||||
public class EditorState {
|
||||
|
||||
public transient boolean dirty = false;
|
||||
|
||||
public final List<KeyframeTrack> keyframeTracks = new ArrayList<>();
|
||||
|
||||
public float zoomMin = 0.0f;
|
||||
public float zoomMax = 1.0f;
|
||||
public int exportStartTicks = -1;
|
||||
public int exportEndTicks = -1;
|
||||
|
||||
public void setExportTicks(int start, int end, int totalTicks) {
|
||||
if (this.exportEndTicks < 0) {
|
||||
this.exportEndTicks = totalTicks;
|
||||
}
|
||||
this.exportStartTicks = Math.max(0, Math.min(totalTicks, this.exportStartTicks));
|
||||
this.exportEndTicks = Math.max(0, Math.min(totalTicks, this.exportEndTicks));
|
||||
|
||||
if (start >= 0) {
|
||||
this.exportStartTicks = start;
|
||||
if (this.exportEndTicks < start) {
|
||||
this.exportEndTicks = start;
|
||||
}
|
||||
}
|
||||
|
||||
if (end >= 0) {
|
||||
this.exportEndTicks = end;
|
||||
if (this.exportStartTicks > end) {
|
||||
this.exportStartTicks = end;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.exportStartTicks == 0 && this.exportEndTicks == totalTicks) {
|
||||
this.exportStartTicks = -1;
|
||||
this.exportEndTicks = -1;
|
||||
}
|
||||
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
public void save(Path path) {
|
||||
this.dirty = false;
|
||||
|
||||
String serialized = FlashbackGson.PRETTY.toJson(this, EditorState.class);
|
||||
|
||||
try {
|
||||
Files.createDirectories(path.getParent());
|
||||
Files.writeString(path, serialized, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE,
|
||||
StandardOpenOption.SYNC);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static EditorState load(Path path) {
|
||||
if (!Files.exists(path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
String serialized = Files.readString(path);
|
||||
return FlashbackGson.PRETTY.fromJson(serialized, EditorState.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void applyKeyframes(T t, float tick) {
|
||||
EnumSet<KeyframeType> applied = EnumSet.noneOf(KeyframeType.class);
|
||||
for (KeyframeTrack keyframeTrack : this.keyframeTracks) {
|
||||
// Ignore lines that are disabled
|
||||
if (!keyframeTrack.enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Already applied a keyframe of this type earlier, skip
|
||||
if (applied.contains(keyframeTrack.keyframeType)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Try to apply keyframes, mark applied if successful
|
||||
if (keyframeTrack.tryApplyKeyframes(t, tick)) {
|
||||
applied.add(keyframeTrack.keyframeType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.moulberry.flashback.state;
|
||||
|
||||
import com.moulberry.flashback.Flashback;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class EditorStateCache {
|
||||
|
||||
private static long AUTOSAVE_INTERVAL_MILLIS = 30 * 1000; // 30 seconds
|
||||
|
||||
private static final ReentrantLock lock = new ReentrantLock();
|
||||
private static UUID currentUuid = null;
|
||||
private static EditorState current = null;
|
||||
private static long lastSave = 0;
|
||||
|
||||
public static void saveIfNeeded() {
|
||||
try {
|
||||
lock.lock();
|
||||
|
||||
if (current == null || currentUuid == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
if (!current.dirty) {
|
||||
lastSave = currentTime;
|
||||
} else if (currentTime < lastSave || currentTime - lastSave > AUTOSAVE_INTERVAL_MILLIS) {
|
||||
save();
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void save() {
|
||||
if (current == null || currentUuid == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Path normalPath = getPath(currentUuid, false);
|
||||
Path oldPath = getPath(currentUuid, true);
|
||||
|
||||
// Backup
|
||||
if (Files.exists(normalPath)) {
|
||||
try {
|
||||
Files.move(normalPath, oldPath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException ignored) {}
|
||||
}
|
||||
|
||||
// Save
|
||||
current.save(normalPath);
|
||||
lastSave = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private static void load() {
|
||||
lastSave = System.currentTimeMillis();
|
||||
|
||||
Path normalPath = getPath(currentUuid, false);
|
||||
Path backupPath = getPath(currentUuid, true);
|
||||
|
||||
if (Files.exists(normalPath)) {
|
||||
current = EditorState.load(normalPath);
|
||||
if (current == null) {
|
||||
try {
|
||||
Files.deleteIfExists(normalPath);
|
||||
} catch (IOException ignored) {}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (Files.exists(backupPath)) {
|
||||
current = EditorState.load(backupPath);
|
||||
if (current == null) {
|
||||
try {
|
||||
Files.deleteIfExists(backupPath);
|
||||
} catch (IOException ignored) {}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
current = new EditorState();
|
||||
}
|
||||
|
||||
public static void reset() {
|
||||
try {
|
||||
lock.lock();
|
||||
|
||||
save();
|
||||
current = null;
|
||||
currentUuid = null;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static EditorState getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public static EditorState get(UUID replayUuid) {
|
||||
Objects.requireNonNull(replayUuid);
|
||||
try {
|
||||
lock.lock();
|
||||
|
||||
if (current == null || !Objects.equals(currentUuid, replayUuid)) {
|
||||
save();
|
||||
currentUuid = replayUuid;
|
||||
load();
|
||||
}
|
||||
|
||||
return current;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private static Path getPath(UUID replayUuid, boolean old) {
|
||||
Objects.requireNonNull(replayUuid);
|
||||
|
||||
String filename = replayUuid + ".json";
|
||||
if (old) {
|
||||
filename += ".old";
|
||||
}
|
||||
return Flashback.getConfigDirectory()
|
||||
.resolve("replay_states")
|
||||
.resolve(filename);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.moulberry.flashback.state;
|
||||
|
||||
import com.moulberry.flashback.keyframe.Keyframe;
|
||||
import com.moulberry.flashback.keyframe.KeyframeType;
|
||||
import com.moulberry.flashback.keyframe.interpolation.SidedInterpolationType;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class KeyframeTrack {
|
||||
|
||||
public final KeyframeType keyframeType;
|
||||
public TreeMap<Integer, Keyframe<?>> keyframesByTick = new TreeMap<>();
|
||||
public boolean enabled = true;
|
||||
|
||||
public KeyframeTrack(KeyframeType keyframeType) {
|
||||
this.keyframeType = keyframeType;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> boolean tryApplyKeyframes(T t, float tick) {
|
||||
// Skip if empty
|
||||
TreeMap<Integer, Keyframe<?>> keyframeTimes = this.keyframesByTick;
|
||||
if (keyframeTimes.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Map.Entry<Integer, Keyframe<?>> lowerEntry = keyframeTimes.floorEntry((int) tick);
|
||||
if (lowerEntry == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (lowerEntry.getValue().operandClass != t.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Keyframe<T> lowerKeyframe = (Keyframe<T>) lowerEntry.getValue();
|
||||
|
||||
SidedInterpolationType leftInterpolation = lowerEntry.getValue().interpolationType().rightSide;
|
||||
|
||||
// Immediately apply hold
|
||||
if (leftInterpolation == SidedInterpolationType.HOLD) {
|
||||
lowerKeyframe.apply(t);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get next entry, skip if tick is not between two keyframes
|
||||
Map.Entry<Integer, Keyframe<?>> ceilEntry = keyframeTimes.ceilingEntry(lowerEntry.getKey() + 1);
|
||||
if (ceilEntry == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SidedInterpolationType rightInterpolation = ceilEntry.getValue().interpolationType().leftSide;
|
||||
if (rightInterpolation == SidedInterpolationType.HOLD) {
|
||||
rightInterpolation = leftInterpolation;
|
||||
}
|
||||
|
||||
float amount = (tick - lowerEntry.getKey()) / (ceilEntry.getKey() - lowerEntry.getKey());
|
||||
|
||||
boolean isUsingSmooth = leftInterpolation == SidedInterpolationType.SMOOTH ||
|
||||
rightInterpolation == SidedInterpolationType.SMOOTH;
|
||||
|
||||
if (isUsingSmooth) {
|
||||
Map.Entry<Integer, Keyframe<?>> beforeEntry = keyframeTimes.floorEntry(lowerEntry.getKey() - 1);
|
||||
if (beforeEntry == null) {
|
||||
beforeEntry = lowerEntry;
|
||||
}
|
||||
|
||||
Map.Entry<Integer, Keyframe<?>> afterAfterEntry = keyframeTimes.ceilingEntry(ceilEntry.getKey() + 1);
|
||||
if (afterAfterEntry == null) {
|
||||
afterAfterEntry = ceilEntry;
|
||||
}
|
||||
|
||||
float lerpAmount;
|
||||
boolean lerpFromRight;
|
||||
if (leftInterpolation != SidedInterpolationType.SMOOTH) {
|
||||
lerpAmount = SidedInterpolationType.interpolate(leftInterpolation, leftInterpolation, amount);
|
||||
lerpFromRight = false;
|
||||
} else if (rightInterpolation != SidedInterpolationType.SMOOTH) {
|
||||
lerpAmount = SidedInterpolationType.interpolate(rightInterpolation, rightInterpolation, amount);
|
||||
lerpFromRight = true;
|
||||
} else {
|
||||
lerpAmount = -1;
|
||||
lerpFromRight = false;
|
||||
}
|
||||
|
||||
((Keyframe<T>)beforeEntry.getValue()).applyInterpolatedSmooth(t, lowerEntry.getValue(), ceilEntry.getValue(), afterAfterEntry.getValue(),
|
||||
beforeEntry.getKey(), lowerEntry.getKey(), ceilEntry.getKey(), afterAfterEntry.getKey(), amount, lerpAmount, lerpFromRight);
|
||||
} else {
|
||||
amount = SidedInterpolationType.interpolate(leftInterpolation, rightInterpolation, amount);
|
||||
|
||||
if (amount == 0.0) {
|
||||
lowerKeyframe.apply(t);
|
||||
} else {
|
||||
lowerKeyframe.applyInterpolated(t, ceilEntry.getValue(), amount);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user