public FMU(string fmuName, string instanceName, string GUID = null, Dictionary <string, uint> vars = null, string path = "", bool loggingOn = false) { if (GUID == null || vars == null) { return; } ValueReferences = vars; var dllPath = path; // TODO test if linux checks are even needed var modelIdentifier = fmuName; DLLGUID = GUID; Web.Config.FMUs.TryGetValue(GUID, out dll); if (dll == default) { // load the DLL #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN //dllPath += "/win" + IntPtr.Size * 8 + "/" + modelIdentifier + ".dll"; dll = LoadLibrary(dllPath); #else //dllPath += "linux64/" + modelIdentifier + ".so"; //.dylib dll = dlopen(dllPath, 4); #endif Web.Config.FMUs.Add(GUID, dll); } fmi2CallbackFunctions functions; functions.logger = new fmi2CallbackLogger(LogMessage); // no log message functions.allocateMemory = new fmi2CallbackAllocateMemory(AllocateMemory); functions.freeMemory = new fmi2CallbackFreeMemory(FreeMemory); functions.stepFinished = new fmi2StepFinished(StepFinished); functions.componentEnvironment = IntPtr.Zero; //unsafe //{ this.Callbacks = Marshal.AllocHGlobal(Marshal.SizeOf(functions)); //Callbacks = (IntPtr) UnsafeUtility.Malloc(UnsafeUtility.SizeOf<fmi2CallbackFunctions>(), 8, Unity.Collections.Allocator.Persistent); Marshal.StructureToPtr(functions, this.Callbacks, false); //UnsafeUtility.CopyStructureToPtr(ref functions, Callbacks.ToPointer()); // Copy the struct to unmanaged memory. //} FMI2Instantiate = GetFunc <fmi2InstantiateDelegate>(dll, "fmi2Instantiate"); FMI2FreeInstance = GetFunc <fmi2FreeInstanceDelegate>(dll, "fmi2FreeInstance"); FMI2SetupExperiment = GetFunc <fmi2SetupExperimentDelegate>(dll, "fmi2SetupExperiment"); FMI2EnterInitializationMode = GetFunc <fmi2EnterInitializationModeDelegate>(dll, "fmi2EnterInitializationMode"); FMI2ExitInitializationMode = GetFunc <fmi2ExitInitializationModeDelegate>(dll, "fmi2ExitInitializationMode"); FMI2Terminate = GetFunc <fmi2TerminateDelegate>(dll, "fmi2Terminate"); FMI2Reset = GetFunc <fmi2ResetDelegate>(dll, "fmi2Reset"); FMI2DoStep = GetFunc <fmi2DoStepDelegate>(dll, "fmi2DoStep"); FMI2GetReal = GetFunc <fmi2GetRealDelegate>(dll, "fmi2GetReal"); FMI2SetReal = GetFunc <fmi2SetRealDelegate>(dll, "fmi2SetReal"); FMI2GetInteger = GetFunc <fmi2GetIntegerDelegate>(dll, "fmi2GetInteger"); FMI2SetInteger = GetFunc <fmi2SetIntegerDelegate>(dll, "fmi2SetInteger"); FMI2GetBoolean = GetFunc <fmi2GetBooleanDelegate>(dll, "fmi2GetBoolean"); FMI2SetBoolean = GetFunc <fmi2SetBooleanDelegate>(dll, "fmi2SetBoolean"); FMI2GetString = GetFunc <fmi2GetStringDelegate>(dll, "fmi2GetString"); FMI2SetString = GetFunc <fmi2SetStringDelegate>(dll, "fmi2SetString"); //var resourceLocation = new Uri(unzipdir).AbsoluteUri; Component = FMI2Instantiate(instanceName, (int)ModelType.ModelCoSimulation, DLLGUID, null, this.Callbacks, false, loggingOn ? true : false); }
public FMU(string fmuName, string instanceName, bool loggingOn = false) { var modelDescription = Resources.Load <ModelDescription>(fmuName); valueReferences = new Dictionary <string, uint>(); // collect the value references foreach (var variable in modelDescription.modelVariables) { valueReferences[variable.name] = variable.valueReference; } var unzipdir = Application.streamingAssetsPath + "/" + modelDescription.modelName; var modelIdentifier = modelDescription.coSimulation.modelIdentifier; var guid = new string(modelDescription.guid); var dllPath = unzipdir + "/binaries/"; // load the DLL #if UNITY_STANDALONE_WIN dllPath += "win" + IntPtr.Size * 8 + "/" + modelIdentifier + ".dll"; dll = LoadLibrary(dllPath); #else dllPath += "darwin64/" + modelIdentifier + ".dylib"; dll = dlopen(dllPath, RTLD_NOW); #endif fmi2CallbackFunctions functions; functions.logger = new fmi2CallbackLogger(logMessage); functions.allocateMemory = new fmi2CallbackAllocateMemory(allocateMemory); functions.freeMemory = new fmi2CallbackFreeMemory(free); functions.stepFinished = new fmi2StepFinished(stepFinished); functions.componentEnvironment = IntPtr.Zero; this.callbacks = Marshal.AllocHGlobal(Marshal.SizeOf(functions)); // Copy the struct to unmanaged memory. Marshal.StructureToPtr(functions, this.callbacks, false); fmi2Instantiate = getFunc <fmi2InstantiateDelegate>(dll, "fmi2Instantiate"); fmi2FreeInstance = getFunc <fmi2FreeInstanceDelegate>(dll, "fmi2FreeInstance"); fmi2SetupExperiment = getFunc <fmi2SetupExperimentDelegate>(dll, "fmi2SetupExperiment"); fmi2EnterInitializationMode = getFunc <fmi2EnterInitializationModeDelegate>(dll, "fmi2EnterInitializationMode"); fmi2ExitInitializationMode = getFunc <fmi2ExitInitializationModeDelegate>(dll, "fmi2ExitInitializationMode"); fmi2Terminate = getFunc <fmi2TerminateDelegate>(dll, "fmi2Terminate"); fmi2Reset = getFunc <fmi2ResetDelegate>(dll, "fmi2Reset"); fmi2DoStep = getFunc <fmi2DoStepDelegate>(dll, "fmi2DoStep"); fmi2GetReal = getFunc <fmi2GetRealDelegate>(dll, "fmi2GetReal"); fmi2SetReal = getFunc <fmi2SetRealDelegate>(dll, "fmi2SetReal"); fmi2GetInteger = getFunc <fmi2GetIntegerDelegate>(dll, "fmi2GetInteger"); fmi2SetInteger = getFunc <fmi2SetIntegerDelegate>(dll, "fmi2SetInteger"); fmi2GetBoolean = getFunc <fmi2GetBooleanDelegate>(dll, "fmi2GetBoolean"); fmi2SetBoolean = getFunc <fmi2SetBooleanDelegate>(dll, "fmi2SetBoolean"); fmi2GetString = getFunc <fmi2GetStringDelegate>(dll, "fmi2GetString"); fmi2SetString = getFunc <fmi2SetStringDelegate>(dll, "fmi2SetString"); var resourceLocation = new Uri(unzipdir).AbsoluteUri; component = fmi2Instantiate(instanceName, (int)fmi2Type.fmi2CoSimulation, guid, resourceLocation, callbacks, fmi2False, loggingOn ? fmi2True : fmi2False); }