Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
giollord committed Feb 27, 2023
1 parent 831347b commit 3c18e2a
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions UnityPackageExtractor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommandLine;
using System.Formats.Tar;
using System.IO.Compression;
using System.Text.RegularExpressions;

var parsedArgs = Parser.Default.ParseArguments<CommandLineOptions>(args)?.Value;
if (parsedArgs == null)
Expand All @@ -22,7 +23,12 @@
Console.WriteLine($"Found {allAssetFiles.Count} *.unitypackage files.");
var packageCounter = 0;

foreach(var unityPackageFile in allAssetFiles)
// Function to get id of asset
string? GetKey(string entryName) =>
entryName.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault(s => Regex.IsMatch(s, @"[0-9a-f]{32}", RegexOptions.IgnoreCase));

foreach (var unityPackageFile in allAssetFiles)
{
Console.WriteLine($"Processing {++packageCounter}/{allAssetFiles.Count} '{Path.GetFileName(unityPackageFile)}' at '{Path.GetDirectoryName(unityPackageFile)}'...");
var currentDestinationPath = copyToDirectoryNearAsset ? Path.GetDirectoryName(unityPackageFile)! : destinationPath;
Expand All @@ -41,11 +47,16 @@
continue;

using var streamReader = new StreamReader(entry.DataStream, leaveOpen: true);
var filePath = streamReader.ReadToEnd();
filePath = filePath.Substring(0, filePath.IndexOf('\n'));
var filePathCandidate = streamReader.ReadToEnd();
var indexOfNewLine = filePathCandidate.IndexOf('\n');
var filePath = indexOfNewLine > 0 ? filePathCandidate.Substring(0, indexOfNewLine) : filePathCandidate;

// getting key of asset - it it's top folder name
var key = entry.Name.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries)[0];
var key = GetKey(entry.Name);
if (key == null)
{
Console.WriteLine($"Can't extract key from asset file '{entry.Name}'");
continue;
}
resultFilePaths[key] = filePath;
}
}
Expand All @@ -67,8 +78,10 @@
if (type != "asset" && (!generateMeta || type != "asset.meta") && (!generatePreview || !type.StartsWith("preview")))
continue;

// getting key of asset - it it's top folder name
var key = entry.Name.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries)[0];
var key = GetKey(entry.Name);
if (key == null)
continue;

var destFilePath = Path.Combine(currentDestinationPath, resultFilePaths[key]);
if (destFilePath == null)
{
Expand Down

0 comments on commit 3c18e2a

Please sign in to comment.