mirror of
https://github.com/LazyDuchess/OpenTS2.git
synced 2025-01-23 08:41:47 -05:00
Implement test for saving compressed entries.
This commit is contained in:
parent
a0350dbe37
commit
c5531b9e6e
8 changed files with 77 additions and 33 deletions
|
@ -15,6 +15,7 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
{
|
||||
public abstract byte[] GetBytes();
|
||||
public abstract AbstractAsset GetAsset();
|
||||
public abstract uint FileSize { get; }
|
||||
public T GetAsset<T>() where T : AbstractAsset
|
||||
{
|
||||
return GetAsset() as T;
|
||||
|
|
|
@ -30,5 +30,6 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
{
|
||||
return _asset;
|
||||
}
|
||||
public override uint FileSize => (uint)GetBytes().Length;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,5 +34,6 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
return null;
|
||||
return _codec.Deserialize(_bytes, _tgi, _package);
|
||||
}
|
||||
public override uint FileSize => (uint)GetBytes().Length;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,9 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
public class ChangedResourceDataEntry : ChangedResourceData
|
||||
{
|
||||
private DBPFEntry _dbpfEntry;
|
||||
|
||||
public override uint FileSize => _dbpfEntry.FileSize;
|
||||
|
||||
public ChangedResourceDataEntry(DBPFEntry entry)
|
||||
{
|
||||
this._dbpfEntry = entry;
|
||||
|
|
|
@ -82,7 +82,7 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
|
||||
public virtual byte[] GetBytes()
|
||||
{
|
||||
return Package.GetBytes(this);
|
||||
return Package.GetBytes(this, true);
|
||||
}
|
||||
|
||||
public T GetAsset<T>() where T : AbstractAsset
|
||||
|
@ -92,7 +92,7 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
|
||||
public virtual AbstractAsset GetAsset()
|
||||
{
|
||||
return Package.GetAsset(this);
|
||||
return Package.GetAsset(this, true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -195,20 +195,20 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
/// </summary>
|
||||
/// <param name="tgi">Resource TGI.</param>
|
||||
/// <param name="compressed">Compress?</param>
|
||||
public void SetCompressed(ResourceKey tgi, bool compressed)
|
||||
public void SetCompressed(DBPFEntry entry, bool compressed)
|
||||
{
|
||||
if (!ChangedEntries.TryGetValue(tgi, out DynamicDBPFEntry changedEntry))
|
||||
if (!ChangedEntries.TryGetValue(entry.TGI, out DynamicDBPFEntry changedEntry))
|
||||
{
|
||||
changedEntry = new DynamicDBPFEntry();
|
||||
changedEntry.Change.Data = new ChangedResourceDataEntry(_owner.GetEntryByTGI(tgi));
|
||||
changedEntry.Change.Data = new ChangedResourceDataEntry(entry);
|
||||
}
|
||||
changedEntry.TGI = tgi;
|
||||
changedEntry.TGI = entry.TGI;
|
||||
changedEntry.Change.Compressed = compressed;
|
||||
ChangedEntries[tgi] = changedEntry;
|
||||
InternalRestore(tgi);
|
||||
ChangedEntries[entry.TGI] = changedEntry;
|
||||
InternalRestore(entry.TGI);
|
||||
Dirty = true;
|
||||
Provider?.UpdateOrAddToResourceMap(changedEntry);
|
||||
RefreshCache(tgi);
|
||||
RefreshCache(entry.TGI);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -579,15 +579,15 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
/// </summary>
|
||||
/// <param name="entry">Entry to retrieve data for.</param>
|
||||
/// <returns>Data for entry.</returns>
|
||||
public byte[] GetBytes(DBPFEntry entry, bool ignoreDeleted = true)
|
||||
public byte[] GetBytes(DBPFEntry entry, bool ignoreChanges = false)
|
||||
{
|
||||
if (ignoreDeleted)
|
||||
if (!ignoreChanges)
|
||||
{
|
||||
if (Changes.DeletedEntries.ContainsKey(entry.TGI))
|
||||
return null;
|
||||
}
|
||||
if (Changes.ChangedEntries.ContainsKey(entry.TGI))
|
||||
return Changes.ChangedEntries[entry.TGI].Change.Data.GetBytes();
|
||||
}
|
||||
_reader.Seek(SeekOrigin.Begin, entry.FileOffset);
|
||||
var fileBytes = _reader.ReadBytes((int)entry.FileSize);
|
||||
var uncompressedSize = InternalGetUncompressedSize(entry);
|
||||
|
@ -603,15 +603,15 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
/// </summary>
|
||||
/// <param name="tgi">The TGI of the entry.</param>
|
||||
/// <returns>The entry's data.</returns>
|
||||
public byte[] GetBytesByTGI(ResourceKey tgi, bool ignoreDeleted = true)
|
||||
public byte[] GetBytesByTGI(ResourceKey tgi, bool ignoreChanges = false)
|
||||
{
|
||||
if (ignoreDeleted)
|
||||
if (!ignoreChanges)
|
||||
{
|
||||
if (Changes.DeletedEntries.ContainsKey(tgi))
|
||||
return null;
|
||||
}
|
||||
if (Changes.ChangedEntries.ContainsKey(tgi))
|
||||
return Changes.ChangedEntries[tgi].Change.Data.GetBytes();
|
||||
}
|
||||
if (_entryByTGI.ContainsKey(tgi))
|
||||
return GetBytes(_entryByTGI[tgi]);
|
||||
else
|
||||
|
@ -635,9 +635,9 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
/// </summary>
|
||||
/// <param name="entry">The DBPF Entry</param>
|
||||
/// <returns></returns>
|
||||
public AbstractAsset GetAsset<T>(DBPFEntry entry, bool ignoreDeleted = true) where T : AbstractAsset
|
||||
public AbstractAsset GetAsset<T>(DBPFEntry entry, bool ignoreChanges = false) where T : AbstractAsset
|
||||
{
|
||||
return GetAsset(entry, ignoreDeleted) as T;
|
||||
return GetAsset(entry, ignoreChanges) as T;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -645,13 +645,13 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
/// </summary>
|
||||
/// <param name="entry">The DBPF Entry</param>
|
||||
/// <returns></returns>
|
||||
public AbstractAsset GetAsset(DBPFEntry entry, bool ignoreDeleted = true)
|
||||
public AbstractAsset GetAsset(DBPFEntry entry, bool ignoreChanges = false)
|
||||
{
|
||||
if (Changes.DeletedEntries.ContainsKey(entry.TGI) && ignoreDeleted)
|
||||
if (Changes.DeletedEntries.ContainsKey(entry.TGI) && !ignoreChanges)
|
||||
return null;
|
||||
if (Changes.ChangedEntries.ContainsKey(entry.TGI))
|
||||
if (Changes.ChangedEntries.ContainsKey(entry.TGI) && !ignoreChanges)
|
||||
return Changes.ChangedEntries[entry.TGI].Change.Data.GetAsset();
|
||||
var item = GetBytes(entry, ignoreDeleted);
|
||||
var item = GetBytes(entry, ignoreChanges);
|
||||
var codec = Codecs.Get(entry.GlobalTGI.TypeID);
|
||||
var asset = codec.Deserialize(item, entry.GlobalTGI, this);
|
||||
asset.Compressed = InternalGetUncompressedSize(entry) > 0;
|
||||
|
@ -665,15 +665,15 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
return InternalGetUncompressedSize(entry) > 0;
|
||||
}
|
||||
|
||||
public T GetAssetByTGI<T>(ResourceKey tgi, bool ignoreDeleted = true) where T : AbstractAsset
|
||||
public T GetAssetByTGI<T>(ResourceKey tgi, bool ignoreChanges = false) where T : AbstractAsset
|
||||
{
|
||||
return GetAssetByTGI(tgi, ignoreDeleted) as T;
|
||||
return GetAssetByTGI(tgi, ignoreChanges) as T;
|
||||
}
|
||||
public AbstractAsset GetAssetByTGI(ResourceKey tgi, bool ignoreDeleted = true)
|
||||
public AbstractAsset GetAssetByTGI(ResourceKey tgi, bool ignoreChanges = false)
|
||||
{
|
||||
var entry = GetEntryByTGI(tgi, ignoreDeleted);
|
||||
var entry = GetEntryByTGI(tgi, ignoreChanges);
|
||||
if (entry != null)
|
||||
return GetAsset(entry, ignoreDeleted);
|
||||
return GetAsset(entry, ignoreChanges);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
@ -683,11 +683,11 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
/// </summary>
|
||||
/// <param name="tgi">The TGI of the entry.</param>
|
||||
/// <returns>The entry.</returns>
|
||||
public DBPFEntry GetEntryByTGI(ResourceKey tgi , bool ignoreDeleted = true)
|
||||
public DBPFEntry GetEntryByTGI(ResourceKey tgi , bool ignoreChanges = false)
|
||||
{
|
||||
if (Changes.DeletedEntries.ContainsKey(tgi) && ignoreDeleted)
|
||||
if (Changes.DeletedEntries.ContainsKey(tgi) && !ignoreChanges)
|
||||
return null;
|
||||
if (Changes.ChangedEntries.ContainsKey(tgi))
|
||||
if (Changes.ChangedEntries.ContainsKey(tgi) && !ignoreChanges)
|
||||
return Changes.ChangedEntries[tgi];
|
||||
if (_entryByTGI.ContainsKey(tgi))
|
||||
return _entryByTGI[tgi];
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace OpenTS2.Files.Formats.DBPF
|
|||
{
|
||||
get
|
||||
{
|
||||
return (uint)Change.Data.GetBytes().Length;
|
||||
return Change.Data.FileSize;
|
||||
}
|
||||
}
|
||||
public override byte[] GetBytes()
|
||||
|
|
|
@ -3,6 +3,8 @@ using OpenTS2.Files.Formats.DBPF;
|
|||
using OpenTS2.Common;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using OpenTS2.Content;
|
||||
using OpenTS2.Content.DBPF;
|
||||
|
||||
public class DPBFFileTest
|
||||
{
|
||||
|
@ -54,4 +56,40 @@ public class DPBFFileTest
|
|||
if (File.Exists(packagePath))
|
||||
File.Delete(packagePath);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DBPPFFileSaveCompressedEntryTest()
|
||||
{
|
||||
TestMain.Initialize();
|
||||
var packageSavePath = "TestFiles/TestPackage.package";
|
||||
var packageLoadPath = "TestAssets/TestPackage.package";
|
||||
var package = new DBPFFile(packageLoadPath);
|
||||
var stringEntry = package.GetEntryByTGI(new ResourceKey(1, GroupIDs.Local, TypeIDs.STR));
|
||||
Assert.IsNotNull(stringEntry);
|
||||
|
||||
// Set compression to true for entry.
|
||||
package.Changes.SetCompressed(stringEntry, true);
|
||||
package.FilePath = packageSavePath;
|
||||
|
||||
// Save the package to a file and dispose of it.
|
||||
package.WriteToFile();
|
||||
package.Dispose();
|
||||
|
||||
// Reload the package we just saved.
|
||||
var loadedPackage = new DBPFFile(packageSavePath);
|
||||
|
||||
// Should have 2 entries (compression directory)
|
||||
Assert.IsTrue(package.Entries.Count == 2);
|
||||
|
||||
var stringEntryLoaded = loadedPackage.GetEntryByTGI(new ResourceKey(1, GroupIDs.Local, TypeIDs.STR));
|
||||
|
||||
// Make sure the entry is compressed.
|
||||
Assert.IsTrue(loadedPackage.IsCompressed(stringEntryLoaded));
|
||||
|
||||
// Dispose of the package and delete the file.
|
||||
loadedPackage.Dispose();
|
||||
|
||||
if (File.Exists(packageSavePath))
|
||||
File.Delete(packageSavePath);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue