Replace GetProcAddrOrNull with GetProcAddrOrZero

both in ILinkedLibManager and its inheritors, and in IImportResolver and its
inheritors; see aa8fe56ef
This commit is contained in:
YoshiRulz 2020-05-16 12:50:33 +10:00
parent 06aa00bc82
commit 4baefd8746
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
7 changed files with 33 additions and 33 deletions

View File

@ -95,9 +95,9 @@ namespace BizHawk.BizInvoke
} }
} }
public IntPtr? GetProcAddrOrNull(string entryPoint) => EntryPoints.TryGetValue(entryPoint, out var ret) ? ret : (IntPtr?) null; public IntPtr GetProcAddrOrZero(string entryPoint) => EntryPoints.TryGetValue(entryPoint, out var ret) ? ret : IntPtr.Zero;
public IntPtr GetProcAddrOrThrow(string entryPoint) => GetProcAddrOrNull(entryPoint) ?? throw new InvalidOperationException($"could not find {entryPoint} in exports"); public IntPtr GetProcAddrOrThrow(string entryPoint) => EntryPoints.TryGetValue(entryPoint, out var ret) ? ret : throw new InvalidOperationException($"could not find {entryPoint} in exports");
} }
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 GetProcAddrOrZero(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 GetProcAddrOrZero(string entryPoint) => OSTailoredCode.LinkedLibManager.GetProcAddrOrZero(_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 GetProcAddrOrZero(string procName) => OSTailoredCode.LinkedLibManager.GetProcAddrOrZero(HModule, procName);
public IntPtr GetProcAddrOrThrow(string procName) => OSTailoredCode.LinkedLibManager.GetProcAddrOrThrow(HModule, procName); public IntPtr GetProcAddrOrThrow(string procName) => OSTailoredCode.LinkedLibManager.GetProcAddrOrThrow(HModule, procName);
@ -106,16 +106,20 @@ namespace BizHawk.Common
_resolvers = resolvers.ToList(); _resolvers = resolvers.ToList();
} }
public IntPtr? GetProcAddrOrNull(string entryPoint) public IntPtr GetProcAddrOrZero(string entryPoint)
{ {
for (var i = _resolvers.Count - 1; i != -1; i--) for (var i = _resolvers.Count - 1; i != 0; i--)
{ {
var ret = _resolvers[i].GetProcAddrOrNull(entryPoint); var ret = _resolvers[i].GetProcAddrOrZero(entryPoint);
if (ret != null) return ret.Value; if (ret != IntPtr.Zero) return ret;
} }
return null; return _resolvers[0].GetProcAddrOrZero(entryPoint); // if it's Zero/NULL, return it anyway - the search failed
} }
public IntPtr GetProcAddrOrThrow(string entryPoint) => GetProcAddrOrNull(entryPoint) ?? throw new InvalidOperationException($"{entryPoint} was not found in any of the aggregated resolvers"); public IntPtr GetProcAddrOrThrow(string entryPoint)
{
var ret = GetProcAddrOrZero(entryPoint);
return ret != IntPtr.Zero ? ret : throw new InvalidOperationException($"{entryPoint} was not found in any of the aggregated resolvers");
}
} }
} }

View File

