Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spd/Spr Merging #10

Merged
merged 52 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b28b209
SpdEmulator WIP
Secre-C Sep 13, 2023
949e670
added ability to replace existing textures
Secre-C Sep 14, 2023
0eb2b25
slight refactoring and optimization
Secre-C Sep 14, 2023
41178b6
implemented api for spd emulator
Secre-C Sep 18, 2023
0e7c13c
fixed swapped height / width when reading from dds
Secre-C Sep 18, 2023
e0382db
fixed spd api interface build path
Secre-C Sep 19, 2023
e153244
add support for sprite id ranges
Secre-C Sep 21, 2023
717f4bd
Merge remote-tracking branch 'upstream/main' into SpdEmulator
Secre-C Sep 21, 2023
9b8abac
remove unused usings
Secre-C Sep 21, 2023
7b62ec9
Wip spr support
Secre-C Oct 9, 2023
ebdb04a
Delete Tmx.cs
Secre-C Oct 9, 2023
3cba029
modify original spr header instead of generating a new one
Secre-C Oct 9, 2023
cc8f7e0
modify original spd header instead of generating a new one
Secre-C Oct 10, 2023
95d2b74
slightly reworked how spd patching works
Secre-C Oct 22, 2023
4c15df3
removed test print
Secre-C Oct 22, 2023
3bcbb9e
add padding before writing sprite entries
Secre-C Oct 22, 2023
960d2ce
Create spd.md
Secre-C Nov 3, 2023
cc0a439
Update spd.md
Secre-C Nov 4, 2023
e75e9b9
Update mkdocs.yml
Secre-C Nov 4, 2023
3755005
reworked texture replacement implementation
Secre-C Nov 7, 2023
bc22251
Update spd.md
Secre-C Nov 8, 2023
5dd4927
Update spd.md
Secre-C Nov 8, 2023
6f00017
fixed some links in spd.md
Secre-C Nov 8, 2023
d718b76
removed unnecessary print
Secre-C Nov 8, 2023
64ab45e
implemented AddDirectory and AddFile spd api methods
Secre-C Nov 8, 2023
02d455a
forgot to implement the addFile methods
Secre-C Nov 9, 2023
cef9b41
Started on suggested changes
Secre-C Nov 11, 2023
1bd8609
removed unused usings
Secre-C Nov 11, 2023
25a4354
revert RegisterSpd change, and use filestream directly instead of cop…
Secre-C Nov 12, 2023
1c9fc7b
reduce loops in texture replacement using dictionary dictionaries :P
Secre-C Nov 12, 2023
83e0273
fix texture replacement not working after last commit
Secre-C Nov 14, 2023
7cc34d8
wrote spd emulator tests
Secre-C Nov 14, 2023
238da40
close handles in api functions
Secre-C Nov 14, 2023
32bce71
removed unused suppressions and removed double dict access
Secre-C Nov 14, 2023
96f14a8
ran code cleanup
Secre-C Nov 14, 2023
750c3f6
added a texture dimension warning to spd.md
Secre-C Nov 21, 2023
7ac596b
removed exponent tag from new warning in favor of a caret '^' since i…
Secre-C Nov 21, 2023
f16de8e
Merge remote-tracking branch 'upstream/main'
Secre-C Nov 21, 2023
a98a9c0
adjusted sprite entry reading logic
Secre-C Nov 21, 2023
a9579c8
added test that combines all spd test
Secre-C Nov 21, 2023
a617ba3
Tests: Copy to output using glob
Sewer56 Dec 15, 2023
1fc66dc
Formatting: Use file scoped namespace for Mod.cs
Sewer56 Dec 15, 2023
9986f9b
Small Cleanup of Warnings
Sewer56 Dec 15, 2023
220a998
Style: Use 'var' in SpriteChecker
Sewer56 Dec 15, 2023
85815bb
Removed: Dead code
Sewer56 Dec 15, 2023
548987a
Update spd.md
Secre-C Dec 15, 2023
564ba83
Port all to Reloaded.Memory 9.X and re-enable SPD Tests
Sewer56 Dec 15, 2023
bed88d3
Merge branch 'main' of https://github.com/Secre-C/FileEmulationFramework
Sewer56 Dec 15, 2023
fe2874a
Merge remote-tracking branch 'origin/main'
Sewer56 Dec 15, 2023
a811576
Added: Trimmer support to SPD Emulator
Sewer56 Dec 15, 2023
b2de507
updated some method summaries
Secre-C Dec 23, 2023
1920c8d
remove file handle closing code from spdEmulatorApi
Secre-C Dec 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/.idea.FileEmulationFramework/.idea/.name

