#region License Information
/* HeuristicLab
 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
 *
 * This file is part of HeuristicLab.
 *
 * HeuristicLab is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HeuristicLab is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
 */
#endregion

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using HeuristicLab.PluginInfrastructure;

namespace HeuristicLab.OrTools {

  [Plugin("HeuristicLab.OrTools", "Provides functionality of Google OR-Tools in HeuristicLab. Requires Windows 64-bit", "7.0.0.$WCREV$")]
  [PluginFile("HeuristicLab.OrTools-7.0.0.dll", PluginFileType.Assembly)]
  [PluginFile("Google.OrTools.dll", PluginFileType.Assembly)]
  [PluginFile("Google.OrTools-license.txt", PluginFileType.License)]
  [PluginFile("Google.OrTools.runtime.win-x64.dll", PluginFileType.NativeDll)]
  [PluginFile("Google.OrTools_version.txt", PluginFileType.Data)]
  [PluginDependency("HeuristicLab.Protobuf", "3.6.1")]
  public class HeuristicLabOrToolsPlugin : PluginBase {

    ~HeuristicLabOrToolsPlugin() {
      // HACK: Free handle to native DLL used temporarily by the Hive Slave.
      // Finalizer must be used because generated finalizers used in
      // HeuristicLab.ExactOptimization that call destructors in native DLL
      // are called after HeuristicLabOrToolsPlugin.OnUnload().
      // This should be solved for all native DLLs used by the Hive Slave.
      var dllDir = new FileInfo(GetType().Assembly.Location).Directory;
      if (dllDir == null || !dllDir.FullName.Contains(Path.DirectorySeparatorChar + "PluginTemp" + Path.DirectorySeparatorChar))
        return;

      var nativeDlls = GetType().GetCustomAttributes(typeof(PluginFileAttribute), true)
        .Cast<PluginFileAttribute>()
        .Where(pf => pf.FileType == PluginFileType.NativeDll)
        .Select(pf => pf.FileName);

      foreach (var nativeDll in dllDir.EnumerateFiles().Where(f => nativeDlls.Contains(f.Name))) {
        if (Process.GetCurrentProcess().Modules.Cast<ProcessModule>().All(m => m.ModuleName != nativeDll.Name))
          continue;

        var handle = LoadLibrary(nativeDll.FullName);
        if (handle == IntPtr.Zero)
          continue;

        FreeLibrary(handle); // close handle obtained above
        FreeLibrary(handle); // close implicitly obtained handle
      }
    }

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool FreeLibrary(IntPtr hModule);

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
    private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);
  }
}