Commit graph

431 commits

Author SHA1 Message Date
Ignacio Roldán Etcheverry 50b603c7dc C#: Begin move to .NET Core
We're targeting .NET 5 for now to make development easier while
.NET 6 is not yet released.

TEMPORARY REGRESSIONS
---------------------

Assembly unloading is not implemented yet. As such, many Godot
resources are leaked at exit. This will be re-implemented later
together with assembly hot-reloading.
2021-09-22 08:27:12 +02:00
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
Ignacio Roldán Etcheverry 483071716e C#: Move marshaling logic and generated glue to C#
We will be progressively moving most code to C#.
The plan is to only use Mono's embedding APIs to set things at launch.
This will make it much easier to later support CoreCLR too which
doesn't have rich embedding APIs.

Additionally the code in C# is more maintainable and makes it easier
to implement new features, e.g.: runtime codegen which we could use to
avoid using reflection for marshaling everytime a field, property or
method is accessed.

SOME NOTES ON INTEROP

We make the same assumptions as GDNative about the size of the Godot
structures we use. We take it a bit further by also assuming the layout
of fields in some cases, which is riskier but let's us squeeze out some
performance by avoiding unnecessary managed to native calls.

Code that deals with native structs is less safe than before as there's
no RAII and copy constructors in C#. It's like using the GDNative C API
directly. One has to take special care to free values they own.
Perhaps we could use roslyn analyzers to check this, but I don't know
any that uses attributes to determine what's owned or borrowed.

As to why we maily use pointers for native structs instead of ref/out:
- AFAIK (and confirmed with a benchmark) ref/out are pinned
  during P/Invoke calls and that has a cost.
- Native struct fields can't be ref/out in the first place.
- A `using` local can't be passed as ref/out, only `in`. Calling a
  method or property on an `in` value makes a silent copy, so we want
  to avoid `in`.

REGARDING THE BUILD SYSTEM