This file was deleted.

4 changes: 2 additions & 2 deletions Emulator/AFS.Stream.Emulator/AFS.Stream.Emulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AFSLib" Version="1.1.0" />
<PackageReference Include="Reloaded.Memory" Version="8.0.1" />
<PackageReference Include="AFSLib" Version="1.1.1" />
<PackageReference Include="Reloaded.Memory" Version="9.3.2" />
<PackageReference Include="Reloaded.Mod.Interfaces" Version="2.3.0" ExcludeAssets="runtime" />
</ItemGroup>

Expand Down
5 changes: 3 additions & 2 deletions Emulator/AFS.Stream.Emulator/Afs/AfsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FileEmulationFramework.Lib.IO.Struct;
using FileEmulationFramework.Lib.Utilities;
using Microsoft.Win32.SafeHandles;
using Reloaded.Memory.Extensions;
using Reloaded.Memory.Streams;

// Aliasing for readability, since our assembly name has priority over 'stream'
Expand Down Expand Up @@ -75,8 +76,8 @@ public unsafe MultiStream Build(IntPtr handle, string filepath, Logger? logger =
headerStream.SetLength(headerLength);

// Write header magic and file count
headerStream.Write<int>(0x00534641); // 'AFS '
headerStream.Write<int>(numFiles);
headerStream.Write(0x00534641); // 'AFS '
headerStream.Write(numFiles);

// Make MultiStream
var pairs = new List<StreamOffsetPair<Strim>>()
Expand Down
2 changes: 1 addition & 1 deletion Emulator/ARC.Stream.Emulator/ARC.Stream.Emulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Reloaded.Memory" Version="8.0.1" />
<PackageReference Include="Reloaded.Memory" Version="9.3.2" />
<PackageReference Include="Reloaded.Mod.Interfaces" Version="2.3.0" ExcludeAssets="runtime" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions Emulator/ARC.Stream.Emulator/Arc/ArcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FileEmulationFramework.Lib.IO.Struct;
using FileEmulationFramework.Lib.Utilities;
using Microsoft.Win32.SafeHandles;
using Reloaded.Memory.Extensions;
using Reloaded.Memory.Streams;

// Aliasing for readability, since our assembly name has priority over 'stream'
Expand Down Expand Up @@ -42,7 +43,7 @@ public unsafe MultiStream Build(IntPtr handle, string filepath, Logger? logger =
MemoryStream headerStream = new MemoryStream(headerLength);

// Write header magic and file count
headerStream.Write( 0x4C435241 ); // 'ARCL'
headerStream.Write(0x4C435241); // 'ARCL'
headerStream.Write(numFiles);

// Make MultiStream
Expand Down Expand Up @@ -70,7 +71,6 @@ public unsafe MultiStream Build(IntPtr handle, string filepath, Logger? logger =
}
else if (x < entries.Length)
{

length = (int)entries[x].Length;

var originalEntry = new FileSlice(entries[x].Offset, length, filepath);
Expand Down
2 changes: 1 addition & 1 deletion Emulator/AWB.Stream.Emulator/AWB.Stream.Emulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Reloaded.Memory" Version="8.0.1" />
<PackageReference Include="Reloaded.Memory" Version="9.3.2" />
<PackageReference Include="Reloaded.Mod.Interfaces" Version="2.3.0" ExcludeAssets="runtime" />
<PackageReference Include="Reloaded.Memory.SigScan.ReloadedII.Interfaces" Version="1.2.0" />
</ItemGroup>
Expand Down
17 changes: 9 additions & 8 deletions Emulator/AWB.Stream.Emulator/Awb/AwbBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FileEmulationFramework.Lib.IO.Struct;
using FileEmulationFramework.Lib.Utilities;
using Microsoft.Win32.SafeHandles;
using Reloaded.Memory.Extensions;
using Reloaded.Memory.Streams;

// Aliasing for readability, since our assembly name has priority over 'stream'
Expand Down Expand Up @@ -73,14 +74,14 @@ public MultiStream Build(IntPtr handle, string filepath, Logger? logger = null)
headerStream.SetLength(headerLength);

// Write header magic and file count
headerStream.Write<int>(Afs2Header.ExpectedMagic); // 'AFS2'
headerStream.Write<byte>((byte)(subKey != 0 ? 2 : 1)); // 'Type'
headerStream.Write<byte>(posFieldSize); // 'PosLength'
headerStream.Write<byte>(idFieldSize); // 'IdLength'
headerStream.Write<byte>(0); // 'Pad'
headerStream.Write<int>(numFiles); // 'Entry Count'
headerStream.Write<short>(AwbAlignment); // 'Alignment'
headerStream.Write<short>(subKey); // 'Encryption Key'
headerStream.Write(Afs2Header.ExpectedMagic); // 'AFS2'
headerStream.Write((byte)(subKey != 0 ? 2 : 1)); // 'Type'
headerStream.Write(posFieldSize); // 'PosLength'
headerStream.Write(idFieldSize); // 'IdLength'
headerStream.Write((byte)0); // 'Pad'
headerStream.Write(numFiles); // 'Entry Count'
headerStream.Write((short)AwbAlignment); // 'Alignment'
headerStream.Write((short)subKey); // 'Encryption Key'

// Make MultiStream
var pairs = new List<StreamOffsetPair<Strim>>()
Expand Down
11 changes: 6 additions & 5 deletions Emulator/AWB.Stream.Emulator/Awb/Utilities/ValueWriters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Reloaded.Memory.Streams;
using Reloaded.Memory.Extensions;
using Reloaded.Memory.Streams;
// ReSharper disable RedundantTypeArgumentsOfMethod

namespace AWB.Stream.Emulator.Awb.Utilities;
Expand All @@ -19,16 +20,16 @@ public static void WriteNumber(this MemoryStream stream, long value, int size)
switch (size)
{
case 1:
stream.Write<byte>((byte)value);
stream.Write((byte)value);
break;
case 2:
stream.Write<short>((short)value);
stream.Write((short)value);
break;
case 4:
stream.Write<int>((int)value);
stream.Write((int)value);
break;
case 8:
stream.Write<long>(value);
stream.Write(value);
break;
default:
ThrowHelpers.ThrowBadFieldSizeException();
Expand Down
57 changes: 57 additions & 0 deletions Emulator/Interfaces/SPD.File.Emulator.Interfaces/ISpdEmulator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using SPD.File.Emulator.Interfaces.Structures.IO;

namespace SPD.File.Emulator.Interfaces;

/// <summary>
/// APIs exposed by Spd Emulator.
/// </summary>
public interface ISpdEmulator
{
/// <summary>
/// Tries to create an emulated SPD file using an SPD file embedded inside another file as source.
/// </summary>
/// <param name="sourcePath">Path to the file from which the data will be sourced.</param>
/// <param name="offset">Offset in the file where the SPD starts.</param>
/// <param name="route">The route of the emulated file.</param>
/// <param name="destinationPath">Path to where the emulated file should be put.</param>
public bool TryCreateFromFileSlice(string sourcePath, long offset, string route, string destinationPath);

/// <summary>
/// Tries to create an emulated SPD file by merging any applicable sprite or texture files, using the input as the base
/// </summary>
/// <param name="sourcePath">Path to the SPD file to use as a base.</param>
/// <param name="route">The route of the emulated SPD file.</param>
/// <param name="destinationPath">Path to where the emulated SPD file should be put.</param>
public bool TryCreateFromSpd(string sourcePath, string route, string destinationPath);

/// <summary>
/// Invalidates a file, i.e. unregisters it, will be recreated on next access.
/// </summary>
/// <param name="SPDPath">Path of the SPD file.</param>
public void InvalidateFile(string SPDPath);

/// <summary>
/// Gets the list of input files from other mods fed into the emulator.
/// </summary>
public RouteGroupTuple[] GetEmulatorInput();

/// <summary>
/// Registers an already merged SPD as an emulated one
/// </summary>
/// <param name="sourcePath">The path to the SPD file to register</param>
/// <param name="destinationPath">The path where the emulated SPD file should be put</param>
public void RegisterSpd(string sourcePath, string destinationPath);

/// <summary>
/// Adds a new file to be injected into emulated SPDs
/// </summary>
/// <param name="file">The path to the file to add</param>
/// <param name="route">The route the file is in</param>
public void AddFile(string file, string route);

/// <summary>
/// Adds a directory to SPD Emulator so it's like the files were in FEmulator\SPD
/// </summary>
/// <param name="dir">The directory to add the files from</param>
public void AddDirectory(string dir);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace SPD.File.Emulator.Interfaces.Structures.IO;


/// <summary>
/// Represents information tied to an individual directory.
/// </summary>
public struct DirectoryInformation
{
/// <summary>
/// Full path to the directory.
/// </summary>
public string FullPath;

/// <summary>
/// Last time this directory was modified.
/// </summary>
public DateTime LastWriteTime;
}

/// <summary>
/// Groups a single directory and a list of files associated with it.
/// </summary>
public class DirectoryFilesGroup
{
/// <summary>
/// The directory in question.
/// </summary>
public DirectoryInformation Directory;

/// <summary>
/// The relative file paths tied to this directory.
/// </summary>
public string[] Files = Array.Empty<string>();
}

/// <summary>
/// A tuple representing a group and a route.
/// </summary>
public struct RouteGroupTuple
{
/// <summary>
/// Route associated with this tuple.
/// </summary>
public string Route;

/// <summary>
/// Files bound by this route.
/// </summary>
public DirectoryFilesGroup Files;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Reloaded.Memory" Version="8.0.1" />
<PackageReference Include="Reloaded.Memory" Version="9.3.2" />
<PackageReference Include="Reloaded.Mod.Interfaces" Version="2.3.0" ExcludeAssets="runtime" />
<PackageReference Include="Reloaded.SharedLib.CSharp.Prs.Interfaces" Version="1.0.2" />
</ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions Emulator/ONE.Heroes.Stream.Emulator/One/OneBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Heroes.SDK.Definitions.Structures.Archive.OneFile;
using Heroes.SDK.Definitions.Structures.RenderWare;
using Microsoft.Win32.SafeHandles;
using Reloaded.Memory.Extensions;
using Reloaded.Memory.Streams;

// Aliasing for readability, since our assembly name has priority over 'stream'
Expand Down Expand Up @@ -145,7 +146,7 @@ public unsafe MultiStream Build(IntPtr handle, string filePath, Logger? log = nu
{
var fileStream = new FileStream(new SafeFileHandle(hFile, false), FileAccess.Read);
var originalPos = fileStream.Position;
using var reader = new BufferedStreamReader(fileStream, 4096); // common cluster size on Windows
using var reader = new BufferedStreamReader<FileStream>(fileStream, 4096); // common cluster size on Windows
var result = new Dictionary<string, OneBuilderItem>();

// Read headers.
Expand All @@ -163,13 +164,13 @@ public unsafe MultiStream Build(IntPtr handle, string filePath, Logger? log = nu
// Note: Heroes leaves 2 blank name slots that are unused at runtime, so file count is that and 2 less.
for (int x = 0; x < fileNameCount - 2; x++)
{
var stillMoreData = reader.Position() < fileStream.Length;
var stillMoreData = reader.Position < fileStream.Length;
if (!stillMoreData)
break;

reader.Read<OneFileEntry>(out var currentEntry);
var fileName = fileNames[currentEntry.FileNameIndex].ToString();
var fileDataPos = reader.Position();
var fileDataPos = reader.Position;
var fileSize = currentEntry.FileSize;
#if DEBUG
if (fileSize == 0)
Expand Down
2 changes: 1 addition & 1 deletion Emulator/PAK.Stream.Emulator/PAK.Stream.Emulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Reloaded.Memory" Version="8.0.1" />
<PackageReference Include="Reloaded.Memory" Version="9.3.2" />
<PackageReference Include="Reloaded.Mod.Interfaces" Version="2.3.0" ExcludeAssets="runtime" />
<PackageReference Include="Reloaded.Memory.SigScan.ReloadedII.Interfaces" Version="1.2.0" />
</ItemGroup>
Expand Down
18 changes: 10 additions & 8 deletions Emulator/PAK.Stream.Emulator/Pak/PakBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using FileEmulationFramework.Lib.Utilities;
using Microsoft.Win32.SafeHandles;
using Reloaded.Memory;
using Reloaded.Memory.Extensions;
using Reloaded.Memory.Streams;
using Reloaded.Memory.Utilities;

// Aliasing for readability, since our assembly name has priority over 'stream'
using Strim = System.IO.Stream;
Expand Down Expand Up @@ -129,7 +131,7 @@ private unsafe MultiStream BuildV1(IEntry[] entries, IntPtr handle, string filep

MemoryStream entrystream = new MemoryStream(sizeof(V1FileEntry));
entrystream.Write(Encoding.ASCII.GetBytes(filename.PadRight(252, '\0')));
entrystream.Write<int>(length);
entrystream.Write(length);
pairs.Add(new(entrystream, OffsetRange.FromStartAndLength(currentOffset, sizeof(V1FileEntry))));

length = (int)Align(length, 64);
Expand Down Expand Up @@ -163,7 +165,7 @@ private unsafe MultiStream BuildV1(IEntry[] entries, IntPtr handle, string filep
length = (int)entryStream.Length;

var headerStream2 = new MemoryStream(4);
headerStream2.Write<int>(length);
headerStream2.Write(length);
pairs.Add(new(headerStream, OffsetRange.FromStartAndLength(currentOffset, sizeofentry - 4)));
pairs.Add(new(headerStream2, OffsetRange.FromStartAndLength(currentOffset + sizeofentry - 4, 4)));
length = (int)Align(length, 64);
Expand Down Expand Up @@ -263,7 +265,7 @@ private unsafe MultiStream BuildV2(IEntry[] entries, IntPtr handle, string filep
var headerLength = 4;
Strim headerStream = new MemoryStream(headerLength);
var writeNum = bigEndian ? Endian.Reverse(numFiles) : numFiles;
headerStream.Write<int>(writeNum);
headerStream.Write(writeNum);

currentOffset = headerLength;
// Add Header
Expand All @@ -286,7 +288,7 @@ private unsafe MultiStream BuildV2(IEntry[] entries, IntPtr handle, string filep
MemoryStream entrystream = new MemoryStream(sizeof(V2FileEntry));
var writelength = bigEndian ? Endian.Reverse(length) : length;
entrystream.Write(Encoding.ASCII.GetBytes(filename.PadRight(32, '\0')));
entrystream.Write<int>(writelength);
entrystream.Write(writelength);
pairs.Add(new(entrystream, OffsetRange.FromStartAndLength(currentOffset, sizeof(V2FileEntry))));

pairs.Add(new(new FileSliceStreamW32(overwrittenFile, logger), OffsetRange.FromStartAndLength(currentOffset + sizeofentry, overwrittenFile.Length)));
Expand Down Expand Up @@ -319,7 +321,7 @@ private unsafe MultiStream BuildV2(IEntry[] entries, IntPtr handle, string filep

var headerStream2 = new MemoryStream(4);
//headerStream.Dispose();
headerStream2.Write<int>(bigEndian ? Endian.Reverse(length) : length);
headerStream2.Write(bigEndian ? Endian.Reverse(length) : length);
pairs.Add(new(headerStream, OffsetRange.FromStartAndLength(currentOffset, sizeofentry - 4)));
pairs.Add(new(headerStream2, OffsetRange.FromStartAndLength(currentOffset + sizeofentry - 4, 4)));

Expand Down Expand Up @@ -419,7 +421,7 @@ private unsafe MultiStream BuildV3(IEntry[] entries, IntPtr handle, string filep
var headerLength = 4;
Strim headerStream = new MemoryStream(headerLength);
var writeNum = bigEndian ? Endian.Reverse(numFiles) : numFiles;
headerStream.Write<int>(writeNum);
headerStream.Write(writeNum);

currentOffset = headerLength;
// Add Header
Expand All @@ -442,7 +444,7 @@ private unsafe MultiStream BuildV3(IEntry[] entries, IntPtr handle, string filep
MemoryStream entrystream = new MemoryStream(sizeof(V3FileEntry));
var writelength = bigEndian ? Endian.Reverse(length) : length;
entrystream.Write(Encoding.ASCII.GetBytes(filename.PadRight(24, '\0')));
entrystream.Write<int>(writelength);
entrystream.Write(writelength);
pairs.Add(new(entrystream, OffsetRange.FromStartAndLength(currentOffset, sizeof(V3FileEntry))));

pairs.Add(new(new FileSliceStreamW32(overwrittenFile, logger), OffsetRange.FromStartAndLength(currentOffset + sizeofentry, overwrittenFile.Length)));
Expand Down Expand Up @@ -474,7 +476,7 @@ private unsafe MultiStream BuildV3(IEntry[] entries, IntPtr handle, string filep
length = (int)entryStream.Length;

var headerStream2 = new MemoryStream(4);
headerStream2.Write<int>(bigEndian ? Endian.Reverse(length) : length);
headerStream2.Write(bigEndian ? Endian.Reverse(length) : length);
pairs.Add(new(headerStream, OffsetRange.FromStartAndLength(currentOffset, sizeofentry - 4)));
pairs.Add(new(headerStream2, OffsetRange.FromStartAndLength(currentOffset + sizeofentry - 4, 4)));

Expand Down
1 change: 1 addition & 0 deletions Emulator/PAK.Stream.Emulator/Utilities/PakReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.ComponentModel;
using System.IO;
using System.Reflection;
using Reloaded.Memory.Utilities;

// Aliasing for readability, since our assembly name has priority over 'stream'
using Strim = System.IO.Stream;
Expand Down
Loading