Revert "Fix up Nullable<IntPtr> brain damage"

This reverts commit aa8fe56ef9.
This commit is contained in:
YoshiRulz 2020-05-16 12:35:04 +10:00 committed by James Groom
parent 9944c9d989
commit 3e51bbe5c2
6 changed files with 21 additions and 57 deletions

View File

@ -95,19 +95,9 @@ namespace BizHawk.BizInvoke
} }
} }
public IntPtr GetProcAddrOrNull(string entryPoint) public IntPtr? GetProcAddrOrNull(string entryPoint) => EntryPoints.TryGetValue(entryPoint, out var ret) ? ret : (IntPtr?) null;
{
EntryPoints.TryGetValue(entryPoint, out var ret);
return ret;
}
public IntPtr GetProcAddrOrThrow(string entryPoint) public IntPtr GetProcAddrOrThrow(string entryPoint) => GetProcAddrOrNull(entryPoint) ?? throw new InvalidOperationException($"could not find {entryPoint} in exports");
{
var ret = GetProcAddrOrNull(entryPoint);
if (ret == IntPtr.Zero)
throw new InvalidOperationException($"could not find {entryPoint} in exports");
return ret;
}
} }
static readonly Dictionary<Type, DelegateStorage> Impls = new Dictionary<Type, DelegateStorage>(); static readonly Dictionary<Type, DelegateStorage> Impls = new Dictionary<Type, DelegateStorage>();

View File

@ -10,7 +10,7 @@ namespace BizHawk.Common
/// <seealso cref="PatchImportResolver"/> /// <seealso cref="PatchImportResolver"/>
public interface IImportResolver public interface IImportResolver
{ {
IntPtr GetProcAddrOrNull(string entryPoint); IntPtr? GetProcAddrOrNull(string entryPoint);
/// <exception cref="InvalidOperationException">could not find symbol</exception> /// <exception cref="InvalidOperationException">could not find symbol</exception>
IntPtr GetProcAddrOrThrow(string entryPoint); IntPtr GetProcAddrOrThrow(string entryPoint);
@ -32,7 +32,7 @@ namespace BizHawk.Common
_p = OSTailoredCode.LinkedLibManager.LoadOrThrow(OSTailoredCode.IsUnixHost ? ResolveFilePath(dllName) : dllName); _p = OSTailoredCode.LinkedLibManager.LoadOrThrow(OSTailoredCode.IsUnixHost ? ResolveFilePath(dllName) : dllName);
} }
public IntPtr GetProcAddrOrNull(string entryPoint) => OSTailoredCode.LinkedLibManager.GetProcAddrOrNull(_p, entryPoint); public IntPtr? GetProcAddrOrNull(string entryPoint) => OSTailoredCode.LinkedLibManager.GetProcAddrOrNull(_p, entryPoint);
public IntPtr GetProcAddrOrThrow(string entryPoint) => OSTailoredCode.LinkedLibManager.GetProcAddrOrThrow(_p, entryPoint); public IntPtr GetProcAddrOrThrow(string entryPoint) => OSTailoredCode.LinkedLibManager.GetProcAddrOrThrow(_p, entryPoint);
@ -84,7 +84,7 @@ namespace BizHawk.Common
Environment.SetEnvironmentVariable("PATH", envpath, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PATH", envpath, EnvironmentVariableTarget.Process);
} }
public IntPtr GetProcAddrOrNull(string procName) => OSTailoredCode.LinkedLibManager.GetProcAddrOrNull(HModule, procName); public IntPtr? GetProcAddrOrNull(string procName) => OSTailoredCode.LinkedLibManager.GetProcAddrOrNull(HModule, procName);
public IntPtr GetProcAddrOrThrow(string procName) => OSTailoredCode.LinkedLibManager.GetProcAddrOrThrow(HModule, procName); public IntPtr GetProcAddrOrThrow(string procName) => OSTailoredCode.LinkedLibManager.GetProcAddrOrThrow(HModule, procName);
@ -106,23 +106,16 @@ namespace BizHawk.Common
_resolvers = resolvers.ToList(); _resolvers = resolvers.ToList();
} }
public IntPtr GetProcAddrOrNull(string entryPoint) public IntPtr? GetProcAddrOrNull(string entryPoint)
{ {
for (var i = _resolvers.Count - 1; i != -1; i--) for (var i = _resolvers.Count - 1; i != -1; i--)
{ {
var ret = _resolvers[i].GetProcAddrOrNull(entryPoint); var ret = _resolvers[i].GetProcAddrOrNull(entryPoint);
if (ret != IntPtr.Zero) if (ret != null) return ret.Value;
return ret;
} }
return IntPtr.Zero; return null;
} }
public IntPtr GetProcAddrOrThrow(string entryPoint) public IntPtr GetProcAddrOrThrow(string entryPoint) => GetProcAddrOrNull(entryPoint) ?? throw new InvalidOperationException($"{entryPoint} was not found in any of the aggregated resolvers");
{
var ret = GetProcAddrOrNull(entryPoint);
if (ret == IntPtr.Zero)
throw new InvalidOperationException($"{entryPoint} was not found in any of the aggregated resolvers");
return ret;
}
} }
} }

