<commit_before>﻿using DotGame.Assets;
using DotGame.Audio;
using DotGame.Graphics;
using DotGame.Rendering;
using DotGame.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace DotGame
{
    public class Engine : IDisposable
    {
        public static IReadOnlyCollection<GraphicsAPI> SupportedGraphicsAPIs { get { if (supportedGraphicsAPIs == null) InitGraphicsAPIs(); return supportedGraphicsAPIs.AsReadOnly(); } }
        public static IReadOnlyCollection<AudioAPI> SupportedAudioAPIs { get { if (supportedAudioAPIs == null) InitAudioAPIs(); return supportedAudioAPIs.AsReadOnly(); } }
        private static List<GraphicsAPI> supportedGraphicsAPIs;
        private static List<AudioAPI> supportedAudioAPIs;

        [DllImport("kernel32", SetLastError = true)]
        private static extern IntPtr LoadLibrary(string lpFileName);
        private static bool CheckLibrary(string fileName)
        {
            return LoadLibrary(fileName) != IntPtr.Zero;
        }

        /// <summary>
        /// Prüft, ob die gegebene Grafik-API zur Verfügung steht.
        /// </summary>
        /// <param name="GraphicsAPI">Die Grafik-API.</param>
        /// <returns>True, wenn die Grafik-API zur Verfügung steht, ansonsten false.</returns>
        public static bool IsAPISupported(GraphicsAPI GraphicsAPI)
        {
            if (supportedGraphicsAPIs == null)
                InitGraphicsAPIs();

            return supportedGraphicsAPIs.Contains(GraphicsAPI);
        }

        private static void InitGraphicsAPIs()
        {
            supportedGraphicsAPIs = new List<GraphicsAPI>();
            // TODO (Joex3): Besseres Überprüfen?
            try
            {
                if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6 && CheckLibrary("d3d11.dll"))
                    supportedGraphicsAPIs.Add(GraphicsAPI.Direct3D11);
            }
            catch (DllNotFoundException) { }

            supportedGraphicsAPIs.Add(GraphicsAPI.OpenGL4);
        }

        /// <summary>
        /// Prüft, ob die gegebene Audio-API zur Verfügung steht.
        /// </summary>
        /// <param name="AudioAPI">Die Audio-API.</param>
        /// <returns>True, wenn die Audio-API zur Verfügung steht, ansonsten false.</returns>
        public static bool IsAPISupported(AudioAPI AudioAPI)
        {<commit_msg>Engine: Fix leak in CheckLibrary<commit_after>﻿using DotGame.Assets;
using DotGame.Audio;
using DotGame.Graphics;
using DotGame.Rendering;
using DotGame.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace DotGame
{
    public class Engine : IDisposable
    {
        public static IReadOnlyCollection<GraphicsAPI> SupportedGraphicsAPIs { get { if (supportedGraphicsAPIs == null) InitGraphicsAPIs(); return supportedGraphicsAPIs.AsReadOnly(); } }
        public static IReadOnlyCollection<AudioAPI> SupportedAudioAPIs { get { if (supportedAudioAPIs == null) InitAudioAPIs(); return supportedAudioAPIs.AsReadOnly(); } }
        private static List<GraphicsAPI> supportedGraphicsAPIs;
        private static List<AudioAPI> supportedAudioAPIs;

        /// <summary>
        /// Prüft, ob die gegebene Grafik-API zur Verfügung steht.
        /// </summary>
        /// <param name="GraphicsAPI">Die Grafik-API.</param>
        /// <returns>True, wenn die Grafik-API zur Verfügung steht, ansonsten false.</returns>
        public static bool IsAPISupported(GraphicsAPI GraphicsAPI)
        {
            if (supportedGraphicsAPIs == null)
                InitGraphicsAPIs();

            return supportedGraphicsAPIs.Contains(GraphicsAPI);
        }

        [DllImport("kernel32", SetLastError = true)]
        private static extern IntPtr LoadLibrary(string fileName);

        [DllImport("kernel32", SetLastError = true)]
        private static extern void FreeLibrary(IntPtr module);

        /// <summary>
        /// Checks the existence of a windows library.
        /// </summary>
        /// <param name="dll"></param>
        /// <returns></returns>
        private static bool CheckLibrary(string fileName)
        {
            // Check if system is windows
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
                return false;

            try
            {
                IntPtr ptr = LoadLibrary(fileName);
                if (ptr == IntPtr.Zero) // Library not loaded
                    return false;

                FreeLibrary(ptr);
                return true;
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            return false;
        }


        private static void InitGraphicsAPIs()
        {
            supportedGraphicsAPIs = new List<GraphicsAPI>();
            // TODO (Joex3): Besseres Überprüfen?
            try
            {
                if (CheckLibrary("d3d11.dll"))
                    supportedGraphicsAPIs.Add(GraphicsAPI.Direct3D11);
            }
            catch (DllNotFoundException) { }

            supportedGraphicsAPIs.Add(GraphicsAPI.OpenGL4);
        }

        /// <summary>
        /// Prüft, ob die gegebene Audio-API zur Verfügung steht.
        /// </summary>
        /// <param name="AudioAPI">Die Audio-API.</param>
        /// <returns>True, wenn die Audio-API zur Verfügung steht, ansonsten false.</returns>
        public static bool IsAPISupported(AudioAPI AudioAPI)
        {
{'fraction_non_alphanumeric': 0.09849690539345712, 'fraction_numerical': 0.004774535809018567, 'mean_word_length': 2.2998833138856476, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '759381', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1492, 'n_words': 363}