Skip to content

Commit

Permalink
[Sonic_Crytek] Create metadata file for strict file ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperbx committed Jan 9, 2024
1 parent d937954 commit 91e923b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 19 deletions.
95 changes: 77 additions & 18 deletions HyperLib/Frameworks/Sonic_Crytek/Archive.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HyperLib.Helpers;
using HyperLib.IO;
using HyperLib.IO.Extensions;

namespace HyperLib.Frameworks.Sonic_Crytek
Expand Down Expand Up @@ -56,12 +57,15 @@ public override void Import(string in_path)
if (!Directory.Exists(in_path))
return;

foreach (var file in Directory.EnumerateFiles(in_path, "*", SearchOption.AllDirectories))
var metadataPath = Path.Combine(in_path, Path.GetFileName(in_path) + ".meta");
var metadataList = new List<string>();

void ImportFile(string in_file)
{
var relativePath = FileSystemHelper.GetRelativeDirectoryName(in_path, file, true);
var relativePath = FileSystemHelper.GetRelativeDirectoryName(in_path, in_file, true);

// TODO: compression and CRC32 hashing.
var data = File.ReadAllBytes(file);
var data = File.ReadAllBytes(in_file);
var node = new ArchiveFile(0, (uint)data.Length, 0, 0, relativePath, data);

SetSpecialFlags(ref node);
Expand All @@ -77,6 +81,49 @@ public override void Import(string in_path)

Files.Add(node);
}

if (File.Exists(metadataPath))
{
// Excludes the metadata file.
metadataList.Add(metadataPath);

using (var reader = File.OpenText(metadataPath))
{
while (!reader.EndOfStream)
{
var relativePath = reader.ReadLine();

if (string.IsNullOrEmpty(relativePath))
continue;

var filePath = Path.Combine(in_path, FileSystemHelper.ConvertPathToWindows(relativePath));

if (File.Exists(filePath))
{
ImportFile(filePath);
}
else
{
Logger.Error($"Importing failed: {relativePath}");
Logger.Warning("Importing files out of order!");
}

metadataList.Add(filePath);
}
}
}
else
{
Logger.Warning("Importing files out of order!");
}

foreach (var file in Directory.EnumerateFiles(in_path, "*", SearchOption.AllDirectories))
{
if (metadataList.Contains(file))
continue;

ImportFile(file);
}
}

public override void Export(string in_path = "")
Expand All @@ -86,30 +133,42 @@ public override void Export(string in_path = "")

var dir = Directory.CreateDirectory(in_path).FullName;

foreach (var file in Files)
// Create metadata file for strict file order.
using (var writer = File.CreateText(Path.Combine(dir, Path.GetFileName(in_path) + ".meta")))
{
Logger.Log($"Exporting file: {file.Name}");
for (int i = 0; i < Files.Count; i++)
{
var file = Files[i];

Logger.Log($"Exporting file: {file.Name}");

#if !DEBUG
try
try
#endif
{
var filePath = Path.Combine(dir, file.Name);
var dirPath = Path.GetDirectoryName(filePath);
{
if (file.SpecialFlags.ToString().EndsWith("Alt"))
{
file.Name = FileSystemHelper.ConvertPathToUnix(
FileSystemHelper.ChangeFileName(file.Name, $"{Path.GetFileNameWithoutExtension(file.Name)}_alt"));
}

Directory.CreateDirectory(dirPath);
var filePath = Path.Combine(dir, file.Name);
var dirPath = Path.GetDirectoryName(filePath);

if (File.Exists(filePath) && file.SpecialFlags != ESpecialFlags.None)
filePath = FileSystemHelper.ChangeFileName(filePath, Path.GetFileNameWithoutExtension(filePath) + "_alt");
Directory.CreateDirectory(dirPath);

File.WriteAllBytes(filePath, file.Data);
}
File.WriteAllBytes(filePath, file.Data);
}
#if !DEBUG
catch (Exception ex)
{
Logger.Error($"Exporting failed: {file}\nReason: {ex}");
}
catch (Exception ex)
{
Logger.Error($"Exporting failed: {file}\nReason: {ex}");
}
#endif

// Export file name to metadata.
writer.WriteLine(file.Name);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion HyperLib/Helpers/FileSystemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public static string ConvertPathToUnix(string in_path)
return in_path.Replace('\\', '/');
}

public static string ConvertPathToWindows(string in_path)
{
return in_path.Replace('/', '\\');
}

public static string ChangeFileName(string in_filePath, string in_newFileName, bool in_isOriginalExtensions = true)
{
if (in_isOriginalExtensions)
Expand All @@ -25,7 +30,7 @@ public static string GetRelativeDirectoryName(string in_rootDir, string in_path,
var relativePath = in_path[in_rootDir.Length..].TrimStart(Path.DirectorySeparatorChar);

if (in_isConvertToUnixSeparators)
relativePath = relativePath.Replace('\\', '/');
relativePath = ConvertPathToUnix(relativePath);

return relativePath;
}
Expand Down

0 comments on commit 91e923b

Please sign in to comment.