Files
integration/Source/Integration/AnimQueue.cpp

230 lines
5.6 KiB
C++
Raw Normal View History

2023-09-08 05:38:09 -04:00
#include "AnimQueue.h"
2023-09-15 13:28:18 -04:00
FlxAnimStep FlxAnimQueueDecoder::ReadStep() {
FlxAnimStep result;
2023-09-08 05:38:09 -04:00
result.Hash = Decoder.read_uint64();
result.Body = Decoder.read_string_view();
return result;
}
2023-09-15 13:28:18 -04:00
FlxAnimField FlxAnimStepDecoder::ReadField() {
FlxAnimField result;
2023-09-08 05:38:09 -04:00
result.Name = Decoder.read_string_view();
result.Persistent = Decoder.read_bool();
2023-09-15 13:28:18 -04:00
result.Type = (ElxAnimValueType)Decoder.read_uint8();
2023-09-08 05:38:09 -04:00
switch (result.Type) {
case T_STRING: {
result.S = Decoder.read_string_view();
break;
}
case T_NUMBER: {
result.X = Decoder.read_double();
break;
}
case T_BOOLEAN: {
result.X = Decoder.read_bool() ? 1.0 : 0.0;
break;
}
case T_XYZ: {
result.X = Decoder.read_double();
result.Y = Decoder.read_double();
result.Z = Decoder.read_double();
break;
}
default: {
Decoder.set_at_eof();
result.Type = T_BOOLEAN;
result.X = 0;
break;
}
}
return result;
}
2023-09-15 13:28:18 -04:00
FString FlxAnimQueueDecoder::DebugString(std::string_view queue) {
FString result;
2023-09-15 13:28:18 -04:00
FlxAnimQueueDecoder decoder(queue);
while (!decoder.AtEOF()) {
2023-09-15 13:28:18 -04:00
FlxAnimStep step = decoder.ReadStep();
FString stepdebug = FlxAnimStepDecoder::DebugString(step);
result.Appendf(TEXT("%s\n"), *stepdebug);
}
return result;
2023-09-08 05:38:09 -04:00
}
2023-09-15 13:28:18 -04:00
FString FlxAnimStepDecoder::DebugString(const FlxAnimStep& step) {
FString result;
2023-09-15 13:28:18 -04:00
FlxAnimStepDecoder decoder(step);
bool first = true;
while (!decoder.AtEOF()) {
2023-09-15 13:28:18 -04:00
FlxAnimField field = decoder.ReadField();
if (!first) {
result.Append(TEXT(" "));
}
result.Append(FString(field.Name.size(), (const UTF8CHAR*)field.Name.data()));
result.Append(field.Persistent ? TEXT("=") : TEXT(":"));
switch (field.Type) {
2023-09-15 13:28:18 -04:00
case ElxAnimValueType::T_STRING:
result.Append(FString(field.S.size(), (const UTF8CHAR*)field.S.data()));
break;
2023-09-15 13:28:18 -04:00
case ElxAnimValueType::T_NUMBER:
result.Appendf(TEXT("%lf"), field.X);
break;
2023-09-15 13:28:18 -04:00
case ElxAnimValueType::T_BOOLEAN:
result.Append((field.X) == 1.0 ? TEXT("true") : TEXT("false"));
break;
2023-09-15 13:28:18 -04:00
case ElxAnimValueType::T_XYZ:
result.Appendf(TEXT("%lf,%lf,%lf"), field.X, field.Y, field.Z);
break;
}
first = false;
}
return result;
2023-09-08 05:38:09 -04:00
}
2023-09-15 00:01:41 -04:00
//// Our own copy of the animation queue. We only
//// store the hashes, not the steps. The First element
//// of the queue is the oldest item.
////
2023-09-15 13:28:18 -04:00
//TDeque<FlxAnimStoredStep> AQ;
2023-09-15 00:01:41 -04:00
//// The sequence number of the first item in AQ.
////
//int32 FirstSeqno;
//// Map from hash to sequence number.
////
//TMap<uint64, int32> HashToSeqno;
//// The sequence number of the first unstarted animation.
////
//int32 UnstartedSeqno;
//// Array of recently-aborted hash values.
//TArray<uint64> AbortedHashes;
2023-09-15 13:28:18 -04:00
FlxAnimTracker::FlxAnimTracker() {
2023-09-15 00:01:41 -04:00
AQ.Empty();
FirstSeqno = 0;
HashToSeqno.Empty();
UnstartedSeqno = 0;
AbortedHashes.Empty();
}
2023-09-15 13:28:18 -04:00
void FlxAnimTracker::Update(std::string_view encqueue) {
2023-09-15 00:01:41 -04:00
// Check for an exact match. If the most recent entry
// in encqueue has the same hash as the most recent
// entry in AQ, then that's an exact match. Or,
// if both are empty, that's also an exact match.
// In case of exact match, abort early, there's no
// further work needed.
//
if (encqueue.empty()) {
if (AQ.IsEmpty()) {
return;
}
} else {
if (!AQ.IsEmpty()) {
2023-09-15 13:28:18 -04:00
FlxStringDecoder qdecoder(encqueue);
2023-09-15 00:01:41 -04:00
uint64 hash = qdecoder.read_uint64();
if (hash == AQ.Last().Hash) {
return;
}
}
}
// Search for a Luprex animation record that matches
// something that we already have. Yields the sequence
// number of the most recent matching record.
//
// At the same time, push all non-matching records
// after the matching record onto a stack of new records.
//
2023-09-15 13:28:18 -04:00
FlxAnimQueueDecoder decoder(encqueue);
TArray<FlxAnimStep> newsteps;
2023-09-15 00:01:41 -04:00
int32 matchingseqno = -1;
while (!decoder.AtEOF()) {
2023-09-15 13:28:18 -04:00
FlxAnimStep step = decoder.ReadStep();
2023-09-15 00:01:41 -04:00
int32* stepseq = HashToSeqno.Find(step.Hash);
if (stepseq == nullptr) {
newsteps.Emplace(step);
} else {
matchingseqno = *stepseq;
break;
}
}
// Abort all animations after the most recent matching
// record. Animations are aborted in most-recent-first order.
//
int32 nabort = (FirstSeqno + AQ.Num()) - (matchingseqno + 1);
check((nabort >= 0) && (nabort <= AQ.Num()));
for (int32 i = 0; i < nabort; i++) {
uint64 lasthash = AQ.Last().Hash;
HashToSeqno.Remove(lasthash);
AbortedHashes.Emplace(lasthash);
AQ.PopLast();
}
// Transfer the new animations onto the queue.
//
while (!newsteps.IsEmpty()) {
2023-09-15 13:28:18 -04:00
FlxAnimStep step = newsteps.Pop();
2023-09-15 00:01:41 -04:00
int32 seqno = FirstSeqno + AQ.Num();
AQ.EmplaceLast(step.Hash, step.Body);
HashToSeqno.Emplace(step.Hash, seqno);
}
// Move back the Unstarted pointer, so that the
// unstarted range includes all the new animations.
//
if (UnstartedSeqno > matchingseqno + 1) {
UnstartedSeqno = matchingseqno + 1;
}
// If there are too many animations in AQ, discard
// any very old ones.
//
if (AQ.Num() > 10) {
int ndiscard = AQ.Num() - 10;
for (int i = 0; i < ndiscard; i++) {
uint64 hash = AQ.First().Hash;
HashToSeqno.Remove(hash);
AQ.PopFirst();
}
FirstSeqno += ndiscard;
if (UnstartedSeqno < FirstSeqno) {
UnstartedSeqno = FirstSeqno;
}
}
}
2023-09-15 13:28:18 -04:00
TArray<uint64> FlxAnimTracker::GetAborted() {
2023-09-15 00:01:41 -04:00
TArray<uint64> result;
result = std::move(AbortedHashes);
AbortedHashes.Empty();
return result;
}
2023-09-15 13:28:18 -04:00
bool FlxAnimTracker::AnyUnstarted() {
2023-09-15 00:01:41 -04:00
int offset = UnstartedSeqno - FirstSeqno;
return (offset < AQ.Num());
}
2023-09-15 13:28:18 -04:00
FlxAnimStoredStep FlxAnimTracker::GetUnstarted() {
2023-09-15 00:01:41 -04:00
int offset = UnstartedSeqno - FirstSeqno;
check(offset < AQ.Num());
return AQ[offset];
}
2023-09-15 13:28:18 -04:00
void FlxAnimTracker::Started(uint64 hash) {
2023-09-15 00:01:41 -04:00
int offset = UnstartedSeqno - FirstSeqno;
check(offset < AQ.Num());
check(AQ[offset].Hash == hash);
UnstartedSeqno += 1;
}