-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deduplicate Gen 6-8 PlayTime Logic (#4208)
Slightly overengineered but a fun experiment to de-duplicate some logic.
- Loading branch information
Showing
13 changed files
with
129 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,14 @@ | ||
using System; | ||
using static System.Buffers.Binary.BinaryPrimitives; | ||
|
||
namespace PKHeX.Core; | ||
|
||
public sealed class PlayTime7b : SaveBlock<SaveFile> | ||
/// <summary> | ||
/// PlayTime object with a 1900-epoch Last Saved timestamp. | ||
/// </summary> | ||
public sealed class PlayTime7b : PlayTimeLastSaved<SaveFile, Epoch1900DateTimeValue> | ||
{ | ||
public PlayTime7b(SAV7b sav, Memory<byte> raw) : base(sav, raw) { } | ||
public PlayTime7b(SAV8SWSH sav, SCBlock block) : base(sav, block.Data) { } | ||
|
||
public int PlayedHours | ||
{ | ||
get => ReadUInt16LittleEndian(Data); | ||
set => WriteUInt16LittleEndian(Data, (ushort)value); | ||
} | ||
|
||
public int PlayedMinutes | ||
{ | ||
get => Data[2]; | ||
set => Data[2] = (byte)value; | ||
} | ||
|
||
public int PlayedSeconds | ||
{ | ||
get => Data[3]; | ||
set => Data[3] = (byte)value; | ||
} | ||
|
||
private Epoch1900DateTimeValue LastSaved => new(Raw.Slice(0x4, 4)); | ||
public string LastSavedTime => LastSaved.DisplayValue; | ||
|
||
public DateTime? LastSavedDate | ||
{ | ||
get => !DateUtil.IsDateValid(LastSaved.Year, LastSaved.Month, LastSaved.Day) | ||
? null | ||
: LastSaved.Timestamp; | ||
set | ||
{ | ||
// Only update the properties if a value is provided. | ||
if (value is { } dt) | ||
{ | ||
LastSaved.Timestamp = dt; | ||
} | ||
else // Clear the date. | ||
{ | ||
// If code tries to access MetDate again, null will be returned. | ||
LastSaved.Year = 0; | ||
LastSaved.Month = 0; | ||
LastSaved.Day = 0; | ||
LastSaved.Hour = 0; | ||
LastSaved.Minute = 0; | ||
} | ||
} | ||
} | ||
protected override Epoch1900DateTimeValue LastSaved => new(Raw.Slice(0x4, 4)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,14 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using static System.Buffers.Binary.BinaryPrimitives; | ||
|
||
namespace PKHeX.Core; | ||
|
||
/// <summary> | ||
/// Playtime storage | ||
/// PlayTime object without a Last Saved timestamp. | ||
/// </summary> | ||
[TypeConverter(typeof(ExpandableObjectConverter))] | ||
public sealed class PlayTime8b(SAV8BS sav, Memory<byte> raw) : SaveBlock<SAV8BS>(sav, raw) | ||
public sealed class PlayTime8b : PlayTime<SaveFile> | ||
{ | ||
public ushort PlayedHours | ||
{ | ||
get => ReadUInt16LittleEndian(Data); | ||
set => WriteUInt16LittleEndian(Data, value); | ||
} | ||
|
||
public byte PlayedMinutes { get => Data[2]; set => Data[2] = value; } | ||
public byte PlayedSeconds { get => Data[3]; set => Data[3] = value; } | ||
public string LastSavedTime => $"{PlayedHours:0000}ː{PlayedMinutes:00}ː{PlayedSeconds:00}"; // not : | ||
public PlayTime8b(SAV8BS sav, Memory<byte> raw) : base(sav, raw) { } | ||
public PlayTime8b(SAV8LA sav, SCBlock block) : base(sav, block.Data) { } | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using static System.Buffers.Binary.BinaryPrimitives; | ||
|
||
namespace PKHeX.Core; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.ComponentModel; | ||
|
||
namespace PKHeX.Core; | ||
|
||
[TypeConverter(typeof(ExpandableObjectConverter))] | ||
public sealed class Epoch0000DateTime(Memory<byte> Data): EpochDateTime(Data) | ||
{ | ||
// Data should be 4 or 8 bytes where we only care about the first 4 bytes i.e. 32 bits | ||
// First 12 bits are year from 0000, next 4 bits are month, next 5 are days, next 5 are hours, next 6 bits are minutes | ||
|
||
private static DateTime Epoch => new(0, 1, 1); | ||
|
||
public override int Year { get => (int)(RawDate & 0xFFF); set => RawDate = (RawDate & 0xFFFFF000) | (uint)(value); } | ||
public override int Month { get => (int)((RawDate >> 12) & 0xF); set => RawDate = (RawDate & 0xFFFF0FFF) | (((uint)value & 0xF) << 12); } | ||
|
||
public override DateTime Timestamp | ||
{ | ||
get => new(Year, Month, Day, Hour, Minute, 0); | ||
set | ||
{ | ||
Year = value.Year; | ||
Month = value.Month; | ||
Day = value.Day; | ||
Hour = value.Hour; | ||
Minute = value.Minute; | ||
} | ||
} | ||
|
||
public override string DisplayValue => $"{Timestamp.Year:0000}-{Timestamp.Month:00}-{Timestamp.Day:00} {Timestamp.Hour:00}ː{Timestamp.Minute:00}ː{Timestamp.Second:00}"; // not : | ||
|
||
/// <summary> | ||
/// time_t | ||
/// </summary> | ||
public override ulong TotalSeconds | ||
{ | ||
get => (ulong)(Timestamp - Epoch).TotalSeconds; | ||
set => Timestamp = Epoch.AddSeconds(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.