Skip to content

Commit

Permalink
Added stream dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
uholeschak committed Dec 22, 2024
1 parent 2735dc5 commit e31cd98
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 38 deletions.
34 changes: 32 additions & 2 deletions Tools/ApkUncompress2/AssemblyStore/AssemblyStoreExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace Xamarin.Android.AssemblyStore;

class AssemblyStoreExplorer
class AssemblyStoreExplorer : IDisposable
{
readonly AssemblyStoreReader reader;
private bool _disposed;

readonly AssemblyStoreReader reader;

public string StorePath { get; }
public AndroidTargetArch? TargetArch { get; }
Expand Down Expand Up @@ -244,4 +246,32 @@ public bool Contains (string assemblyName, AndroidTargetArch? targetArch = null)

return true;
}
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!_disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
reader?.Dispose();
}

// Note disposing has been done.
_disposed = true;
}
}

}
81 changes: 45 additions & 36 deletions Tools/ApkUncompress2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,63 @@ static int Main(string[] args)
haveErrors = true;
continue;
}

string baseFileName = Path.GetFileNameWithoutExtension(inputFile);
string? srcDir = Path.GetDirectoryName(inputFile);
if (string.IsNullOrEmpty(srcDir))
{
Console.WriteLine("Invalid directory");
haveErrors = true;
continue;
}

string outDirBase = Path.Combine(srcDir, baseFileName);
if (Directory.Exists(outDirBase))
try
{
Directory.Delete(outDirBase, true);
}
string baseFileName = Path.GetFileNameWithoutExtension(inputFile);
string? srcDir = Path.GetDirectoryName(inputFile);
if (string.IsNullOrEmpty(srcDir))
{
Console.WriteLine("Invalid directory");
haveErrors = true;
continue;
}

foreach (AssemblyStoreExplorer store in explorers)
{
if (store.Assemblies != null)
string outDirBase = Path.Combine(srcDir, baseFileName);
if (Directory.Exists(outDirBase))
{
Directory.Delete(outDirBase, true);
}

foreach (AssemblyStoreExplorer store in explorers)
{
foreach (AssemblyStoreItem storeItem in store.Assemblies)
if (store.Assemblies != null)
{
Stream? stream = store.ReadImageData(storeItem);
if (stream == null)
foreach (AssemblyStoreItem storeItem in store.Assemblies)
{
Console.WriteLine($"Failed to read image data for {storeItem.Name}");
continue;
}
Stream? stream = store.ReadImageData(storeItem);
if (stream == null)
{
Console.WriteLine($"Failed to read image data for {storeItem.Name}");
continue;
}

string archName = store.TargetArch.HasValue ? store.TargetArch.Value.ToString().ToLowerInvariant() : "unknown";
string outFile = Path.Combine(outDirBase, archName, storeItem.Name);
string? outDir = Path.GetDirectoryName(outFile);
if (string.IsNullOrEmpty(outDir))
{
continue;
}
string archName = store.TargetArch.HasValue ? store.TargetArch.Value.ToString().ToLowerInvariant() : "unknown";
string outFile = Path.Combine(outDirBase, archName, storeItem.Name);
string? outDir = Path.GetDirectoryName(outFile);
if (string.IsNullOrEmpty(outDir))
{
continue;
}

Directory.CreateDirectory(outDir);
using (FileStream fileStream = File.Create(outFile))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fileStream);
Directory.CreateDirectory(outDir);
using (FileStream fileStream = File.Create(outFile))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fileStream);
}
stream.Dispose();
}
stream.Dispose();
}
}
}

finally
{
foreach (AssemblyStoreExplorer store in explorers)
{
store.Dispose();
}
}
}

return haveErrors ? 1 : 0;
Expand Down

0 comments on commit e31cd98

Please sign in to comment.