Exemplo n.º 1
0
        public static void UnLoadFromTexture(Material material, int id, bool force, List <TexRefCnt> list)
        {
            Texture2D texture = (Texture2D)material.GetTexture(id);

            if (texture != null)
            {
                //Loader.Log("List: " + texture.name);
                TexRefCnt texRef;
                if (textureDictionary.ContainsKey(texture.name))
                {
                    //Loader.Log("From Dictionary");
                    texRef = textureDictionary[texture.name];
                }
                else
                {
                    texRef = new TexRefCnt(texture);
                }

                texRef.Unload(force);
                if (list != null && !list.Contains(texRef))
                {
                    list.Add(texRef);
                }
            }
        }
Exemplo n.º 2
0
        public void Unload(bool force = false)
        {
            if (loaded || force)
            {
                string partUrl = this.part.partInfo.partUrl;
                Loader.Log("Unloading: " + partUrl);
                foreach (Renderer mr in part.FindModelComponents <Renderer>())
                {
                    Loader.Log("Renderer: " + mr.name);
                    TexRefCnt.UnLoadFromRenderer(mr, force);
                }

                if (!internalCache.ContainsKey(partUrl))
                {
                    if (part.partInfo.internalConfig.HasData && HighLogic.LoadedSceneIsGame)
                    {
                        Part             iPart         = fetchInternalPart();
                        InternalModel    internalModel = iPart.internalModel;
                        List <TexRefCnt> list          = new List <TexRefCnt>();
                        foreach (Renderer mr in internalModel.FindModelComponents <Renderer>())
                        {
                            TexRefCnt.UnLoadFromRenderer(mr, force, list);
                        }
                        internalCache[partUrl] = list;
                        GameObject.DestroyImmediate(iPart);
                    }
                }
                else
                {
                    List <TexRefCnt> list = internalCache[partUrl];
                    TexRefCnt.UnLoadFromList(list, force);
                }
                loaded = false;
            }
        }
Exemplo n.º 3
0
 public override IEnumerator Load(UrlDir.UrlFile urlFile, FileInfo file)
 {
     GameDatabase.TextureInfo texInfo =
         TexRefCnt.Load(urlFile);
     obj        = texInfo;
     successful = true;
     yield return(null);
 }
Exemplo n.º 4
0
        internal static GameDatabase.TextureInfo Load(UrlDir.UrlFile urlFile)
        {
            UrlDir.UrlFile file       = urlFile;
            string         hash       = TexRefCnt.GetMD5String(file.fullPath);
            string         urlReplace = TexPatchLoader.GetReplacement(file.url);

            if (urlReplace != null)
            {
                //   file = getReplacementFile(urlReplace);
                //   hash = TexRefCnt.GetMD5String(file.fullPath);
                GameDatabase.TextureInfo dummy = new GameDatabase.TextureInfo(file, new Texture2D(2, 2), false, false, false);
                dummy.name         = file.url;
                dummy.texture.name = dummy.name;
                return(dummy);
            }

            GameDatabase.TextureInfo texInfo = new GameDatabase.TextureInfo(file, null, false, false, false);
            bool   hasMipmaps = updateToStockSettings(texInfo);
            string cached     = Directory.GetParent(Assembly.GetExecutingAssembly().Location) + "/ScaledTexCache/" + Path.GetFileName(file.url) + "_hash_" + hash;

            Loader.Log("hash: " + hash);

            if (texHashDictionary.ContainsKey(hash))
            {
                Loader.Log("Loaded From hash");
                texInfo.texture = texHashDictionary[hash];
            }
            else if (File.Exists(cached))
            {
                Loader.Log("Loaded From cache @" + cached);
                byte[]        cache  = System.IO.File.ReadAllBytes(cached);
                TextureFormat format = texInfo.isCompressed ? TextureFormat.DXT5 : TextureFormat.ARGB32;
                texInfo.texture = new Texture2D(32, 32, format, hasMipmaps);
                if (texInfo.file.fileExtension == "dds")
                {
                    //texInfo.isReadable = true;
                }
                texInfo.texture.Apply(hasMipmaps, !texInfo.isReadable);
                texInfo.texture.LoadImage(cache);
                texHashDictionary[hash] = texInfo.texture;
                //add texture reference.
                TexRefCnt texRef = new TexRefCnt(texInfo, hash, true);
                texInfo.texture.name = file.url;
            }
            else
            {
                TextureConverter.Reload(texInfo, false, default(Vector2), null, hasMipmaps);
                texHashDictionary[hash] = texInfo.texture;
                TexRefCnt texRef = new TexRefCnt(texInfo, hash, false);
                texInfo.texture.name = file.url;
            }

            Loader.Log(texInfo.file.fileExtension + " c: " + texInfo.isCompressed + " n: " + texInfo.isNormalMap + " r: " + texInfo.isReadable + " m: " + (texInfo.texture.mipmapCount > 1));

            texInfo.name = urlFile.url;
            Loader.Log(texInfo.name + " -> " + texInfo.texture.name);
            return(texInfo);
        }
