Skip to content

Commit

Permalink
Memory usage optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikelChan committed Aug 28, 2020
1 parent 4a38e99 commit 6ca9d1d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
27 changes: 13 additions & 14 deletions AFSPacker/AFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void CreateAFS(string inputDirectory, string outputFile, string fi
TableOfContents[] toc = new TableOfContents[inputFiles.Length];
FileAttributes[] attributes = new FileAttributes[inputFiles.Length];

uint currentOffset = Pad((uint)(8 + (8 * inputFiles.Length) + 8), 0x800); // Header + TOC + AttributeTable Offset and size
uint currentOffset = Utils.Pad((uint)(8 + (8 * inputFiles.Length) + 8), 0x800); // Header + TOC + AttributeTable Offset and size

for (int n = 0; n < inputFiles.Length; n++)
{
Expand Down Expand Up @@ -69,7 +69,7 @@ public static void CreateAFS(string inputDirectory, string outputFile, string fi
toc[n].Offset = currentOffset;

currentOffset += toc[n].FileSize;
currentOffset = Pad(currentOffset, 0x800);
currentOffset = Utils.Pad(currentOffset, 0x800);

if (preserveFileNames)
{
Expand Down Expand Up @@ -122,9 +122,12 @@ public static void CreateAFS(string inputDirectory, string outputFile, string fi
{
if (inputFiles[n] != NULL_FILE)
{
byte[] data = File.ReadAllBytes(inputFiles[n]);
fs1.Seek(toc[n].Offset, SeekOrigin.Begin);
fs1.Write(data, 0, data.Length);

using (FileStream fs = File.OpenRead(inputFiles[n]))
{
fs.CopyTo(fs1);
}
}

Console.CursorLeft = 0;
Expand Down Expand Up @@ -159,7 +162,7 @@ public static void CreateAFS(string inputDirectory, string outputFile, string fi

//Pad final 0s
long currentPosition = fs1.Position;
long eof = Pad((uint)fs1.Position, 0x800);
long eof = Utils.Pad((uint)fs1.Position, 0x800);
for (long n = currentPosition; n < eof; n++) bw.Write((byte)0);
}
}
Expand Down Expand Up @@ -270,9 +273,7 @@ public static void ExtractAFS(string inputFile, string outputDirectory, string f
Console.CursorLeft = 0;
Console.WriteLine($"\nReading files... {n}/{numberOfFiles - 1}");

byte[] filedata = new byte[toc[n].FileSize];
fs1.Seek(toc[n].Offset, SeekOrigin.Begin);
fs1.Read(filedata, 0, filedata.Length);

string outputFile = Path.Combine(outputDirectory, fileName[n]);
if (File.Exists(outputFile))
Expand All @@ -281,7 +282,11 @@ public static void ExtractAFS(string inputFile, string outputDirectory, string f
Console.WriteLine($"Warning: File \"{outputFile}\" already exists. Overwriting.");
Console.ForegroundColor = ConsoleColor.White;
}
File.WriteAllBytes(outputFile, filedata);

using (FileStream fs = File.OpenWrite(outputFile))
{
fs1.CopySliceTo(fs, (int)toc[n].FileSize);
}

if (areThereAttributes)
{
Expand All @@ -307,12 +312,6 @@ public static void ExtractAFS(string inputFile, string outputDirectory, string f
}
}

static uint Pad(uint value, uint padBytes)
{
if ((value % padBytes) != 0) return value + (padBytes - (value % padBytes));
else return value;
}

static string[] CheckForDuplicatedFilenames(string[] fileNames)
{
string[] output = new string[fileNames.Length];
Expand Down
3 changes: 2 additions & 1 deletion AFSPacker/AFSPacker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
Expand All @@ -41,6 +41,7 @@
<Compile Include="AFS.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
4 changes: 2 additions & 2 deletions AFSPacker/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.1.0")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyVersion("1.2.2.0")]
[assembly: AssemblyFileVersion("1.2.2.0")]
26 changes: 26 additions & 0 deletions AFSPacker/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.IO;

namespace AFSPacker
{
static class Utils
{
public static uint Pad(uint value, uint padBytes)
{
if ((value % padBytes) != 0) return value + (padBytes - (value % padBytes));
else return value;
}

public static void CopySliceTo(this Stream origin, Stream destination, int bytesCount)
{
byte[] buffer = new byte[65536];
int count;

while ((count = origin.Read(buffer, 0, Math.Min(buffer.Length, bytesCount))) != 0)
{
destination.Write(buffer, 0, count);
bytesCount -= count;
}
}
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ AFSPacker -c <input_dir> <output_file> [list_file] [-nf] : Create AFS archive
```

## Changelog
### [1.2.2] - 2020-08-28
- Memory usage optimizations.

### [1.2.1] - 2019-11-16
- Fixed error when extracting files that contain invalid dates. Those dates will be ignored.

Expand Down

0 comments on commit 6ca9d1d

Please sign in to comment.