166 lines
3.7 KiB
C#
Raw Normal View History

2016-11-12 19:02:48 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
2016-12-25 21:43:22 +03:00
using System.Reflection;
2016-11-12 19:02:48 +03:00
using SonicAudioLib.IO;
using SonicAudioLib.FileBases;
2016-11-12 19:02:48 +03:00
using System.Collections;
namespace SonicAudioLib.Archives
2016-11-12 19:02:48 +03:00
{
public abstract class EntryBase
{
protected long length;
public virtual long Position { get; set; }
public virtual long Length
{
get
{
if (FilePath != null)
{
return FilePath.Length;
}
return length;
}
set
{
length = value;
}
}
public virtual FileInfo FilePath { get; set; }
public virtual Stream Open(Stream source)
{
return new SubStream(source, Position, length);
2016-11-12 19:02:48 +03:00
}
public virtual Stream Open()
{
return FilePath.OpenRead();
}
}
public abstract class ArchiveBase<T> : FileBase, IList<T>
2016-11-12 19:02:48 +03:00
{
protected List<T> entries = new List<T>();
public virtual int Count
{
get
{
return entries.Count;
}
}
public virtual bool IsReadOnly
2016-11-12 19:02:48 +03:00
{
get
{
return false;
2016-11-12 19:02:48 +03:00
}
}
public virtual T this[int index]
2016-11-12 19:02:48 +03:00
{
get
{
return entries[index];
}
set
{
entries[index] = value;
2016-11-12 19:02:48 +03:00
}
}
public virtual event ProgressChanged ProgressChanged;
public virtual int IndexOf(T item)
2016-11-12 19:02:48 +03:00
{
return entries.IndexOf(item);
}
public virtual void Insert(int index, T item)
{
entries.Insert(index, item);
2016-11-12 19:02:48 +03:00
}
public virtual void RemoveAt(int index)
2016-11-12 19:02:48 +03:00
{
entries.RemoveAt(index);
}
public virtual void Add(T item)
{
entries.Add(item);
2016-11-12 19:02:48 +03:00
}
public virtual void Clear()
{
entries.Clear();
}
public virtual bool Contains(T item)
{
return entries.Contains(item);
}
public virtual void CopyTo(T[] array, int arrayIndex)
{
entries.CopyTo(array, arrayIndex);
}
public virtual bool Remove(T item)
2016-11-12 19:02:48 +03:00
{
return entries.Remove(item);
2016-11-12 19:02:48 +03:00
}
public virtual IEnumerator<T> GetEnumerator()
2016-11-12 19:02:48 +03:00
{
return entries.GetEnumerator();
2016-11-12 19:02:48 +03:00
}
IEnumerator IEnumerable.GetEnumerator()
{
return entries.GetEnumerator();
}
2016-12-25 21:43:22 +03:00
protected virtual void OnProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressChanged?.Invoke(this, e);
}
2016-12-25 21:43:22 +03:00
#if DEBUG
public virtual void Print()
2016-12-25 21:43:22 +03:00
{
Type archiveType = GetType();
Console.WriteLine("{0}:", archiveType.Name);
foreach (PropertyInfo property in archiveType.GetProperties().Where(p => p.GetIndexParameters().Length == 0))
{
Console.WriteLine(" {0}: {1}", property.Name, property.GetValue(this));
}
foreach (T entry in entries)
{
Type entryType = entry.GetType();
Console.WriteLine("{0}:", entryType.Name);
foreach (PropertyInfo property in entryType.GetProperties().Where(p => p.GetIndexParameters().Length == 0))
{
Console.WriteLine(" {0}: {1}", property.Name, property.GetValue(entry));
}
}
}
#endif
2016-11-12 19:02:48 +03:00
}
}