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.WPIUtilJNI;
028import java.io.IOException;
029import org.opencv.core.Core;
030
031public class LibraryLoader {
032    private static boolean hasWpiLoaded = false;
033    private static boolean hasTargetingLoaded = false;
034
035    public static boolean loadWpiLibraries() {
036        if (hasWpiLoaded) return true;
037
038        NetworkTablesJNI.Helper.setExtractOnStaticLoad(false);
039        WPIUtilJNI.Helper.setExtractOnStaticLoad(false);
040        CameraServerJNI.Helper.setExtractOnStaticLoad(false);
041        OpenCvLoader.Helper.setExtractOnStaticLoad(false);
042        JNIWrapper.Helper.setExtractOnStaticLoad(false);
043        WPINetJNI.Helper.setExtractOnStaticLoad(false);
044        WPIMathJNI.Helper.setExtractOnStaticLoad(false);
045        AprilTagJNI.Helper.setExtractOnStaticLoad(false);
046        try {
047            // Need to load wpiutil first before checking if the MSVC runtime is valid
048            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, "wpiutiljni");
049            WPIUtilJNI.checkMsvcRuntime();
050            CombinedRuntimeLoader.loadLibraries(
051                    LibraryLoader.class,
052                    "wpimathjni",
053                    "ntcorejni",
054                    "wpinetjni",
055                    "wpiHaljni",
056                    "cscorejni",
057                    "apriltagjni");
058
059            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, Core.NATIVE_LIBRARY_NAME);
060            hasWpiLoaded = true;
061        } catch (IOException e) {
062            e.printStackTrace();
063            hasWpiLoaded = false;
064        }
065
066        return hasWpiLoaded;
067    }
068
069    public static boolean loadTargeting() {
070        if (hasTargetingLoaded) return true;
071        try {
072            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, "photontargetingJNI");
073            hasTargetingLoaded = true;
074        } catch (IOException e) {
075            e.printStackTrace();
076            hasTargetingLoaded = false;
077        }
078        return hasTargetingLoaded;
079    }
080}