diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp
index 0d9b02b2e2..409680b8fc 100644
--- a/modules/mono/csharp_script.cpp
+++ b/modules/mono/csharp_script.cpp
@@ -106,7 +106,7 @@ Error CSharpLanguage::execute_file(const String &p_path) {
return OK;
}
-extern void *godotsharp_pinvoke_funcs[95];
+extern void *godotsharp_pinvoke_funcs[130];
[[maybe_unused]] volatile void **do_not_strip_godotsharp_pinvoke_funcs;
void CSharpLanguage::init() {
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
index 92ace4798c..a75a991d36 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
-using System.Runtime.CompilerServices;
using Godot.NativeInterop;
namespace Godot.Collections
@@ -22,7 +21,7 @@ namespace Godot.Collections
///
public Array()
{
- godot_icall_Array_Ctor(out NativeValue);
+ NativeValue = NativeFuncs.godotsharp_array_new();
}
///
@@ -48,11 +47,15 @@ namespace Godot.Collections
public Array(params object[] array) : this()
{
if (array == null)
- {
throw new NullReferenceException($"Parameter '{nameof(array)} cannot be null.'");
- }
- godot_icall_Array_Ctor_MonoArray(array, out NativeValue);
+ NativeValue = NativeFuncs.godotsharp_array_new();
+ int length = array.Length;
+
+ Resize(length);
+
+ for (int i = 0; i < length; i++)
+ this[i] = array[i];
}
private Array(godot_array nativeValueToOwn)
@@ -92,7 +95,7 @@ namespace Godot.Collections
public Array Duplicate(bool deep = false)
{
godot_array newArray;
- godot_icall_Array_Duplicate(ref NativeValue, deep, out newArray);
+ NativeFuncs.godotsharp_array_duplicate(ref NativeValue, deep, out newArray);
return CreateTakingOwnershipOfDisposableValue(newArray);
}
@@ -101,18 +104,12 @@ namespace Godot.Collections
///
/// The new size of the array.
/// if successful, or an error code.
- public Error Resize(int newSize)
- {
- return godot_icall_Array_Resize(ref NativeValue, newSize);
- }
+ public Error Resize(int newSize) => NativeFuncs.godotsharp_array_resize(ref NativeValue, newSize);
///
/// Shuffles the contents of this into a random order.
///
- public void Shuffle()
- {
- godot_icall_Array_Shuffle(ref NativeValue);
- }
+ public void Shuffle() => NativeFuncs.godotsharp_array_shuffle(ref NativeValue);
///
/// Concatenates these two s.
@@ -122,9 +119,16 @@ namespace Godot.Collections
/// A new Godot Array with the contents of both arrays.
public static Array operator +(Array left, Array right)
{
- godot_array newArray;
- godot_icall_Array_Concatenate(ref left.NativeValue, ref right.NativeValue, out newArray);
- return CreateTakingOwnershipOfDisposableValue(newArray);
+ int leftCount = left.Count;
+ int rightCount = right.Count;
+
+ Array newArray = left.Duplicate(deep: false);
+ newArray.Resize(leftCount + rightCount);
+
+ for (int i = 0; i < rightCount; i++)
+ newArray[i + leftCount] = right[i];
+
+ return newArray;
}
// IList
@@ -137,18 +141,20 @@ namespace Godot.Collections
/// Returns the object at the given index.
///
/// The object at the given index.
- public object this[int index]
+ public unsafe object this[int index]
{
get
{
- godot_icall_Array_At(ref NativeValue, index, out godot_variant elem);
- unsafe
- {
- using (elem)
- return Marshaling.variant_to_mono_object(&elem);
- }
+ GetVariantBorrowElementAt(index, out godot_variant borrowElem);
+ return Marshaling.variant_to_mono_object(&borrowElem);
+ }
+ set
+ {
+ if (index < 0 || index >= Count)
+ throw new IndexOutOfRangeException();
+ godot_variant* ptrw = NativeFuncs.godotsharp_array_ptrw(ref NativeValue);
+ ptrw[index] = Marshaling.mono_object_to_variant(value);
}
- set => godot_icall_Array_SetAt(ref NativeValue, index, value);
}
///
@@ -157,19 +163,23 @@ namespace Godot.Collections
///
/// The object to add.
/// The new size after adding the object.
- public int Add(object value) => godot_icall_Array_Add(ref NativeValue, value);
+ public unsafe int Add(object value)
+ {
+ using godot_variant variantValue = Marshaling.mono_object_to_variant(value);
+ return NativeFuncs.godotsharp_array_add(ref NativeValue, &variantValue);
+ }
///
/// Checks if this contains the given object.
///
/// The item to look for.
/// Whether or not this array contains the given object.
- public bool Contains(object value) => godot_icall_Array_Contains(ref NativeValue, value);
+ public bool Contains(object value) => IndexOf(value) != -1;
///
/// Erases all items from this .
///
- public void Clear() => godot_icall_Array_Clear(ref NativeValue);
+ public void Clear() => Resize(0);
///
/// Searches this for an object
@@ -177,7 +187,11 @@ namespace Godot.Collections
///
/// The object to search for.
/// The index of the object, or -1 if not found.
- public int IndexOf(object value) => godot_icall_Array_IndexOf(ref NativeValue, value);
+ public unsafe int IndexOf(object value)
+ {
+ using godot_variant variantValue = Marshaling.mono_object_to_variant(value);
+ return NativeFuncs.godotsharp_array_index_of(ref NativeValue, &variantValue);
+ }
///
/// Inserts a new object at a given position in the array.
@@ -187,20 +201,38 @@ namespace Godot.Collections
///
/// The index to insert at.
/// The object to insert.
- public void Insert(int index, object value) => godot_icall_Array_Insert(ref NativeValue, index, value);
+ public unsafe void Insert(int index, object value)
+ {
+ if (index < 0 || index > Count)
+ throw new IndexOutOfRangeException();
+
+ using godot_variant variantValue = Marshaling.mono_object_to_variant(value);
+ NativeFuncs.godotsharp_array_insert(ref NativeValue, index, &variantValue);
+ }
///
/// Removes the first occurrence of the specified value
/// from this .
///
/// The value to remove.
- public void Remove(object value) => godot_icall_Array_Remove(ref NativeValue, value);
+ public void Remove(object value)
+ {
+ int index = IndexOf(value);
+ if (index >= 0)
+ RemoveAt(index);
+ }
///
/// Removes an element from this by index.
///
/// The index of the element to remove.
- public void RemoveAt(int index) => godot_icall_Array_RemoveAt(ref NativeValue, index);
+ public void RemoveAt(int index)
+ {
+ if (index < 0 || index > Count)
+ throw new IndexOutOfRangeException();
+
+ NativeFuncs.godotsharp_array_remove_at(ref NativeValue, index);
+ }
// ICollection
@@ -209,7 +241,7 @@ namespace Godot.Collections
/// This is also known as the size or length of the array.
///
/// The number of elements.
- public int Count => godot_icall_Array_Count(ref NativeValue);
+ public int Count => NativeValue.Size;
object ICollection.SyncRoot => this;
@@ -220,17 +252,35 @@ namespace Godot.Collections
/// untyped C# array, starting at the given index.
///
/// The array to copy to.
- /// The index to start at.
- public void CopyTo(System.Array array, int index)
+ /// The index to start at.
+ public void CopyTo(System.Array array, int destIndex)
{
if (array == null)
throw new ArgumentNullException(nameof(array), "Value cannot be null.");
- if (index < 0)
- throw new ArgumentOutOfRangeException(nameof(index), "Number was less than the array's lower bound in the first dimension.");
+ if (destIndex < 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(destIndex),
+ "Number was less than the array's lower bound in the first dimension.");
+ }
- // Internal call may throw ArgumentException
- godot_icall_Array_CopyTo(ref NativeValue, array, index);
+ int count = Count;
+
+ if (array.Length < (destIndex + count))
+ {
+ throw new ArgumentException(
+ "Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
+ }
+
+ unsafe
+ {
+ for (int i = 0; i < count; i++)
+ {
+ object obj = Marshaling.variant_to_mono_object(&(*NativeValue._p)._arrayVector._ptr[i]);
+ array.SetValue(obj, destIndex);
+ destIndex++;
+ }
+ }
}
// IEnumerable
@@ -253,64 +303,30 @@ namespace Godot.Collections
/// Converts this to a string.
///
/// A string representation of this array.
- public override string ToString()
+ public override unsafe string ToString()
{
- return godot_icall_Array_ToString(ref NativeValue);
+ using godot_string str = default;
+ NativeFuncs.godotsharp_array_to_string(ref NativeValue, &str);
+ return Marshaling.mono_string_from_godot(&str);
}
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_Ctor(out godot_array dest);
+ ///
+ /// The variant returned via the parameter is owned by the Array and must not be disposed.
+ ///
+ internal void GetVariantBorrowElementAt(int index, out godot_variant elem)
+ {
+ if (index < 0 || index >= Count)
+ throw new IndexOutOfRangeException();
+ GetVariantBorrowElementAtUnchecked(index, out elem);
+ }
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_Ctor_MonoArray(System.Array array, out godot_array dest);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_At(ref godot_array ptr, int index, out godot_variant elem);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_SetAt(ref godot_array ptr, int index, object value);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern int godot_icall_Array_Count(ref godot_array ptr);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern int godot_icall_Array_Add(ref godot_array ptr, object item);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_Clear(ref godot_array ptr);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_Concatenate(ref godot_array left, ref godot_array right, out godot_array dest);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern bool godot_icall_Array_Contains(ref godot_array ptr, object item);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_CopyTo(ref godot_array ptr, System.Array array, int arrayIndex);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_Duplicate(ref godot_array ptr, bool deep, out godot_array dest);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern int godot_icall_Array_IndexOf(ref godot_array ptr, object item);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_Insert(ref godot_array ptr, int index, object item);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern bool godot_icall_Array_Remove(ref godot_array ptr, object item);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Array_RemoveAt(ref godot_array ptr, int index);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern Error godot_icall_Array_Resize(ref godot_array ptr, int newSize);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern Error godot_icall_Array_Shuffle(ref godot_array ptr);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern string godot_icall_Array_ToString(ref godot_array ptr);
+ ///
+ /// The variant returned via the parameter is owned by the Array and must not be disposed.
+ ///
+ internal unsafe void GetVariantBorrowElementAtUnchecked(int index, out godot_variant elem)
+ {
+ elem = (*NativeValue._p)._arrayVector._ptr[index];
+ }
}
internal interface IGenericGodotArray
@@ -449,11 +465,10 @@ namespace Godot.Collections
{
get
{
- Array.godot_icall_Array_At(ref _underlyingArray.NativeValue, index, out godot_variant elem);
+ _underlyingArray.GetVariantBorrowElementAt(index, out godot_variant borrowElem);
unsafe
{
- using (elem)
- return (T)Marshaling.variant_to_mono_object_of_type(&elem, TypeOfElements);
+ return (T)Marshaling.variant_to_mono_object_of_type(&borrowElem, TypeOfElements);
}
}
set => _underlyingArray[index] = value;
@@ -544,12 +559,14 @@ namespace Godot.Collections
throw new ArgumentNullException(nameof(array), "Value cannot be null.");
if (arrayIndex < 0)
- throw new ArgumentOutOfRangeException(nameof(arrayIndex), "Number was less than the array's lower bound in the first dimension.");
+ throw new ArgumentOutOfRangeException(nameof(arrayIndex),
+ "Number was less than the array's lower bound in the first dimension.");
int count = _underlyingArray.Count;
if (array.Length < (arrayIndex + count))
- throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
+ throw new ArgumentException(
+ "Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
for (int i = 0; i < count; i++)
{
@@ -566,7 +583,14 @@ namespace Godot.Collections
/// A bool indicating success or failure.
public bool Remove(T item)
{
- return Array.godot_icall_Array_Remove(ref _underlyingArray.NativeValue, item);
+ int index = IndexOf(item);
+ if (index >= 0)
+ {
+ RemoveAt(index);
+ return true;
+ }
+
+ return false;
}
// IEnumerable
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs
index 344acd65e0..e5887e475c 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections;
-using System.Runtime.CompilerServices;
using Godot.NativeInterop;
using System.Diagnostics.CodeAnalysis;
@@ -23,7 +22,7 @@ namespace Godot.Collections
///
public Dictionary()
{
- godot_icall_Dictionary_Ctor(out NativeValue);
+ NativeValue = NativeFuncs.godotsharp_dictionary_new();
}
///
@@ -77,7 +76,7 @@ namespace Godot.Collections
public Dictionary Duplicate(bool deep = false)
{
godot_dictionary newDictionary;
- godot_icall_Dictionary_Duplicate(ref NativeValue, deep, out newDictionary);
+ NativeFuncs.godotsharp_dictionary_duplicate(ref NativeValue, deep, out newDictionary);
return CreateTakingOwnershipOfDisposableValue(newDictionary);
}
@@ -91,7 +90,7 @@ namespace Godot.Collections
get
{
godot_array keysArray;
- godot_icall_Dictionary_Keys(ref NativeValue, out keysArray);
+ NativeFuncs.godotsharp_dictionary_keys(ref NativeValue, out keysArray);
return Array.CreateTakingOwnershipOfDisposableValue(keysArray);
}
}
@@ -104,7 +103,7 @@ namespace Godot.Collections
get
{
godot_array valuesArray;
- godot_icall_Dictionary_Values(ref NativeValue, out valuesArray);
+ NativeFuncs.godotsharp_dictionary_values(ref NativeValue, out valuesArray);
return Array.CreateTakingOwnershipOfDisposableValue(valuesArray);
}
}
@@ -112,10 +111,15 @@ namespace Godot.Collections
private (Array keys, Array values, int count) GetKeyValuePairs()
{
godot_array keysArray;
- godot_array valuesArray;
- int count = godot_icall_Dictionary_KeyValuePairs(ref NativeValue, out keysArray, out valuesArray);
+ NativeFuncs.godotsharp_dictionary_keys(ref NativeValue, out keysArray);
var keys = Array.CreateTakingOwnershipOfDisposableValue(keysArray);
+
+ godot_array valuesArray;
+ NativeFuncs.godotsharp_dictionary_keys(ref NativeValue, out valuesArray);
var values = Array.CreateTakingOwnershipOfDisposableValue(valuesArray);
+
+ int count = NativeFuncs.godotsharp_dictionary_count(ref NativeValue);
+
return (keys, values, count);
}
@@ -127,18 +131,28 @@ namespace Godot.Collections
/// Returns the object at the given .
///
/// The object at the given .
- public object this[object key]
+ public unsafe object this[object key]
{
get
{
- godot_icall_Dictionary_GetValue(ref NativeValue, key, out godot_variant value);
- unsafe
+ using godot_variant variantKey = Marshaling.mono_object_to_variant(key);
+ if (NativeFuncs.godotsharp_dictionary_try_get_value(ref NativeValue, &variantKey,
+ out godot_variant value))
{
using (value)
return Marshaling.variant_to_mono_object(&value);
}
+ else
+ {
+ throw new KeyNotFoundException();
+ }
+ }
+ set
+ {
+ using godot_variant variantKey = Marshaling.mono_object_to_variant(key);
+ using godot_variant variantValue = Marshaling.mono_object_to_variant(value);
+ NativeFuncs.godotsharp_dictionary_set_value(ref NativeValue, &variantKey, &variantValue);
}
- set => godot_icall_Dictionary_SetValue(ref NativeValue, key, value);
}
///
@@ -147,19 +161,32 @@ namespace Godot.Collections
///
/// The key at which to add the object.
/// The object to add.
- public void Add(object key, object value) => godot_icall_Dictionary_Add(ref NativeValue, key, value);
+ public unsafe void Add(object key, object value)
+ {
+ using godot_variant variantKey = Marshaling.mono_object_to_variant(key);
+
+ if (NativeFuncs.godotsharp_dictionary_contains_key(ref NativeValue, &variantKey))
+ throw new ArgumentException("An element with the same key already exists", nameof(key));
+
+ using godot_variant variantValue = Marshaling.mono_object_to_variant(value);
+ NativeFuncs.godotsharp_dictionary_add(ref NativeValue, &variantKey, &variantValue);
+ }
///
/// Erases all items from this .
///
- public void Clear() => godot_icall_Dictionary_Clear(ref NativeValue);
+ public void Clear() => NativeFuncs.godotsharp_dictionary_clear(ref NativeValue);
///
/// Checks if this contains the given key.
///
/// The key to look for.
/// Whether or not this dictionary contains the given key.
- public bool Contains(object key) => godot_icall_Dictionary_ContainsKey(ref NativeValue, key);
+ public unsafe bool Contains(object key)
+ {
+ using godot_variant variantKey = Marshaling.mono_object_to_variant(key);
+ return NativeFuncs.godotsharp_dictionary_contains_key(ref NativeValue, &variantKey);
+ }
///
/// Gets an enumerator for this .
@@ -171,7 +198,11 @@ namespace Godot.Collections
/// Removes an element from this by key.
///
/// The key of the element to remove.
- public void Remove(object key) => godot_icall_Dictionary_RemoveKey(ref NativeValue, key);
+ public unsafe void Remove(object key)
+ {
+ using godot_variant variantKey = Marshaling.mono_object_to_variant(key);
+ NativeFuncs.godotsharp_dictionary_remove_key(ref NativeValue, &variantKey);
+ }
// ICollection
@@ -184,7 +215,7 @@ namespace Godot.Collections
/// This is also known as the size or length of the dictionary.
///
/// The number of elements.
- public int Count => godot_icall_Dictionary_Count(ref NativeValue);
+ public int Count => NativeFuncs.godotsharp_dictionary_count(ref NativeValue);
///
/// Copies the elements of this to the given
@@ -198,12 +229,14 @@ namespace Godot.Collections
throw new ArgumentNullException(nameof(array), "Value cannot be null.");
if (index < 0)
- throw new ArgumentOutOfRangeException(nameof(index), "Number was less than the array's lower bound in the first dimension.");
+ throw new ArgumentOutOfRangeException(nameof(index),
+ "Number was less than the array's lower bound in the first dimension.");
var (keys, values, count) = GetKeyValuePairs();
if (array.Length < (index + count))
- throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
+ throw new ArgumentException(
+ "Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
for (int i = 0; i < count; i++)
{
@@ -241,15 +274,23 @@ namespace Godot.Collections
{
UpdateEntry();
}
+
return entry;
}
}
- private void UpdateEntry()
+ private unsafe void UpdateEntry()
{
dirty = false;
- godot_icall_Dictionary_KeyValuePairAt(ref dictionary.NativeValue, index, out object key, out object value);
- entry = new DictionaryEntry(key, value);
+ NativeFuncs.godotsharp_dictionary_key_value_pair_at(ref dictionary.NativeValue, index,
+ out godot_variant key,
+ out godot_variant value);
+ using (key)
+ using (value)
+ {
+ entry = new DictionaryEntry(Marshaling.variant_to_mono_object(&key),
+ Marshaling.variant_to_mono_object(&value));
+ }
}
public object Key => Entry.Key;
@@ -274,61 +315,12 @@ namespace Godot.Collections
/// Converts this to a string.
///
/// A string representation of this dictionary.
- public override string ToString()
+ public override unsafe string ToString()
{
- return godot_icall_Dictionary_ToString(ref NativeValue);
+ using godot_string str = default;
+ NativeFuncs.godotsharp_dictionary_to_string(ref NativeValue, &str);
+ return Marshaling.mono_string_from_godot(&str);
}
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_Ctor(out godot_dictionary dest);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_GetValue(ref godot_dictionary ptr, object key, out godot_variant value);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_SetValue(ref godot_dictionary ptr, object key, object value);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_Keys(ref godot_dictionary ptr, out godot_array dest);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_Values(ref godot_dictionary ptr, out godot_array dest);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern int godot_icall_Dictionary_KeyValuePairs(ref godot_dictionary ptr, out godot_array keys, out godot_array values);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_KeyValuePairAt(ref godot_dictionary ptr, int index, out object key, out object value);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_Add(ref godot_dictionary ptr, object key, object value);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern int godot_icall_Dictionary_Count(ref godot_dictionary ptr);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_Clear(ref godot_dictionary ptr);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern bool godot_icall_Dictionary_Contains(ref godot_dictionary ptr, object key, object value);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern bool godot_icall_Dictionary_ContainsKey(ref godot_dictionary ptr, object key);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void godot_icall_Dictionary_Duplicate(ref godot_dictionary ptr, bool deep, out godot_dictionary dest);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern bool godot_icall_Dictionary_RemoveKey(ref godot_dictionary ptr, object key);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern bool godot_icall_Dictionary_Remove(ref godot_dictionary ptr, object key, object value);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern bool godot_icall_Dictionary_TryGetValue(ref godot_dictionary ptr, object key, out godot_variant value);
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern string godot_icall_Dictionary_ToString(ref godot_dictionary ptr);
}
internal interface IGenericGodotDictionary
@@ -401,7 +393,8 @@ namespace Godot.Collections
}
// Explicit name to make it very clear
- internal static Dictionary CreateTakingOwnershipOfDisposableValue(godot_dictionary nativeValueToOwn)
+ internal static Dictionary CreateTakingOwnershipOfDisposableValue(
+ godot_dictionary nativeValueToOwn)
=> new Dictionary(Dictionary.CreateTakingOwnershipOfDisposableValue(nativeValueToOwn));
///
@@ -433,11 +426,19 @@ namespace Godot.Collections
{
get
{
- Dictionary.godot_icall_Dictionary_GetValue(ref _underlyingDict.NativeValue, key, out godot_variant value);
unsafe
{
- using (value)
- return (TValue)Marshaling.variant_to_mono_object_of_type(&value, TypeOfValues);
+ using godot_variant variantKey = Marshaling.mono_object_to_variant(key);
+ if (NativeFuncs.godotsharp_dictionary_try_get_value(ref _underlyingDict.NativeValue,
+ &variantKey, out godot_variant value))
+ {
+ using (value)
+ return (TValue)Marshaling.variant_to_mono_object_of_type(&value, TypeOfValues);
+ }
+ else
+ {
+ throw new KeyNotFoundException();
+ }
}
}
set => _underlyingDict[key] = value;
@@ -451,7 +452,7 @@ namespace Godot.Collections
get
{
godot_array keyArray;
- Dictionary.godot_icall_Dictionary_Keys(ref _underlyingDict.NativeValue, out keyArray);
+ NativeFuncs.godotsharp_dictionary_keys(ref _underlyingDict.NativeValue, out keyArray);
return Array.CreateTakingOwnershipOfDisposableValue(keyArray);
}
}
@@ -464,15 +465,22 @@ namespace Godot.Collections
get
{
godot_array valuesArray;
- Dictionary.godot_icall_Dictionary_Values(ref _underlyingDict.NativeValue, out valuesArray);
+ NativeFuncs.godotsharp_dictionary_values(ref _underlyingDict.NativeValue, out valuesArray);
return Array.CreateTakingOwnershipOfDisposableValue(valuesArray);
}
}
- private KeyValuePair GetKeyValuePair(int index)
+ private unsafe KeyValuePair GetKeyValuePair(int index)
{
- Dictionary.godot_icall_Dictionary_KeyValuePairAt(ref _underlyingDict.NativeValue, index, out object key, out object value);
- return new KeyValuePair((TKey)key, (TValue)value);
+ NativeFuncs.godotsharp_dictionary_key_value_pair_at(ref _underlyingDict.NativeValue, index,
+ out godot_variant key,
+ out godot_variant value);
+ using (key)
+ using (value)
+ {
+ return new KeyValuePair((TKey)Marshaling.variant_to_mono_object(&key),
+ (TValue)Marshaling.variant_to_mono_object(&value));
+ }
}
///
@@ -500,9 +508,10 @@ namespace Godot.Collections
/// Removes an element from this by key.
///
/// The key of the element to remove.
- public bool Remove(TKey key)
+ public unsafe bool Remove(TKey key)
{
- return Dictionary.godot_icall_Dictionary_RemoveKey(ref _underlyingDict.NativeValue, key);
+ using godot_variant variantKey = Marshaling.mono_object_to_variant(key);
+ return NativeFuncs.godotsharp_dictionary_remove_key(ref _underlyingDict.NativeValue, &variantKey);
}
///
@@ -511,18 +520,17 @@ namespace Godot.Collections
/// The key of the element to get.
/// The value at the given .
/// If an object was found for the given .
- public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
+ public unsafe bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
{
- bool found = Dictionary.godot_icall_Dictionary_TryGetValue(ref _underlyingDict.NativeValue, key, out godot_variant retValue);
+ using godot_variant variantKey = Marshaling.mono_object_to_variant(key);
+ bool found = NativeFuncs.godotsharp_dictionary_try_get_value(ref _underlyingDict.NativeValue,
+ &variantKey, out godot_variant retValue);
- unsafe
+ using (retValue)
{
- using (retValue)
- {
- value = found ?
- (TValue)Marshaling.variant_to_mono_object_of_type(&retValue, TypeOfValues) :
- default;
- }
+ value = found ?
+ (TValue)Marshaling.variant_to_mono_object_of_type(&retValue, TypeOfValues) :
+ default;
}
return found;
@@ -552,9 +560,19 @@ namespace Godot.Collections
_underlyingDict.Clear();
}
- bool ICollection>.Contains(KeyValuePair item)
+ unsafe bool ICollection>.Contains(KeyValuePair item)
{
- return _underlyingDict.Contains(new KeyValuePair