.net bindings for Urho3D


#21

Silly me , I read your Wiki ,
For some reason I thought Urho3D.dll and Urho3DCSharp.dll are Dot Net Dlls
But they are the Windows counterparts of libUrho3D.dylib and libUrho3DCSharp.dylib.
Anyway I am getting exceptions on my Mac either using libUrho3D.dylib or libUrho3DCSharp.dylib .
I barely used Dot Net in the past (always CPP,C,Assembly) , this is a new territory for me.
I Will have more spare time during the weekend to continue playing with it.


#22

What kind of exceptions? It is true that i have not tested this code on Mac OS because no hardware. Just made sure everything compiles.


#23

Elis-MBP:bin elialoni$ csc Program.cs -r:Urho3DNet.dll
Microsoft ® Visual C# Compiler version 2.3.2.61928 (ec1cde8b)
Copyright © Microsoft Corporation. All rights reserved.

Elis-MBP:bin elialoni$ mono Program.exe
mono_os_mutex_lock: pthread_mutex_lock failed with “Invalid argument” (22)
Stacktrace:

at <0xffffffff>
at (wrapper managed-to-native) Urho3D.Context.Urho3DRegisterMonoInternalCalls () [0x00002] in :0
at Urho3D.Context…cctor () [0x00001] in :0
at (wrapper runtime-invoke) object.runtime_invoke_void (object,intptr,intptr,intptr) [0x0001e] in :0
at <0xffffffff>
at (wrapper managed-to-native) object.__icall_wrapper_mono_generic_class_init (intptr) [0x00000] in :0
at DemoApplication.Program.Main (string[]) [0x00001] in :0
at (wrapper runtime-invoke) .runtime_invoke_void_object (object,intptr,intptr,intptr) [0x0004e] in :0

Native stacktrace:

0   mono                                0x00000001019feb01 mono_handle_native_crash + 257
1   libsystem_platform.dylib            0x00007fff6751ff5a _sigtramp + 26
2   libmonosgen-2.0.1.dylib             0x000000010586a008 simple_lower_case_mapping_higharea_table0 + 20520
3   libsystem_c.dylib                   0x00007fff672bd1ae abort + 127
4   libmonosgen-2.0.1.dylib             0x00000001057aca52 monoeg_g_log + 0
5   libmonosgen-2.0.1.dylib             0x00000001057ac9d7 monoeg_g_logv + 83
6   libmonosgen-2.0.1.dylib             0x00000001057acaca monoeg_g_log + 120
7   libmonosgen-2.0.1.dylib             0x00000001056a444e mono_icall_lock + 72
8   libmonosgen-2.0.1.dylib             0x00000001056a43c4 mono_add_internal_call + 21
9   libUrho3DCSharp.dylib               0x000000010439a1ba Urho3DRegisterMonoInternalCalls + 26
10  ???                                 0x0000000101ecf527 0x0 + 4327273767
11  mono                                0x0000000101953a07 mono_jit_runtime_invoke + 1383
12  mono                                0x0000000101b15db4 do_runtime_invoke + 84
13  mono                                0x0000000101b11f04 mono_runtime_class_init_full + 996
14  mono                                0x00000001019af3d5 mono_generic_class_init + 21
15  ???                                 0x0000000101ecf2f7 0x0 + 4327273207
16  ???                                 0x0000000101ecf239 0x0 + 4327273017
17  mono                                0x0000000101953a07 mono_jit_runtime_invoke + 1383
18  mono                                0x0000000101b15db4 do_runtime_invoke + 84
19  mono                                0x0000000101b193d9 do_exec_main_checked + 137
20  mono                                0x00000001019c23bf mono_jit_exec + 287
21  mono                                0x00000001019c4bb4 mono_main + 9140
22  mono                                0x00000001019433cd main + 253
23  mono                                0x00000001019432c4 start + 52
24  ???                                 0x0000000000000002 0x0 + 2

Debug info from gdb:

