Skip to content

Commit 5c0c93f

Browse files
authored
Better detection of body length (#139)
* Better detection of body length * Tweaks * Simplify adorner * Fixed
1 parent 8a37b87 commit 5c0c93f

File tree

7 files changed

+33
-49
lines changed

7 files changed

+33
-49
lines changed

ItemTesting/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ static HashSet<uint> BuildVariants()
384384
}
385385
static void Main()
386386
{
387+
ConvertSymbolsToCsv(@"C:\e\", @"C:\e\o");
387388
CreateBatchArchive2(@"C:\e\134230570_klua.batch",@"C:\e\o\klua.zip");
388389
ParseOutItemInformationTemplate();
389-
//CreateSimTypeCsv(@"C:\e\symbol_table_globalobjectref.bin", @"..\..\..\gor.csv");
390390
ConvertSymbolsToLua(@"C:\e\", @"C:\Program Files (x86)\Steam\steamapps\common\Kingdoms of Amalur Re-Reckoning\mods\resources\","simtype","buff");
391391

392392

KoAR.Core/Amalur.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static Amalur()
6262
0x70, 0x38, 0x0A, 0x00, // playerElfFemale
6363
});
6464

65-
public static Buff GetBuff(uint buffId) => Buffs.GetValueOrDefault(buffId, new() { Id = buffId, Name = "Unknown" });
65+
public static Buff GetBuff(uint buffId) => Buffs.GetValueOrDefault(buffId, new(buffId, Name: "Unknown"));
6666

6767
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6868
internal static T SetFlag<T>(this T @enum, T flag, bool on) where T : struct, Enum

KoAR.Core/Buff.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44

55
namespace KoAR.Core;
66

7-
public class Buff : IDefinition
7+
8+
public record Buff(uint Id, string? Modifier = null, string Name = null!, string? Flavor = null, Rarity Rarity = default,
9+
BuffTypes BuffType = default, ApplyType ApplyType = default) : IDefinition
810
{
9-
public uint Id { get; set; }
10-
public string? Modifier { get; set; }
11-
public string Name { get; set; } = string.Empty;
12-
public string? Flavor { get; set; }
13-
public Rarity Rarity { get; set; }
14-
public BuffTypes BuffType { get; set; }
15-
public ApplyType ApplyType { get; set; }
1611
[JsonPropertyName("desc")]
1712
public BuffDescription[] Descriptions { get; set; } = Array.Empty<BuffDescription>();
1813

@@ -23,13 +18,9 @@ public class Buff : IDefinition
2318
public bool RequiresFatesworn => Name.StartsWith("mit_");
2419
}
2520

26-
public class BuffDescription
21+
public record BuffDescription([property: JsonPropertyName("param_icon")] string? Icon, string? Text)
2722
{
28-
public static readonly BuffDescription Empty = new() { Icon = "Default", Text = "None" };
29-
30-
[JsonPropertyName("param_icon")]
31-
public string? Icon { get; set; }
32-
public string? Text { get; set; }
23+
public static readonly BuffDescription Empty = new(Icon: "Default", Text: "None");
3324
}
3425

3526
[Flags]

KoAR.Core/GameSave.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ true when Path.GetExtension(fileName) is "" => SaveType.Switch,
5252

5353
if (BitConverter.ToInt32(Bytes, BodyStart) == CompressedFlag)
5454
{
55-
Body = new byte[_header.BodyDataLength];
55+
Body = new byte[BitConverter.ToInt32(Bytes, BodyStart + 4)];
56+
if(Body.Length != _header.BodyDataLength)
57+
{
58+
throw new NotSupportedException($"Save file appears corrupted. The header states that the body should have {_header.BodyDataLength} bytes, but the decompressed size is {Body.Length}");
59+
}
5660
var bundleInfoStart = BodyStart + 12;
5761
var bundleInfoSize = BitConverter.ToInt32(Bytes, bundleInfoStart - 4);
5862
using var bundleInfoData = new ZlibStream(new MemoryStream(Bytes, bundleInfoStart, bundleInfoSize), CompressionMode.Decompress);
@@ -206,16 +210,16 @@ public int InventorySize
206210
{
207211
get => BitConverter.ToInt32(Body, _bagOffset);
208212
set => Unsafe.WriteUnaligned(ref Body[_bagOffset], value);
209-
}
213+
}
210214

211215
private int BodyDataLength
212216
{
213-
get => IsRemaster ? BitConverter.ToInt32(Bytes, 8 + _header.Length) : Bytes.Length - BodyStart;
217+
get => IsRemaster ? BitConverter.ToInt32(Bytes, BodyStart - 4) : Bytes.Length - BodyStart;
214218
set
215219
{
216220
if (IsRemaster)
217221
{
218-
Unsafe.WriteUnaligned(ref Bytes[8 + _header.Length], value);
222+
Unsafe.WriteUnaligned(ref Bytes[BodyStart - 4], value);
219223
}
220224
}
221225
}
@@ -383,4 +387,4 @@ int WriteSection(int itemOffset, int dataLength, ReadOnlySpan<byte> newBytes)
383387

384388
UpdateDataLengths(item.ItemOffset, delta + delta2);
385389
}
386-
}
390+
}

