Skip to content

Commit

Permalink
Better detection of body length (#139)
Browse files Browse the repository at this point in the history
* Better detection of body length

* Tweaks

* Simplify adorner

* Fixed
  • Loading branch information
mburbea authored Dec 29, 2021
1 parent 8a37b87 commit 5c0c93f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 49 deletions.
2 changes: 1 addition & 1 deletion ItemTesting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ static HashSet<uint> BuildVariants()
}
static void Main()
{
ConvertSymbolsToCsv(@"C:\e\", @"C:\e\o");
CreateBatchArchive2(@"C:\e\134230570_klua.batch",@"C:\e\o\klua.zip");
ParseOutItemInformationTemplate();
//CreateSimTypeCsv(@"C:\e\symbol_table_globalobjectref.bin", @"..\..\..\gor.csv");
ConvertSymbolsToLua(@"C:\e\", @"C:\Program Files (x86)\Steam\steamapps\common\Kingdoms of Amalur Re-Reckoning\mods\resources\","simtype","buff");


Expand Down
2 changes: 1 addition & 1 deletion KoAR.Core/Amalur.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static Amalur()
0x70, 0x38, 0x0A, 0x00, // playerElfFemale
});

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

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T SetFlag<T>(this T @enum, T flag, bool on) where T : struct, Enum
Expand Down
19 changes: 5 additions & 14 deletions KoAR.Core/Buff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

namespace KoAR.Core;

public class Buff : IDefinition

public record Buff(uint Id, string? Modifier = null, string Name = null!, string? Flavor = null, Rarity Rarity = default,
BuffTypes BuffType = default, ApplyType ApplyType = default) : IDefinition
{
public uint Id { get; set; }
public string? Modifier { get; set; }
public string Name { get; set; } = string.Empty;
public string? Flavor { get; set; }
public Rarity Rarity { get; set; }
public BuffTypes BuffType { get; set; }
public ApplyType ApplyType { get; set; }
[JsonPropertyName("desc")]
public BuffDescription[] Descriptions { get; set; } = Array.Empty<BuffDescription>();

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

public class BuffDescription
public record BuffDescription([property: JsonPropertyName("param_icon")] string? Icon, string? Text)
{
public static readonly BuffDescription Empty = new() { Icon = "Default", Text = "None" };

[JsonPropertyName("param_icon")]
public string? Icon { get; set; }
public string? Text { get; set; }
public static readonly BuffDescription Empty = new(Icon: "Default", Text: "None");
}

[Flags]
Expand Down
14 changes: 9 additions & 5 deletions KoAR.Core/GameSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ true when Path.GetExtension(fileName) is "" => SaveType.Switch,

if (BitConverter.ToInt32(Bytes, BodyStart) == CompressedFlag)
{
Body = new byte[_header.BodyDataLength];
Body = new byte[BitConverter.ToInt32(Bytes, BodyStart + 4)];
if(Body.Length != _header.BodyDataLength)
{
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}");
}
var bundleInfoStart = BodyStart + 12;
var bundleInfoSize = BitConverter.ToInt32(Bytes, bundleInfoStart - 4);
using var bundleInfoData = new ZlibStream(new MemoryStream(Bytes, bundleInfoStart, bundleInfoSize), CompressionMode.Decompress);
Expand Down Expand Up @@ -206,16 +210,16 @@ public int InventorySize
{
get => BitConverter.ToInt32(Body, _bagOffset);
set => Unsafe.WriteUnaligned(ref Body[_bagOffset], value);
}
}

private int BodyDataLength
{
get => IsRemaster ? BitConverter.ToInt32(Bytes, 8 + _header.Length) : Bytes.Length - BodyStart;
get => IsRemaster ? BitConverter.ToInt32(Bytes, BodyStart - 4) : Bytes.Length - BodyStart;
set
{
if (IsRemaster)
{
Unsafe.WriteUnaligned(ref Bytes[8 + _header.Length], value);
Unsafe.WriteUnaligned(ref Bytes[BodyStart - 4], value);
}
}
}
Expand Down Expand Up @@ -383,4 +387,4 @@ int WriteSection(int itemOffset, int dataLength, ReadOnlySpan<byte> newBytes)

UpdateDataLengths(item.ItemOffset, delta + delta2);
}
}
}
13 changes: 7 additions & 6 deletions KoAR.Core/GameSaveHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ public sealed class GameSaveHeader
public GameSaveHeader(GameSave gameSave)
{
_gameSave = gameSave;
_dataLengthOffset = gameSave.Bytes.AsSpan().IndexOf(gameSave.IsRemaster
? new byte[12] { 0, 0, 0, 0, 0xA, 0, 0, 0, 0, 0, 0, 0}
: new byte[8] { 0, 0, 0, 0, 0xA, 0, 0, 0 }) - 4;
_dataLengthOffset = gameSave.Bytes.AsSpan().IndexOf(
new byte[8] { 0, 0, 0, 0, 0xA, 0, 0, 0 }) - 4;
if(gameSave.IsRemaster)
{
var packagesList = gameSave.Bytes.AsSpan(gameSave.Bytes.AsSpan().IndexOf(new byte[8] { 0, 0, 0, 1, 0, 0, 0, 2 }) -1);
IsFateswornAware = MemoryMarshal.Cast<byte, int>(packagesList.Slice(4, 4 * BitConverter.ToInt32(packagesList)))
.Contains(FateswornPackageId);
var offset = gameSave.Bytes.AsSpan().IndexOf(new byte[8] { 0, 0, 0, 1, 0, 0, 0, 2 }) - 1;
var packagesList = MemoryMarshal.Cast<byte, int>(gameSave.Bytes.AsSpan(offset));
var length = packagesList[0];
_dataLengthOffset = offset + 4 * (length + 1);
IsFateswornAware = packagesList.Slice(4, length).Contains(FateswornPackageId);
}
}

