package co.notime.lwjglnatives;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.lang.reflect.Field;

/**
 * User: lachlan.krautz
 * Date: 14/09/2014
 * Time: 2:55 PM
 */
public class NativesHandler {

    private static Logger logger = LogManager.getLogger(NativesHandler.class.getName());

    private File cacheDir;
    private File projectDir;

    public NativesHandler () {
        cacheDir   = findCacheDir();
        projectDir = new File("").getAbsoluteFile();
        logger.info("Cache dir: "   + cacheDir);
        logger.info("Project dir: " + projectDir);
    }

    public void openNatives () {
        logger.info("opening natives");
        // fixLibraryPath();
    }

    public void closeNatives () {
        logger.info("closing natives");
    }


    private void fixLibraryPath () {
        System.setProperty( "java.library.path", "natives" );
        Field fieldSysPath;
        try {
            fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
            fieldSysPath.setAccessible( true );
            fieldSysPath.set( null, null );
        } catch (NoSuchFieldException e) {
            logger.warn("Sys path not found", e);
        } catch (IllegalAccessException e) {
            logger.warn("Illegal access on sys path", e);
        }
    }

    private File findCacheDir () {
        String cacheDirPath = System.getProperty("deployment.user.cachedir");
        if ((cacheDirPath == null) || (System.getProperty("os.name").startsWith("Win"))) {
            cacheDirPath = System.getProperty("java.io.tmpdir");
        }
        return new File(cacheDirPath);
    }

    public String toString () {
        return "Project dir: " + projectDir + "\r\n"
                + "Cache dir: " + cacheDir;
    }

}

@@ -147,26 +147,82 @@
 ava.
-lang.reflect.Field
+io.IOException;%0Aimport java.lang.reflect.Field;%0Aimport java.util.ArrayList
 ;%0A%0A/
@@ -410,16 +410,61 @@
 me());%0A%0A
+    private ArrayList%3CString%3E possiblePaths;%0A
     priv
@@ -539,24 +539,73 @@
 andler () %7B%0A
+        possiblePaths = new ArrayList%3CString%3E();%0A
         cach
@@ -611,16 +611,19 @@
 heDir   
+   
 = findCa
@@ -651,16 +651,19 @@
 jectDir 
+   
 = new Fi
@@ -810,69 +810,469 @@
 lic 
-void openNatives () %7B%0A        logger.info(%22opening natives%22);
+boolean canCacheNatives () %7B%0A        return cacheDir != null;%0A    %7D%0A%0A    public void cacheNatives () throws IOException %7B%0A        logger.info(%22opening natives%22);%0A        if (cacheDir == null) %7B%0A            throw new IOException(%22Cache dir not found%22);%0A        %7D%0A        if (possiblePaths.size() == 0) %7B%0A            logger.info(%22No possible paths set; where should I look?%22);%0A        %7D else %7B%0A            for (String s : possiblePaths) %7B%0A%0A            %7D%0A        %7D
 %0A   
@@ -1326,11 +1326,13 @@
 d cl
-ose
+eanup
 Nati
@@ -1387,17 +1387,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -1940,24 +1940,270 @@
 cheDir () %7B%0A
+        File cacheDir = null;%0A        String cacheDirPath = findCacheDirPath();%0A        if (cacheDirPath != null) %7B%0A            cacheDir = new File(cacheDirPath);%0A        %7D%0A        return cacheDir;%0A    %7D%0A%0A    private String findCacheDirPath () %7B%0A
         Stri
@@ -2449,25 +2449,16 @@
  return 
-new File(
 cacheDir
@@ -2457,25 +2457,24 @@
 cacheDirPath
-)
 ;%0A    %7D%0A%0A   

throw exceptions and check for files etc