Example #1
0
 public static void ExtractSkin(Transform kerbal, Skin skin)
 {
     foreach (SkinnedMeshRenderer smr in kerbal.GetComponentsInChildren <SkinnedMeshRenderer>())
     {
         var texture = smr.material.mainTexture as Texture2D;
         if (texture != null)
         {
             skin.SetTexture(texture.name, texture);
         }
     }
 }
Example #2
0
        /// <summary>
        /// Post-load initialisation.
        /// </summary>
        public void Load()
        {
            var skinDirs = new Dictionary <string, int>();
            var suitDirs = new Dictionary <string, int>();

            foreach (GameDatabase.TextureInfo texInfo in GameDatabase.Instance.databaseTexture)
            {
                Texture2D texture = texInfo.texture;
                if (texture == null)
                {
                    continue;
                }

                int skinsPrefixIndex   = texture.name.IndexOf(SkinsPrefix, StringComparison.Ordinal);
                int suitsPrefixIndex   = texture.name.IndexOf(SuitsPrefix, StringComparison.Ordinal);
                int defaultPrefixIndex = texture.name.IndexOf(Replacer.DefaultPrefix, StringComparison.Ordinal);

                if (skinsPrefixIndex != -1)
                {
                    int prefixLength   = skinsPrefixIndex + SkinsPrefix.Length;
                    int skinNameLength = texture.name.LastIndexOf('/') - prefixLength;

                    if (skinNameLength < 1)
                    {
                        log.Print("Skin texture is not inside a subdirectory: {0}", texture.name);
                    }
                    else // Add a skin texture.
                    {
                        string skinName     = texture.name.Substring(prefixLength, skinNameLength);
                        string originalName = texture.name.Substring(prefixLength + skinNameLength + 1);

                        if (!skinDirs.TryGetValue(skinName, out int index))
                        {
                            index = Skins.Count;
                            Skins.Add(new Skin {
                                Name = skinName
                            });
                            skinDirs.Add(skinName, index);
                        }

                        Skin skin = Skins[index];
                        if (skin.SetTexture(originalName, texture))
                        {
                            texture.wrapMode = TextureWrapMode.Clamp;
                        }
                        else
                        {
                            log.Print("Unknown skin texture name \"{0}\": {1}", originalName, texture.name);
                        }
                    }
                }
                else if (suitsPrefixIndex != -1)
                {
                    int prefixLength   = suitsPrefixIndex + SuitsPrefix.Length;
                    int suitNameLength = texture.name.LastIndexOf('/') - prefixLength;

                    if (suitNameLength < 1)
                    {
                        log.Print("Suit texture is not inside a subdirectory: {0}", texture.name);
                    }
                    else // Add a suit texture.
                    {
                        string suitName     = texture.name.Substring(prefixLength, suitNameLength);
                        string originalName = texture.name.Substring(prefixLength + suitNameLength + 1);

                        if (!suitDirs.TryGetValue(suitName, out int index))
                        {
                            index = Suits.Count;
                            Suits.Add(new Suit {
                                Name = suitName
                            });
                            suitDirs.Add(suitName, index);
                        }

                        Suit suit = Suits[index];
                        if (suit.SetTexture(originalName, texture))
                        {
                            texture.wrapMode = TextureWrapMode.Clamp;
                        }
                        else
                        {
                            log.Print("Unknown suit texture name \"{0}\": {1}", originalName, texture.name);
                        }
                    }
                }
                else if (defaultPrefixIndex != -1)
                {
                    int    prefixLength = defaultPrefixIndex + Replacer.DefaultPrefix.Length;
                    string originalName = texture.name.Substring(prefixLength);

                    switch (originalName)
                    {
                    case "eyeballLeft":
                    case "eyeballRight":
                    case "pupilLeft":
                    case "pupilRight":
                        DefaultSkin[0].SetTexture(originalName, texture);
                        DefaultSkin[1].SetTexture(originalName, texture);
                        texture.wrapMode = TextureWrapMode.Clamp;
                        break;

                    case "kerbalHead":
                    case "kerbalHeadNRM":
                        DefaultSkin[0].SetTexture(originalName, texture);
                        texture.wrapMode = TextureWrapMode.Clamp;
                        break;

                    case "kerbalGirl_06_BaseColor":
                    case "kerbalGirl_06_BaseColorNRM":
                        DefaultSkin[1].SetTexture(originalName, texture);
                        texture.wrapMode = TextureWrapMode.Clamp;
                        break;

                    default:
                        if (DefaultSuit.SetTexture(originalName, texture))
                        {
                            texture.wrapMode = TextureWrapMode.Clamp;
                        }
                        break;
                    }
                }
            }

            // Visor needs to be replaced every time, not only on the proto-Kerbal model, so the visor from the default suit
            // must be set on all suits without a custom visor.
            VintageSuit.IvaVisor = VintageSuit.IvaVisor ?? DefaultSuit.IvaVisor;
            VintageSuit.EvaVisor = VintageSuit.EvaVisor ?? DefaultSuit.EvaVisor;

            foreach (var suit in Suits)
            {
                suit.IvaVisor = suit.IvaVisor ?? DefaultSuit.IvaVisor;
                suit.EvaVisor = suit.EvaVisor ?? DefaultSuit.EvaVisor;
            }

            ReadKerbalsConfigs();

            // Initialise default Kerbal, which is only loaded when the main menu shows.
            foreach (Texture2D texture in Resources.FindObjectsOfTypeAll <Texture2D>())
            {
                if (texture.name != null)
                {
                    if (texture.name == "kerbalHead")
                    {
                        DefaultSkin[0].Head = DefaultSkin[0].Head ?? texture;
                    }
                    else if (texture.name == "kerbalGirl_06_BaseColor")
                    {
                        DefaultSkin[1].Head = DefaultSkin[1].Head ?? texture;
                    }
                    else
                    {
                        DefaultSuit.SetTexture(texture.name, texture);
                    }
                }
            }

            // The previous loop filled the default suit, we still have to get the vintage one. Since IVA textures for vintage
            // suit are only instantiated when the Kerbals are created, we are in trouble here. Let's just use vintage EVA
            // suit for IVA too (both veteran and standard) and be happy with that :)
            Part vintageEva = PartLoader.getPartInfoByName("kerbalEVAVintage").partPrefab;

            foreach (SkinnedMeshRenderer smr in vintageEva.GetComponentsInChildren <SkinnedMeshRenderer>())
            {
                if (smr.name == "body01")
                {
                    var suitTexture = smr.material.mainTexture as Texture2D;

                    VintageSuit.IvaSuitVeteran = suitTexture;
                    VintageSuit.SetIvaSuit(suitTexture, 0, true);
                    VintageSuit.SetEvaSuit(suitTexture, 0, true);
                }
            }

            foreach (Kerbal kerbal in Resources.FindObjectsOfTypeAll <Kerbal>())
            {
                // After na IVA space is initialised, suits are reset to these values. Replace stock textures with default ones.
                kerbal.textureStandard = DefaultSuit.IvaSuit[0];
                kerbal.textureVeteran  = DefaultSuit.IvaSuitVeteran;
            }

            // `TRIvaModelModule` makes sure that internal spaces personalise all Kerbals inside them on instantiation.
            // This will not suffice for Ship Manifest, we will also need to re-add these modules on any crew transfer.
            foreach (InternalModel model in Resources.FindObjectsOfTypeAll <InternalModel>())
            {
                if (model.GetComponent <TRIvaModelModule>() == null)
                {
                    model.gameObject.AddComponent <TRIvaModelModule>();
                }
            }

            Part[] evas =
            {
                PartLoader.getPartInfoByName("kerbalEVA").partPrefab,
                PartLoader.getPartInfoByName("kerbalEVAfemale").partPrefab,
                PartLoader.getPartInfoByName("kerbalEVAVintage").partPrefab,
                PartLoader.getPartInfoByName("kerbalEVAfemaleVintage").partPrefab
            };

            foreach (Part eva in evas)
            {
                if (eva.GetComponent <TREvaModule>() == null)
                {
                    eva.gameObject.AddComponent <TREvaModule>();
                }
            }

            // Re-read scenario if database is reloaded during the space centre scene to avoid losing all per-game settings.
            if (HighLogic.CurrentGame != null)
            {
                ConfigNode scenarioNode = HighLogic.CurrentGame.config.GetNodes("SCENARIO")
                                          .FirstOrDefault(n => n.GetValue("name") == "TRScenario");

                if (scenarioNode != null)
                {
                    OnLoadScenario(scenarioNode);
                }
            }
        }