Revert "Pointers are headache"

This reverts commit 1ca007b8d5.
This commit is contained in:
Alessandro Proto 2023-02-12 20:54:29 +01:00
parent 34cf7a6bc4
commit 6d9dbda7b5
2 changed files with 28 additions and 53 deletions

View file

@ -1,5 +1,4 @@
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;
@ -14,13 +13,16 @@ 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 unsafe void PushObject<T>(Lua L, T obj) public static void PushObject<T>(Lua L, T obj)
{ {
if (obj == null) if (obj == null)
{ {
@ -28,79 +30,52 @@ public class ObjectManager : IPlugin
return; return;
} }
var p = (nint*)L.NewUserData(sizeof(nint)); var p = L.NewUserData(1);
var handle = GCHandle.Alloc(obj); _objects[p] = obj;
var op = GCHandle.ToIntPtr(handle);
*p = op;
//_objects[p] = obj;
} }
public static unsafe T ToObject<T>(Lua L, int index, bool freeGCHandle = true) public static 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);
/*if (p == IntPtr.Zero) var data = L.ToUserData(index);
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)
handle.Free(); _objects.Remove(data, out _);
return value; return reference;
} }
public static unsafe T CheckObject<T>(Lua L, int argument, string typeName, bool freeGCHandle = true) public static 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);
var p = (nint*)L.CheckUserData(argument, typeName); IntPtr data = L.CheckUserData(argument, typeName);
var op = *p; if (data == IntPtr.Zero)
var handle = GCHandle.FromIntPtr(op);
if (!handle.IsAllocated)
return default;
var reference = handle.Target;
if (reference == null)
return default(T); return default(T);
T value; if (!_objects.ContainsKey(data))
try return default(T);
{
value = (T)reference; var reference = (T)_objects[data];
}
catch (Exception ex)
{
value = default;
}
if (freeGCHandle) if (freeGCHandle)
handle.Free(); _objects.Remove(data, out _);
return value; return reference;
}
private void OnClose(object sender, EventArgs e)
{
_objects.Clear();
} }
} }

View file

@ -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, false); var buffer = ToBuffer(L);
if (buffer is not null) if (buffer is not null)
{ {
L.PushString("GPUBuffer ({0:X})", (ulong)&buffer); L.PushString("GPUBuffer ({0:X})", (ulong)&buffer);