001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.jni;
019
020import edu.wpi.first.apriltag.jni.AprilTagJNI;
021import edu.wpi.first.cscore.CameraServerJNI;
022import edu.wpi.first.cscore.OpenCvLoader;
023import edu.wpi.first.hal.JNIWrapper;
024import edu.wpi.first.math.jni.WPIMathJNI;
025import edu.wpi.first.net.WPINetJNI;
026import edu.wpi.first.networktables.NetworkTablesJNI;
027import edu.wpi.first.util.CombinedRuntimeLoader;
028import edu.wpi.first.util.WPIUtilJNI;
029import java.io.IOException;
030import org.opencv.core.Core;
031
032public class LibraryLoader {
033    private static boolean hasWpiLoaded = false;
034    private static boolean hasTargetingLoaded = false;
035
036    public static boolean loadWpiLibraries() {
037        if (hasWpiLoaded) return true;
038
039        NetworkTablesJNI.Helper.setExtractOnStaticLoad(false);
040        WPIUtilJNI.Helper.setExtractOnStaticLoad(false);
041        CameraServerJNI.Helper.setExtractOnStaticLoad(false);
042        OpenCvLoader.Helper.setExtractOnStaticLoad(false);
043        JNIWrapper.Helper.setExtractOnStaticLoad(false);
044        WPINetJNI.Helper.setExtractOnStaticLoad(false);
045        WPIMathJNI.Helper.setExtractOnStaticLoad(false);
046        AprilTagJNI.Helper.setExtractOnStaticLoad(false);
047        try {
048            // Need to load wpiutil first before checking if the MSVC runtime is valid
049            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, "wpiutiljni");
050            WPIUtilJNI.checkMsvcRuntime();
051            CombinedRuntimeLoader.loadLibraries(
052                    LibraryLoader.class,
053                    "wpimathjni",
054                    "ntcorejni",
055                    "wpinetjni",
056                    "wpiHaljni",
057                    "cscorejni",
058                    "apriltagjni");
059
060            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, Core.NATIVE_LIBRARY_NAME);
061            hasWpiLoaded = true;
062        } catch (IOException e) {
063            e.printStackTrace();
064            hasWpiLoaded = false;
065        }
066
067        return hasWpiLoaded;
068    }
069
070    public static boolean loadTargeting() {
071        if (hasTargetingLoaded) return true;
072        try {
073            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, "photontargetingJNI");
074            hasTargetingLoaded = true;
075        } catch (IOException e) {
076            e.printStackTrace();
077            hasTargetingLoaded = false;
078        }
079        return hasTargetingLoaded;
080    }
081}