Beispiel #1
0
        public IMotionModelUnitDev InstantiateMMU(MMULoadingProperty mmuLoadingProperty)
        {
            if (mmuLoadingProperty.Description.ID == Data.CoSimMMUDescription.ID)
            {
                CoSimulationMMUImpl instance = new CoSimulationMMUImpl();

                return(instance);
            }
            return(null);
        }
        /// <summary>
        /// Tries to add/read the respective MMU
        /// </summary>
        /// <param name="zipFilePath">The filepath of the zip archive</param>
        /// <returns></returns>
        private bool TryAddMMU(string zipFilePath)
        {
            //Check whether a description is available
            MMUDescription mmuDescription = this.GetMMUDescription(zipFilePath);

            //Only continue if description is not null
            if (mmuDescription != null)
            {
                //Check if the MMU supports the specified language
                if (!this.supportedLanguages.Contains(mmuDescription.Language))
                {
                    return(false);
                }

                //Check if already available -> skip
                if (this.sessionData.MMULoadingProperties.ContainsKey(mmuDescription.ID))
                {
                    return(false);
                }

                //Skip if already registered
                if (this.availableMMUs.ContainsKey(mmuDescription.ID))
                {
                    return(false);
                }

                //Create the new loading properties for the MMU
                MMULoadingProperty loadingProperties = new MMULoadingProperty()
                {
                    //Assign the description
                    Description = mmuDescription,

                    //Store all the data
                    Data = GetZipContent(zipFilePath),

                    //Store the path to the zip file
                    Path = zipFilePath
                };

                //Add to available MMUs
                this.availableMMUs.Add(mmuDescription.ID, loadingProperties);


                Logger.Log(Log_level.L_INFO, $"Successfully added MMU: {mmuDescription.Name} {mmuDescription.AssemblyName}");

                //Success return true
                return(true);
            }

            //Problem -> return false
            return(false);
        }
        /// <summary>
        /// Instantiates a MMU  from file
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="mmuDescription"></param>
        /// <param name="sessionId"></param>
        /// <returns></returns>
        public IMotionModelUnitDev InstantiateMMU(MMULoadingProperty mmuLoadingProperty)
        {
            //Check if the MMU supports the specified language
            if (mmuLoadingProperty.Description.Language != "C#" && mmuLoadingProperty.Description.Language != "C++CLR")
            {
                return(null);
            }


            //Load the mmu from filesystem and instantiate
            if (mmuLoadingProperty.Path != null)
            {
                Assembly assembly = Assembly.LoadFrom(mmuLoadingProperty.Path);

                //Get the specific type of the class which implementd the IMotionModelUnitDev interface
                Type classType = GetMMUClassType(assembly);

                if (classType != null)
                {
                    try
                    {
                        //Create a new mmu instance within the same app domain
                        IMotionModelUnitDev mmuInstance = Activator.CreateInstance(classType) as IMotionModelUnitDev;

                        //To do -> in future instantiate directly from ram -> Loading properties already provide within the data dictionary all dlls and resources

                        //To do  -> incorporate app domain -> currently not working
                        //IMotionModelUnitDev mmuInstance = InstantiateInAppDomain(mmuDescription, sessionContent.SessionId, filePath);

                        return(mmuInstance);
                    }
                    catch (Exception e)
                    {
                        Logger.Log(Log_level.L_ERROR, $"Could not load MMU: {mmuLoadingProperty.Description.Name} {mmuLoadingProperty.Description.AssemblyName}, exception: {e.Message}");
                    }
                }
            }

            return(null);
        }
