Ejemplo n.º 1
0
        public override void Init(MyObjectBuilder_SessionComponent sessionComponent)
        {
            base.Init(sessionComponent);

            // Only servers can run this session component
            if (!Session.IsServer)
            {
                return;
            }
            MyObjectBuilder_VisualScriptManagerSessionComponent ob = (MyObjectBuilder_VisualScriptManagerSessionComponent)sessionComponent;

            m_objectBuilder = ob;
            m_relativePathsToAbsolute.Clear();
            m_stateMachineDefinitionFilePaths.Clear();

            // Runs game started event
            m_firstUpdate = ob.FirstRun;

            // load vanilla level script files
            if (ob.LevelScriptFiles != null)
            {
                foreach (string relativeFilePath in ob.LevelScriptFiles)
                {
                    var absolutePath = Path.Combine(MyFileSystem.ContentPath, relativeFilePath);
                    // Add only existing files
                    if (MyFileSystem.FileExists(absolutePath))
                    {
                        m_relativePathsToAbsolute.Add(relativeFilePath, absolutePath);
                    }
                    else
                    {
                        MyLog.Default.WriteLine(relativeFilePath + " Level Script was not found.");
                        Debug.Fail(relativeFilePath + " Level Script was not found.");
                    }
                }
            }

            // load vanilla mission manchines
            if (ob.StateMachines != null)
            {
                foreach (var relativeFilePath in ob.StateMachines)
                {
                    var absolutePath = Path.Combine(MyFileSystem.ContentPath, relativeFilePath);
                    // Add only existing files
                    if (MyFileSystem.FileExists(absolutePath))
                    {
                        if (!m_relativePathsToAbsolute.ContainsKey(relativeFilePath))
                        {
                            m_stateMachineDefinitionFilePaths.Add(absolutePath);
                        }

                        m_relativePathsToAbsolute.Add(relativeFilePath, absolutePath);
                    }
                    else
                    {
                        MyLog.Default.WriteLine(relativeFilePath + " Mission File was not found.");
                        Debug.Fail(relativeFilePath + " Mission File was not found.");
                    }
                }
            }

            // Load mission machines and level scripts from mods
            // Overrides vanilla files
            if (Session.Mods != null)
            {
                foreach (var modItem in Session.Mods)
                {
                    // First try is a mod archive
                    var directoryPath = Path.Combine(MyFileSystem.ModsPath, modItem.PublishedFileId + ".sbm");
                    if (!MyFileSystem.DirectoryExists(directoryPath))
                    {
                        directoryPath = Path.Combine(MyFileSystem.ModsPath, modItem.Name);
                        if (!MyFileSystem.DirectoryExists(directoryPath))
                        {
                            directoryPath = null;
                        }
                    }

                    if (!string.IsNullOrEmpty(directoryPath))
                    {
                        foreach (var filePath in MyFileSystem.GetFiles(directoryPath, "*", MySearchOption.AllDirectories))
                        {
                            var extension    = Path.GetExtension(filePath);
                            var relativePath = MyFileSystem.MakeRelativePath(Path.Combine(directoryPath, "VisualScripts"), filePath);
                            if (extension == ".vs" || extension == ".vsc")
                            {
                                if (m_relativePathsToAbsolute.ContainsKey(relativePath))
                                {
                                    m_relativePathsToAbsolute[relativePath] = filePath;
                                }
                                else
                                {
                                    m_relativePathsToAbsolute.Add(relativePath, filePath);
                                }
                            }
                        }
                    }
                }
            }

            // Provider will compile all required scripts for the session.
            MyVSAssemblyProvider.Init(m_relativePathsToAbsolute.Values);
            // Retrive the Level script instances
            m_levelScripts = new CachingList <IMyLevelScript>();
            var scriptInstances = MyVSAssemblyProvider.GetLevelScriptInstances();

            if (scriptInstances != null)
            {
                scriptInstances.ForEach(script => m_levelScripts.Add(script));
            }
            m_levelScripts.ApplyAdditions();

            // Store the names of the level scripts
            m_runningLevelScriptNames         = m_levelScripts.Select(x => x.GetType().Name).ToArray();
            m_failedLevelScriptExceptionTexts = new string[m_runningLevelScriptNames.Length];

            // mission manager initialization - state machine definitions
            m_smManager = new MyVSStateMachineManager();
            foreach (var stateMachineFilePath in m_stateMachineDefinitionFilePaths)
            {
                m_smManager.AddMachine(stateMachineFilePath);
            }
        }