1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-23 14:45:37 +01:00
GiantsTools/Giants.Launcher/Native/RendererInterop.cs

80 lines
2.9 KiB
C#
Raw Normal View History

2020-08-10 06:58:51 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Giants.Launcher
{
public class RendererInterop
2020-08-10 06:58:51 +02:00
{
#pragma warning disable 649
public struct GFXCapabilityInfo
{
public int maxAnisotropy;
2020-08-13 06:58:53 +02:00
public uint flags;
2020-08-10 06:58:51 +02:00
public int priority;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string rendererName;
public int interfaceVersion;
2020-08-10 06:58:51 +02:00
};
#pragma warning restore 649
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool GFXGetCapabilities(ref GFXCapabilityInfo outCapabilities);
2020-08-13 06:58:53 +02:00
/// <summary>
/// Makes interop call to native renderer DLL to obtain capability information.
/// </summary>
/// <returns>True if the given renderer is supported by the system.</returns>
public static bool GetRendererCapabilities(string dllPath, ref GFXCapabilityInfo capabilities)
{
bool rendererSupported = false;
IntPtr pDll = NativeMethods.LoadLibrary(dllPath);
if (pDll == IntPtr.Zero)
throw new System.Exception(string.Format("LoadLibrary() for {0} failed", dllPath));
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "GFXGetCapabilities");
if (pAddressOfFunctionToCall == IntPtr.Zero)
return false;
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
var prcGetCapabilities = (GFXGetCapabilities)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GFXGetCapabilities));
rendererSupported = prcGetCapabilities(ref capabilities);
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
NativeMethods.FreeLibrary(pDll);
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
return rendererSupported;
}
2020-08-10 06:58:51 +02:00
public static List<RendererInfo> GetCompatibleRenderers(string gamePath)
2020-08-13 06:58:53 +02:00
{
var dir = new DirectoryInfo(
Path.GetDirectoryName(gamePath));
var capabilities = new List<RendererInfo>();
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
// Search current directory for compatible renderers:
foreach (FileInfo file in dir.GetFiles("gg_*.dll"))
{
try
{
// Make interop call to native renderer DLLs to get capability info
var interopCaps = new RendererInterop.GFXCapabilityInfo();
2020-08-13 06:58:53 +02:00
string path = Path.Combine(file.DirectoryName, file.Name);
if (GetRendererCapabilities(path, ref interopCaps) && interopCaps.priority >= 0)
2020-08-13 06:58:53 +02:00
{
RendererInfo caps = new RendererInfo(path, ref interopCaps);
capabilities.Add(caps);
2020-08-13 06:58:53 +02:00
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
2020-08-13 06:58:53 +02:00
}
}
2020-08-10 06:58:51 +02:00
return capabilities;
2020-08-13 06:58:53 +02:00
}
2020-08-10 06:58:51 +02:00
}
}