openDecal/Managed/Decal.Adapter/Decal.Adapter.Support/Util.cs
erik d1442e3747 Initial commit: Complete open-source Decal rebuild
All 5 phases of the open-source Decal rebuild:

Phase 1: 14 decompiled .NET projects (Interop.*, Adapter, FileService, DecalUtil)
Phase 2: 10 native DLLs rewritten as C# COM servers with matching GUIDs
  - DecalDat, DHS, SpellFilter, DecalInput, DecalNet, DecalFilters
  - Decal.Core, DecalControls, DecalRender, D3DService
Phase 3: C++ shims for Inject.DLL (D3D9 hooking) and LauncherHook.DLL
Phase 4: DenAgent WinForms tray application
Phase 5: WiX installer and build script

25 C# projects building with 0 errors.
Native C++ projects require VS 2022 + Windows SDK (x86).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 18:27:56 +01:00

176 lines
3.7 KiB
C#

#define TRACE
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using Decal.Interop.Core;
using Microsoft.CSharp;
using Microsoft.Win32;
namespace Decal.Adapter.Support;
internal static class Util
{
private static int mTrace;
internal static int TraceLevel
{
get
{
return mTrace;
}
set
{
mTrace = value;
}
}
internal static Color ColorFromBGR(int color)
{
return Color.FromArgb(color & 0xFF, (color & 0xFF00) >> 8, (color & 0xFF0000) >> 16);
}
internal static int ColorToBGR(Color color)
{
return (color.B << 16) | (color.G << 8) | color.R;
}
internal static Rectangle toRectangle(tagRECT tr)
{
return new Rectangle(tr.left, tr.top, tr.right - tr.left, tr.bottom - tr.top);
}
internal static tagRECT toTagRECT(Rectangle r)
{
tagRECT result = default(tagRECT);
result.top = r.Top;
result.left = r.Left;
result.bottom = r.Bottom;
result.right = r.Right;
return result;
}
internal static T UnboxTo<T>(object obj)
{
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
try
{
return (T)obj;
}
catch (InvalidCastException)
{
TypeConverter converter = ((CodeDomProvider)new CSharpCodeProvider()).GetConverter(obj.GetType());
if (converter.CanConvertTo(typeof(T)))
{
return (T)converter.ConvertTo(obj, typeof(T));
}
WriteLine("Error Converting object type {0} to {1}", obj.GetType(), typeof(T));
throw;
}
}
internal static void InitializeTracing(string key, string value, int defaultValue)
{
try
{
TraceLevel = (int)Registry.GetValue(key, value, defaultValue);
}
catch
{
TraceLevel = 0;
}
}
public static void WriteLine(string fmt, params object[] args)
{
WriteLine(1, fmt, args);
}
public static void WriteLine(int traceLevel, string fmt, params object[] args)
{
WriteLine(traceLevel, string.Format(fmt, args));
}
public static void WriteLine(int traceLevel, string msg)
{
Trace.WriteLineIf(mTrace >= traceLevel, msg);
}
public static void Write(string fmt, params object[] args)
{
Write(1, string.Format(fmt, args));
}
public static void Write(int traceLevel, string msg)
{
Trace.WriteIf(mTrace >= traceLevel, msg);
}
public static void SafeFireEvent(object sender, EventHandler eventHandler, EventArgs args)
{
if (eventHandler == null)
{
return;
}
Delegate[] invocationList = eventHandler.GetInvocationList();
for (int i = 0; i < invocationList.Length; i++)
{
EventHandler eventHandler2 = (EventHandler)invocationList[i];
try
{
eventHandler2(sender, args);
}
catch (Exception ex)
{
WriteLine("SafeFire exception: {0}", ex.ToString());
}
}
}
public static void SafeFireEvent<T>(object sender, EventHandler<T> eventHandler, T args) where T : EventArgs
{
if (eventHandler == null)
{
return;
}
Delegate[] invocationList = eventHandler.GetInvocationList();
for (int i = 0; i < invocationList.Length; i++)
{
EventHandler<T> eventHandler2 = (EventHandler<T>)invocationList[i];
try
{
eventHandler2(sender, args);
}
catch (Exception ex)
{
WriteLine("SafeFire exception: {0}", ex.ToString());
}
}
}
public static void SafeFireEvent<T>(object sender, EventHandler<T> eventHandler, T args, ref bool eaten) where T : EatableEventArgs
{
if (eventHandler == null)
{
return;
}
Delegate[] invocationList = eventHandler.GetInvocationList();
for (int i = 0; i < invocationList.Length; i++)
{
EventHandler<T> eventHandler2 = (EventHandler<T>)invocationList[i];
try
{
eventHandler2(sender, args);
if (args.Eat)
{
eaten = true;
}
}
catch (Exception ex)
{
WriteLine("SafeFire exception: {0}", ex.ToString());
}
}
}
}