#include "AnimQueue.h" FlxAnimationStep::FlxAnimationStep(uint64 hash, std::string_view body) { Finished = false; Hash = hash; Body.SetNum(body.size()); memcpy(Body.GetData(), body.data(), body.size()); Blueprint = UlxAnimationStepLibrary::AnimationStepGetString(*this, "bp"); } static bool ClearProperties(const FString& prefix, UObject* obj) { UClass* uclass = obj->GetClass(); if (prefix.IsEmpty()) { return false; } FName prefixlo(prefix); FName prefixhi(prefix + TEXT("\xFFFF")); for (TFieldIterator 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(obj); fprop->ClearValue(pptr); } } return true; } static bool SetProperty(const FString& name, UObject* obj, const FlxAnimationField& field) { UClass* uclass = obj->GetClass(); FName nname(name); switch (field.Type) { case ElxAnimValueType::STRING: { FStrProperty* fprop = FindFProperty(uclass, nname); if (fprop == nullptr) return false; FString* pptr = fprop->ContainerPtrToValuePtr(obj); *pptr = FString(field.S.size(), (const UTF8CHAR*)field.S.data()); return true; } case ElxAnimValueType::NUMBER: { FDoubleProperty* fprop = FindFProperty(uclass, nname); if (fprop == nullptr) return false; double* pptr = fprop->ContainerPtrToValuePtr(obj); *pptr = field.X; return true; } case ElxAnimValueType::BOOLEAN: { FBoolProperty* fprop = FindFProperty(uclass, nname); if (fprop == nullptr) return false; uint8* pptr = fprop->ContainerPtrToValuePtr(obj); fprop->SetPropertyValue(pptr, (field.X == 1.0)); return true; } case ElxAnimValueType::XYZ: { FStructProperty* fprop = FindFProperty(uclass, nname); if (fprop == nullptr) return false; if (fprop->Struct != TBaseStructure::Get()) return false; FVector* pptr = fprop->ContainerPtrToValuePtr(obj); *pptr = FVector(field.X, field.Y, field.Z); return true; } } return false; } 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); bool ok = true; if (preclear) { ok &= ClearProperties(prefix, into); } while (!decoder.AtEOF()) { FlxAnimationField field = decoder.ReadField(); FString sname(field.Name.size(), (const UTF8CHAR*)field.Name.data()); ok &= SetProperty(prefix + sname, into, field); } return ok; } FString UlxAnimationStepLibrary::AnimationStepDebugString(const FlxAnimationStep& step) { std::string_view body((const char*)(step.Body.GetData()), step.Body.Num()); return FlxAnimationStepDecoder::DebugString(step.Hash, body); } void UlxAnimationStepLibrary::UnpackAnimationStep(UObject* into, const FlxAnimationStep& step, const FString& prefix) { step.Unpack(prefix, into, true); } static FlxAnimationField FindAnimationFieldLL(const FlxAnimationStep& step, std::string_view name) { std::string_view body((const char*)(step.Body.GetData()), step.Body.Num()); FlxAnimationStepDecoder decoder(body); FlxAnimationField result; while (!decoder.AtEOF()) { result = decoder.ReadField(); if (result.Name == name) { return result; } } result.Type = ElxAnimValueType::INVALID; return result; } static FlxAnimationField FindAnimationField(const FlxAnimationStep& step, const FString& name) { FTCHARToUTF8 utf8name(name); std::string_view uname(utf8name.Get(), utf8name.Length()); return FindAnimationFieldLL(step, uname); } void FlxAnimationStep::AutoUpdateXYZ(AActor *actor) const { FlxAnimationField xyz = FindAnimationFieldLL(*this, "xyz"); if (xyz.Type == ElxAnimValueType::XYZ) { actor->SetActorLocation(FVector(xyz.X, xyz.Y, xyz.Z)); } } void FlxAnimationStep::AutoUpdateFacing(AActor *actor) const { FlxAnimationField facing = FindAnimationFieldLL(*this, "facing"); if (facing.Type == ElxAnimValueType::NUMBER) { actor->SetActorRotation(FRotator(0, facing.X, 0)); } } void FlxAnimationStep::AutoUpdatePlane(FName *planep) const { FlxAnimationField plane = FindAnimationFieldLL(*this, "plane"); if (plane.Type == ElxAnimValueType::STRING) { FString pname(plane.S.size(), (const UTF8CHAR*)(plane.S.data())); *planep = FName(pname); } } bool UlxAnimationStepLibrary::AnimationStepIsIdle(const FlxAnimationStep &step) { return step.Finished; } 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; } FlxAnimationStepView FlxAnimQueueDecoder::ReadStep() { FlxAnimationStepView result; result.Hash = Decoder.read_uint64(); result.Body = Decoder.read_string_view(); return result; } uint64 FlxAnimQueueDecoder::PeekHash() { std::string_view rest = Decoder.GetRest(); uint64 result = Decoder.read_uint64(); Decoder.Reset(rest, false); return result; } 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; } FlxAnimQueueDecoder::FlxAnimQueueDecoder(std::string_view queue) : Decoder(queue) { SizeLimit = Decoder.read_uint8(); ActualSize = Decoder.read_uint8(); } 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; } FlxAnimTracker::FlxAnimTracker() { Clear(); } void FlxAnimTracker::Clear() { AQ.Empty(); 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; } } } void FlxAnimTracker::Update(std::string_view encqueue) { check(!encqueue.empty()); // If the first hash matches, we don't bother updating at all. // FlxAnimQueueDecoder decoder(encqueue); if (!AQ.IsEmpty()) { if (decoder.PeekHash() == AQ.Last().Hash) { return; } } // 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 HashToIndex; for (int i = 0; i < AQ.Num(); i++) { HashToIndex.Emplace(AQ[i].Hash, i); } // 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. // TArray newsteps; int32 matchingindex = -1; while (!decoder.AtEOF()) { FlxAnimationStepView step = decoder.ReadStep(); int32* indexp = HashToIndex.Find(step.Hash); if (indexp == nullptr) { newsteps.Emplace(step); } else { matchingindex = *indexp; break; } } // Remove all animations after the most recent matching // record. // int32 nremove = (AQ.Num() - (matchingindex + 1)); check((nremove >= 0) && (nremove <= AQ.Num())); for (int32 i = 0; i < nremove; i++) { AQ.PopLast(); } // Transfer the new animations onto the queue. // while (!newsteps.IsEmpty()) { FlxAnimationStepView step = newsteps.Pop(); AQ.EmplaceLast(step.Hash, step.Body); } // If there are too many animations in AQ, discard // any very old ones. // // 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) { for (int i = 0; i < ndiscard; i++) { AQ.PopFirst(); } } } FlxAnimationStep FlxAnimTracker::GetCurrentAnimation() { for (int i = 0; i < AQ.Num(); i++) { if (!AQ[i].Finished) { return AQ[i]; } } return AQ.Last(); }