(lldb) command source -s 0 ‘/tmp/mono-gdb-commands.2ms21c’
Executing commands in ‘/tmp/mono-gdb-commands.2ms21c’.
(lldb) process attach --pid 37629
warning: (x86_64) /Library/Frameworks/Mono.framework/Versions/5.8.1/lib/mono/4.5/mscorlib.dll.dylib empty dSYM file detected, dSYM was created with an executable with no debug info.
Process 37629 stopped

  • thread #1, name = ‘tid_307’, queue = ‘com.apple.main-thread’, stop reason = signal SIGSTOP
    frame #0: 0x00007fff67362232 libsystem_kernel.dylib__wait4 + 10 libsystem_kernel.dylib__wait4:
    -> 0x7fff67362232 <+10>: jae 0x7fff6736223c ; <+20>
    0x7fff67362234 <+12>: movq %rax, %rdi
    0x7fff67362237 <+15>: jmp 0x7fff67358b25 ; cerror
    0x7fff6736223c <+20>: retq
    Target 0: (mono) stopped.

Executable module set to “/Library/Frameworks/Mono.framework/Versions/Current/Commands/mono”.
Architecture set to: x86_64h-apple-macosx.
(lldb) thread list
Process 37629 stopped

  • thread #1: tid = 0x64999, 0x00007fff67362232 libsystem_kernel.dylib__wait4 + 10, name = 'tid_307', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP thread #2: tid = 0x6499e, 0x00007fff67361a1e libsystem_kernel.dylib__psynch_cvwait + 10, name = ‘SGen worker’
    thread #3: tid = 0x6499f, 0x00007fff67358246 libsystem_kernel.dylib`semaphore_wait_trap + 10, name = ‘Finalizer’
    (lldb) thread backtrace all
  • thread #1, name = ‘tid_307’, queue = ‘com.apple.main-thread’, stop reason = signal SIGSTOP
    • frame #0: 0x00007fff67362232 libsystem_kernel.dylib__wait4 + 10 frame #1: 0x00000001019feb8e monomono_handle_native_crash(signal=, ctx=, info=) at mini-exceptions.c:2726 [opt]
      frame #2: 0x00007fff6751ff5a libsystem_platform.dylib_sigtramp + 26 frame #3: 0x00007fff67361b6f libsystem_kernel.dylib__pthread_kill + 11
      frame #4: 0x00007fff6752c080 libsystem_pthread.dylibpthread_kill + 333 frame #5: 0x00007fff672bd1ae libsystem_c.dylibabort + 127
      frame #6: 0x00000001057aca52 libmonosgen-2.0.1.dylibmonoeg_log_default_handler + 105 frame #7: 0x00000001057ac9d7 libmonosgen-2.0.1.dylibmonoeg_g_logv + 83
      frame #8: 0x00000001057acaca libmonosgen-2.0.1.dylibmonoeg_g_log + 120 frame #9: 0x00000001056a444e libmonosgen-2.0.1.dylibmono_icall_lock + 72
      frame #10: 0x00000001056a43c4 libmonosgen-2.0.1.dylibmono_add_internal_call + 21 frame #11: 0x000000010439a1ba libUrho3DCSharp.dylibUrho3DRegisterMonoInternalCalls + 26
      frame #12: 0x0000000101ecf527
      frame #13: 0x0000000101953a07 monomono_jit_runtime_invoke(method=<unavailable>, obj=<unavailable>, params=0x0000000000000000, exc=0x00007fdcee844610, error=<unavailable>) at mini-runtime.c:2800 [opt] frame #14: 0x0000000101b15db4 monodo_runtime_invoke(method=0x00007fdcef0141e0, obj=0x0000000000000000, params=0x0000000000000000, exc=0x00007ffeee2be1d8, error=0x00007ffeee2be220) at object.c:2849 [opt]
      frame #15: 0x0000000101b11f04 monomono_runtime_class_init_full [inlined] mono_runtime_try_invoke(method=<unavailable>, obj=<unavailable>, params=<unavailable>, error=0x00007ffeee2be220) at object.c:2956 [opt] frame #16: 0x0000000101b11ebf monomono_runtime_class_init_full(vtable=0x00007fdcee001c70, error=0x00007ffeee2be220) at object.c:473 [opt]
      frame #17: 0x00000001019af3d5 monomono_generic_class_init(vtable=<unavailable>) at jit-icalls.c:1462 [opt] frame #18: 0x0000000101ecf2f7 frame #19: 0x0000000101ecf239 frame #20: 0x0000000101953a07 monomono_jit_runtime_invoke(method=, obj=, params=0x00007ffeee2be5b8, exc=0x00007fdcee001c70, error=) at mini-runtime.c:2800 [opt]
      frame #21: 0x0000000101b15db4 monodo_runtime_invoke(method=0x00007fdced407f98, obj=0x0000000000000000, params=0x00007ffeee2be5b8, exc=0x0000000000000000, error=0x00007ffeee2be5f8) at object.c:2849 [opt] frame #22: 0x0000000101b193d9 monodo_exec_main_checked [inlined] mono_runtime_invoke_checked(method=, obj=, error=) at object.c:3002 [opt]
      frame #23: 0x0000000101b19398 monodo_exec_main_checked(method=0x00007fdced407f98, args=<unavailable>, error=0x00007ffeee2be5f8) at object.c:4726 [opt] frame #24: 0x00000001019c23bf monomono_jit_exec(domain=, assembly=, argc=1, argv=0x00007ffeee2be918) at driver.g.c:1040 [opt]
      frame #25: 0x00000001019c4bb4 monomono_main [inlined] main_thread_handler at driver.g.c:1109 [opt] frame #26: 0x00000001019c4b81 monomono_main(argc=2, argv=) at driver.g.c:2222 [opt]
      frame #27: 0x00000001019433cd monomain [inlined] mono_main_with_options(argc=<unavailable>, argv=<unavailable>) at main.c:46 [opt] frame #28: 0x00000001019433b9 monomain(argc=2, argv=) at main.c:339 [opt]
      frame #29: 0x00000001019432c4 monostart + 52 thread #2, name = 'SGen worker' frame #0: 0x00007fff67361a1e libsystem_kernel.dylib__psynch_cvwait + 10
      frame #1: 0x00007fff6752a589 libsystem_pthread.dylib_pthread_cond_wait + 732 frame #2: 0x0000000101bcec9e monothread_func [inlined] mono_os_cond_wait(mutex=) at mono-os-mutex.h:173 [opt]
      frame #3: 0x0000000101bcec8b monothread_func at sgen-thread-pool.c:165 [opt] frame #4: 0x0000000101bcec7d monothread_func(data=0x0000000000000000) at sgen-thread-pool.c:196 [opt]
      frame #5: 0x00007fff67529661 libsystem_pthread.dylib_pthread_body + 340 frame #6: 0x00007fff6752950d libsystem_pthread.dylib_pthread_start + 377
      frame #7: 0x00007fff67528bf9 libsystem_pthread.dylibthread_start + 13 thread #3, name = 'Finalizer' frame #0: 0x00007fff67358246 libsystem_kernel.dylibsemaphore_wait_trap + 10
      frame #1: 0x0000000101b7b2fc monofinalizer_thread [inlined] mono_os_sem_wait(flags=MONO_SEM_FLAGS_ALERTABLE) at mono-os-semaphore.h:90 [opt] frame #2: 0x0000000101b7b2f1 monofinalizer_thread at mono-coop-semaphore.h:43 [opt]
      frame #3: 0x0000000101b7b2e5 monofinalizer_thread(unused=<unavailable>) at gc.c:866 [opt] frame #4: 0x0000000101b375b0 monostart_wrapper [inlined] start_wrapper_internal at threads.c:1003 [opt]
      frame #5: 0x0000000101b37513 monostart_wrapper(data=<unavailable>) at threads.c:1063 [opt] frame #6: 0x00007fff67529661 libsystem_pthread.dylib_pthread_body + 340
      frame #7: 0x00007fff6752950d libsystem_pthread.dylib_pthread_start + 377 frame #8: 0x00007fff67528bf9 libsystem_pthread.dylibthread_start + 13
      (lldb) detach

