static EngineLoaderHelper() { EnginePath = Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location); string envValue = Environment.GetEnvironmentVariable("NGS_ENGINE_PATH"); if (!string.IsNullOrWhiteSpace(envValue)) { EnginePath = envValue; } try { string helperPath; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { helperPath = Path.Combine(EnginePath, "ngsloader.dll"); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { helperPath = Path.Combine(EnginePath, "libngsloader.dylib"); } else { helperPath = Path.Combine(EnginePath, "libngsloader.so"); } dynamicLibrary = DynamicLibrary.Load(helperPath); } catch (Exception ex) { loadError = ex; } }
static EngineInstance() { try { // Load the loader config string enginePath = EngineLoaderHelper.EnginePath; string loaderConfigPath = Path.Combine(enginePath, LoaderConfigFileName); if (!File.Exists(loaderConfigPath)) { throw new Exception($"Could not locate the engine loader configuration file ({LoaderConfigFileName})."); } EngineLoaderConfig loaderConfig; using (var stream = File.OpenRead(loaderConfigPath)) { var serializer = new XmlSerializer(typeof(EngineLoaderConfig)); loaderConfig = (EngineLoaderConfig)serializer.Deserialize(stream); } // Get the processor capability var processorInfo = EngineLoaderHelper.ProcessorInfo; // Choose the engine image string imagePath = null; foreach (var imageConfig in loaderConfig.Images) { if (imageConfig.SupportedPlatforms == null) { throw new Exception("Invalid loader configuration file: Must support at least one platform."); } if (imageConfig.Path == null) { throw new Exception("Invalid loader configuration file: Path cannot be null."); } bool processorCompatible = imageConfig.RequiredProcessorFeatures? .All(processorInfo.SupportsFeature) ?? true; bool platformCompatible = imageConfig.SupportedPlatforms .Any(name => { switch (name) { case "Windows": return(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); case "OSX": return(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); case "Linux": return(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); default: return(false); } }); var thisPath = Path.Combine(enginePath, imageConfig.Path); if (processorCompatible && platformCompatible && File.Exists(thisPath)) { imagePath = thisPath; break; } } if (imagePath == null) { throw new Exception("Could not find a engine image compatible with the current platform and processor."); } var library = DynamicLibrary.Load(imagePath); var entryPtr = library.GetSymbol("ngsengine_create"); if (entryPtr == IntPtr.Zero) { throw new Exception("Could not find the engine entry point."); } var entryDelegate = Marshal.GetDelegateForFunctionPointer <NgsEngineCreate>(entryPtr); Marshal.ThrowExceptionForHR(entryDelegate(out var enginePtr)); nativeEngine = NgscomMarshal.GetRcwForInterfacePtr <INgsEngine>(enginePtr, false); dynamicLibrary = library; } catch (Exception ex) { loadError = ex; } }