Ejemplo n.º 1
0
        /// <summary>
        /// Perform needed configuration.
        /// </summary>
        public static void Setup()
        {
            if (Ready)
            {
                Engine.Log.Warning("Native libraries are already setup.", MessageSource.Bootstrap);
                return;
            }

            Engine.Log.Info("Adfectus Native Loader", MessageSource.Bootstrap);
            Engine.Log.Info("-----------", MessageSource.Bootstrap);
            Engine.Log.Info($"OS: {RuntimeInformation.OSDescription}", MessageSource.Bootstrap);
            Engine.Log.Info($"64Bit: {Environment.Is64BitProcess}", MessageSource.Bootstrap);
            Engine.Log.Info($"Execution Directory: {Environment.CurrentDirectory}", MessageSource.Bootstrap);
            Engine.Log.Info($"Execution Assembly: {Assembly.GetCallingAssembly()}", MessageSource.Bootstrap);

            // Get lib folder and libs depending on platform.
            Dictionary <string, string> libs = null;

            // Check if a 64 bit system.
            if (RuntimeInformation.OSArchitecture != Architecture.X64)
            {
                ErrorHandler.SubmitError(new Exception("Only 64bit OSes are supported."));
                return;
            }

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                LibFolder = $"{Environment.CurrentDirectory}\\Libraries\\win\\";
                libs      = LibrariesWindows;
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // Check for capitalized folder. Yes, really.
                if (Directory.Exists($"{Environment.CurrentDirectory}/Libraries/MacOS/"))
                {
                    LibFolder = $"{Environment.CurrentDirectory}/Libraries/MacOS/";
                }

                LibFolder = $"{Environment.CurrentDirectory}/Libraries/macOS/";

                libs = LibrariesMacOs;
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                // Check for capitalized folder. Yes, really.
                if (Directory.Exists($"{Environment.CurrentDirectory}/Libraries/Linux/"))
                {
                    LibFolder = $"{Environment.CurrentDirectory}/Libraries/Linux/";
                }

                LibFolder = $"{Environment.CurrentDirectory}/Libraries/linux/";

                libs = LibrariesLinux;
            }

            // If no lib folder or libs - exit.
            if (LibFolder == null || libs == null)
            {
                ErrorHandler.SubmitError(new Exception("Cannot identify library folder or libraries to load."));
                return;
            }

            // Check if the folder exists.
            if (!Directory.Exists(LibFolder))
            {
                ErrorHandler.SubmitError(new Exception($"Library folder {LibFolder} not found."));
                return;
            }

            Engine.Log.Info($"Library Folder: {LibFolder}", MessageSource.Bootstrap);

            // Load libraries.
            LoadedLibraries = new Dictionary <string, IntPtr>();
            foreach (KeyValuePair <string, string> libArr in libs)
            {
                string curLib = libArr.Value;
                Engine.Log.Trace($"Loading library {curLib}...", MessageSource.Bootstrap);

                // Get the true path to the file.
                string libPath = $"{LibFolder}{curLib}";

                // Check if the file exists.
                if (!File.Exists(libPath))
                {
                    Engine.Log.Warning($"Couldn't load library {curLib} - it doesn't exist.", MessageSource.Bootstrap);
                    continue;
                }

                // Load the library.
                bool success = NativeLibrary.TryLoad(libPath, out IntPtr libAddr);
                if (!success)
                {
                    ErrorHandler.SubmitError(new Exception($"Couldn't load library {curLib}."));
                }
                else
                {
                    // Add to the list of loaded libraries with the specified key. Library invokers will use this afterward.
                    LoadedLibraries.Add(libArr.Key, libAddr);
                    Engine.Log.Trace($"Loaded library at address {libAddr}.", MessageSource.Bootstrap);
                }
            }

            // Init libraries.
            FreeImage.Init(LoadedLibraries["freeimage"]);
            FT.Init(LoadedLibraries["freetype"]);
            Al.Init(LoadedLibraries["openal"]);
            Alc.Init(LoadedLibraries["openal"]);

            Ready = true;
            Engine.Log.Info("Bootstrap complete!", MessageSource.Bootstrap);
        }