/// <summary> /// Calls alGetProcAddress and converts the resulting pointer into a delegate. /// </summary> /// <typeparam name="TDelegate">The delegate type to create.</typeparam> /// <param name="name">The name of the AL proc.</param> /// <returns>The created delegate.</returns> protected internal static TDelegate LoadDelegate <TDelegate>(string name) where TDelegate : Delegate { IntPtr ptr = AL.GetProcAddress(name); if (ptr == IntPtr.Zero) { // If we can't load the function for whatever reason we dynamically generate a delegate to // give the user an error message that is actually understandable. MethodInfo invoke = typeof(TDelegate).GetMethod("Invoke"); Type returnType = invoke.ReturnType; Type[] parameters = invoke.GetParameters().Select(p => p.ParameterType).ToArray(); DynamicMethod method = new DynamicMethod( "OpenAL_AL_Extension_Null_GetProcAddress_Exception_Delegate_" + Guid.NewGuid(), returnType, parameters); ILGenerator generator = method.GetILGenerator(); // Here we are generating a delegate that looks like this: // ((<the arguments that the delegate type takes>) => // throw new Exception(<error string>); generator.Emit(OpCodes.Ldstr, $"This OpenAL function could not be loaded. This likely means that this extension isn't present in the current context."); generator.Emit(OpCodes.Newobj, typeof(Exception).GetConstructor(new[] { typeof(string) })); generator.Emit(OpCodes.Throw); return((TDelegate)method.CreateDelegate(typeof(TDelegate))); } else { return(Marshal.GetDelegateForFunctionPointer <TDelegate>(ptr)); } }
/// <summary> /// Constructs a new XRamExtension instance. /// </summary> public XRamExtension() { // Query if Extension supported and retrieve Tokens/Pointers if it is. _valid = false; if (AL.IsExtensionPresent("EAX-RAM") == false) { return; } AL_EAX_RAM_SIZE = AL.GetEnumValue("AL_EAX_RAM_SIZE"); AL_EAX_RAM_FREE = AL.GetEnumValue("AL_EAX_RAM_FREE"); AL_STORAGE_AUTOMATIC = AL.GetEnumValue("AL_STORAGE_AUTOMATIC"); AL_STORAGE_HARDWARE = AL.GetEnumValue("AL_STORAGE_HARDWARE"); AL_STORAGE_ACCESSIBLE = AL.GetEnumValue("AL_STORAGE_ACCESSIBLE"); // Console.WriteLine("RamSize: {0} RamFree: {1} StorageAuto: {2} StorageHW: {3} StorageAccess: {4}",AL_EAX_RAM_SIZE,AL_EAX_RAM_FREE,AL_STORAGE_AUTOMATIC,AL_STORAGE_HARDWARE,AL_STORAGE_ACCESSIBLE); if (AL_EAX_RAM_SIZE == 0 || AL_EAX_RAM_FREE == 0 || AL_STORAGE_AUTOMATIC == 0 || AL_STORAGE_HARDWARE == 0 || AL_STORAGE_ACCESSIBLE == 0) { Debug.WriteLine("X-Ram: Token values could not be retrieved."); return; } // Console.WriteLine("Free Ram: {0} / {1}",GetRamFree( ),GetRamSize( )); try { Imported_GetBufferMode = (Delegate_GetBufferMode)Marshal.GetDelegateForFunctionPointer(AL.GetProcAddress("EAXGetBufferMode"), typeof(Delegate_GetBufferMode)); Imported_SetBufferMode = (Delegate_SetBufferMode)Marshal.GetDelegateForFunctionPointer(AL.GetProcAddress("EAXSetBufferMode"), typeof(Delegate_SetBufferMode)); } catch (Exception e) { Debug.WriteLine("X-Ram: Attempt to marshal function pointers with AL.GetProcAddress failed. " + e.ToString()); return; } _valid = true; }
public XRamExtension() { this._valid = false; if (!AL.IsExtensionPresent("EAX-RAM")) { return; } this.AL_EAX_RAM_SIZE = AL.GetEnumValue("AL_EAX_RAM_SIZE"); this.AL_EAX_RAM_FREE = AL.GetEnumValue("AL_EAX_RAM_FREE"); this.AL_STORAGE_AUTOMATIC = AL.GetEnumValue("AL_STORAGE_AUTOMATIC"); this.AL_STORAGE_HARDWARE = AL.GetEnumValue("AL_STORAGE_HARDWARE"); this.AL_STORAGE_ACCESSIBLE = AL.GetEnumValue("AL_STORAGE_ACCESSIBLE"); if (this.AL_EAX_RAM_SIZE == 0 || this.AL_EAX_RAM_FREE == 0 || (this.AL_STORAGE_AUTOMATIC == 0 || this.AL_STORAGE_HARDWARE == 0)) { return; } if (this.AL_STORAGE_ACCESSIBLE == 0) { return; } try { this.Imported_GetBufferMode = (XRamExtension.Delegate_GetBufferMode)Marshal.GetDelegateForFunctionPointer(AL.GetProcAddress("EAXGetBufferMode"), typeof(XRamExtension.Delegate_GetBufferMode)); this.Imported_SetBufferMode = (XRamExtension.Delegate_SetBufferMode)Marshal.GetDelegateForFunctionPointer(AL.GetProcAddress("EAXSetBufferMode"), typeof(XRamExtension.Delegate_SetBufferMode)); } catch (Exception ex) { return; } this._valid = true; }
/// <summary> /// Calls alGetProcAddress and converts the resulting pointer into a delegate. /// </summary> /// <typeparam name="TDelegate">The delegate type to create.</typeparam> /// <param name="name">The name of the AL proc.</param> /// <returns>The created delegate.</returns> protected internal static TDelegate LoadDelegate <TDelegate>(string name) where TDelegate : Delegate { IntPtr ptr = AL.GetProcAddress(name); return(Marshal.GetDelegateForFunctionPointer <TDelegate>(ptr)); }