Example #1
0
    public static bool Load()
    {
        s_pDll = NativeMethods.LoadLibrary(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "lib", dllName));
        if (s_pDll == IntPtr.Zero)
        {
            return(false);
        }
        s_pOodleLzCompress = NativeMethods.GetProcAddress(s_pDll, "Kraken_Compress");
        if (s_pOodleLzCompress == IntPtr.Zero)
        {
            return(false);
        }
        s_pOodleLzDecompress = NativeMethods.GetProcAddress(s_pDll, "Kraken_Decompress");
        if (s_pOodleLzDecompress == IntPtr.Zero)
        {
            return(false);
        }

        s_decompress = (DecompressDelegate)Marshal.GetDelegateForFunctionPointer(
            s_pOodleLzDecompress,
            typeof(DecompressDelegate)
            );
        s_compress = (CompressDelegate)Marshal.GetDelegateForFunctionPointer(
            s_pOodleLzCompress,
            typeof(CompressDelegate)
            );

        return(true);
    }
Example #2
0
 // Get function pointers from x86 assembly
 private static void Getx86Delegates()
 {
     GetStorageRequirementsFunction = NativeSquish_x86.Squish.GetStorageRequirements;
     CompressFunction = NativeSquish_x86.Squish.Compress;
     CompressMaskedFunction = NativeSquish_x86.Squish.CompressMasked;
     DecompressFunction = NativeSquish_x86.Squish.Decompress;
     CompressImageFunction = NativeSquish_x86.Squish.CompressImage;
     DecompressImageFunction = NativeSquish_x86.Squish.DecompressImage;
 }
Example #3
0
 // Get function pointers from x64 assembly
 private static void Getx64Delegates()
 {
     GetStorageRequirementsFunction = NativeSquish_x64.Squish.GetStorageRequirements;
     CompressFunction        = NativeSquish_x64.Squish.Compress;
     CompressMaskedFunction  = NativeSquish_x64.Squish.CompressMasked;
     DecompressFunction      = NativeSquish_x64.Squish.Decompress;
     CompressImageFunction   = NativeSquish_x64.Squish.CompressImage;
     DecompressImageFunction = NativeSquish_x64.Squish.DecompressImage;
 }
Example #4
0
        public static bool Load(string oodlepath)
        {
            s_pDll = NativeMethods.LoadLibrary(oodlepath);
            if (s_pDll == IntPtr.Zero)
            {
                return(false);
            }
            s_pOodleLzCompress = NativeMethods.GetProcAddress(s_pDll, "OodleLZ_Compress");
            if (s_pOodleLzCompress == IntPtr.Zero)
            {
                return(false);
            }
            s_pOodleLzGetCompressedBufferSizeNeeded = NativeMethods.GetProcAddress(s_pDll, "OodleLZ_GetCompressedBufferSizeNeeded");
            if (s_pOodleLzGetCompressedBufferSizeNeeded == IntPtr.Zero)
            {
                return(false);
            }
            s_pOodleLzDecompress = NativeMethods.GetProcAddress(s_pDll, "OodleLZ_Decompress");
            if (s_pOodleLzDecompress == IntPtr.Zero)
            {
                return(false);
            }

            s_decompress = (DecompressDelegate)Marshal.GetDelegateForFunctionPointer(
                s_pOodleLzDecompress,
                typeof(DecompressDelegate)
                );
            s_getCompressedBufferSizeNeeded = (GetCompressedBufferSizeNeededDelegate)Marshal.GetDelegateForFunctionPointer(
                s_pOodleLzGetCompressedBufferSizeNeeded,
                typeof(GetCompressedBufferSizeNeededDelegate)
                );
            s_compress = (CompressDelegate)Marshal.GetDelegateForFunctionPointer(
                s_pOodleLzCompress,
                typeof(CompressDelegate)
                );

            return(true);
        }
