53 lines
1.2 KiB
C#
53 lines
1.2 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using PkmnLibSharp.Utils;
|
|
|
|
namespace PkmnLibSharp.FFI
|
|
{
|
|
internal struct NativeResult
|
|
{
|
|
private readonly IntPtr Error = IntPtr.Zero;
|
|
|
|
internal NativeResult(IntPtr error)
|
|
{
|
|
Error = error;
|
|
}
|
|
|
|
public void Result()
|
|
{
|
|
if (Error != IntPtr.Zero)
|
|
{
|
|
throw new PkmnLibException(Error.PtrString()!);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal struct NativeResult<T> where T : new()
|
|
{
|
|
private readonly IntPtr Error = IntPtr.Zero;
|
|
private readonly T Data = new();
|
|
|
|
internal NativeResult(IntPtr error, T data)
|
|
{
|
|
Error = error;
|
|
Data = data;
|
|
}
|
|
|
|
public T Result()
|
|
{
|
|
if (Error != IntPtr.Zero)
|
|
{
|
|
throw new PkmnLibException(Error.PtrString()!);
|
|
}
|
|
return Data;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// An error thrown by the library.
|
|
/// </summary>
|
|
public class PkmnLibException : Exception
|
|
{
|
|
internal PkmnLibException(string message) : base(message){}
|
|
}
|
|
} |