Exemplo n.º 5
0
 private static void UnLoadFromQueue()
 {
     while (unloadQueue.Count > 0)
     {
         TexRefCnt texRef = unloadQueue.Dequeue();
         if (!texRef.unloaded && texRef.count <= 0)
         {
             texRef.Minimize(false);
             texRef.unloaded = true;
         }
     }
 }
        private void Load()
        {
            if (!loaded)
            {
                string partUrl = this.part.partInfo.partUrl;

                Loader.Log("Loading: " + partUrl);

                if (!texCache.ContainsKey(partUrl))
                {
                    List <TexRefCnt> list = new List <TexRefCnt>();
                    foreach (Renderer mr in part.FindModelComponents <Renderer>())
                    {
                        //Loader.Log("Renderer: " + mr.name);
                        TexRefCnt.LoadFromRenderer(mr, list);
                    }


                    if (part.partInfo.internalConfig.HasData && HighLogic.LoadedSceneIsGame)
                    {
                        Loader.Log("loading IVA: ");
                        Part iPart = fetchInternalPart();
                        if (iPart != null)
                        {
                            InternalModel internalModel = iPart.internalModel;
                            foreach (Renderer mr in internalModel.FindModelComponents <Renderer>())
                            {
                                //Loader.Log("ImRenderer: " + mr.name);
                                TexRefCnt.LoadFromRenderer(mr, list);
                            }
                            GameObject.DestroyImmediate(iPart);
                        }
                    }
                    else
                    {
                        Loader.Log(part.partInfo.internalConfig.HasData + " " + HighLogic.LoadedSceneIsGame);
                    }

                    texCache[partUrl] = list;
                }
                else
                {
                    Loader.Log("Loading from cache...");
                    List <TexRefCnt> list = texCache[partUrl];
                    TexRefCnt.LoadFromList(list);
                }
                loaded = true;
            }
        }
Exemplo n.º 7
0
        public static void LoadFromTexture(Material material, int id, List <TexRefCnt> list)
        {
            Texture2D texture = (Texture2D)material.GetTexture(id);

            if (texture != null)
            {
                TexRefCnt texRef;
                if (textureDictionary.ContainsKey(texture.name))
                {
                    texRef = textureDictionary[texture.name];
                }
                else
                {
                    texRef = new TexRefCnt(texture);
                }
                texRef.Load();
                if (list != null && !list.Contains(texRef))
                {
                    list.Add(texRef);
                }
            }
        }
Exemplo n.º 8
0
        internal static GameDatabase.TextureInfo Load(UrlDir.UrlFile urlFile)
        {
            string hash         = GetMD5String(urlFile.fullPath);
            bool   hasMipmaps   = urlFile.fileExtension == "png" ? false : true;
            bool   isNormalMap  = urlFile.name.EndsWith("NRM");
            bool   isReadable   = urlFile.fileExtension == "dds" || isNormalMap ? false : true;
            bool   isCompressed = urlFile.fileExtension == "tga" ? false : true;

            GameDatabase.TextureInfo texInfo = new GameDatabase.TextureInfo(urlFile, null, isNormalMap, isReadable, isCompressed);
            texInfo.name = urlFile.url;

            string cached = Directory.GetParent(Assembly.GetExecutingAssembly().Location) + "/ScaledTexCache/" + texInfo.file.name + "_hash_" + hash;

            /*
             * if (texHashDictionary.ContainsKey(hash))
             * {
             *  texInfo.texture = texHashDictionary[hash];
             *
             *  foreach(UrlDir.UrlConfig config in GameDatabase.Instance.root.AllConfigs)
             *  {
             *      ConfigNode model = config.config.GetNode("MODEL");
             *      if (config.type == "PART" && model != null)
             *      {
             *          int i = 0;
             *          string value = model.GetValue("texture", i);
             *          while(value != null)
             *          {
             *              String replace = @"(.*),\s*" + Regex.Escape(urlFile.url);
             *              if (Regex.IsMatch(value, replace))
             *              {
             *                  model.SetValue("texture", Regex.Replace(value, replace, "$1, " + texInfo.texture.name), i);
             *              }
             *              i++;
             *              value = model.GetValue("texture", i);
             *          }
             *
             *      }
             *  }
             *
             * }
             * else */
            if (File.Exists(cached))
            {
                Loader.Log("Loaded From cache @" + cached);
                byte[]        cache  = System.IO.File.ReadAllBytes(cached);
                TextureFormat format = isCompressed ? TextureFormat.DXT5 : TextureFormat.ARGB32;
                texInfo.texture = new Texture2D(32, 32, format, hasMipmaps);
                texInfo.texture.Apply(hasMipmaps, !isReadable);
                texInfo.texture.LoadImage(cache);
                texHashDictionary[hash] = texInfo.texture;
                //add texture reference.

                texInfo.texture.name = texInfo.name;
                TexRefCnt texRef = new TexRefCnt(texInfo, hash, true);
            }
            else
            {
                TextureConverter.Reload(texInfo, false, default(Vector2), null, hasMipmaps);
                texHashDictionary[hash] = texInfo.texture;
                texInfo.texture.name    = texInfo.name;
                TexRefCnt texRef = new TexRefCnt(texInfo, hash, false);
            }

            return(texInfo);
        }