mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-01-18 18:46:43 +00:00
Pointers are headache
This commit is contained in:
parent
33037a40fc
commit
1ca007b8d5
2 changed files with 53 additions and 28 deletions
|
@ -1,4 +1,5 @@
|
||||||
using Capy64.API;
|
using Capy64.API;
|
||||||
|
using Capy64.Runtime.Libraries;
|
||||||
using KeraLua;
|
using KeraLua;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
@ -13,16 +14,13 @@ namespace Capy64.Runtime;
|
||||||
|
|
||||||
public class ObjectManager : IPlugin
|
public class ObjectManager : IPlugin
|
||||||
{
|
{
|
||||||
private static ConcurrentDictionary<nint, object> _objects = new();
|
|
||||||
|
|
||||||
private static IGame _game;
|
private static IGame _game;
|
||||||
public ObjectManager(IGame game)
|
public ObjectManager(IGame game)
|
||||||
{
|
{
|
||||||
_game = game;
|
_game = game;
|
||||||
_game.EventEmitter.OnClose += OnClose;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void PushObject<T>(Lua L, T obj)
|
public static unsafe void PushObject<T>(Lua L, T obj)
|
||||||
{
|
{
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
{
|
{
|
||||||
|
@ -30,52 +28,79 @@ public class ObjectManager : IPlugin
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = L.NewUserData(1);
|
var p = (nint*)L.NewUserData(sizeof(nint));
|
||||||
_objects[p] = obj;
|
var handle = GCHandle.Alloc(obj);
|
||||||
|
var op = GCHandle.ToIntPtr(handle);
|
||||||
|
*p = op;
|
||||||
|
//_objects[p] = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T ToObject<T>(Lua L, int index, bool freeGCHandle = true)
|
public static unsafe T ToObject<T>(Lua L, int index, bool freeGCHandle = true)
|
||||||
{
|
{
|
||||||
if (L.IsNil(index) || !L.IsUserData(index))
|
if (L.IsNil(index) || !L.IsUserData(index))
|
||||||
return default(T);
|
return default(T);
|
||||||
|
|
||||||
var data = L.ToUserData(index);
|
/*if (p == IntPtr.Zero)
|
||||||
if (data == IntPtr.Zero)
|
|
||||||
return default(T);
|
return default(T);
|
||||||
|
|
||||||
if (!_objects.ContainsKey(data))
|
/*if (!_objects.ContainsKey(data))
|
||||||
return default(T);
|
return default(T);
|
||||||
|
|
||||||
var reference = (T)_objects[data];
|
var reference = (T)_objects[data];*/
|
||||||
|
|
||||||
|
var p = (nint*)L.ToUserData(index);
|
||||||
|
var op = *p;
|
||||||
|
var handle = GCHandle.FromIntPtr(op);
|
||||||
|
|
||||||
|
var reference = handle.Target;
|
||||||
|
if (reference == null)
|
||||||
|
return default(T);
|
||||||
|
|
||||||
|
T value;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
value = (T)reference;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
value = default;
|
||||||
|
}
|
||||||
|
|
||||||
if (freeGCHandle)
|
if (freeGCHandle)
|
||||||
_objects.Remove(data, out _);
|
handle.Free();
|
||||||
|
|
||||||
return reference;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T CheckObject<T>(Lua L, int argument, string typeName, bool freeGCHandle = true)
|
public static unsafe T CheckObject<T>(Lua L, int argument, string typeName, bool freeGCHandle = true)
|
||||||
{
|
{
|
||||||
if (L.IsNil(argument) || !L.IsUserData(argument))
|
if (L.IsNil(argument) || !L.IsUserData(argument))
|
||||||
return default(T);
|
return default(T);
|
||||||
|
|
||||||
IntPtr data = L.CheckUserData(argument, typeName);
|
var p = (nint*)L.CheckUserData(argument, typeName);
|
||||||
if (data == IntPtr.Zero)
|
var op = *p;
|
||||||
|
var handle = GCHandle.FromIntPtr(op);
|
||||||
|
|
||||||
|
if (!handle.IsAllocated)
|
||||||
|
return default;
|
||||||
|
|
||||||
|
var reference = handle.Target;
|
||||||
|
if (reference == null)
|
||||||
return default(T);
|
return default(T);
|
||||||
|
|
||||||
if (!_objects.ContainsKey(data))
|
T value;
|
||||||
return default(T);
|
try
|
||||||
|
{
|
||||||
var reference = (T)_objects[data];
|
value = (T)reference;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
value = default;
|
||||||
|
}
|
||||||
|
|
||||||
if (freeGCHandle)
|
if (freeGCHandle)
|
||||||
_objects.Remove(data, out _);
|
handle.Free();
|
||||||
|
|
||||||
return reference;
|
return value;
|
||||||
}
|
|
||||||
|
|
||||||
private void OnClose(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
_objects.Clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,7 +165,7 @@ public class GPUBuffer : IPlugin
|
||||||
private static unsafe int LM_ToString(IntPtr state)
|
private static unsafe int LM_ToString(IntPtr state)
|
||||||
{
|
{
|
||||||
var L = Lua.FromIntPtr(state);
|
var L = Lua.FromIntPtr(state);
|
||||||
var buffer = ToBuffer(L);
|
var buffer = ToBuffer(L, false);
|
||||||
if (buffer is not null)
|
if (buffer is not null)
|
||||||
{
|
{
|
||||||
L.PushString("GPUBuffer ({0:X})", (ulong)&buffer);
|
L.PushString("GPUBuffer ({0:X})", (ulong)&buffer);
|
||||||
|
|
Loading…
Reference in a new issue