=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.

Abort trap: 6
(lldb) quit


#24

Does 102 sample run?


#25

The output of the crash is from Program.cs from sample 102

I copied Program.cs to the build/bin folder
csc Program.cs -r:Urho3DNet.dll
mono Program.exe


#26

Maybe try sample that gets built by cmake


#27

@rku Do you have any plan to make Nuget package?


#28

No immediate plans. Unsure how useful it would be without rest of the tools though.


#29

Hey @Omid and anyone else interested - nuget is here!

Here is how to get it working:

  1. Create C# project
  2. Install rbfx.Urho3DNet package
  3. Set your project framework to .net 4.7.1
  4. Set your project cpu architecture to x64

A sample Program.cs to get things running faster:

//
// Copyright (c) 2017-2019 Rokas Kupstys.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.Diagnostics;
using System.IO;
using Urho3DNet;

namespace DemoApplication
{
    [ObjectFactory]
    class RotateObject : LogicComponent
    {
        public RotateObject(Context context) : base(context)
        {
            SetUpdateEventMask(UpdateEvent.UseUpdate);
        }

        public override void Update(float timeStep)
        {
            var d = new Quaternion(10 * timeStep, 20 * timeStep, 30 * timeStep);
            GetNode().Rotate(d);
        }
    }

    class DemoApplication : Application
    {
        private Scene _scene;
        private Viewport _viewport;
        private Node _camera;
        private Node _cube;
        private Node _light;

