2023-09-08 05:38:09 -04:00
|
|
|
|
|
|
|
|
#include "AnimQueue.h"
|
|
|
|
|
|
2023-09-19 22:08:15 -04:00
|
|
|
FlxAnimationStep::FlxAnimationStep(uint64 hash, std::string_view body) {
|
2023-10-02 15:48:42 -04:00
|
|
|
Finished = false;
|
2023-09-19 22:08:15 -04:00
|
|
|
Hash = hash;
|
|
|
|
|
Body.SetNum(body.size());
|
|
|
|
|
memcpy(Body.GetData(), body.data(), body.size());
|
2023-10-02 15:48:42 -04:00
|
|
|
Blueprint = UlxAnimationStepLibrary::AnimationStepGetString(*this, "bp");
|
2023-09-08 05:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
2023-09-19 22:08:15 -04:00
|
|
|
static bool ClearProperties(const FString& prefix, UObject* obj) {
|
2023-09-18 19:24:52 -04:00
|
|
|
UClass* uclass = obj->GetClass();
|
|
|
|
|
if (prefix.IsEmpty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
FName prefixlo(prefix);
|
|
|
|
|
FName prefixhi(prefix + TEXT("\xFFFF"));
|
|
|
|
|
for (TFieldIterator<FProperty> It(uclass); It; ++It)
|
|
|
|
|
{
|
|
|
|
|
FProperty* fprop = *It;
|
|
|
|
|
bool match1 = (fprop->GetFName().Compare(prefixlo) > 0);
|
|
|
|
|
bool match2 = (fprop->GetFName().Compare(prefixhi) < 0);
|
|
|
|
|
if (match1 && match2) {
|
|
|
|
|
uint8* pptr = fprop->ContainerPtrToValuePtr<uint8>(obj);
|
|
|
|
|
fprop->ClearValue(pptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 22:08:15 -04:00
|
|
|
static bool SetProperty(const FString& name, UObject* obj, const FlxAnimationField& field) {
|
2023-09-18 19:24:52 -04:00
|
|
|
UClass* uclass = obj->GetClass();
|
|
|
|
|
FName nname(name);
|
|
|
|
|
switch (field.Type) {
|
|
|
|
|
case ElxAnimValueType::STRING: {
|
|
|
|
|
FStrProperty* fprop = FindFProperty<FStrProperty>(uclass, nname);
|
|
|
|
|
if (fprop == nullptr) return false;
|
|
|
|
|
FString* pptr = fprop->ContainerPtrToValuePtr<FString>(obj);
|
|
|
|
|
*pptr = FString(field.S.size(), (const UTF8CHAR*)field.S.data());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case ElxAnimValueType::NUMBER: {
|
|
|
|
|
FDoubleProperty* fprop = FindFProperty<FDoubleProperty>(uclass, nname);
|
|
|
|
|
if (fprop == nullptr) return false;
|
|
|
|
|
double* pptr = fprop->ContainerPtrToValuePtr<double>(obj);
|
2023-09-25 14:25:24 -04:00
|
|
|
*pptr = field.X;
|
2023-09-18 19:24:52 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case ElxAnimValueType::BOOLEAN: {
|
|
|
|
|
FBoolProperty* fprop = FindFProperty<FBoolProperty>(uclass, nname);
|
|
|
|
|
if (fprop == nullptr) return false;
|
|
|
|
|
uint8* pptr = fprop->ContainerPtrToValuePtr<uint8>(obj);
|
|
|
|
|
fprop->SetPropertyValue(pptr, (field.X == 1.0));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case ElxAnimValueType::XYZ: {
|
|
|
|
|
FStructProperty* fprop = FindFProperty<FStructProperty>(uclass, nname);
|
|
|
|
|
if (fprop == nullptr) return false;
|
|
|
|
|
if (fprop->Struct != TBaseStructure<FVector>::Get()) return false;
|
|
|
|
|
FVector* pptr = fprop->ContainerPtrToValuePtr<FVector>(obj);
|
|
|
|
|
*pptr = FVector(field.X, field.Y, field.Z);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 22:08:15 -04:00
|
|
|
bool FlxAnimationStep::Unpack(const FString& prefix, UObject* into, bool preclear) const {
|
|
|
|
|
UClass* uclass = into->GetClass();
|
|
|
|
|
std::string_view body((const char*)(Body.GetData()), Body.Num());
|
|
|
|
|
FlxAnimationStepDecoder decoder(body);
|
2023-09-18 19:24:52 -04:00
|
|
|
bool ok = true;
|
|
|
|
|
if (preclear) {
|
2023-09-19 22:08:15 -04:00
|
|
|
ok &= ClearProperties(prefix, into);
|
2023-09-18 19:24:52 -04:00
|
|
|
}
|
|
|
|
|
while (!decoder.AtEOF()) {
|
2023-09-19 22:08:15 -04:00
|
|
|
FlxAnimationField field = decoder.ReadField();
|
2023-09-18 19:24:52 -04:00
|
|
|
FString sname(field.Name.size(), (const UTF8CHAR*)field.Name.data());
|
2023-09-19 22:08:15 -04:00
|
|
|
ok &= SetProperty(prefix + sname, into, field);
|
2023-09-18 19:24:52 -04:00
|
|
|
}
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
2023-09-15 00:01:41 -04:00
|
|
|
|
2023-09-19 22:08:15 -04:00
|
|
|
FString UlxAnimationStepLibrary::AnimationStepDebugString(const FlxAnimationStep& step) {
|
|
|
|
|
std::string_view body((const char*)(step.Body.GetData()), step.Body.Num());
|
|
|
|
|
return FlxAnimationStepDecoder::DebugString(step.Hash, body);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 15:48:42 -04:00
|
|
|
void UlxAnimationStepLibrary::UnpackAnimationStep(UObject* into, const FlxAnimationStep& step, const FString& prefix) {
|
2023-09-20 01:40:58 -04:00
|
|
|
step.Unpack(prefix, into, true);
|
2023-10-02 15:48:42 -04:00
|
|
|
}
|
2023-09-19 22:08:15 -04:00
|
|
|
|
2023-09-20 01:40:58 -04:00
|
|
|
static FlxAnimationField FindAnimationField(const FlxAnimationStep& step, const FString& name) {
|
|
|
|
|
std::string_view body((const char*)(step.Body.GetData()), step.Body.Num());
|
|
|
|
|
FTCHARToUTF8 utf8name(name);
|
|
|
|
|
std::string_view uname(utf8name.Get(), utf8name.Length());
|
|
|
|
|
FlxAnimationStepDecoder decoder(body);
|
|
|
|
|
FlxAnimationField result;
|
|
|
|
|
while (!decoder.AtEOF()) {
|
|
|
|
|
result = decoder.ReadField();
|
|
|
|
|
if (result.Name == uname) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.Type = ElxAnimValueType::INVALID;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 15:48:42 -04:00
|
|
|
bool UlxAnimationStepLibrary::AnimationStepIsIdle(const FlxAnimationStep &step) {
|
|
|
|
|
return step.Finished;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 01:40:58 -04:00
|
|
|
FVector UlxAnimationStepLibrary::AnimationStepGetVector(const FlxAnimationStep& step, const FString& name) {
|
|
|
|
|
FlxAnimationField field = FindAnimationField(step, name);
|
|
|
|
|
if (field.Type != ElxAnimValueType::XYZ) return FVector(0, 0, 0);
|
|
|
|
|
return FVector(field.X, field.Y, field.Z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double UlxAnimationStepLibrary::AnimationStepGetFloat(const FlxAnimationStep& step, const FString& name) {
|
|
|
|
|
FlxAnimationField field = FindAnimationField(step, name);
|
|
|
|
|
if (field.Type != ElxAnimValueType::NUMBER) return 0.0;
|
|
|
|
|
return field.X;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma optimize("", off)
|
|
|
|
|
FString UlxAnimationStepLibrary::AnimationStepGetString(const FlxAnimationStep& step, const FString& name) {
|
|
|
|
|
FlxAnimationField field = FindAnimationField(step, name);
|
|
|
|
|
if (field.Type != ElxAnimValueType::STRING) return TEXT("");
|
|
|
|
|
return FString(field.S.size(), (const UTF8CHAR*)(field.S.data()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UlxAnimationStepLibrary::AnimationStepGetBool(const FlxAnimationStep& step, const FString& name) {
|
|
|
|
|
FlxAnimationField field = FindAnimationField(step, name);
|
|
|
|
|
if (field.Type != ElxAnimValueType::BOOLEAN) return false;
|
|
|
|
|
return field.X == 1.0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 22:08:15 -04:00
|
|
|
FlxAnimationStepView FlxAnimQueueDecoder::ReadStep() {
|
|
|
|
|
FlxAnimationStepView result;
|
|
|
|
|
result.Hash = Decoder.read_uint64();
|
|
|
|
|
result.Body = Decoder.read_string_view();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 15:48:42 -04:00
|
|
|
uint64 FlxAnimQueueDecoder::PeekHash() {
|
|
|
|
|
std::string_view rest = Decoder.GetRest();
|
|
|
|
|
uint64 result = Decoder.read_uint64();
|
|
|
|
|
Decoder.Reset(rest, false);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 22:08:15 -04:00
|
|
|
FlxAnimationField FlxAnimationStepDecoder::ReadField() {
|
|
|
|
|
FlxAnimationField result;
|
|
|
|
|
result.Name = Decoder.read_string_view();
|
|
|
|
|
result.Persistent = Decoder.read_bool();
|
|
|
|
|
result.Type = (ElxAnimValueType)Decoder.read_uint8();
|
|
|
|
|
switch (result.Type) {
|
|
|
|
|
case ElxAnimValueType::STRING: {
|
|
|
|
|
result.S = Decoder.read_string_view();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ElxAnimValueType::NUMBER: {
|
|
|
|
|
result.X = Decoder.read_double();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ElxAnimValueType::BOOLEAN: {
|
|
|
|
|
result.X = Decoder.read_bool() ? 1.0 : 0.0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ElxAnimValueType::XYZ: {
|
|
|
|
|
result.X = Decoder.read_double();
|
|
|
|
|
result.Y = Decoder.read_double();
|
|
|
|
|
result.Z = Decoder.read_double();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
Decoder.set_at_eof();
|
|
|
|
|
result.Type = ElxAnimValueType::BOOLEAN;
|
|
|
|
|
result.X = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FString FlxAnimationStepDecoder::DebugString(uint64 hash, std::string_view body) {
|
|
|
|
|
FString result;
|
|
|
|
|
FlxAnimationStepDecoder decoder(body);
|
|
|
|
|
result.Appendf(TEXT("Hash=%016llx"), hash);
|
|
|
|
|
while (!decoder.AtEOF()) {
|
|
|
|
|
FlxAnimationField field = decoder.ReadField();
|
|
|
|
|
result.Append(TEXT(" "));
|
|
|
|
|
result.Append(FString(field.Name.size(), (const UTF8CHAR*)field.Name.data()));
|
|
|
|
|
result.Append(field.Persistent ? TEXT("=") : TEXT(":"));
|
|
|
|
|
switch (field.Type) {
|
|
|
|
|
case ElxAnimValueType::STRING:
|
|
|
|
|
result.Append(FString(field.S.size(), (const UTF8CHAR*)field.S.data()));
|
|
|
|
|
break;
|
|
|
|
|
case ElxAnimValueType::NUMBER:
|
|
|
|
|
result.Appendf(TEXT("%lf"), field.X);
|
|
|
|
|
break;
|
|
|
|
|
case ElxAnimValueType::BOOLEAN:
|
|
|
|
|
result.Append((field.X) == 1.0 ? TEXT("true") : TEXT("false"));
|
|
|
|
|
break;
|
|
|
|
|
case ElxAnimValueType::XYZ:
|
|
|
|
|
result.Appendf(TEXT("%lf,%lf,%lf"), field.X, field.Y, field.Z);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 15:48:42 -04:00
|
|
|
FlxAnimQueueDecoder::FlxAnimQueueDecoder(std::string_view queue) : Decoder(queue) {
|
|
|
|
|
SizeLimit = Decoder.read_uint8();
|
|
|
|
|
ActualSize = Decoder.read_uint8();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 22:08:15 -04:00
|
|
|
FString FlxAnimQueueDecoder::DebugString(std::string_view queue) {
|
|
|
|
|
FString result;
|
|
|
|
|
FlxAnimQueueDecoder decoder(queue);
|
|
|
|
|
while (!decoder.AtEOF()) {
|
|
|
|
|
FlxAnimationStepView step = decoder.ReadStep();
|
|
|
|
|
FString stepdebug = FlxAnimationStepDecoder::DebugString(step.Hash, step.Body);
|
|
|
|
|
result.Appendf(TEXT("%s\n"), *stepdebug);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 13:28:18 -04:00
|
|
|
FlxAnimTracker::FlxAnimTracker() {
|
2023-09-28 14:32:48 -04:00
|
|
|
Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlxAnimTracker::Clear() {
|
2023-09-15 00:01:41 -04:00
|
|
|
AQ.Empty();
|
2023-10-02 15:48:42 -04:00
|
|
|
Changed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlxAnimTracker::FinishedAnimation(uint64 hash) {
|
|
|
|
|
for (int i = 0; i < AQ.Num(); i++) {
|
|
|
|
|
if (AQ[i].Hash == hash) {
|
|
|
|
|
AQ[i].Finished = true;
|
|
|
|
|
Changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlxAnimTracker::SkipToEnd() {
|
|
|
|
|
for (int i = 0; i < AQ.Num(); i++) {
|
|
|
|
|
if (!AQ[i].Finished) {
|
|
|
|
|
AQ[i].Finished = true;
|
|
|
|
|
Changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-15 00:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 13:28:18 -04:00
|
|
|
void FlxAnimTracker::Update(std::string_view encqueue) {
|
2023-10-02 15:48:42 -04:00
|
|
|
check(!encqueue.empty());
|
|
|
|
|
|
|
|
|
|
// If the first hash matches, we don't bother updating at all.
|
2023-09-15 00:01:41 -04:00
|
|
|
//
|
2023-10-02 15:48:42 -04:00
|
|
|
FlxAnimQueueDecoder decoder(encqueue);
|
|
|
|
|
if (!AQ.IsEmpty()) {
|
|
|
|
|
if (decoder.PeekHash() == AQ.Last().Hash) {
|
2023-09-15 00:01:41 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2023-10-02 15:48:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The Changed flag is set whenever there is any change to
|
|
|
|
|
// the animation queue of any kind.
|
|
|
|
|
//
|
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
|
|
// Build a map indexing the steps in AQ.
|
|
|
|
|
//
|
|
|
|
|
TMap<uint64, int32> HashToIndex;
|
|
|
|
|
for (int i = 0; i < AQ.Num(); i++) {
|
|
|
|
|
HashToIndex.Emplace(AQ[i].Hash, i);
|
2023-09-15 00:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-19 22:08:15 -04:00
|
|
|
TArray<FlxAnimationStepView> newsteps;
|
2023-10-02 15:48:42 -04:00
|
|
|
int32 matchingindex = -1;
|
2023-09-15 00:01:41 -04:00
|
|
|
while (!decoder.AtEOF()) {
|
2023-09-19 22:08:15 -04:00
|
|
|
FlxAnimationStepView step = decoder.ReadStep();
|
2023-10-02 15:48:42 -04:00
|
|
|
int32* indexp = HashToIndex.Find(step.Hash);
|
|
|
|
|
if (indexp == nullptr) {
|
2023-09-15 00:01:41 -04:00
|
|
|
newsteps.Emplace(step);
|
|
|
|
|
} else {
|
2023-10-02 15:48:42 -04:00
|
|
|
matchingindex = *indexp;
|
2023-09-15 00:01:41 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 15:44:01 -04:00
|
|
|
// Remove all animations after the most recent matching
|
2023-10-02 15:48:42 -04:00
|
|
|
// record.
|
2023-09-15 15:44:01 -04:00
|
|
|
//
|
2023-10-02 15:48:42 -04:00
|
|
|
int32 nremove = (AQ.Num() - (matchingindex + 1));
|
2023-09-15 15:44:01 -04:00
|
|
|
check((nremove >= 0) && (nremove <= AQ.Num()));
|
|
|
|
|
for (int32 i = 0; i < nremove; i++) {
|
2023-09-15 00:01:41 -04:00
|
|
|
AQ.PopLast();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Transfer the new animations onto the queue.
|
|
|
|
|
//
|
|
|
|
|
while (!newsteps.IsEmpty()) {
|
2023-09-19 22:08:15 -04:00
|
|
|
FlxAnimationStepView step = newsteps.Pop();
|
2023-09-15 00:01:41 -04:00
|
|
|
AQ.EmplaceLast(step.Hash, step.Body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there are too many animations in AQ, discard
|
|
|
|
|
// any very old ones.
|
|
|
|
|
//
|
2023-10-02 15:48:42 -04:00
|
|
|
// TODO: this is hardwired to keep 10. Instead, we
|
|
|
|
|
// should keep the number specified in the queue.
|
|
|
|
|
//
|
|
|
|
|
int limit = 10;
|
|
|
|
|
int ndiscard = AQ.Num() - limit;
|
|
|
|
|
if (ndiscard > 0) {
|
2023-09-15 00:01:41 -04:00
|
|
|
for (int i = 0; i < ndiscard; i++) {
|
|
|
|
|
AQ.PopFirst();
|
|
|
|
|
}
|
2023-09-15 15:44:01 -04:00
|
|
|
}
|
2023-09-15 00:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
2023-10-02 15:48:42 -04:00
|
|
|
FlxAnimationStep FlxAnimTracker::GetCurrentAnimation() {
|
|
|
|
|
for (int i = 0; i < AQ.Num(); i++) {
|
|
|
|
|
if (!AQ[i].Finished) {
|
|
|
|
|
return AQ[i];
|
|
|
|
|
}
|
2023-09-15 15:44:01 -04:00
|
|
|
}
|
2023-10-02 15:48:42 -04:00
|
|
|
return AQ.Last();
|
2023-09-15 00:01:41 -04:00
|
|
|
}
|