internal Thumbnail ReplaceOrAdd(MemoryStream stream, Thumbnail old)
 {
     // Decide if new thumb can fit where old thumb was
     if (stream.Length <= old.Length)
         return Add(stream, old.Offset);
     else
         return Add(stream);  // Append otherwise
 }
        public Thumbnail Add(MemoryStream thumb)
        {
            Thumbnail thumbnail = new Thumbnail(GameDirecs.ThumbnailCachePath);
            lock (locker)
            {
                thumbnail.Offset = (int)writer.Position;
                thumb.WriteTo(writer);
                thumbnail.Length = (int)thumb.Length;
            }

            return thumbnail;
        }
 public AbstractTexInfo()
 {
     Thumb = new Thumbnail();
 }
Example #4
0
        public bool ReadFromFile(string fileName = null)
        {
            lock (Textures)
            {
                if (Textures.Count > 0)  // When it comes back into this after Texplorer has been closed but the Toolset hasn't, it needs to "rebuild" itself i.e. mark itself as valid if it's been loaded previously.
                {
                    Valid = true;
                    return true;
                }
            }
            

            OnPropertyChanged(nameof(Exists));

            string tempFilename = fileName;
            if (fileName == null)
                tempFilename = TreePath;

            if (!File.Exists(tempFilename))
                return false;

            List<TreeTexInfo> TempTextures = new List<TreeTexInfo>();
            try
            {
                using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(tempFilename)))
                {
                    using (GZipStream compressed = new GZipStream(ms, CompressionMode.Decompress))  // Compressed for nice small trees
                    {
                        using (BinaryReader bin = new BinaryReader(compressed))
                        {
                            // Check tree is suitable for this version
                            int magic = bin.ReadInt32();
                            if (magic != 631991)
                            {
                                DebugOutput.PrintLn("Tree too old. Delete and rebuild tree.");
                                return false;
                            }

                            // Tree is suitable. Begin reading
                            int gameVersion = bin.ReadInt32();
                            if (GameDirecs.GameVersion != GameVersion)
                                throw new InvalidOperationException($"Incorrect Tree Loaded. Expected: ME{GameDirecs.GameVersion}, Got: {GameVersion}");

                            TreeVersion = bin.ReadString();

                            // PCCS
                            lock (ScannedPCCs)
                            {
                                int pccCount = bin.ReadInt32();
                                for (int i = 0; i < pccCount; i++)
                                    ScannedPCCs.Add(bin.ReadString());
                            }
                            

                            // Textures
                            lock (Textures)
                            {
                                int texCount = bin.ReadInt32();
                                for (int i = 0; i < texCount; i++)
                                {
                                    TreeTexInfo tex = new TreeTexInfo(GameDirecs);
                                    tex.TexName = bin.ReadString();
                                    tex.Hash = bin.ReadUInt32();
                                    //tex.StorageType = (Texture2D.storage)bin.ReadInt32();
                                    tex.FullPackage = bin.ReadString();
                                    tex.Format = (ImageEngineFormat)bin.ReadInt32();

                                    Thumbnail thumb = new Thumbnail(GameDirecs.ThumbnailCachePath);
                                    thumb.Offset = bin.ReadInt64();
                                    thumb.Length = bin.ReadInt32();
                                    tex.Thumb = thumb;

                                    tex.Mips = bin.ReadInt32();
                                    tex.Width = bin.ReadInt32();
                                    tex.Height = bin.ReadInt32();
                                    tex.LODGroup = bin.ReadString();

                                    int numPccs = bin.ReadInt32();
                                    for (int j = 0; j < numPccs; j++)
                                    {
                                        string userAgnosticPath = ScannedPCCs[bin.ReadInt32()];
                                        int ExpID = bin.ReadInt32();
                                        tex.PCCs.Add(new PCCEntry(Path.Combine(GameDirecs.BasePath, userAgnosticPath), ExpID, GameDirecs));
                                    }

                                    TempTextures.Add(tex);
                                }
                            }

                            lock (Textures)
                                Textures.AddRange(TempTextures);

                            // Sort ME1 files
                            if (GameVersion == 1)
                                ToolsetTextureEngine.ME1_SortTexturesPCCs(Textures);

                            // Texture folders
                            // Top all encompassing node
                            lock (TextureFolders)
                            {
                                TexplorerTextureFolder TopTextureFolder = new TexplorerTextureFolder("All Texture Files", null, null);

                                var folderCount = bin.ReadInt32();
                                var tempFolders = new List<TexplorerTextureFolder>();
                                for (int i = 0; i < folderCount; i++)
                                {
                                    var folder = ReadTreeFolders(bin, TopTextureFolder);
                                    tempFolders.Add(folder);
                                }

                                TopTextureFolder.Folders.AddRange(tempFolders);
                                TextureFolders.Add(TopTextureFolder);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                DebugOutput.PrintLn($"Failed to load tree: {fileName}. Reason: {e.ToString()}");
                return false;
            }

            Valid = true;
            return true;
        }