Skip to content

Commit

Permalink
Fix extraction failure when some paths exceed the 255 character limit (
Browse files Browse the repository at this point in the history
…#20)

* Fix extraction failure when some paths exceed the 255 character limit

* Update MainWindow.xaml.cs

formatting

* use string.format and log the error

* working solution
  • Loading branch information
Morilli authored and Crauzer committed Aug 10, 2019
1 parent b48c454 commit d0cd70c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Obsidian/App.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v3">
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
</assembly>
72 changes: 64 additions & 8 deletions Obsidian/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Fantome.Libraries.League.Helpers.Cryptography;
using System.Text;
using MaterialDesignThemes.Wpf;
using Fantome.Libraries.League.IO.BIN;

namespace Obsidian
{
Expand Down Expand Up @@ -563,7 +564,8 @@ private void ExtractWADEntries(string selectedPath, List<WADEntry> entries)
if (StringDictionary.ContainsKey(entry.XXHash))
{
entryName = StringDictionary[entry.XXHash];
Directory.CreateDirectory(string.Format("{0}//{1}", selectedPath, Path.GetDirectoryName(entryName)));
int lastSeparatorPosition = entryName.LastIndexOf('/');
Directory.CreateDirectory(Path.Combine(selectedPath, entryName.Substring(0, lastSeparatorPosition + 1)));
}
else
{
Expand All @@ -578,9 +580,17 @@ private void ExtractWADEntries(string selectedPath, List<WADEntry> entries)

if ((bool)this.Config["ParallelExtraction"])
{
Parallel.ForEach(fileEntries, (entry) =>
Parallel.ForEach(fileEntries, entry =>
{
File.WriteAllBytes(string.Format("{0}//{1}", selectedPath, entry.Key), entry.Value);
if(entry.Key.StartsWith("data"))
{
ProcessPackedFile(selectedPath, entry.Key, entry.Value);
}
else
{
File.WriteAllBytes(Path.Combine(selectedPath, entry.Key), entry.Value);
}

progress += 0.5;
wadExtractor.ReportProgress((int)progress);
});
Expand All @@ -589,7 +599,15 @@ private void ExtractWADEntries(string selectedPath, List<WADEntry> entries)
{
foreach (KeyValuePair<string, byte[]> entry in fileEntries)
{
File.WriteAllBytes(string.Format("{0}//{1}", selectedPath, entry.Key), entry.Value);
if (entry.Key.StartsWith("data"))
{
ProcessPackedFile(selectedPath, entry.Key, entry.Value);
}
else
{
File.WriteAllBytes(Path.Combine(selectedPath, entry.Key), entry.Value);
}

progress += 0.5;
wadExtractor.ReportProgress((int)progress);
}
Expand All @@ -598,16 +616,54 @@ private void ExtractWADEntries(string selectedPath, List<WADEntry> entries)

wadExtractor.RunWorkerCompleted += (sender, args) =>
{
this.IsEnabled = true;
if (args.Error != null)
{
MessageBox.Show(string.Format("An error occured:\n{0}", args.Error), "", MessageBoxButton.OK, MessageBoxImage.Error);
Logger.Error(string.Format("WAD extraction failed:\n{0}", args.Error));
}
else
{
MessageBox.Show("Extraction Successful!", "", MessageBoxButton.OK, MessageBoxImage.Information);
Logger.Info("WAD Extraction Successful!");
}

this.progressBarWadExtraction.Maximum = 100;
this.progressBarWadExtraction.Value = 100;
MessageBox.Show("Extraction Succesfull!", "", MessageBoxButton.OK, MessageBoxImage.Information);
Logger.Info("WAD Extraction Successfull!");
this.IsEnabled = true;
};

wadExtractor.RunWorkerAsync();
}

private void ProcessPackedFile(string selectedPath, string filePath, byte[] data)
{
string extension = Path.GetExtension(filePath);
string basePath = filePath.Substring(0, filePath.IndexOf('_')).Replace("/", "\\");
string[] packed = Path.GetFileNameWithoutExtension(filePath.Substring(filePath.IndexOf('_') + 1)).Split('_');

for(int i = 0; i < packed.Length; i += 2)
{
string currentPath = string.Format("{0}\\{1}\\{2}{3}", basePath, packed[i], packed[i + 1], extension);
string fullPath = Path.Combine(selectedPath, currentPath);

Directory.CreateDirectory(Path.GetDirectoryName(fullPath));

if(File.Exists(fullPath))
{
BINFile binOld = new BINFile(fullPath);
BINFile binNew = new BINFile(new MemoryStream(data));

binOld.Entries.AddRange(binNew.Entries);

binOld.Write(fullPath);
}
else
{
File.WriteAllBytes(Path.Combine(selectedPath, currentPath), data);
}
}
}

private ulong HexStringToUInt64(string hexString)
{
byte[] byteArray = new byte[hexString.Length / 2];
Expand Down Expand Up @@ -659,4 +715,4 @@ private void OpenWADFile(string filePath)
Logger.Info("Opened WAD File: " + filePath);
}
}
}
}
5 changes: 5 additions & 0 deletions Obsidian/Obsidian.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<PropertyGroup>
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>App.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="Fantome.Libraries.League, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand All @@ -58,6 +61,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Transactions" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -113,6 +117,7 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="App.manifest" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down

0 comments on commit d0cd70c

Please sign in to comment.