Expand Down
6 changes: 1 addition & 5 deletions KoAR.Core/QuestItemDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
namespace KoAR.Core;

public record QuestItemDefinition(
uint Id,
string Name,
string InternalName
);
public record QuestItemDefinition(uint Id, string Name, string InternalName);
26 changes: 9 additions & 17 deletions KoAR.SaveEditor/Views/IndicatorAdornerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected IndicatorAdornerBase(FrameworkElement adornedElement, AdornerPosition

protected enum AdornerPosition
{
UpperLeft = 0,
UpperLeft,
UpperRight,
LowerLeft,
LowerRight,
Expand All @@ -58,8 +58,8 @@ public virtual void Dispose()

public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
{
Rect bounds = this.GetAdornedElementBounds();
if (bounds.IsEmpty)
Size size = this.AdornedElement.RenderSize;
if (size.IsEmpty)
{
return base.GetDesiredTransform(transform);
}
Expand All @@ -68,7 +68,7 @@ public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
Children =
{
base.GetDesiredTransform(transform),
new ScaleTransform(0.5, 0.5, bounds.Width * this._widthMultiple, bounds.Height * this._heightMultiple),
new ScaleTransform(0.5, 0.5, size.Width * this._widthMultiple, size.Height * this._heightMultiple),
}
};
}
Expand Down Expand Up @@ -114,29 +114,21 @@ protected override Size MeasureOverride(Size constraint)

protected override void OnRender(DrawingContext drawingContext)
{
Rect bounds = this.GetAdornedElementBounds();
if (bounds.IsEmpty)
Size size = this.AdornedElement.RenderSize;
if (size.IsEmpty)
{
return;
}
RenderTargetBitmap bitmap = new((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);
RenderTargetBitmap bitmap = new((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual visual = new();
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush brush = new(this._contentPresenter);
context.DrawRectangle(brush, null, new(default, bounds.Size));
context.DrawRectangle(brush, null, new(size));
}
bitmap.Render(visual);
bitmap.Freeze();
drawingContext.DrawImage(bitmap, new(default, bounds.Size));
}

private Rect GetAdornedElementBounds()
{
Rect bounds = VisualTreeHelper.GetDescendantBounds(this.AdornedElement);
return bounds.IsEmpty && (this.AdornedElement.ActualHeight > 0d || this.AdornedElement.ActualWidth > 0d)
? new(new(this.AdornedElement.ActualWidth, this.AdornedElement.ActualHeight))
: bounds;
drawingContext.DrawImage(bitmap, new(size));
}

private static class AdornerAttacher<TAdorner>
Expand Down

0 comments on commit 5c0c93f

Please sign in to comment.