Example #5
0
        /// <summary>
        /// Creates a new ccrush# instance. <para> </para>
        /// Make sure to create one only once and cache it as needed, since loading the DLLs into memory can negatively affect the performance.
        /// <param name="sharedLibPathOverride">[OPTIONAL] Don't look for a <c>lib/</c> folder and directly use this path as a pre-resolved, platform-specific shared lib/DLL file path. Pass this if you want to handle the various platform's paths yourself.</param>
        /// </summary>
        public CcrushSharpContext(string sharedLibPathOverride = null)
        {
            string os;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                os        = "windows";
                loadUtils = new SharedLibLoadUtilsWindows();
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                os        = "linux";
                loadUtils = new SharedLibLoadUtilsLinux();
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                os        = "mac";
                loadUtils = new SharedLibLoadUtilsMac();
            }
            else
            {
                throw new PlatformNotSupportedException("Unsupported OS");
            }

            if (!string.IsNullOrEmpty(sharedLibPathOverride))
            {
                LoadedLibraryPath = sharedLibPathOverride;
            }
            else
            {
                string cpu;

                switch (RuntimeInformation.ProcessArchitecture)
                {
                case Architecture.X64:
                    cpu = "x64";
                    break;

                case Architecture.X86:
                    cpu = "x86";
                    break;

                case Architecture.Arm:
                    cpu = "armeabi-v7a";
                    break;

                case Architecture.Arm64:
                    cpu = "arm64-v8a";
                    break;

                default:
                    throw new PlatformNotSupportedException("CPU Architecture not supported!");
                }

                string path = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) ?? "."), "lib", cpu, os);

                if (!Directory.Exists(path))
                {
                    throw new PlatformNotSupportedException($"Shared library not found in {path} and/or unsupported CPU architecture. Please don't forget to copy the shared libraries/DLL into the 'lib/{{CPU_ARCHITECTURE}}/{{OS}}/{{SHARED_LIB_FILE}}' folder of your output build directory. ");
                }

                bool found = false;
                foreach (string file in Directory.GetFiles(path))
                {
                    if (file.ToLower().Contains("ccrush"))
                    {
                        LoadedLibraryPath = Path.GetFullPath(Path.Combine(path, file));
                        found             = true;
                        break;
                    }
                }

                if (!found)
                {
                    throw new FileLoadException($"Shared library not found in {path} and/or unsupported CPU architecture. Please don't forget to copy the shared libraries/DLL into the 'lib/{{CPU_ARCHITECTURE}}/{{OS}}/{{SHARED_LIB_FILE}}' folder of your output build directory. ");
                }
            }

            lib = loadUtils.LoadLibrary(LoadedLibraryPath);
            if (lib == IntPtr.Zero)
            {
                goto hell;
            }

            IntPtr compress = loadUtils.GetProcAddress(lib, "ccrush_compress");

            if (compress == IntPtr.Zero)
            {
                goto hell;
            }

            IntPtr decompress = loadUtils.GetProcAddress(lib, "ccrush_decompress");

            if (decompress == IntPtr.Zero)
            {
                goto hell;
            }

            IntPtr free = loadUtils.GetProcAddress(lib, "ccrush_free");

            if (free == IntPtr.Zero)
            {
                goto hell;
            }

            IntPtr getVersionNumber = loadUtils.GetProcAddress(lib, "ccrush_get_version_nr");

            if (getVersionNumber == IntPtr.Zero)
            {
                goto hell;
            }

            IntPtr getVersionNumberString = loadUtils.GetProcAddress(lib, "ccrush_get_version_nr_string");

            if (getVersionNumberString == IntPtr.Zero)
            {
                goto hell;
            }

            compressDelegate   = Marshal.GetDelegateForFunctionPointer <CompressDelegate>(compress);
            decompressDelegate = Marshal.GetDelegateForFunctionPointer <DecompressDelegate>(decompress);
            freeDelegate       = Marshal.GetDelegateForFunctionPointer <FreeDelegate>(free);

            var getVersionNumberDelegate       = Marshal.GetDelegateForFunctionPointer <GetVersionNumberDelegate>(getVersionNumber);
            var getVersionNumberStringDelegate = Marshal.GetDelegateForFunctionPointer <GetVersionNumberStringDelegate>(getVersionNumberString);

            Version       = getVersionNumberDelegate.Invoke();
            VersionString = Marshal.PtrToStringAnsi(getVersionNumberStringDelegate.Invoke());

            return;

hell:
            throw new Exception($"Failed to load one or more functions from the shared library \"{LoadedLibraryPath}\"!");
        }
Example #6
0
 private static void SetWin32()
 {
     Compress   = MySpace.Win32.ManagedZLib.Compress;
     Decompress = MySpace.Win32.ManagedZLib.Decompress;
 }
Example #7
0
 private static void SetX64()
 {
     Compress   = MySpace.x64.ManagedZLib.Compress;
     Decompress = MySpace.x64.ManagedZLib.Decompress;
 }