コード例 #1
0
 /// <summary>
 /// Converts one or more arguments of any type to string in the best way possible.
 /// </summary>
 /// <param name="what">Arguments that will converted to string.</param>
 /// <returns>The string formed by the given arguments.</returns>
 public static string Str(params Variant[] what)
 {
     using var whatGodot = new Godot.Collections.Array(what);
     NativeFuncs.godotsharp_str((godot_array)whatGodot.NativeValue, out godot_string ret);
     using (ret)
         return(Marshaling.ConvertStringToManaged(ret));
 }
コード例 #2
0
        /// <summary>
        /// Calls the method represented by this <see cref="Callable"/>.
        /// Arguments can be passed and should match the method's signature.
        /// </summary>
        /// <param name="args">Arguments that will be passed to the method call.</param>
        /// <returns>The value returned by the method.</returns>
        public unsafe Variant Call(params Variant[] args)
        {
            using godot_callable callable = Marshaling.ConvertCallableToNative(this);

            int argc = args.Length;

            Span <godot_variant.movable> argsStoreSpan = argc <= VarArgsSpanThreshold ?
                                                         stackalloc godot_variant.movable[VarArgsSpanThreshold].Cleared() :
                                                         new godot_variant.movable[argc];

            Span <IntPtr> argsSpan = argc <= 10 ?
                                     stackalloc IntPtr[argc] :
                                     new IntPtr[argc];

            using var variantSpanDisposer = new VariantSpanDisposer(argsStoreSpan);

            fixed(godot_variant *varargs = &MemoryMarshal.GetReference(argsStoreSpan).DangerousSelfRef)
            fixed(IntPtr * argsPtr = &MemoryMarshal.GetReference(argsSpan))
            {
                for (int i = 0; i < argc; i++)
                {
                    varargs[i] = (godot_variant)args[i].NativeVar;
                    argsPtr[i] = new IntPtr(&varargs[i]);
                }

                godot_variant ret = NativeFuncs.godotsharp_callable_call(callable,
                                                                         (godot_variant **)argsPtr, argc, out _);

                return(Variant.CreateTakingOwnershipOfDisposableValue(ret));
            }
        }
コード例 #3
0
        /// <summary>
        /// Prints one or more arguments to strings in the best way possible to console.
        /// No newline is added at the end.
        ///
        /// Note: Due to limitations with Godot's built-in console, this only prints to the terminal.
        /// If you need to print in the editor, use another method, such as <see cref="Print(object[])"/>.
        /// </summary>
        /// <example>
        /// <code>
        /// GD.PrintRaw("A");
        /// GD.PrintRaw("B");
        /// // Prints AB
        /// </code>
        /// </example>
        /// <param name="what">Arguments that will be printed.</param>
        public static void PrintRaw(params object[] what)
        {
            string str = string.Concat(GetPrintParams(what));

            using var godotStr = Marshaling.ConvertStringToNative(str);
            NativeFuncs.godotsharp_printraw(godotStr);
        }
コード例 #4
0
ファイル: DelegateUtils.cs プロジェクト: huisedenanhai/godot
        internal static unsafe void InvokeWithVariantArgs(IntPtr delegateGCHandle, godot_variant **args, uint argc,
                                                          godot_variant *outRet)
        {
            try
            {
                // TODO: Optimize
                var @delegate   = (Delegate)GCHandle.FromIntPtr(delegateGCHandle).Target !;
                var managedArgs = new object?[argc];

                var parameterInfos = @delegate.Method.GetParameters();
                var paramsLength   = parameterInfos.Length;

                if (argc != paramsLength)
                {
                    throw new InvalidOperationException(
                              $"The delegate expects {paramsLength} arguments, but received {argc}.");
                }

                for (uint i = 0; i < argc; i++)
                {
                    managedArgs[i] = Marshaling.ConvertVariantToManagedObjectOfType(
                        *args[i], parameterInfos[i].ParameterType);
                }

                object?invokeRet = @delegate.DynamicInvoke(managedArgs);

                *outRet = Marshaling.ConvertManagedObjectToVariant(invokeRet);
            }
            catch (Exception e)
            {
                ExceptionUtils.LogException(e);
                *outRet = default;
            }
        }
