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

140 lines
4.8 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
{
class RendererInterop
{
#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;
};
#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
2020-08-13 06:58:53 +02:00
public static List<Capabilities> GetCompatibleRenderers(string gamePath)
{
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(gamePath));
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
List<Capabilities> Capabilities = new List<Capabilities>();
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
RendererInterop.GFXCapabilityInfo interopCaps = new RendererInterop.GFXCapabilityInfo();
string path = Path.Combine(file.DirectoryName, file.Name);
if (RendererInterop.GetRendererCapabilities(path, ref interopCaps))
{
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
Capabilities caps = new Capabilities(path, ref interopCaps);
Capabilities.Add(caps);
//cmbRenderer.Items.Add(caps);
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
return Capabilities;
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
// Select highest priority renderer
//cmbRenderer.SelectedItem = _RendererCaps.Max();
}
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public class Capabilities : IComparable
{
[Flags]
public enum RendererFlag
{
LowBitDepthAllowed = 0x1,
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
// Multisampling support flags:
MSAA2x = 0x2,
MSAA4x = 0x4,
MSAA8x = 0x8,
MSAA16x = 0x10,
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
// Other options:
VSync = 0x20,
TripleBuffer = 0x40,
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
};
2020-08-10 06:58:51 +02:00
2020-08-13 06:58:53 +02:00
public Capabilities(string filePath, ref RendererInterop.GFXCapabilityInfo gfxCaps)
{
this.FilePath = filePath;
this.FileName = Path.GetFileName(filePath);
this.MaxAnisotropy = gfxCaps.maxAnisotropy;
this.Flags = (RendererFlag)gfxCaps.flags;
this.Priority = gfxCaps.priority;
this.Name = gfxCaps.rendererName;
2020-08-13 06:58:53 +02:00
}
public override string ToString()
{
return string.Format("{0} ({1})", this.Name, Path.GetFileName(this.FilePath));
}
public int CompareTo(object obj)
{
if (obj == null) return 1;
Capabilities other = obj as Capabilities;
if (other != null)
return this.Priority.CompareTo(other.Priority);
else
throw new ArgumentException();
}
public string FilePath { get; private set; }
public string FileName { get; private set; }
public int MaxAnisotropy { get; private set; }
public RendererFlag Flags { get; private set; }
public int Priority { get; private set; }
public string Name { get; private set; }
}
2020-08-10 06:58:51 +02:00
}
}