Beispiel #4
0
 public IMotionModelUnitDev InstantiateMMU(MMULoadingProperty loadingProperty)
 {
     //Instantiate the respective MMU -> Change this line for testing a different MMU
     return(Activator.CreateInstance(mmuType) as IMotionModelUnitDev);
 }
        /// <summary>
        /// Instantiates a MMU  from file
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="mmuDescription"></param>
        /// <param name="sessionId"></param>
        /// <returns></returns>
        public IMotionModelUnitDev InstantiateMMU(MMULoadingProperty mmuLoadingProperty)
        {
            Debug.Log("Calling Unity MMU instantiator " + mmuLoadingProperty.Path);

            MMUDescription mmuDescription = mmuLoadingProperty.Description;
            string         filePath       = mmuLoadingProperty.Path;

            //Check if the MMU supports the specified language
            if (mmuDescription.Language != "UnityC#" && mmuDescription.Language != "Unity")
            {
                throw new NotSupportedException("MMU is not supported");
            }

            //Skip if file path invalid
            if (filePath == null)
            {
                throw new NullReferenceException("File path of MMU is null " + mmuDescription.Name);
            }

            //Skip if the MMU has no dependencies
            if (mmuDescription.Dependencies.Count == 0)
            {
                throw new Exception("No dependencies of MMU specified " + mmuDescription.Name);
            }

            //Create a variable which hols the mmu
            IMotionModelUnitDev mmu = null;

            //Get the paths
            string folderPath      = System.IO.Directory.GetParent(filePath).ToString();
            string assetBundlePath = folderPath + "\\" + mmuDescription.Dependencies[0].Name;


            //Perform on unity main thread
            MainThreadDispatcher.Instance.ExecuteBlocking(delegate
            {
                //To do in futre -> Create a new scene for each MMU
                //Scene scene = SceneManager.CreateScene("Scene" + mmuDescription.Name);
                //SceneManager.SetActiveScene(scene);

                //First load the resources
                AssetBundle mmuAssetBundle = null;
                try
                {
                    if (!mmuLoadingProperty.Data.ContainsKey(assetBundlePath))
                    {
                        mmuAssetBundle = AssetBundle.LoadFromFile(assetBundlePath);
                        if (mmuAssetBundle == null)
                        {
                            MMICSharp.Adapter.Logger.Log(Log_level.L_ERROR, "Failed to load AssetBundle! AssetBundle is null!" + mmuDescription.Name);
                            return;
                        }

                        mmuLoadingProperty.Data.Add(assetBundlePath, mmuAssetBundle);
                    }
                    else
                    {
                        mmuAssetBundle = mmuLoadingProperty.Data[assetBundlePath] as AssetBundle;
                    }
                }
                catch (Exception e)
                {
                    MMICSharp.Adapter.Logger.Log(Log_level.L_ERROR, "Cannot load asset bundle: " + mmuDescription.Name + " " + e.Message);
                    return;
                }

                GameObject prefab = null;
                try
                {
                    prefab = mmuAssetBundle.LoadAsset <GameObject>(mmuDescription.Name);
                }
                catch (Exception e)
                {
                    MMICSharp.Adapter.Logger.Log(Log_level.L_ERROR, "Cannot load prefab: " + mmuDescription.Name + " " + e.Message);
                }

                GameObject mmuObj = null;

                try
                {
                    mmuObj = GameObject.Instantiate(prefab);
                }
                catch (Exception e)
                {
                    MMICSharp.Adapter.Logger.Log(Log_level.L_ERROR, "Cannot instantiate prefab: " + mmuDescription.Name + " " + e.Message);
                }

                try
                {
                    Assembly assembly = Assembly.LoadFile(filePath);

                    //Find the class type which implements the IMotionModelInterface
                    Type classType = assembly.GetTypes().ToList().Find(s => s.GetInterfaces().Contains(typeof(IMotionModelUnitDev)));
                    if (classType != null)
                    {
                        //Add the script to the game object
                        mmu = mmuObj.AddComponent(classType) as IMotionModelUnitDev;

                        ///Loading and assigning the configuration file
                        if (mmuObj.GetComponent <UnityMMUBase>() != null)
                        {
                            MMICSharp.Adapter.Logger.Log(Log_level.L_DEBUG, $"MMU {mmuDescription.Name} implements the UnityMMUBase");

                            //Get the UnityMMUBse interface
                            UnityMMUBase mmuBase = mmuObj.GetComponent <UnityMMUBase>();

                            //By default set the configuration file path and define it globally
                            string avatarConfigFilePath = AppDomain.CurrentDomain.BaseDirectory + "/" + "avatar.mos";

                            //Check if the MMU has a custom configuration file
                            if (this.GetLocalAvatarConfigurationFile(folderPath, out string localConfigFilePath))
                            {
                                //Setting the local avatar as reference for retargeting
                                MMICSharp.Adapter.Logger.Log(Log_level.L_DEBUG, $"Local avatar configuration was found: {mmuDescription.Name} ({avatarConfigFilePath}).");

                                //Set to the utilized path
                                avatarConfigFilePath = localConfigFilePath;
                            }


                            //Further perform a check whether the file is available at all
                            if (System.IO.File.Exists(avatarConfigFilePath))
                            {
                                mmuBase.ConfigurationFilePath = avatarConfigFilePath;
                                MMICSharp.Adapter.Logger.Log(Log_level.L_DEBUG, $"Setting the configuration file path for loading the avatar of {mmuDescription.Name} to {avatarConfigFilePath}");
                            }

                            //Problem in here -> File is obviously not available
                            else
                            {
                                MMICSharp.Adapter.Logger.Log(Log_level.L_ERROR, $"Problem setting the configuration file path for loading the avatar of {mmuDescription.Name} to {avatarConfigFilePath}. File not available.");
                            }
                        }
                    }
                    else
                    {
                        MMICSharp.Adapter.Logger.Log(Log_level.L_ERROR, "Cannot instantiate MMU: " + mmuDescription.Name);
                    }
                }
                catch (Exception e)
                {
                    MMICSharp.Adapter.Logger.Log(Log_level.L_ERROR, "Problem at instantiating class of MMU: " + mmuDescription.Name + " " + e.Message + " " + e.StackTrace);
                }


                //Disable all renderers in the scene
                foreach (Renderer renderer in GameObject.FindObjectsOfType <Renderer>())
                {
                    renderer.enabled = false;
                }
            });
Beispiel #6
0
        /// <summary>
        /// Tries to add/read the respective MMU
        /// </summary>
        /// <param name="zipFilePath">The filepath of the zip archive</param>
        /// <returns></returns>
        private bool TryAddMMU(string directoryPath)
        {
            try
            {
                //Check whether a description is available
                MMUDescription mmuDescription = this.GetMMUDescription(directoryPath);

                //Only continue if description is not null
                if (mmuDescription != null)
                {
                    //Check if the MMU supports the specified language
                    if (!this.supportedLanguages.Contains(mmuDescription.Language))
                    {
                        return(false);
                    }

                    //Check if already available -> skip
                    if (this.sessionData.MMULoadingProperties.ContainsKey(mmuDescription.ID))
                    {
                        return(false);
                    }

                    //Skip if already registered
                    if (this.availableMMUs.ContainsKey(mmuDescription.ID))
                    {
                        return(false);
                    }


                    //Determine the mmu file
                    string mmuFile = Directory.GetFiles(directoryPath).ToList().Find(s => s.Contains(mmuDescription.AssemblyName));


                    //Create the new loading properties for the MMU
                    MMULoadingProperty loadingProperties = new MMULoadingProperty()
                    {
                        //Assign the description
                        Description = mmuDescription,

                        //Store all the data
                        Data = GetFolderContent(directoryPath),

                        //Set the path of the directory
                        Path = mmuFile //directory path -> in future use directory path in here
                    };

                    //Add to available MMUs
                    this.availableMMUs.Add(mmuDescription.ID, loadingProperties);

                    Logger.Log(Log_level.L_INFO, $"Successfully added MMU: {mmuDescription.Name} {mmuDescription.AssemblyName}");

                    //Success return true
                    return(true);
                }

                //Problem -> return false
                return(false);
            }
            catch (Exception)
            {
                return(false);
            }
        }
 public IMotionModelUnitDev InstantiateMMU(MMULoadingProperty loadingProperty)
 {
     //Instantiate the respective MMU -> Change this line for testing a different MMU
     return(new CarryMMUConcurrentImpl());
 }