        public DemoApplication(Context context) : base(context)
        {
        }

        public override void Setup()
        {
            var currentDir = Directory.GetCurrentDirectory();
            EngineParameters[Urho3D.EpFullScreen] = false;
            EngineParameters[Urho3D.EpWindowWidth] = 1920;
            EngineParameters[Urho3D.EpWindowHeight] = 1080;
            EngineParameters[Urho3D.EpWindowTitle] = "Hello C#";
            EngineParameters[Urho3D.EpResourcePaths] = "CoreData";
            EngineParameters[Urho3D.EpResourcePrefixPaths] = $"{currentDir};{currentDir}/..";
        }

        public override void Start()
        {
            GetInput().SetMouseVisible(true);

            // Viewport
            _scene = new Scene(GetContext());
            _scene.CreateComponent<Octree>();

            _camera = _scene.CreateChild("Camera");
            _viewport = new Viewport(GetContext());
            _viewport.SetScene(_scene);
            _viewport.SetCamera(_camera.CreateComponent<Camera>());
            GetRenderer().SetViewport(0, _viewport);

            // Background
            GetRenderer().GetDefaultZone().SetFogColor(new Color(0.5f, 0.5f, 0.7f));

            // Scene
            _camera.SetPosition(new Vector3(0, 2, -2));
            _camera.LookAt(Vector3.Zero);

            // Cube
            _cube = _scene.CreateChild("Cube");
            var model = _cube.CreateComponent<StaticModel>();
            model.SetModel(GetCache().GetResource<Model>("Models/Box.mdl"));
            model.SetMaterial(0, GetCache().GetResource<Material>("Materials/DefaultGrey.xml"));
            var rotator = _cube.CreateComponent<RotateObject>();

            // Light
            _light = _scene.CreateChild("Light");
            _light.CreateComponent<Light>();
            _light.SetPosition(new Vector3(0, 2, -1));
            _light.LookAt(Vector3.Zero);
        }
    }

    internal class Program
    {
        public static void Main(string[] args)
        {
            using (var context = new Context())
            {
                using (var application = new DemoApplication(context))
                {
                    application.Run();
                }
            }
        }
    }
}

Edit:
On gitter we discovered that package does not work correctly with old style .csproj projects. If you run into problems - use sample csproj for testing.


#30

Thanks. Good job :+1: