godot/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs
Ignacio Roldán Etcheverry f744d99179 C#: Restructure code prior move to .NET Core
The main focus here was to remove the majority of code that relied on
Mono's embedding APIs, specially the reflection APIs. The embedding
APIs we still use are the bare minimum we need for things to work.
A lot of code was moved to C#. We no longer deal with any managed
objects (`MonoObject*`, and such) in native code, and all marshaling
is done in C#.

The reason for restructuring the code and move away from embedding APIs
is that once we move to .NET Core, we will be limited by the much more
minimal .NET hosting.

PERFORMANCE REGRESSIONS
-----------------------

Some parts of the code were written with little to no concern about
performance. This includes code that calls into script methods and
accesses script fields, properties and events.
The reason for this is that all of that will be moved to source
generators, so any work prior to that would be a waste of time.

DISABLED FEATURES
-----------------

Some code was removed as it no longer makes sense (or won't make sense
in the future).
Other parts were commented out with `#if 0`s and TODO warnings because
it doesn't make much sense to work on them yet as those parts will
change heavily when we switch to .NET Core but also when we start
introducing source generators.
As such, the following features were disabled temporarily:
- Assembly-reloading (will be done with ALCs in .NET Core).
- Properties/fields exports and script method listing (will be
  handled by source generators in the future).
- Exception logging in the editor and stack info for errors.
- Exporting games.
- Building of C# projects. We no longer copy the Godot API assemblies
  to the project directory, so MSBuild won't be able to find them. The
  idea is to turn them into NuGet packages in the future, which could
  also be obtained from local NuGet sources during development.
2021-09-22 06:38:00 +02:00

229 lines
7.2 KiB
C#

