Merge remote-tracking branch 'origin/master' into 1.7

This commit is contained in:
Wojtek Figat
2023-08-18 10:33:51 +02:00
18 changed files with 324 additions and 124 deletions

View File

@@ -233,11 +233,10 @@ void AnimGraphExecutor::ProcessAnimation(AnimGraphImpulse* nodes, AnimGraphNode*
// Get nested animation time position
float nestedAnimPrevPos = animPrevPos - nestedAnim.Time;
const float nestedAnimLength = nestedAnim.Anim->GetLength();
const float nestedAnimDuration = nestedAnim.Anim->GetDuration();
const float nestedAnimSpeed = nestedAnim.Speed * speed;
const float frameRateMatchScale = (float)nestedAnim.Anim->Data.FramesPerSecond / (float)anim->Data.FramesPerSecond;
nestedAnimPos = nestedAnimPos / nestedAnimDuration * nestedAnimSpeed * frameRateMatchScale;
nestedAnimPrevPos = nestedAnimPrevPos / nestedAnimDuration * nestedAnimSpeed * frameRateMatchScale;
const float frameRateMatchScale = nestedAnimSpeed / (float)anim->Data.FramesPerSecond;
nestedAnimPos = nestedAnimPos * frameRateMatchScale;
nestedAnimPrevPos = nestedAnimPrevPos * frameRateMatchScale;
GetAnimSamplePos(nestedAnim.Loop, nestedAnimLength, nestedAnim.StartTime, nestedAnimPrevPos, nestedAnimPos, nestedAnimPos, nestedAnimPrevPos);
ProcessAnimation(nodes, node, true, nestedAnimLength, nestedAnimPos, nestedAnimPrevPos, nestedAnim.Anim, 1.0f, weight, mode);

View File

@@ -395,18 +395,17 @@ namespace FlaxEngine.Interop
{
if (managed is null)
return;
sourceArray = managed;
(managedHandle, managedArray) = ManagedArray.AllocatePooledArray<TUnmanagedElement>(managed.Length);
}
public ReadOnlySpan<T> GetManagedValuesSource() => sourceArray;
public Span<TUnmanagedElement> GetUnmanagedValuesDestination() => managedArray.ToSpan<TUnmanagedElement>();
public Span<TUnmanagedElement> GetUnmanagedValuesDestination() => managedArray != null ? managedArray.ToSpan<TUnmanagedElement>() : Span<TUnmanagedElement>.Empty;
public TUnmanagedElement* ToUnmanaged() => (TUnmanagedElement*)ManagedHandle.ToIntPtr(managedHandle);
public void Free() => managedArray.FreePooled();
public void Free() => managedArray?.FreePooled();
}
#if FLAX_EDITOR

View File