KoAR.Core/GameSaveHeader.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ public sealed class GameSaveHeader
1414
public GameSaveHeader(GameSave gameSave)
1515
{
1616
_gameSave = gameSave;
17-
_dataLengthOffset = gameSave.Bytes.AsSpan().IndexOf(gameSave.IsRemaster
18-
? new byte[12] { 0, 0, 0, 0, 0xA, 0, 0, 0, 0, 0, 0, 0}
19-
: new byte[8] { 0, 0, 0, 0, 0xA, 0, 0, 0 }) - 4;
17+
_dataLengthOffset = gameSave.Bytes.AsSpan().IndexOf(
18+
new byte[8] { 0, 0, 0, 0, 0xA, 0, 0, 0 }) - 4;
2019
if(gameSave.IsRemaster)
2120
{
22-
var packagesList = gameSave.Bytes.AsSpan(gameSave.Bytes.AsSpan().IndexOf(new byte[8] { 0, 0, 0, 1, 0, 0, 0, 2 }) -1);
23-
IsFateswornAware = MemoryMarshal.Cast<byte, int>(packagesList.Slice(4, 4 * BitConverter.ToInt32(packagesList)))
24-
.Contains(FateswornPackageId);
21+
var offset = gameSave.Bytes.AsSpan().IndexOf(new byte[8] { 0, 0, 0, 1, 0, 0, 0, 2 }) - 1;
22+
var packagesList = MemoryMarshal.Cast<byte, int>(gameSave.Bytes.AsSpan(offset));
23+
var length = packagesList[0];
24+
_dataLengthOffset = offset + 4 * (length + 1);
25+
IsFateswornAware = packagesList.Slice(4, length).Contains(FateswornPackageId);
2526
}
2627
}
2728

KoAR.Core/QuestItemDefinition.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
namespace KoAR.Core;
22

3-
public record QuestItemDefinition(
4-
uint Id,
5-
string Name,
6-
string InternalName
7-
);
3+
public record QuestItemDefinition(uint Id, string Name, string InternalName);

KoAR.SaveEditor/Views/IndicatorAdornerBase.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected IndicatorAdornerBase(FrameworkElement adornedElement, AdornerPosition
4242

4343
protected enum AdornerPosition
4444
{
45-
UpperLeft = 0,
45+
UpperLeft,
4646
UpperRight,
4747
LowerLeft,
4848
LowerRight,
@@ -58,8 +58,8 @@ public virtual void Dispose()
5858

5959
public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
6060
{
61-
Rect bounds = this.GetAdornedElementBounds();
62-
if (bounds.IsEmpty)
61+
Size size = this.AdornedElement.RenderSize;
62+
if (size.IsEmpty)
6363
{
6464
return base.GetDesiredTransform(transform);
6565
}
@@ -68,7 +68,7 @@ public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
6868
Children =
6969
{
7070
base.GetDesiredTransform(transform),
71-
new ScaleTransform(0.5, 0.5, bounds.Width * this._widthMultiple, bounds.Height * this._heightMultiple),
71+
new ScaleTransform(0.5, 0.5, size.Width * this._widthMultiple, size.Height * this._heightMultiple),
7272
}
7373
};
7474
}
@@ -114,29 +114,21 @@ protected override Size MeasureOverride(Size constraint)
114114

115115
protected override void OnRender(DrawingContext drawingContext)
116116
{
117-
Rect bounds = this.GetAdornedElementBounds();
118-
if (bounds.IsEmpty)
117+
Size size = this.AdornedElement.RenderSize;
118+
if (size.IsEmpty)
119119
{
120120
return;
121121
}
122-
RenderTargetBitmap bitmap = new((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);
122+
RenderTargetBitmap bitmap = new((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);
123123
DrawingVisual visual = new();
124124
using (DrawingContext context = visual.RenderOpen())
125125
{
126126
VisualBrush brush = new(this._contentPresenter);
127-
context.DrawRectangle(brush, null, new(default, bounds.Size));
127+
context.DrawRectangle(brush, null, new(size));
128128
}
129129
bitmap.Render(visual);
130130
bitmap.Freeze();
131-
drawingContext.DrawImage(bitmap, new(default, bounds.Size));
132-
}
133-
134-
private Rect GetAdornedElementBounds()
135-
{
136-
Rect bounds = VisualTreeHelper.GetDescendantBounds(this.AdornedElement);
137-
return bounds.IsEmpty && (this.AdornedElement.ActualHeight > 0d || this.AdornedElement.ActualWidth > 0d)
138-
? new(new(this.AdornedElement.ActualWidth, this.AdornedElement.ActualHeight))
139-
: bounds;
131+
drawingContext.DrawImage(bitmap, new(size));
140132
}
141133

142134
private static class AdornerAttacher<TAdorner>

0 commit comments

Comments
 (0)