Beispiel #1
0
 public void Dispose()
 {
     NgscomMarshal.FreeBString(p);
     p = IntPtr.Zero;
 }
Beispiel #2
0
 public void IntendedContents()
 {
     Assert.Equal("Pudding in the freezy🎈🎉!", NgscomMarshal.BStringToString(p));
 }
Beispiel #3
0
 public void IntendedLength()
 {
     Assert.Equal(30, NgscomMarshal.GetBStringLength(p));
 }
Beispiel #4
0
        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;
            }
        }