Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions Shared/SharedCore/PackFiles/Models/PackFileContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,24 @@ public void MergePackFileContainer(PackFileContainer other)

public void SaveToByteArray(BinaryWriter writer, GameInformation gameInformation)
{
long fileNamesOffset = 0;
var sortedFiles = FileList.OrderBy(x => x.Key, StringComparer.Ordinal).ToList();
foreach (var file in sortedFiles)
{
if (Header.Version == PackFileVersion.PFH5)
fileNamesOffset += 1;
if (Header.HasIndexWithTimeStamp)
fileNamesOffset += 4;
fileNamesOffset += 4 + file.Key.Length + 1; // Size + filename with zero terminator
}
if (Header.HasEncryptedData || Header.HasEncryptedIndex)
throw new InvalidOperationException("Saving encrypted packs is not supported.");

long headerSpesificBytes = 0;
long headerSpecificBytes = 0;
if (Header.HasIndexWithTimeStamp)
headerSpesificBytes += 4;
headerSpecificBytes += 4;
if (Header.Version == PackFileVersion.PFH5)
headerSpesificBytes += 1;
headerSpecificBytes += 1;

long fileNamesOffset2 = 0;
long fileNamesOffset = 0;

var sortedFiles = FileList.OrderBy(x => x.Key, StringComparer.Ordinal).ToList();
foreach (var file in sortedFiles)
{
var fileSize = 4;
var strLength = file.Key.Length + 1;
fileNamesOffset2 += fileSize + headerSpesificBytes + strLength;
var zeroTerminator = 1;
var fileNameBytes = Encoding.UTF8.GetByteCount(file.Key);
fileNamesOffset += fileSize + headerSpecificBytes + fileNameBytes + zeroTerminator;
}

Header.FileCount = (uint)FileList.Count;
Expand Down Expand Up @@ -110,7 +105,6 @@ public void SaveToByteArray(BinaryWriter writer, GameInformation gameInformation
{
var packFile = file.PackFile;
byte[] data;
var fileSize = 0;
uint uncompressedFileSize = 0;

// Read the data
Expand All @@ -125,21 +119,18 @@ public void SaveToByteArray(BinaryWriter writer, GameInformation gameInformation
// Compress the data into the right format
var compressedData = PackFileCompression.Compress(uncompressedData, file.IntendedCompressionFormat);
data = compressedData;
fileSize = compressedData.Length;
}
else if (packFile.DataSource is PackedFileSource packedFileSource && isCorrectCompressionFormat)
{
// The data is already in the right format so just get the compressed data
uncompressedFileSize = packedFileSource.UncompressedSize;
var compressedData = packedFileSource.ReadDataWithoutDecompressing();
data = compressedData;
fileSize = data.Length;
}
else
{
data = packFile.DataSource.ReadData();
fileSize = data.Length;
}

var fileSize = (uint)data.Length;

// Write the data
var offset = writer.BaseStream.Position;
Expand All @@ -151,12 +142,15 @@ public void SaveToByteArray(BinaryWriter writer, GameInformation gameInformation
writer.Write(fileSize);
writer.BaseStream.Position = currentPosition;

// We do not encrypt
var isEncrypted = false;

// Update DataSource
packFile.DataSource = new PackedFileSource(
packedFileSourceParent,
offset,
fileSize,
Header.HasEncryptedData,
isEncrypted,
shouldCompress,
file.IntendedCompressionFormat,
uncompressedFileSize);
Expand Down
4 changes: 2 additions & 2 deletions Shared/SharedCore/PackFiles/PackFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ public void SavePackContainer(PackFileContainer pf, string path, bool createBack
throw new Exception("File has been changed outside of AssetEditor. Can not save the file as it will cause corruptions");
}

pf.SystemFilePath = path;
using (var memoryStream = new FileStream(path + "_temp", FileMode.OpenOrCreate))
using (var memoryStream = new FileStream(path + "_temp", FileMode.Create))
{
using var writer = new BinaryWriter(memoryStream);
pf.SaveToByteArray(writer, gameInformation);
Expand All @@ -283,6 +282,7 @@ public void SavePackContainer(PackFileContainer pf, string path, bool createBack
File.Delete(path);
File.Move(path + "_temp", path);

pf.SystemFilePath = path;
pf.OriginalLoadByteSize = new FileInfo(path).Length;

_globalEventHub?.PublishGlobalEvent(new PackFileContainerSavedEvent(pf));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ public class ExportToDirectoryCommand() : IContextMenuCommand
public string GetDisplayName(TreeNode node) => "Export to system folder";
public bool IsEnabled(TreeNode node) => true;

public void Execute(TreeNode _selectedNode)
public void Execute(TreeNode selectedNode)
{
// TODO: Fix bug where if you export the packfilecontainer itself it doesn't export correctly.
var dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
var nodeStartDir = Path.GetDirectoryName(_selectedNode.GetFullPath());
var nodeStartDir = Path.GetDirectoryName(selectedNode.GetFullPath());
var fileCounter = 0;
SaveSelfAndChildren(_selectedNode, dialog.SelectedPath, nodeStartDir, ref fileCounter);
SaveSelfAndChildren(selectedNode, dialog.SelectedPath, nodeStartDir, ref fileCounter);
MessageBox.Show($"{fileCounter} files exported!");
}
}
Expand All @@ -33,7 +34,7 @@ void SaveSelfAndChildren(TreeNode node, string outputDirectory, string rootPath,
var nodeOriginalPath = node.GetFullPath();

var nodePathWithoutRoot = nodeOriginalPath;
if (rootPath.Length != 0)
if (rootPath != null && rootPath.Length != 0)
nodePathWithoutRoot = nodeOriginalPath.Replace(rootPath, "");

if (nodePathWithoutRoot.StartsWith("\\") == false)
Expand All @@ -56,7 +57,3 @@ void SaveSelfAndChildren(TreeNode node, string outputDirectory, string rootPath,
}

}

/*

*/
Loading