示例#1
0
        private void InstallMode(ModuleSTPModularCorridor.ModeConfig m, bool isLoading = false)
        {
            ConfigNode conf      = m.config;
            int        idx       = m.path.LastIndexOf('/');
            string     modelPath = m.path.Substring(0, idx) + "/" + conf.GetValue("model");

            Debug.Log("[STB]: Loading: " + conf.GetValue("modeName") + " mode.");
            ConfigNode[] array = m.inRes;
            for (int j = 0; j < array.Length; j++)
            {
                ConfigNode i = array[j];
                if (MBToolbox.AmountAvailable(i.GetValue("name"), FlightGlobals.ActiveVessel.rootPart) < System.Convert.ToDouble(i.GetValue("amount")))
                {
                    ScreenMessages.PostScreenMessage("Not enough " + i.GetValue("name"), 5f);
                    return;
                }
                MBToolbox.RequestResource(i.GetValue("name"), System.Convert.ToDouble(i.GetValue("amount")), FlightGlobals.ActiveVessel.rootPart);
            }
            if (this.installedModule.hasInit() && !isLoading)
            {
                Debug.Log("[STB]: Reseting modules...");
                if (this.installedModule.moduleconfs.Length != 0)
                {
                    System.Collections.Generic.List <PartModule> pmToRemoveList = new System.Collections.Generic.List <PartModule>();
                    using (var enumerator = base.part.Modules.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            PartModule pm = (PartModule)enumerator.Current;
                            array = this.installedModule.moduleconfs;
                            for (int j = 0; j < array.Length; j++)
                            {
                                ConfigNode i = array[j];
                                if (i.GetValue("name") == pm.moduleName)
                                {
                                    pmToRemoveList.Add(pm);
                                }
                            }
                        }
                    }
                    foreach (PartModule pm in pmToRemoveList)
                    {
                        base.part.RemoveModule(pm);
                        Debug.Log("[STB]: Removing: " + pm.moduleName + " module.");
                    }
                }
                Debug.Log("[STB]: Reseting resources...");
                foreach (PartResource r in base.part.Resources)
                {
                    array = this.installedModule.resconfs;
                    for (int j = 0; j < array.Length; j++)
                    {
                        ConfigNode i = array[j];
                        if (i.GetValue("name") == r.resourceName)
                        {
                            ConfigNode node = i.CreateCopy();
                            node.SetValue("name", r.resourceName, false);
                            node.SetValue("amount", "0", false);
                            node.SetValue("maxAmount", "0", false);
                            base.part.SetResource(node);
                            Debug.Log("[STB]: Reseting " + r.resourceName);
                        }
                    }
                }
                Debug.Log("[STB]: Reseting model...");
                if (this.currModel != null)
                {
                    this.currModel.SetActive(false);
                }
            }
            if (m.isDepsOk)
            {
                Debug.Log("[STB]: Adding modules...");
                array = m.moduleconfs;
                for (int j = 0; j < array.Length; j++)
                {
                    ConfigNode i  = array[j];
                    PartModule pm = base.part.AddModule(i.GetValue("name"));
                    pm.OnAwake();
                    pm.OnInitialize();
                    pm.Load(i);
                    pm.OnStart(PartModule.StartState.Landed);
                    Debug.Log(i.GetValue("name") + " is being added from: " + i.name);
                }
            }
            if (!isLoading)
            {
                Debug.Log("[STB]: Adding resources...");
                array = m.resconfs;
                for (int j = 0; j < array.Length; j++)
                {
                    ConfigNode i = array[j];
                    if (i.GetValue("amount") != "0")
                    {
                        i.SetValue("amount", "0", false);
                    }
                    base.part.SetResource(i);
                    Debug.Log(i.GetValue("name") + " " + i.name + " is being added");
                }
            }
            Debug.Log("[STB]: Adding model...");
            if (GameDatabase.Instance.ExistsModel(modelPath))
            {
                GameObject go = GameDatabase.Instance.GetModel(modelPath);
                go.SetActive(true);
                go.layer = 15;
                go.name  = conf.GetValue("modeName") + go.GetInstanceID();
                go.transform.position = base.part.partTransform.position;
                go.transform.rotation = base.part.partTransform.rotation;
                go.transform.parent   = base.part.partTransform;
                this.currModel        = go;
            }
            else
            {
                Debug.Log("[STB] says: " + modelPath + " is not found!");
            }
            this.installedModule = m;
            this.currModeName    = conf.GetValue("modeName");
            Debug.Log("[STB]: Installing done!");
        }