Skip to content

Commit

Permalink
implemented wherearen.dat and Player.WhereAreN.Poki (kwsch#564)
Browse files Browse the repository at this point in the history
* implemented wherearen.data and Player.WhereAreN.Poki

* Make wherearen savedata presence optional
  • Loading branch information
CJBok authored Dec 12, 2021
1 parent 1d3ae75 commit c437e37
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 32 deletions.
8 changes: 7 additions & 1 deletion NHSE.Core/Hashing/FileHashRevision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class FileHashRevision
private const string FN_POSTBOX = "postbox.dat";
private const string FN_PHOTO = "photo_studio_island.dat";
private const string FN_PROFILE = "profile.dat";
private const string FN_WHEREAREN = "wherearen.dat";

#region REVISION 1.0.0

Expand Down Expand Up @@ -572,6 +573,7 @@ public static class FileHashRevision
internal const int REV_200_POSTBOX = REV_170_POSTBOX;
internal const int REV_200_PHOTO = 0x2F650;
internal const int REV_200_PROFILE = REV_110_PROFILE;
internal const int REV_200_WHEREAREN = 0xB8A4E0;

public static readonly FileHashInfo REV_200 = new(new FileHashDetails[]
{
Expand Down Expand Up @@ -614,7 +616,11 @@ public static class FileHashRevision
{
new(0x100, 0x6945c),
}),
});
new(FN_WHEREAREN, REV_200_WHEREAREN, new FileHashRegion[]
{
new(0x100, 0xB8A3DC),
}),
} );

#endregion
}
Expand Down
18 changes: 18 additions & 0 deletions NHSE.Core/Save/Files/WhereAreN.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace NHSE.Core
{
/// <summary>
/// </summary>
public sealed class WhereAreN : EncryptedFilePair
{
public const string FileName = "wherearen";

public readonly WhereAreNOffsets Offsets;
public WhereAreN(string folder) : base(folder, FileName) => Offsets = WhereAreNOffsets.GetOffsets(Info);

public EncryptedInt32 Poki
{
get => EncryptedInt32.ReadVerify(Data, Offsets.Poki);
set => value.Write(Data, Offsets.Poki);
}
}
}
9 changes: 9 additions & 0 deletions NHSE.Core/Save/Meta/EncryptedFilePair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public abstract class EncryptedFilePair
public readonly string NameData;
public readonly string NameHeader;

public static bool Exists(string folder, string name)
{
var NameData = $"{name}.dat";
var NameHeader = $"{name}Header.dat";
var hdr = Path.Combine(folder, NameHeader);
var dat = Path.Combine(folder, NameData);
return File.Exists(hdr) && File.Exists(dat);
}

protected EncryptedFilePair(string folder, string name)
{
NameData = $"{name}.dat";
Expand Down
2 changes: 2 additions & 0 deletions NHSE.Core/Save/Meta/HorizonSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public bool ValidateSizes()
return false;
if (p.Profile.Data.Length != sizes.Profile)
return false;
if (p.WhereAreN is { } x && x.Data.Length != sizes.WhereAreN)
return false;
}
return true;
}
Expand Down
13 changes: 12 additions & 1 deletion NHSE.Core/Save/Meta/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ public sealed class Player : IEnumerable<EncryptedFilePair>
public readonly PhotoStudioIsland Photo;
public readonly PostBox PostBox;
public readonly Profile Profile;
public readonly WhereAreN? WhereAreN;

/// <summary>
/// Directory Name where the player data was loaded from. Not the full path.
/// </summary>
public readonly string DirectoryName;

#region Override Implementations
public IEnumerator<EncryptedFilePair> GetEnumerator() => new EncryptedFilePair[] {Personal, Photo, PostBox, Profile}.AsEnumerable().GetEnumerator();
public IEnumerator<EncryptedFilePair> GetEnumerator()
{
IEnumerable<EncryptedFilePair> baseFiles = new EncryptedFilePair[] { Personal, Photo, PostBox, Profile };
if (WhereAreN is not null)
baseFiles = baseFiles.Concat(new EncryptedFilePair[] { WhereAreN });
return baseFiles.AsEnumerable().GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public override string ToString() => Personal.PlayerName;
Expand Down Expand Up @@ -49,6 +57,9 @@ private Player(string folder)
Photo = new PhotoStudioIsland(folder);
PostBox = new PostBox(folder);
Profile = new Profile(folder);

if (EncryptedFilePair.Exists(folder, WhereAreN.FileName))
WhereAreN = new WhereAreN(folder);
}
}
}
2 changes: 1 addition & 1 deletion NHSE.Core/Save/Meta/RevisionChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class RevisionChecker
new(REV_190_MAIN, REV_190_PERSONAL, REV_190_PHOTO, REV_190_POSTBOX, REV_190_PROFILE), // 1.9.0
new(REV_1100_MAIN,REV_1100_PERSONAL,REV_1100_PHOTO,REV_1100_POSTBOX,REV_1100_PROFILE),// 1.10.0
new(REV_1110_MAIN,REV_1110_PERSONAL,REV_1110_PHOTO,REV_1110_POSTBOX,REV_1110_PROFILE),// 1.11.0
new(REV_200_MAIN, REV_200_PERSONAL, REV_200_PHOTO, REV_200_POSTBOX, REV_200_PROFILE), // 2.0.0
new(REV_200_MAIN, REV_200_PERSONAL, REV_200_PHOTO, REV_200_POSTBOX, REV_200_PROFILE, REV_200_WHEREAREN), // 2.0.0
};

private static readonly FileHeaderInfo[] RevisionInfo =
Expand Down
6 changes: 4 additions & 2 deletions NHSE.Core/Save/Meta/SaveFileSizes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ public class SaveFileSizes
public readonly uint PhotoStudioIsland;
public readonly uint PostBox;
public readonly uint Profile;
public readonly uint WhereAreN;

public SaveFileSizes(uint main, uint personal, uint photo, uint postbox, uint profile)
public SaveFileSizes(uint main, uint personal, uint photo, uint postbox, uint profile, uint wherearen = 0)
{
Main = main;
Personal = personal;
PhotoStudioIsland = photo;
PostBox = postbox;
Profile = profile;
WhereAreN = wherearen;
}
}
}
}
25 changes: 25 additions & 0 deletions NHSE.Core/Save/Offsets/WhereAreNOffsets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace NHSE.Core
{
/// <summary>
/// Offset info and object retrieval logic for <see cref="Personal"/>
/// </summary>
public abstract class WhereAreNOffsets
{
public abstract int Poki { get; }

public static WhereAreNOffsets GetOffsets(FileHeaderInfo Info)
{
var rev = Info.GetKnownRevisionIndex();
return rev switch
{
22 => new WhereAreNOffsets20(),
23 => new WhereAreNOffsets20(),
24 => new WhereAreNOffsets20(),
25 => new WhereAreNOffsets20(),
_ => throw new IndexOutOfRangeException("Unknown revision!" + Environment.NewLine + Info),
};
}
}
}
10 changes: 10 additions & 0 deletions NHSE.Core/Save/Offsets/WhereAreNOffsets20.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace NHSE.Core
{
/// <summary>
/// <inheritdoc cref="PersonalOffsets"/>
/// </summary>
public sealed class WhereAreNOffsets20 : WhereAreNOffsets
{
public override int Poki => 0xB84228;
}
}
Loading

0 comments on commit c437e37

Please sign in to comment.