There's no longer a `mono_glue=yes/no` SCons options. We no longer
need to build with `mono_glue=no`, generate the glue and then build
again with `mono_glue=yes`. We build only once and generate the glue
(which is in C# now).
However, SCons no longer builds the C# projects for us. Instead one
must run `build_assemblies.py`, e.g.:
```sh
%godot_src_root%/modules/mono/build_scripts/build_assemblies.py \
        --godot-output-dir=%godot_src_root%/bin \
        --godot-target=release_debug`
```
We could turn this into a custom build target, but I don't know how
to do that with SCons (it's possible with Meson).

OTHER NOTES

Most of the moved code doesn't follow the C# naming convention and
still has the word Mono in the names despite no longer dealing with
Mono's embedding APIs. This is just temporary while transitioning,
to make it easier to understand what was moved where.
2021-08-20 10:24:56 +02:00
Max Hilbrunner 81f7d1890b Namespaces instead of underscore prefix for binds
Thanks to neikeq for the initial work.

Co-authored-by: Ignacio Roldán Etcheverry <neikeq@users.noreply.github.com>
2021-08-17 16:10:09 +02:00
Aaron Franke 03e2544d50
Use real_t and double where appropriate in Particles 2021-08-12 11:35:31 -05:00
Raul Santos f20db58271 Ensure MSBuildPanel buttons are instantiated 2021-08-06 22:30:38 +02:00
Yuri Sizov d3e413c659 Fix the editor theme application for the Mono build log 2021-08-03 22:13:32 +03:00
Rémi Verschelde 7cbdb9b4c3
Merge pull request #50917 from raulsntos/more-iterators 2021-07-27 08:05:15 +02:00
Raul Santos d636ebbfe9 Ignore paths with invalid chars in PathWhich 2021-07-27 04:16:20 +02:00
Raul Santos 3fe67fb5ad Use C++ iterators in the Mono module 2021-07-27 02:48:28 +02:00
Raul Santos accd05f4ad Use Array.Empty instead of allocating a every time
Use `System.Array.Empty<T>` to get an empty array instead of allocating
a new one every time. Since arrays are immutable there is no need to
allocate them every time.
2021-07-26 03:52:31 +02:00
Raul Santos b3274a7064 Fix bindings generator range iterator errors 2021-07-25 21:32:54 +02:00
Rémi Verschelde ac3322b0af
Use const references where possible for List range iterators 2021-07-25 12:22:25 +02:00
Ignacio Roldán Etcheverry 2ee395a277
Merge pull request #50757 from aaronfranke/simple-cs-editorconfig
Add a simple C# `.editorconfig`
2021-07-24 21:48:41 +02:00
Aaron Franke 4e6efd1b07
Use C++ iterators for Lists in many situations 2021-07-23 17:38:28 -04:00
Aaron Franke 5f8275d9ac
Add a simple C# .editorconfig 2021-07-23 17:04:53 -04:00
reduz 6631f66c2a Optimize StringName usage
* Added a new macro SNAME() that constructs and caches a local stringname.
* Subsequent usages use the cached version.
* Since these use a global static variable, a second refcounter of static usages need to be kept for cleanup time.
* Replaced all theme usages by this new macro.
* Replace all signal emission usages by this new macro.
* Replace all call_deferred usages by this new macro.

This is part of ongoing work to optimize GUI and the editor.
2021-07-18 21:20:02 -03:00
Rémi Verschelde 671467b888
Mono: Remove info dialog discouraging use in production
While there are still various bugs to solve and features to implement, the C#
support as of Godot 3.4 is fairly mature and already used by a number of users
in production. Now that we default to dotnet CLI as build tool, it also seems
to be more reliable than MSBuild.

The documentation can (and does for the most part) point out some caveats that
users should be aware of, but this info dialog has outlived its intended
purpose.
2021-07-15 11:48:19 +02:00
Grzegorz Puławski fa82727f5b Fixing mono build after instance() -> instanciate() name change 2021-06-23 16:07:23 +02:00
Lightning_A e28fd07b2b Rename instance()->instantiate() when it's a verb 2021-06-19 20:49:18 -06:00
Aaron Franke e6c7567da7
Fix a typo in C# bindings generator for default value types 2021-06-18 01:13:00 -04:00
Rémi Verschelde 600b4c9c7b
Merge pull request #34668 from aaronfranke/to-string
[Core] Reformat structure string operators
2021-06-13 11:58:24 +02:00
Pedro J. Estébanez f59488e94e Improve & fix Mono build
- Fix C++ compile errors about pending variable renames after the `Reference` to `RefCount` change.
- Fix C# compile errors due to the recent rename of `EnablePlugin()` and `Build()`, which are now underscore-prefixed in bindings.
- Additional rename: `godot_icall_Reference_Dtor` to  `godot_icall_RefCounted_Dtor`.
2021-06-13 11:21:14 +02:00
Pedro J. Estébanez 04688b92ff Rename Reference to RefCounted 2021-06-11 18:48:42 +02:00
Aaron Franke 554c776e08
Reformat structure string operators
The order of numbers is not changed except for Transform2D. All logic is done inside of their structures (and not in Variant).

For the number of decimals printed, they now use String::num_real which works best with real_t, except for Color which is fixed at 4 decimals (this is a reliable number of float digits when converting from 16-bpc so it seems like a good choice)
2021-06-11 10:53:20 -04:00
Rémi Verschelde 9e328bb5b7
Core: Move DirAccess and FileAccess to core/io
File handling APIs are typically considered part of I/O, and we did have most
`FileAccess` implementations in `core/io` already.
2021-06-11 14:52:39 +02:00
Marcel Admiraal 8acd13a456 Rename Quat to Quaternion 2021-06-04 18:14:32 +01:00
Aaron Franke a3c29ed899
Rename files and the exposed name for Transform3D 2021-06-03 07:30:01 -04:00
Aaron Franke 08a85352fb
Rename Variant TRANSFORM to TRANSFORM3D
Also _transform to _transform3d
2021-06-03 07:30:01 -04:00
Aaron Franke de3f6699a5
Rename Transform to Transform3D in core 2021-06-03 07:30:01 -04:00
Rémi Verschelde 5b16020846
Replace remaining uses of NULL with nullptr
Follow-up to #38736 (these uses were likely added after this PR was merged).
2021-04-29 11:53:27 +02:00
Rémi Verschelde 8368f53941
Merge pull request #46954 from neikeq/reggr-46307
C#: Fix StringName leak warnings after generating bindings
2021-03-13 09:13:34 +01:00
Ignacio Etcheverry d9603b2d73 C#: Fix StringName leak warnings after generating bindings
`Main::cleanup()` prints warnings if it finds `StringName`s still alive.
We need the `BindingsGenerator` to be destructed before calling cleanup.
2021-03-13 01:16:31 +01:00
Ignacio Etcheverry ee8e5146a4 C#: Fix ScriptPathAttribute generator with none or nested namespaces
The following two bugs were fixed:
- For classes without namespace we were still generating `namespace {`
without a namespace identifier, causing a syntax error.
- For classes with nested namespaces we were generating only the innermost
part of the namespace was being generated, e.g.: for `Foo.Bar` we were
generating `namespace Bar {` instead of `namespace Foo.Bar {`.
This wasn't causing any build error, but because of the wrong namespace
Godot wasn't able to find the class associated with the script.
2021-03-13 01:04:59 +01:00
Rémi Verschelde 15bd2bf03f
Merge pull request #46713 from neikeq/csharp-source-generators-init
Add C# source generator for ScriptPathAttribute
2021-03-07 01:04:47 +01:00
Ignacio Etcheverry e2afe700f6 Add C# source generator for a new ScriptPath attribute
This source generator adds a newly introduced attribute,
`ScriptPath` to all classes that:

- Are top-level classes (not inner/nested).
- Have the `partial` modifier.
- Inherit `Godot.Object`.
- The class name matches the file name.

A build error is thrown if the generator finds a class that meets these
conditions but is not declared `partial`, unless the class is annotated
with the `DisableGodotGenerators` attribute.

We also generate an `AssemblyHasScripts` assembly attribute which Godot
uses to get all the script classes in the assembly, eliminating the need
for Godot to search them. We can also avoid searching in assemblies that
don't have this attribute. This will be good for performance in the
future once we support multiple assemblies with Godot script classes.

This is an example of what the generated code looks like:

```
using Godot;
namespace Foo {
	[ScriptPathAttribute("res://Player.cs")]
	// Multiple partial declarations are allowed
	[ScriptPathAttribute("res://Foo/Player.cs")]
	partial class Player {}
}

[assembly:AssemblyHasScripts(new System.Type[] { typeof(Foo.Player) })]
```

The new attributes replace script metadata which we were generating by
determining the namespace of script classes with a very simple parser.
This fixes several issues with the old approach related to parser
errors and conditional compilation.
It also makes the task part of the MSBuild project build, rather than
a separate step executed by the Godot editor.
2021-03-06 21:50:32 +01:00
Aaron Franke 4b4721f5bd
Fix C# bindings generator for default value types 2021-02-25 08:30:48 -05:00
Pedro J. Estébanez 23907e6f19 Make glue generation shutdown more graceful 2021-02-22 11:06:33 +01:00
Rémi Verschelde ed8333f6bc
Merge pull request #45158 from aaronfranke/cs-packedarray
Add C# array features from core PackedArrays
2021-02-20 09:42:30 +01:00
Rémi Verschelde 8eaea1db53
Merge pull request #45032 from neikeq/classdb-tests-for-44856
Add ClassDB tests to look for core API deps on editor API
2021-02-18 15:16:52 +01:00
Aaron Franke 614299aedb
Add C# array features from core PackedArrays 2021-02-16 15:24:44 -05:00
bruvzg e304f716c7
[Mono] Use the same search logic for both MSBuild and dotnet, add custom search paths on macOS. 2021-02-04 22:39:38 +02:00
Ivan Shakhov deef9a73a5 Fix off by one error navigating to line number in Rider
Fixes https://github.com/JetBrains/godot-support/issues/61
2021-01-28 22:41:12 +01:00
Rémi Verschelde 76c6007aa6
Cleanup: Remove executable bit from files which don't need it
Drop unused xpmfix.sh script.
2021-01-19 23:36:42 +01:00
Rémi Verschelde 0b409d89d0
Merge pull request #45136 from akien-mga/clang-format-11
CI: Update to clang-format 11 and apply ternary operator changes
2021-01-13 16:10:44 +01:00
Marcel Admiraal 3ee860e3d1 Update Mono module to use new execute method. 2021-01-13 09:34:14 +00:00
Rémi Verschelde af878716f2
CI: Update to clang-format 11 and apply ternary operator changes 2021-01-12 19:32:53 +01:00
Ignacio Etcheverry fafdc0b0c1 Add ClassDB tests to look for core API deps on editor API
The ClassDB tests will detect when the core API has dependencies on
the editor API, which is not allowed.

This should prevent or warn early about issues like #44856
2021-01-09 00:18:03 +01:00
Rémi Verschelde b5334d14f7
Update copyright statements to 2021
Happy new year to the wonderful Godot community!

2020 has been a tough year for most of us personally, but a good year for
Godot development nonetheless with a huge amount of work done towards Godot
4.0 and great improvements backported to the long-lived 3.2 branch.

We've had close to 400 contributors to engine code this year, authoring near
7,000 commit! (And that's only for the `master` branch and for the engine code,
there's a lot more when counting docs, demos and other first-party repos.)

Here's to a great year 2021 for all Godot users 🎆
2021-01-01 20:19:21 +01:00
Marcel Admiraal 5b937d493f Rename empty() to is_empty() 2020-12-28 10:39:56 +00:00