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 java.io.IOException;
021import org.opencv.core.Core;
022import org.wpilib.datalog.DataLogJNI;
023import org.wpilib.hardware.hal.JNIWrapper;
024import org.wpilib.math.jni.WPIMathJNI;
025import org.wpilib.net.WPINetJNI;
026import org.wpilib.networktables.NetworkTablesJNI;
027import org.wpilib.util.WPIUtilJNI;
028import org.wpilib.vision.apriltag.jni.AprilTagJNI;
029import org.wpilib.vision.camera.CameraServerJNI;
030import org.wpilib.vision.camera.OpenCvLoader;
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        DataLogJNI.Helper.setExtractOnStaticLoad(false);
042        CameraServerJNI.Helper.setExtractOnStaticLoad(false);
043        OpenCvLoader.Helper.setExtractOnStaticLoad(false);
044        JNIWrapper.Helper.setExtractOnStaticLoad(false);
045        WPINetJNI.Helper.setExtractOnStaticLoad(false);
046        WPIMathJNI.Helper.setExtractOnStaticLoad(false);
047        AprilTagJNI.Helper.setExtractOnStaticLoad(false);
048        try {
049            // Need to load wpiutil first before checking if the MSVC runtime is valid
050            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, "wpiutiljni");
051            WPIUtilJNI.checkMsvcRuntime();
052            CombinedRuntimeLoader.loadLibraries(
053                    LibraryLoader.class,
054                    "wpimathjni",
055                    "ntcorejni",
056                    "wpinetjni",
057                    "wpiHaljni",
058                    "cscorejni",
059                    "datalogjni",
060                    "apriltagjni");
061
062            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, Core.NATIVE_LIBRARY_NAME);
063            hasWpiLoaded = true;
064        } catch (IOException e) {
065            e.printStackTrace();
066            hasWpiLoaded = false;
067        }
068
069        return hasWpiLoaded;
070    }
071
072    public static boolean loadTargeting() {
073        if (hasTargetingLoaded) return true;
074        try {
075            CombinedRuntimeLoader.loadLibraries(LibraryLoader.class, "photontargetingJNI");
076            hasTargetingLoaded = true;
077        } catch (IOException e) {
078            e.printStackTrace();
079            hasTargetingLoaded = false;
080        }
081        return hasTargetingLoaded;
082    }
083}