]> gerrit.simantics Code Review - simantics/fmil.git/blobdiff - org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java
Changed FMIL core and fragments logic to properly find files
[simantics/fmil.git] / org.simantics.fmil.core / src / org / simantics / fmil / core / FMIL.java
index 41ebf95522967d70ede8c7bc856f4e6f636ec2f7..3a7c031dc2522a60d8d1ff2e2028947a0e16cb8b 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.fmil.core;
 import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.net.URL;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.nio.file.Paths;
@@ -13,9 +14,9 @@ import java.util.Set;
 import java.util.UUID;
 
 import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Platform;
 import org.osgi.framework.Bundle;
-import org.simantics.fmil.core.ExecEnvironment.ARCHType;
 import org.simantics.fmil.core.ExecEnvironment.OSType;
 import org.simantics.utils.FileUtils;
 import org.slf4j.Logger;
@@ -34,9 +35,9 @@ public class FMIL {
      */
     private static int      OK                  = 0;
     private static int      ERROR               = 1;
-    private static String   UNSATISFIED_LINK    = "Method not found. DLL might not be loaded properly.";      
-    public static final String TEMP_FMU_DIRECTORY_NAME = "fmil";  
-    public static String    TEMP_FMU_COMMON_DIRECTORY;  
+    private static String   UNSATISFIED_LINK    = "Method not found. DLL might not be loaded properly.";
+    public static final String TEMP_FMU_DIRECTORY_NAME = "fmil";
+    public static String    TEMP_FMU_COMMON_DIRECTORY;
     public static String    LOCK_FILE_NAME      = "fmil.lock";
 
     public static Object syncObject = new Object();
@@ -45,35 +46,28 @@ public class FMIL {
      * Static: load native libraries required for the FMU simulation to work.
      */
     static {
-       
+
         File[] libraries = new File[2];
 
-        Bundle bundle = null;
-        
         ExecEnvironment env = ExecEnvironment.calculate();
-        if (env.os == OSType.WINDOWS) {
-            if (env.arch == ARCHType.X86) {
-                bundle = Platform.getBundle("org.simantics.fmil.win32");
-            } else if (env.arch == ARCHType.X86_64) {
-                bundle = Platform.getBundle("org.simantics.fmil.win64");
-            }
-        }
-        
-        if (bundle != null) {
-            try{
-                String root = FileLocator.getBundleFile(bundle).getAbsolutePath();
-//                if (env.arch == ARCHType.X86_64) {
-//                    File newFIle = new File(root, "libraries/libexpat.dll");
-//                    System.load(newFIle.getAbsolutePath());
-//                }
-//                libraries[0] = new File(root, "libraries/zlibwapi.dll");
-//                libraries[1] = new File(root, "libraries/miniunz.dll");
-                libraries[0] = new File(root, "libraries/fmilib_shared.dll");
-                libraries[1] = new File(root, "libraries/FMUSimulator.dll");
-            }
-            catch (Exception e) {
-                e.printStackTrace();
+
+        try {
+            URL sharedFMILIBUrl = null;
+            URL simulatorFMIUrl = null;
+            Bundle b = Platform.getBundle("org.simantics.fmil.core");
+
+            if (env.os == OSType.WINDOWS) {
+                sharedFMILIBUrl = FileLocator.find(b, new Path("libraries/fmilib_shared.dll"), null);
+                simulatorFMIUrl = FileLocator.find(b, new Path("libraries/FMUSimulator.dll"), null);
+            } else if(env.os == OSType.LINUX) {
+                sharedFMILIBUrl = FileLocator.find(b, new Path("libraries/libfmilib_shared.so"), null);
+                simulatorFMIUrl = FileLocator.find(b, new Path("libraries/FMUSimulator.so"), null);
             }
+
+            libraries[0] = new File(FileLocator.toFileURL(sharedFMILIBUrl).getPath());
+            libraries[1] = new File(FileLocator.toFileURL(simulatorFMIUrl).getPath());
+        } catch (Exception e) {
+            LOGGER.error("Failed to resolve native FMU simulation library for execution environment {}.{}", env.os, env.arch, e);
         }
 
         for(File library : libraries) {
@@ -873,7 +867,7 @@ public class FMIL {
         synchronized(syncObject) {
 
             try {
-                // TODO: printtaa id ja name, jotta saadaan virheessä kiinni
+                // TODO: printtaa id ja name, jotta saadaan virheessä kiinni
                double result = getRealValue_(getModelIDNew(), variableMap.get(name));
                System.err.println("getRealValue " + name + " = " + result);
                 return  result;