@@ -333,6 +333,8 @@ void Keyboard::OnCharInput(Char c, Window* target)
void Keyboard::OnKeyUp(KeyboardKeys key, Window* target)
{
if (key >= KeyboardKeys::MAX)
return;
Event& e = _queue.AddOne();
e.Type = EventType::KeyUp;
e.Target = target;
@@ -341,6 +343,8 @@ void Keyboard::OnKeyUp(KeyboardKeys key, Window* target)
void Keyboard::OnKeyDown(KeyboardKeys key, Window* target)
{
if (key >= KeyboardKeys::MAX)
return;
Event& e = _queue.AddOne();
e.Type = EventType::KeyDown;
e.Target = target;

View File

@@ -34,10 +34,10 @@ void NavMeshBoundsVolume::Deserialize(DeserializeStream& stream, ISerializeModif
void NavMeshBoundsVolume::OnEnable()
{
GetScene()->Navigation.Volumes.Add(this);
// Base
Actor::OnEnable();
GetScene()->Navigation.Volumes.Add(this);
}
void NavMeshBoundsVolume::OnDisable()

View File

@@ -941,6 +941,13 @@ bool NetworkReplicator::HasObject(const ScriptingObject* obj)
return false;
}
ScriptingObject* NetworkReplicator::ResolveForeignObject(Guid objectId)
{
if (const auto& object = ResolveObject(objectId))
return object->Object.Get();
return nullptr;
}
uint32 NetworkReplicator::GetObjectOwnerClientId(const ScriptingObject* obj)
{
uint32 id = NetworkManager::ServerClientId;

View File

@@ -116,6 +116,13 @@ public:
/// <param name="obj">The network object.</param>
/// <returns>True if object exists in networking, otherwise false.</returns>
API_FUNCTION() static bool HasObject(const ScriptingObject* obj);
/// <summary>
/// Resolves foreign Guid into a local ScriptingObject
/// </summary>
/// <param name="objectId">The Guid of a foreign object.</param>
/// <returns>Object if managed to resolve, otherwise null.</returns>
API_FUNCTION() static ScriptingObject* ResolveForeignObject(Guid objectId);
/// <summary>
/// Gets the Client Id of the network object owner.

View File

@@ -2191,6 +2191,7 @@ bool LinuxPlatform::Init()
// Initialize "X11 keycode" -> "Flax KeyboardKeys" map
KeyCodeMap.Resize(desc->max_key_code + 1);
Platform::MemoryClear(KeyCodeMap.Get(), KeyCodeMap.Count() * sizeof(KeyboardKeys));
XkbFreeNames(desc, XkbKeyNamesMask, 1);
X11::XkbFreeKeyboard(desc, 0, 1);
for (int32 keyIdx = (int32)KeyboardKeys::None; keyIdx < MAX_uint8; keyIdx++)

View File

@@ -0,0 +1,14 @@
#include "ManagedDictionary.h"
Dictionary<ManagedDictionary::KeyValueType, MTypeObject*> ManagedDictionary::CachedDictionaryTypes;
#if !USE_MONO_AOT
ManagedDictionary::MakeGenericTypeThunk ManagedDictionary::MakeGenericType;
ManagedDictionary::CreateInstanceThunk ManagedDictionary::CreateInstance;
ManagedDictionary::AddDictionaryItemThunk ManagedDictionary::AddDictionaryItem;
ManagedDictionary::GetDictionaryKeysThunk ManagedDictionary::GetDictionaryKeys;
#else
MMethod* ManagedDictionary::MakeGenericType;
MMethod* ManagedDictionary::CreateInstance;
MMethod* ManagedDictionary::AddDictionaryItem;
MMethod* ManagedDictionary::GetDictionaryKeys;
#endif

View File

@@ -12,17 +12,96 @@
#include "Engine/Scripting/ManagedCLR/MAssembly.h"
#include "Engine/Scripting/ManagedCLR/MException.h"
#include "Engine/Scripting/Internal/StdTypesContainer.h"
#include "Engine/Core/Collections/Dictionary.h"
/// <summary>
/// Utility interop between C++ and C# for Dictionary collection.
/// </summary>
struct FLAXENGINE_API ManagedDictionary
{
public:
struct KeyValueType
{
MType* keyType;
MType* valueType;
bool operator==(const KeyValueType& other) const
{
return keyType == other.keyType && valueType == other.valueType;
}
};
private:
static Dictionary<KeyValueType, MTypeObject*> CachedDictionaryTypes;
#if !USE_MONO_AOT
typedef MTypeObject* (*MakeGenericTypeThunk)(MObject* instance, MTypeObject* genericType, MArray* genericArgs, MObject** exception);
static MakeGenericTypeThunk MakeGenericType;
typedef MObject* (*CreateInstanceThunk)(MObject* instance, MTypeObject* type, void* arr, MObject** exception);
static CreateInstanceThunk CreateInstance;
typedef void (*AddDictionaryItemThunk)(MObject* instance, MObject* dictionary, MObject* key, MObject* value, MObject** exception);
static AddDictionaryItemThunk AddDictionaryItem;
typedef MArray* (*GetDictionaryKeysThunk)(MObject* instance, MObject* dictionary, MObject** exception);
static GetDictionaryKeysThunk GetDictionaryKeys;
#else
static MMethod* MakeGenericType;
static MMethod* CreateInstance;
static MMethod* AddDictionaryItem;
static MMethod* GetDictionaryKeys;
#endif
public:
MObject* Instance;
ManagedDictionary(MObject* instance = nullptr)
{
Instance = instance;
#if !USE_MONO_AOT
// Cache the thunks of the dictionary helper methods
if (MakeGenericType == nullptr)
{
MClass* scriptingClass = Scripting::GetStaticClass();
CHECK(scriptingClass);
MMethod* makeGenericTypeMethod = scriptingClass->GetMethod("MakeGenericType", 2);
CHECK(makeGenericTypeMethod);
MakeGenericType = (MakeGenericTypeThunk)makeGenericTypeMethod->GetThunk();
MMethod* createInstanceMethod = StdTypesContainer::Instance()->ActivatorClass->GetMethod("CreateInstance", 2);
CHECK(createInstanceMethod);
CreateInstance = (CreateInstanceThunk)createInstanceMethod->GetThunk();
MMethod* addDictionaryItemMethod = scriptingClass->GetMethod("AddDictionaryItem", 3);
CHECK(addDictionaryItemMethod);
AddDictionaryItem = (AddDictionaryItemThunk)addDictionaryItemMethod->GetThunk();
MMethod* getDictionaryKeysItemMethod = scriptingClass->GetMethod("GetDictionaryKeys", 1);
CHECK(getDictionaryKeysItemMethod);
GetDictionaryKeys = (GetDictionaryKeysThunk)getDictionaryKeysItemMethod->GetThunk();
}
#else
if (MakeGenericType == nullptr)
{
MClass* scriptingClass = Scripting::GetStaticClass();
CHECK(scriptingClass);
MakeGenericType = scriptingClass->GetMethod("MakeGenericType", 2);
CHECK(MakeGenericType);
CreateInstance = StdTypesContainer::Instance()->ActivatorClass->GetMethod("CreateInstance", 2);
CHECK(CreateInstance);
AddDictionaryItem = scriptingClass->GetMethod("AddDictionaryItem", 3);
CHECK(AddDictionaryItem);
GetDictionaryKeys = scriptingClass->GetMethod("GetDictionaryKeys", 1);
CHECK(GetDictionaryKeys);
}
#endif
}
template<typename KeyType, typename ValueType>
@@ -76,10 +155,11 @@ struct FLAXENGINE_API ManagedDictionary
static MTypeObject* GetClass(MType* keyType, MType* valueType)
{
MClass* scriptingClass = Scripting::GetStaticClass();
CHECK_RETURN(scriptingClass, nullptr);
MMethod* makeGenericMethod = scriptingClass->GetMethod("MakeGenericType", 2);
CHECK_RETURN(makeGenericMethod, nullptr);
// Check if the generic type was generated earlier
KeyValueType cacheKey = { keyType, valueType };
MTypeObject* dictionaryType;
if (CachedDictionaryTypes.TryGet(cacheKey, dictionaryType))
return dictionaryType;
MTypeObject* genericType = MUtils::GetType(StdTypesContainer::Instance()->DictionaryClass);
#if USE_NETCORE
@@ -91,18 +171,23 @@ struct FLAXENGINE_API ManagedDictionary
genericArgsPtr[0] = INTERNAL_TYPE_GET_OBJECT(keyType);
genericArgsPtr[1] = INTERNAL_TYPE_GET_OBJECT(valueType);
MObject* exception = nullptr;
#if !USE_MONO_AOT
dictionaryType = MakeGenericType(nullptr, genericType, genericArgs, &exception);
#else
void* params[2];
params[0] = genericType;
params[1] = genericArgs;
MObject* exception = nullptr;
MObject* dictionaryType = makeGenericMethod->Invoke(nullptr, params, &exception);
dictionaryType = (MTypeObject*)MakeGenericType->Invoke(nullptr, params, &exception);
#endif
if (exception)
{
MException ex(exception);
ex.Log(LogType::Error, TEXT(""));
return nullptr;
}
return (MTypeObject*)dictionaryType;
CachedDictionaryTypes.Add(cacheKey, dictionaryType);
return dictionaryType;
}
static ManagedDictionary New(MType* keyType, MType* valueType)
@@ -112,16 +197,15 @@ struct FLAXENGINE_API ManagedDictionary
if (!dictionaryType)
return result;
MClass* scriptingClass = Scripting::GetStaticClass();
CHECK_RETURN(scriptingClass, result);
MMethod* createMethod = StdTypesContainer::Instance()->ActivatorClass->GetMethod("CreateInstance", 2);
CHECK_RETURN(createMethod, result);
MObject* exception = nullptr;
#if !USE_MONO_AOT
MObject* instance = CreateInstance(nullptr, dictionaryType, nullptr, &exception);
#else
void* params[2];
params[0] = dictionaryType;
params[1] = nullptr;
MObject* instance = createMethod->Invoke(nullptr, params, &exception);
MObject* instance = CreateInstance->Invoke(nullptr, params, &exception);
#endif
if (exception)
{
MException ex(exception);
@@ -136,16 +220,17 @@ struct FLAXENGINE_API ManagedDictionary
void Add(MObject* key, MObject* value)
{
CHECK(Instance);
MClass* scriptingClass = Scripting::GetStaticClass();
CHECK(scriptingClass);
MMethod* addDictionaryItemMethod = scriptingClass->GetMethod("AddDictionaryItem", 3);
CHECK(addDictionaryItemMethod);
MObject* exception = nullptr;
#if !USE_MONO_AOT
AddDictionaryItem(nullptr, Instance, key, value, &exception);
#else
void* params[3];
params[0] = Instance;
params[1] = key;
params[2] = value;
MObject* exception = nullptr;
addDictionaryItemMethod->Invoke(Instance, params, &exception);
AddDictionaryItem->Invoke(Instance, params, &exception);
#endif
if (exception)
{
MException ex(exception);
@@ -156,13 +241,13 @@ struct FLAXENGINE_API ManagedDictionary
MArray* GetKeys() const
{
CHECK_RETURN(Instance, nullptr);
MClass* scriptingClass = Scripting::GetStaticClass();
CHECK_RETURN(scriptingClass, nullptr);
MMethod* getDictionaryKeysMethod = scriptingClass->GetMethod("GetDictionaryKeys", 1);
CHECK_RETURN(getDictionaryKeysMethod, nullptr);
#if !USE_MONO_AOT
return GetDictionaryKeys(nullptr, Instance, nullptr);
#else
void* params[1];
params[0] = Instance;
return (MArray*)getDictionaryKeysMethod->Invoke( nullptr, params, nullptr);
return (MArray*)GetDictionaryKeys->Invoke(nullptr, params, nullptr);
#endif
}
MObject* GetValue(MObject* key) const
@@ -177,4 +262,11 @@ struct FLAXENGINE_API ManagedDictionary
}
};
inline uint32 GetHash(const ManagedDictionary::KeyValueType& other)
{
uint32 hash = ::GetHash((void*)other.keyType);
CombineHash(hash, ::GetHash((void*)other.valueType));
return hash;
}
#endif