#if REAL_T_IS_DOUBLE
using real_t = System.Double;
#else
using real_t = System.Single;
#endif
using System;
using System.Collections.Generic;
using Godot.NativeInterop;
// TODO: Add comments describing what this class does. It is not obvious.
namespace Godot
{
public static partial class GD
{
public static unsafe object Bytes2Var(byte[] bytes, bool allowObjects = false)
{
using var varBytes = Marshaling.mono_array_to_PackedByteArray(bytes);
using godot_variant ret = default;
NativeFuncs.godotsharp_bytes2var(&varBytes, allowObjects, &ret);
return Marshaling.variant_to_mono_object(&ret);
}
public static unsafe object Convert(object what, Variant.Type type)
{
using var whatVariant = Marshaling.mono_object_to_variant(what);
using godot_variant ret = default;
NativeFuncs.godotsharp_convert(&whatVariant, type, &ret);
return Marshaling.variant_to_mono_object(&ret);
}
public static real_t Db2Linear(real_t db)
{
return (real_t)Math.Exp(db * 0.11512925464970228420089957273422);
}
private static string[] GetPrintParams(object[] parameters)
{
if (parameters == null)
{
return new[] { "null" };
}
return Array.ConvertAll(parameters, x => x?.ToString() ?? "null");
}
public static unsafe int Hash(object var)
{
using var variant = Marshaling.mono_object_to_variant(var);
return NativeFuncs.godotsharp_hash(&variant);
}
public static Object InstanceFromId(ulong instanceId)
{
return InteropUtils.UnmanagedGetManaged(NativeFuncs.godotsharp_instance_from_id(instanceId));
}
public static real_t Linear2Db(real_t linear)
{
return (real_t)(Math.Log(linear) * 8.6858896380650365530225783783321);
}
public static Resource Load(string path)
{
return ResourceLoader.Load(path);
}
public static T Load<T>(string path) where T : class
{
return ResourceLoader.Load<T>(path);
}
public static unsafe void PushError(string message)
{
using var godotStr = Marshaling.mono_string_to_godot(message);
NativeFuncs.godotsharp_pusherror(&godotStr);
}
public static unsafe void PushWarning(string message)
{
using var godotStr = Marshaling.mono_string_to_godot(message);
NativeFuncs.godotsharp_pushwarning(&godotStr);
}
public static unsafe void Print(params object[] what)
{
string str = string.Concat(GetPrintParams(what));
using var godotStr = Marshaling.mono_string_to_godot(str);
NativeFuncs.godotsharp_print(&godotStr);
}
public static void PrintStack()
{
Print(System.Environment.StackTrace);
}
public static unsafe void PrintErr(params object[] what)
{
string str = string.Concat(GetPrintParams(what));
using var godotStr = Marshaling.mono_string_to_godot(str);
NativeFuncs.godotsharp_printerr(&godotStr);
}
public static unsafe void PrintRaw(params object[] what)
{
string str = string.Concat(GetPrintParams(what));
using var godotStr = Marshaling.mono_string_to_godot(str);
NativeFuncs.godotsharp_printraw(&godotStr);
}
public static unsafe void PrintS(params object[] what)
{
string str = string.Join(' ', GetPrintParams(what));
using var godotStr = Marshaling.mono_string_to_godot(str);
NativeFuncs.godotsharp_prints(&godotStr);
}
public static unsafe void PrintT(params object[] what)
{
string str = string.Join('\t', GetPrintParams(what));
using var godotStr = Marshaling.mono_string_to_godot(str);
NativeFuncs.godotsharp_printt(&godotStr);
}
public static float Randf()
{
return NativeFuncs.godotsharp_randf();
}
public static uint Randi()
{
return NativeFuncs.godotsharp_randi();
}
public static void Randomize()
{
NativeFuncs.godotsharp_randomize();
}
public static double RandRange(double from, double to)
{
return NativeFuncs.godotsharp_randf_range(from, to);
}
public static int RandRange(int from, int to)
{
return NativeFuncs.godotsharp_randi_range(from, to);
}
public static uint RandFromSeed(ref ulong seed)
{
return NativeFuncs.godotsharp_rand_from_seed(seed, out seed);
}
public static IEnumerable<int> Range(int end)
{
return Range(0, end, 1);
}
public static IEnumerable<int> Range(int start, int end)
{
return Range(start, end, 1);
}
public static IEnumerable<int> Range(int start, int end, int step)
{
if (end < start && step > 0)
yield break;
if (end > start && step < 0)
yield break;
if (step > 0)
{
for (int i = start; i < end; i += step)
yield return i;
}
else
{
for (int i = start; i > end; i += step)
yield return i;
}
}
public static void Seed(ulong seed)
{
NativeFuncs.godotsharp_seed(seed);
}
public static unsafe string Str(params object[] what)
{
using var whatGodotArray = Marshaling.mono_array_to_Array(what);
using godot_string ret = default;
NativeFuncs.godotsharp_str(&whatGodotArray, &ret);
return Marshaling.mono_string_from_godot(ret);
}
public static unsafe object Str2Var(string str)
{
using var godotStr = Marshaling.mono_string_to_godot(str);
using godot_variant ret = default;
NativeFuncs.godotsharp_str2var(&godotStr, &ret);
return Marshaling.variant_to_mono_object(&ret);
}
public static unsafe byte[] Var2Bytes(object var, bool fullObjects = false)
{
using var variant = Marshaling.mono_object_to_variant(var);
using godot_packed_byte_array varBytes = default;
NativeFuncs.godotsharp_var2bytes(&variant, fullObjects, &varBytes);
using (varBytes)
return Marshaling.PackedByteArray_to_mono_array(&varBytes);
}
public static unsafe string Var2Str(object var)
{
using var variant = Marshaling.mono_object_to_variant(var);
using godot_string ret = default;
NativeFuncs.godotsharp_var2str(&variant, &ret);
return Marshaling.mono_string_from_godot(ret);
}
public static Variant.Type TypeToVariantType(Type type)
{
return Marshaling.managed_to_variant_type(type, out bool _);
}
}
}