View File

@ -34,7 +34,7 @@ namespace BizHawk.Common
{ {
int FreeByPtr(IntPtr hModule); int FreeByPtr(IntPtr hModule);
IntPtr GetProcAddrOrNull(IntPtr hModule, string procName); IntPtr? GetProcAddrOrNull(IntPtr hModule, string procName);
/// <exception cref="InvalidOperationException">could not find symbol</exception> /// <exception cref="InvalidOperationException">could not find symbol</exception>
IntPtr GetProcAddrOrThrow(IntPtr hModule, string procName); IntPtr GetProcAddrOrThrow(IntPtr hModule, string procName);
@ -61,18 +61,17 @@ namespace BizHawk.Common
public int FreeByPtr(IntPtr hModule) => dlclose(hModule); public int FreeByPtr(IntPtr hModule) => dlclose(hModule);
public IntPtr GetProcAddrOrNull(IntPtr hModule, string procName) public IntPtr? GetProcAddrOrNull(IntPtr hModule, string procName)
{ {
var p = dlsym(hModule, procName); var p = dlsym(hModule, procName);
return p; return p == IntPtr.Zero ? (IntPtr?) null : p;
} }
public IntPtr GetProcAddrOrThrow(IntPtr hModule, string procName) public IntPtr GetProcAddrOrThrow(IntPtr hModule, string procName)
{ {
_ = dlerror(); // the Internet said to do this _ = dlerror(); // the Internet said to do this
var p = GetProcAddrOrNull(hModule, procName); var p = GetProcAddrOrNull(hModule, procName);
if (p != IntPtr.Zero) if (p != null) return p.Value;
return p;
var errCharPtr = dlerror(); var errCharPtr = dlerror();
throw new InvalidOperationException($"error in {nameof(dlsym)}{(errCharPtr == IntPtr.Zero ? string.Empty : $": {Marshal.PtrToStringAnsi(errCharPtr)}")}"); throw new InvalidOperationException($"error in {nameof(dlsym)}{(errCharPtr == IntPtr.Zero ? string.Empty : $": {Marshal.PtrToStringAnsi(errCharPtr)}")}");
} }
@ -105,19 +104,13 @@ namespace BizHawk.Common
public int FreeByPtr(IntPtr hModule) => FreeLibrary(hModule) ? 0 : 1; public int FreeByPtr(IntPtr hModule) => FreeLibrary(hModule) ? 0 : 1;
public IntPtr GetProcAddrOrNull(IntPtr hModule, string procName) public IntPtr? GetProcAddrOrNull(IntPtr hModule, string procName)
{ {
var p = GetProcAddress(hModule, procName); var p = GetProcAddress(hModule, procName);
return p; return p == IntPtr.Zero ? (IntPtr?) null : p;
} }
public IntPtr GetProcAddrOrThrow(IntPtr hModule, string procName) public IntPtr GetProcAddrOrThrow(IntPtr hModule, string procName) => GetProcAddrOrNull(hModule, procName) ?? throw new InvalidOperationException($"got null pointer from {nameof(GetProcAddress)}, error code: {GetLastError()}");
{
var ret = GetProcAddrOrNull(hModule, procName);
if (ret == IntPtr.Zero)
throw new InvalidOperationException($"got null pointer from {nameof(GetProcAddress)}, error code: {GetLastError()}");
return ret;
}
public IntPtr? LoadOrNull(string dllToLoad) public IntPtr? LoadOrNull(string dllToLoad)
{ {

View File

@ -61,8 +61,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64.NativeApi
GFXReadScreen2 = mupen64plusApi.GetTypedDelegate<ReadScreen2>(GfxDll, "ReadScreen2"); GFXReadScreen2 = mupen64plusApi.GetTypedDelegate<ReadScreen2>(GfxDll, "ReadScreen2");
GFXReadScreen2Res = mupen64plusApi.GetTypedDelegate<ReadScreen2Res>(GfxDll, "ReadScreen2"); GFXReadScreen2Res = mupen64plusApi.GetTypedDelegate<ReadScreen2Res>(GfxDll, "ReadScreen2");
var funcPtr = OSTailoredCode.LinkedLibManager.GetProcAddrOrNull(GfxDll, "GetScreenTextureID"); var funcPtr = OSTailoredCode.LinkedLibManager.GetProcAddrOrNull(GfxDll, "GetScreenTextureID");
if (funcPtr != IntPtr.Zero) if (funcPtr != null) GFXGetScreenTextureID = (GetScreenTextureID) Marshal.GetDelegateForFunctionPointer(funcPtr.Value, typeof(GetScreenTextureID));
GFXGetScreenTextureID = (GetScreenTextureID) Marshal.GetDelegateForFunctionPointer(funcPtr, typeof(GetScreenTextureID));
} }
public void GetScreenDimensions(ref int width, ref int height) public void GetScreenDimensions(ref int width, ref int height)

View File

@ -108,8 +108,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
var pointers = entries.Select(e => var pointers = entries.Select(e =>
{ {
var ptr = imports.GetProcAddrOrNull(e); var ptr = imports.GetProcAddrOrNull(e);
if (ptr != IntPtr.Zero) if (ptr != null) return ptr.Value;
return ptr;
var s = $"Trapped on unimplemented function {moduleName}:{e}"; var s = $"Trapped on unimplemented function {moduleName}:{e}";
Action del = () => Action del = () =>
{ {
@ -1105,7 +1104,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
} }
} }
public IntPtr GetProcAddrOrNull(string entryPoint) => _modules[0].GetProcAddrOrNull(entryPoint); // _modules[0] is always the main module public IntPtr? GetProcAddrOrNull(string entryPoint) => _modules[0].GetProcAddrOrNull(entryPoint); // _modules[0] is always the main module
public IntPtr GetProcAddrOrThrow(string entryPoint) => _modules[0].GetProcAddrOrThrow(entryPoint); public IntPtr GetProcAddrOrThrow(string entryPoint) => _modules[0].GetProcAddrOrThrow(entryPoint);

View File

@ -304,19 +304,9 @@ namespace BizHawk.Emulation.Cores.Waterbox
} }
} }
public IntPtr GetProcAddrOrNull(string entryPoint) public IntPtr? GetProcAddrOrNull(string entryPoint) => ExportsByName.TryGetValue(entryPoint, out var ret) ? ret : (IntPtr?) null;
{
ExportsByName.TryGetValue(entryPoint, out var ret);
return ret;
}
public IntPtr GetProcAddrOrThrow(string entryPoint) public IntPtr GetProcAddrOrThrow(string entryPoint) => GetProcAddrOrNull(entryPoint) ?? throw new InvalidOperationException($"could not find {entryPoint} in exports");
{
var ret = GetProcAddrOrNull(entryPoint);
if (ret == IntPtr.Zero)
throw new InvalidOperationException($"could not find {entryPoint} in exports");
return ret;
}
public void ConnectImports(string moduleName, IImportResolver module) public void ConnectImports(string moduleName, IImportResolver module)
{ {