Files
sbox-public/engine/Tools/InteropGen/Arguments/ArgManagedClass.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

63 lines
1.5 KiB
C#

namespace Facepunch.InteropGen;
public class ArgManagedClass : Arg
{
public Class Class { get; set; }
public ArgManagedClass( Class c, string name, string[] flags )
{
Class = c;
Name = name;
Flags = flags;
}
public override string ManagedType => "global::" + Class.ManagedNameWithNamespace;
public override string ManagedDelegateType => "uint";
public override string GetManagedDelegateType( bool incoming )
{
return "uint";
}
public override string NativeType => $"uint";
public override string NativeDelegateType => "uint";
public override string FromInterop( bool native, string code = null )
{
code ??= Name;
//
// Passing a managed class to native. Incoming is a pointer - so we crea
// a new instance of the class. In reality we should only be calling this
// once per class to save a new instance.
//
if ( native )
{
return code;
}
//
// Getting a managed class back from interop. We use Facepunch.Interop.NativePointer's built in
// function to try to convert it from a GHandle to a Facepunch.Interop.NativePointer, then to the
// target class.
//
return $"Sandbox.InteropSystem.Get<{Class.ManagedNameWithNamespace}>( {code} )";
}
public override string ToInterop( bool native, string code = null )
{
code ??= Name;
//
// Passing a managed class to native - we just use the .NativePointer property
// Which should be using the NativePointer class to create a GCHandle.
//
return !native ? $" Sandbox.InteropSystem.GetAddress( {code}, true )" : $"{code}";
}
}