コード例 #5
0
        void ICollection.CopyTo(System.Array array, int index)
        {
            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.");
            }

            int count = Count;

            if (array.Length < (index + 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.ConvertVariantToManagedObject(NativeValue.DangerousSelfRef.Elements[i]);
                    array.SetValue(obj, index);
                    index++;
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Prints one or more arguments to the console with a tab between each argument.
        /// </summary>
        /// <example>
        /// <code>
        /// GD.PrintT("A", "B", "C"); // Prints A       B       C
        /// </code>
        /// </example>
        /// <param name="what">Arguments that will be printed.</param>
        public static void PrintT(params object[] what)
        {
            string str = string.Join('\t', GetPrintParams(what));

            using var godotStr = Marshaling.ConvertStringToNative(str);
            NativeFuncs.godotsharp_printt(godotStr);
        }
コード例 #7
0
ファイル: Globals.cs プロジェクト: huisedenanhai/godot
 public static object EditorShortcut(string setting)
 {
     using godot_string settingIn = Marshaling.ConvertStringToNative(setting);
     Internal.godot_icall_Globals_EditorShortcut(settingIn, out godot_variant result);
     using (result)
         return(Marshaling.ConvertVariantToManagedObject(result));
 }
コード例 #8
0
ファイル: Globals.cs プロジェクト: huisedenanhai/godot
 public static string TTR(this string text)
 {
     using godot_string textIn = Marshaling.ConvertStringToNative(text);
     Internal.godot_icall_Globals_TTR(textIn, out godot_string dest);
     using (dest)
         return(Marshaling.ConvertStringToManaged(dest));
 }
コード例 #9
0
        internal static unsafe void CallToString(IntPtr godotObjectGCHandle, godot_string *outRes, godot_bool *outValid)
        {
            try
            {
                var self = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

                if (self == null)
                {
                    *outRes   = default;
                    *outValid = godot_bool.False;
                    return;
                }

                var resultStr = self.ToString();

                if (resultStr == null)
                {
                    *outRes   = default;
                    *outValid = godot_bool.False;
                    return;
                }

                *outRes   = Marshaling.ConvertStringToNative(resultStr);
                *outValid = godot_bool.True;
            }
            catch (Exception e)
            {
                ExceptionUtils.LogException(e);
                *outRes   = default;
                *outValid = godot_bool.False;
            }
        }
コード例 #10
0
ファイル: EditorProgress.cs プロジェクト: huisedenanhai/godot
 public EditorProgress(string task, string label, int amount, bool canCancel = false)
 {
     Task = task;
     using godot_string taskIn  = Marshaling.ConvertStringToNative(task);
     using godot_string labelIn = Marshaling.ConvertStringToNative(label);
     Internal.godot_icall_EditorProgress_Create(taskIn, labelIn, amount, canCancel);
 }
コード例 #11
0
ファイル: Globals.cs プロジェクト: huisedenanhai/godot
 public static unsafe object EditorDef(string setting, object defaultValue, bool restartIfChanged = false)
 {
     using godot_string settingIn       = Marshaling.ConvertStringToNative(setting);
     using godot_variant defaultValueIn = Marshaling.ConvertManagedObjectToVariant(defaultValue);
     Internal.godot_icall_Globals_EditorDef(settingIn, defaultValueIn, restartIfChanged, out godot_variant result);
     using (result)
         return(Marshaling.ConvertVariantToManagedObject(result));
 }
コード例 #12
0
        /// <summary>
        /// Gets the resource or property name indicated by <paramref name="idx"/> (0 to <see cref="GetSubNameCount"/>).
        /// </summary>
        /// <param name="idx">The subname index.</param>
        /// <returns>The subname at the given index <paramref name="idx"/>.</returns>
        public string GetSubName(int idx)
        {
            var self = (godot_node_path)NativeValue;

            NativeFuncs.godotsharp_node_path_get_subname(self, idx, out godot_string subName);
            using (subName)
                return(Marshaling.ConvertStringToManaged(subName));
        }
コード例 #13
0
        /// <summary>
        /// Returns all subnames concatenated with a colon character (<c>:</c>)
        /// as separator, i.e. the right side of the first colon in a node path.
        /// </summary>
        /// <example>
        /// <code>
        /// var nodepath = new NodePath("Path2D/PathFollow2D/Sprite2D:texture:load_path");
        /// GD.Print(nodepath.GetConcatenatedSubnames()); // texture:load_path
        /// </code>
        /// </example>
        /// <returns>The subnames concatenated with <c>:</c>.</returns>
        public string GetConcatenatedSubNames()
        {
            var self = (godot_node_path)NativeValue;

            NativeFuncs.godotsharp_node_path_get_concatenated_subnames(self, out godot_string subNames);
            using (subNames)
                return(Marshaling.ConvertStringToManaged(subNames));
        }
コード例 #14
0
        /// <summary>
        /// Converts this <see cref="Array"/> to a string.
        /// </summary>
        /// <returns>A string representation of this array.</returns>
        public override string ToString()
        {
            var self = (godot_array)NativeValue;

            NativeFuncs.godotsharp_array_to_string(ref self, out godot_string str);
            using (str)
                return(Marshaling.ConvertStringToManaged(str));
        }
コード例 #15
0
ファイル: Internal.cs プロジェクト: huisedenanhai/godot
 public static string[] CodeCompletionRequest(CodeCompletionRequest.CompletionKind kind,
                                              string scriptFile)
 {
     using godot_string scriptFileIn = Marshaling.ConvertStringToNative(scriptFile);
     godot_icall_Internal_CodeCompletionRequest((int)kind, scriptFileIn, out godot_packed_string_array res);
     using (res)
         return(Marshaling.ConvertNativePackedStringArrayToSystemArray(res));
 }
コード例 #16
0
 private static bool IsAnyOS(IEnumerable <string> names)
 {
     Internal.godot_icall_Utils_OS_GetPlatformName(out godot_string dest);
     using (dest)
     {
         string platformName = Marshaling.ConvertStringToManaged(dest);
         return(names.Any(p => p.Equals(platformName, StringComparison.OrdinalIgnoreCase)));
     }
 }
コード例 #17
0
 private static bool IsOS(string name)
 {
     Internal.godot_icall_Utils_OS_GetPlatformName(out godot_string dest);
     using (dest)
     {
         string platformName = Marshaling.ConvertStringToManaged(dest);
         return(name.Equals(platformName, StringComparison.OrdinalIgnoreCase));
     }
 }
コード例 #18
0
        /// <summary>
        /// Converts this <see cref="NodePath"/> to a string.
        /// </summary>
        /// <returns>A string representation of this <see cref="NodePath"/>.</returns>
        public override string ToString()
        {
            if (IsEmpty)
            {
                return(string.Empty);
            }

            var src = (godot_node_path)NativeValue;

            NativeFuncs.godotsharp_node_path_as_string(out godot_string dest, src);
            using (dest)
                return(Marshaling.ConvertStringToManaged(dest));
        }
コード例 #19
0
        public string GetDatapath()
        {
            IntPtr pointer = Native.DllImports.TessBaseAPIGetDatapath((HandleRef)this);

            if (pointer == null || pointer == IntPtr.Zero)
            {
                return(null);
            }
            else
            {
                return(Marshaling.PtrToStringUTF8(pointer));
            }
        }
コード例 #20
0
        /// <summary>
        /// The recognized text is returned as a char* which is coded
        /// as UNLV format Latin-1 with specific reject and suspect codes
        /// and must be freed with the delete[] operator.
        /// </summary>
        public string GetUNLVText()
        {
            IntPtr pointer = Native.DllImports.TessBaseAPIGetUNLVText((HandleRef)this);

            if (pointer == null || pointer == IntPtr.Zero)
            {
                return(null);
            }
            else
            {
                string returnObject = Marshaling.PtrToStringUTF8(pointer);
                Native.DllImports.TessDeleteText(pointer); //Delete the text pointer to clear memory
                return(returnObject);
            }
        }
コード例 #21
0
        public string GetUTF8Text(PageIteratorLevel level)
        {
            IntPtr pointer = Native.DllImports.TessResultIteratorGetUTF8Text(handleRef, level);

            if (IntPtr.Zero != pointer)
            {
                string returnObject = Marshaling.PtrToStringUTF8(pointer);
                Native.DllImports.TessDeleteText(pointer);
                return(returnObject);
            }
            else
            {
                return(null);
            }
        }
コード例 #22
0
        /// <summary>
        /// These functions are required for searchable PDF output.
        /// We need our hands on the input file so that we can include
        /// it in the PDF without transcoding.If that is not possible,
        /// we need the original image. Finally, resolution metadata
        /// is stored in the PDF so we need that as well.
        /// </summary>
        public string GetInputName()
        {
            IntPtr pointer = Native.DllImports.TessBaseAPIGetInputName((HandleRef)this);

            if (pointer == null || pointer == IntPtr.Zero)
            {
                return(null);
            }
            else
            {
                string returnObject = Marshaling.PtrToStringUTF8(pointer);
                Native.DllImports.TessDeleteText(pointer);
                return(returnObject);
            }
        }
コード例 #23
0
ファイル: ChoiceIterator.cs プロジェクト: bclgenki/tvn-cosine
        public string GetUTF8Text()
        {
            IntPtr pointer = Native.DllImports.TessChoiceIteratorGetUTF8Text(handleRef);

            if (IntPtr.Zero != pointer)
            {
                string returnObject = Marshaling.PtrToStringUTF8(pointer);
                DllImports.TessDeleteText(pointer);
                return(returnObject);
            }
            else
            {
                return(null);
            }
        }
コード例 #24
0
        internal static unsafe godot_bool Get(IntPtr godotObjectGCHandle, godot_string_name *name,
                                              godot_variant *outRet)
        {
            try
            {
                var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target;

                if (godotObject == null)
                {
                    throw new InvalidOperationException();
                }

                if (godotObject.GetGodotClassPropertyValue(CustomUnsafe.AsRef(name), out godot_variant outRetValue))
                {
                    *outRet = outRetValue;
                    return(godot_bool.True);
                }

                var nameManaged = StringName.CreateTakingOwnershipOfDisposableValue(
                    NativeFuncs.godotsharp_string_name_new_copy(CustomUnsafe.AsRef(name)));

                Variant ret = godotObject._Get(nameManaged);

                if (ret.VariantType == Variant.Type.Nil)
                {
                    *outRet = default;
                    return(godot_bool.False);
                }

                *outRet = Marshaling.ConvertManagedObjectToVariant(ret);
                return(godot_bool.True);
            }
            catch (Exception e)
            {
                ExceptionUtils.LogException(e);
                *outRet = default;
                return(godot_bool.False);
            }
        }
コード例 #25
0
 /// <summary>
 /// Encodes a <c>Variant</c> value to a byte array.
 /// If <paramref name="fullObjects"/> is <see langword="true"/> encoding objects is allowed
 /// (and can potentially include code).
 /// Deserialization can be done with <see cref="BytesToVar(Span{byte}, bool)"/>.
 /// </summary>
 /// <param name="var">Variant that will be encoded.</param>
 /// <param name="fullObjects">If objects should be serialized.</param>
 /// <returns>The <c>Variant</c> encoded as an array of bytes.</returns>
 public static byte[] VarToBytes(Variant var, bool fullObjects = false)
 {
     NativeFuncs.godotsharp_var_to_bytes((godot_variant)var.NativeVar, fullObjects.ToGodotBool(), out var varBytes);
     using (varBytes)
         return(Marshaling.ConvertNativePackedByteArrayToSystemArray(varBytes));
 }
コード例 #26
0
ファイル: EditorProgress.cs プロジェクト: huisedenanhai/godot
 public bool TryStep(string state, int step = -1, bool forceRefresh = true)
 {
     using godot_string taskIn  = Marshaling.ConvertStringToNative(Task);
     using godot_string stateIn = Marshaling.ConvertStringToNative(state);
     return(Internal.godot_icall_EditorProgress_Step(taskIn, stateIn, step, forceRefresh));
 }
コード例 #27
0
ファイル: EditorProgress.cs プロジェクト: huisedenanhai/godot
 public void Dispose()
 {
     using godot_string taskIn = Marshaling.ConvertStringToNative(Task);
     Internal.godot_icall_EditorProgress_Dispose(taskIn);
     GC.SuppressFinalize(this);
 }
コード例 #28
0
 /// <summary>
 /// Get the <see cref="Variant.Type"/> that corresponds for the given <see cref="Type"/>.
 /// </summary>
 /// <returns>The <see cref="Variant.Type"/> for the given <paramref name="type"/>.</returns>
 public static Variant.Type TypeToVariantType(Type type)
 {
     return(Marshaling.ConvertManagedTypeToVariantType(type, out bool _));
 }
コード例 #29
0
 /// <summary>
 /// Converts a <c>Variant</c> <paramref name="var"/> to a formatted string that
 /// can later be parsed using <see cref="StrToVar(string)"/>.
 /// </summary>
 /// <example>
 /// <code>
 /// var a = new Godot.Collections.Dictionary { ["a"] = 1, ["b"] = 2 };
 /// GD.Print(GD.VarToStr(a));
 /// // Prints
 /// // {
 /// //    "a": 1,
 /// //    "b": 2
 /// // }
 /// </code>
 /// </example>
 /// <param name="var">Variant that will be converted to string.</param>
 /// <returns>The <c>Variant</c> encoded as a string.</returns>
 public static string VarToStr(Variant var)
 {
     NativeFuncs.godotsharp_var_to_str((godot_variant)var.NativeVar, out godot_string ret);
     using (ret)
         return(Marshaling.ConvertStringToManaged(ret));
 }
コード例 #30
0
ファイル: Internal.cs プロジェクト: huisedenanhai/godot
 public static bool IsMacOSAppBundleInstalled(string bundleId)
 {
     using godot_string bundleIdIn = Marshaling.ConvertStringToNative(bundleId);
     return(godot_icall_Internal_IsMacOSAppBundleInstalled(bundleIdIn));
 }