2022-06-18 18:04:24 +00:00
|
|
|
|
using System;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2019-06-09 02:56:11 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
2024-03-10 14:50:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Simple 4-byte block storing time played in a save file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class PlayTime<TSave>(TSave sav, Memory<byte> raw) : SaveBlock<TSave>(sav, raw)
|
|
|
|
|
where TSave : SaveFile
|
2019-06-09 02:56:11 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public int PlayedHours
|
|
|
|
|
{
|
2024-03-04 05:13:16 +00:00
|
|
|
|
get => ReadUInt16LittleEndian(Data);
|
|
|
|
|
set => WriteUInt16LittleEndian(Data, (ushort)value);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public int PlayedMinutes
|
|
|
|
|
{
|
2024-03-04 05:13:16 +00:00
|
|
|
|
get => Data[2];
|
|
|
|
|
set => Data[2] = (byte)value;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public int PlayedSeconds
|
|
|
|
|
{
|
2024-03-04 05:13:16 +00:00
|
|
|
|
get => Data[3];
|
|
|
|
|
set => Data[3] = (byte)value;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
|
|
2024-03-10 14:50:32 +00:00
|
|
|
|
public string PlayedTime => $"{PlayedHours:0000}ː{PlayedMinutes:00}ː{PlayedSeconds:00}"; // not :
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Object storing the playtime of a save file as well as the last saved date.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TSave">Type of Save File</typeparam>
|
|
|
|
|
/// <typeparam name="TEpoch">Type of Epoch for the <see cref="LastSaved"/> timestamp.</typeparam>
|
|
|
|
|
public abstract class PlayTimeLastSaved<TSave, TEpoch>(TSave sav, Memory<byte> raw) : PlayTime<TSave>(sav, raw)
|
|
|
|
|
where TSave : SaveFile
|
|
|
|
|
where TEpoch : EpochDateTime
|
|
|
|
|
{
|
|
|
|
|
protected abstract TEpoch LastSaved { get; }
|
|
|
|
|
public string LastSavedTime => $"{LastSaved.Year:0000}-{LastSaved.Month:00}-{LastSaved.Day:00} {LastSaved.Hour:00}ː{LastSaved.Minute:00}"; // not :
|
2019-06-09 02:56:11 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public DateTime? LastSavedDate
|
|
|
|
|
{
|
2024-03-10 14:50:32 +00:00
|
|
|
|
get => !DateUtil.IsDateValid(LastSaved.Year, LastSaved.Month, LastSaved.Day)
|
2022-06-18 18:04:24 +00:00
|
|
|
|
? null
|
2024-03-10 14:50:32 +00:00
|
|
|
|
: LastSaved.Timestamp;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
set
|
2019-06-09 02:56:11 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// Only update the properties if a value is provided.
|
2024-03-09 03:30:21 +00:00
|
|
|
|
if (value is { } dt)
|
2022-06-18 18:04:24 +00:00
|
|
|
|
{
|
2024-03-10 14:50:32 +00:00
|
|
|
|
LastSaved.Timestamp = dt;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
else // Clear the date.
|
2019-06-09 02:56:11 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// If code tries to access MetDate again, null will be returned.
|
2024-03-10 14:50:32 +00:00
|
|
|
|
LastSaved.Year = 0;
|
|
|
|
|
LastSaved.Month = 0;
|
|
|
|
|
LastSaved.Day = 0;
|
|
|
|
|
LastSaved.Hour = 0;
|
|
|
|
|
LastSaved.Minute = 0;
|
2019-06-09 02:56:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2024-03-10 14:50:32 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// PlayTime object with a zero-epoch Last Saved timestamp.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class PlayTime6 : PlayTimeLastSaved<SaveFile, Epoch0000DateTime>
|
|
|
|
|
{
|
|
|
|
|
public PlayTime6(SAV6 sav, Memory<byte> raw) : base(sav, raw) { }
|
|
|
|
|
public PlayTime6(SAV7 sav, Memory<byte> raw) : base(sav, raw) { }
|
|
|
|
|
|
|
|
|
|
protected override Epoch0000DateTime LastSaved => new(Raw.Slice(0x4, 4));
|
|
|
|
|
}
|