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 WpilibLoader {
033    private static boolean has_loaded = false;
034
035    public static boolean loadLibraries() {
036        if (has_loaded) 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(WpilibLoader.class, "wpiutiljni");
049            WPIUtilJNI.checkMsvcRuntime();
050            CombinedRuntimeLoader.loadLibraries(
051                    WpilibLoader.class,
052                    "wpimathjni",
053                    "ntcorejni",
054                    "wpinetjni",
055                    "wpiHaljni",
056                    "cscorejni",
057                    "apriltagjni");
058
059            CombinedRuntimeLoader.loadLibraries(WpilibLoader.class, Core.NATIVE_LIBRARY_NAME);
060            has_loaded = true;
061        } catch (IOException e) {
062            e.printStackTrace();
063            has_loaded = false;
064        }
065
066        return has_loaded;
067    }
068}