Пример #1
0
        public static D2pEntry CreateEntryDefinition(D2pFile container, IDataReader reader)
        {
            D2pEntry entry = new D2pEntry(container);

            entry.ReadEntryDefinition(reader);
            return(entry);
        }
Пример #2
0
        public D2pEntry AddFile(string fileName, byte[] data)
        {
            D2pEntry entry = new D2pEntry(this, fileName, data);

            this.AddEntry(entry);
            return(entry);
        }
Пример #3
0
        public bool RemoveEntry(D2pEntry entry)
        {
            bool result;

            if (entry.Container != this)
            {
                if (!entry.Container.RemoveEntry(entry))
                {
                    result = false;
                    return(result);
                }
                if (!this.m_linksToSave.Contains(entry.Container))
                {
                    this.m_linksToSave.Enqueue(entry.Container);
                }
            }
            if (this.m_entries.Remove(entry.FullFileName))
            {
                entry.State = D2pEntryState.Removed;
                this.InternalRemoveDirectories(entry);
                this.OnPropertyChanged("Entries");
                if (entry.Container == this)
                {
                    this.IndexTable.EntriesCount--;
                }
                result = true;
            }
            else
            {
                result = false;
            }
            return(result);
        }
Пример #4
0
 public void AddEntry(D2pEntry entry)
 {
     entry.State = D2pEntryState.Added;
     this.InternalAddEntry(entry);
     this.IndexTable.EntriesCount++;
     this.OnPropertyChanged("Entries");
 }
Пример #5
0
 private void ReadEntriesDefinitions()
 {
     this.m_reader.Seek(this.IndexTable.EntriesDefinitionOffset, SeekOrigin.Begin);
     for (int i = 0; i < this.IndexTable.EntriesCount; i++)
     {
         D2pEntry entry = D2pEntry.CreateEntryDefinition(this, this.m_reader);
         this.InternalAddEntry(entry);
     }
 }
Пример #6
0
        public void SaveAs(string destination, bool overwrite = true)
        {
            while (this.m_linksToSave.Count > 0)
            {
                D2pFile link = this.m_linksToSave.Dequeue();
                link.Save();
            }
            Stream stream;

            if (!File.Exists(destination))
            {
                stream = File.Create(destination);
            }
            else
            {
                if (!overwrite)
                {
                    throw new InvalidOperationException("Cannot perform SaveAs : file already exist, notify overwrite parameter to true");
                }
                stream = File.OpenWrite(destination);
            }
            using (BigEndianWriter writer = new BigEndianWriter(stream))
            {
                writer.WriteByte(2);
                writer.WriteByte(1);
                D2pEntry[] entries = this.GetEntriesOfInstanceOnly();
                int        offset  = 2;
                D2pEntry[] array   = entries;
                for (int i = 0; i < array.Length; i++)
                {
                    D2pEntry entry = array[i];
                    byte[]   data  = this.ReadFile(entry);
                    entry.Index = (int)writer.Position - offset;
                    writer.WriteBytes(data);
                }
                int entriesDefOffset = (int)writer.Position;
                array = entries;
                for (int i = 0; i < array.Length; i++)
                {
                    D2pEntry entry = array[i];
                    entry.WriteEntryDefinition(writer);
                }
                int propertiesOffset = (int)writer.Position;
                foreach (D2pProperty property in this.m_properties)
                {
                    property.WriteProperty(writer);
                }
                this.IndexTable.OffsetBase              = offset;
                this.IndexTable.EntriesCount            = entries.Length;
                this.IndexTable.EntriesDefinitionOffset = entriesDefOffset;
                this.IndexTable.PropertiesCount         = this.m_properties.Count;
                this.IndexTable.PropertiesOffset        = propertiesOffset;
                this.IndexTable.Size = this.IndexTable.EntriesDefinitionOffset - this.IndexTable.OffsetBase;
                this.IndexTable.WriteTable(writer);
            }
        }
Пример #7
0
        public byte[] ReadFile(string fileName)
        {
            if (!this.Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }
            D2pEntry entry = this.GetEntry(fileName);

            return(this.ReadFile(entry));
        }
Пример #8
0
        public void ExtractFile(string fileName, bool overwrite = false)
        {
            if (!this.Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }
            D2pEntry entry = this.GetEntry(fileName);
            string   dest  = Path.Combine("./", entry.FullFileName);

            if (!Directory.Exists(Path.GetDirectoryName(dest)))
            {
                Directory.CreateDirectory(dest);
            }
            this.ExtractFile(fileName, dest, overwrite);
        }
Пример #9
0
 public byte[] ReadFile(D2pEntry entry)
 {
     byte[] result;
     if (entry.Container != this)
     {
         result = entry.Container.ReadFile(entry);
     }
     else
     {
         if (entry.Index >= 0 && this.IndexTable.OffsetBase + entry.Index >= 0)
         {
             this.m_reader.Seek(this.IndexTable.OffsetBase + entry.Index, SeekOrigin.Begin);
         }
         byte[] data = entry.ReadEntry(this.m_reader);
         result = data;
     }
     return(result);
 }
Пример #10
0
 private void InternalRemoveDirectories(D2pEntry entry)
 {
     for (D2pDirectory current = entry.Directory; current != null; current = current.Parent)
     {
         current.Entries.Remove(entry);
         if (current.Parent != null && current.Entries.Count == 0)
         {
             current.Parent.Directories.Remove(current);
         }
         else
         {
             if (current.IsRoot && current.Entries.Count == 0)
             {
                 this.m_rootDirectories.Remove(current);
             }
         }
     }
 }
Пример #11
0
        public bool ModifyFile(string file, byte[] data)
        {
            D2pEntry entry = this.TryGetEntry(file);
            bool     result;

            if (entry == null)
            {
                result = false;
            }
            else
            {
                entry.ModifyEntry(data);
                if (entry.Container != this && !this.m_linksToSave.Contains(entry.Container))
                {
                    this.m_linksToSave.Enqueue(entry.Container);
                }
                result = true;
            }
            return(result);
        }
Пример #12
0
 private void InternalAddDirectories(D2pEntry entry)
 {
     string[] directories = entry.GetDirectoriesName();
     if (directories.Length != 0)
     {
         D2pDirectory current = null;
         if (!this.HasDirectory(directories[0]))
         {
             current = new D2pDirectory(directories[0]);
             this.m_rootDirectories.Add(current);
         }
         else
         {
             current = this.TryGetDirectory(directories[0]);
         }
         current.Entries.Add(entry);
         foreach (string directory in directories.Skip(1))
         {
             if (!current.HasDirectory(directory))
             {
                 D2pDirectory dir = new D2pDirectory(directory)
                 {
                     Parent = current
                 };
                 current.Directories.Add(dir);
                 current = dir;
             }
             else
             {
                 current = current.TryGetDirectory(directory);
             }
             current.Entries.Add(entry);
         }
         entry.Directory = current;
     }
 }
Пример #13
0
        public bool RemoveFile(string file)
        {
            D2pEntry entry = this.TryGetEntry(file);

            return(entry != null && this.RemoveEntry(entry));
        }
Пример #14
0
 private void InternalAddEntry(D2pEntry entry)
 {
     this.m_entries.Add(entry.FullFileName, entry);
     this.InternalAddDirectories(entry);
 }