@ -34,7 +34,7 @@ namespace BizHawk.Common
{ {
int FreeByPtr(IntPtr hModule); int FreeByPtr(IntPtr hModule);
IntPtr? GetProcAddrOrNull(IntPtr hModule, string procName); IntPtr GetProcAddrOrZero(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,17 +61,13 @@ 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 GetProcAddrOrZero(IntPtr hModule, string procName) => dlsym(hModule, procName);
{
var p = dlsym(hModule, procName);
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 = GetProcAddrOrZero(hModule, procName);
if (p != null) return p.Value; if (p != IntPtr.Zero) 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)}")}");
} }
@ -104,13 +100,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 GetProcAddrOrZero(IntPtr hModule, string procName) => GetProcAddress(hModule, procName);
{
var p = GetProcAddress(hModule, procName);
return p == IntPtr.Zero ? (IntPtr?) null : p;
}
public IntPtr GetProcAddrOrThrow(IntPtr hModule, string procName) => GetProcAddrOrNull(hModule, procName) ?? throw new InvalidOperationException($"got null pointer from {nameof(GetProcAddress)}, error code: {GetLastError()}"); public IntPtr GetProcAddrOrThrow(IntPtr hModule, string procName)
{
var ret = GetProcAddrOrZero(hModule, procName);
return ret != IntPtr.Zero ? ret : throw new InvalidOperationException($"got null pointer from {nameof(GetProcAddress)}, error code: {GetLastError()}");
}
public IntPtr? LoadOrNull(string dllToLoad) public IntPtr? LoadOrNull(string dllToLoad)
{ {

View File

@ -60,8 +60,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64.NativeApi
videoplugin); videoplugin);
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.GetProcAddrOrZero(GfxDll, "GetScreenTextureID");
if (funcPtr != null) GFXGetScreenTextureID = (GetScreenTextureID) Marshal.GetDelegateForFunctionPointer(funcPtr.Value, typeof(GetScreenTextureID)); if (funcPtr != IntPtr.Zero) 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

@ -107,8 +107,8 @@ namespace BizHawk.Emulation.Cores.Waterbox
var imports = _parent._exports[moduleName]; var imports = _parent._exports[moduleName];
var pointers = entries.Select(e => var pointers = entries.Select(e =>
{ {
var ptr = imports.GetProcAddrOrNull(e); var ptr = imports.GetProcAddrOrZero(e);
if (ptr != null) return ptr.Value; if (ptr != IntPtr.Zero) return ptr;
var s = $"Trapped on unimplemented function {moduleName}:{e}"; var s = $"Trapped on unimplemented function {moduleName}:{e}";
Action del = () => Action del = () =>
{ {
@ -1104,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 GetProcAddrOrZero(string entryPoint) => _modules[0].GetProcAddrOrZero(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,9 +304,9 @@ namespace BizHawk.Emulation.Cores.Waterbox
} }
} }
public IntPtr? GetProcAddrOrNull(string entryPoint) => ExportsByName.TryGetValue(entryPoint, out var ret) ? ret : (IntPtr?) null; public IntPtr GetProcAddrOrZero(string entryPoint) => ExportsByName.TryGetValue(entryPoint, out var ret) ? ret : IntPtr.Zero;
public IntPtr GetProcAddrOrThrow(string entryPoint) => GetProcAddrOrNull(entryPoint) ?? throw new InvalidOperationException($"could not find {entryPoint} in exports"); public IntPtr GetProcAddrOrThrow(string entryPoint) => ExportsByName.TryGetValue(entryPoint, out var ret) ? ret : throw new InvalidOperationException($"could not find {entryPoint} in exports");
public void ConnectImports(string moduleName, IImportResolver module) public void ConnectImports(string moduleName, IImportResolver module)
{ {

View File

@ -95,7 +95,7 @@ namespace BizHawk.Emulation.DiscSystem
{ {
var lib = OSTailoredCode.LinkedLibManager.LoadOrNull("mednadisc.dll"); var lib = OSTailoredCode.LinkedLibManager.LoadOrNull("mednadisc.dll");
_IsLibraryAvailable = lib != null _IsLibraryAvailable = lib != null
&& OSTailoredCode.LinkedLibManager.GetProcAddrOrNull(lib.Value, "mednadisc_LoadCD") != null; && OSTailoredCode.LinkedLibManager.GetProcAddrOrZero(lib.Value, "mednadisc_LoadCD") != IntPtr.Zero;
if (lib != null) OSTailoredCode.LinkedLibManager.FreeByPtr(lib.Value); if (lib != null) OSTailoredCode.LinkedLibManager.FreeByPtr(lib.Value);
} }