ModernUO/Projects/Server/Serialization/BinaryFileReader.cs
Kamron Batman 5570537aa0
fix: anchored times shift only while still running at save start (#2730)
Follow-up to #2729.

## Problem

At load, `ReadAnchoredTime` shifted **every** anchored value forward by the downtime, including values that were already in the past when the save started. That caused two problems.

**An expired deadline came back.** A deadline that passed 5 minutes before a 30-minute outage loaded 25 minutes in the future. For example:
- `Sheep.NextWoolTime`: a sheep that was ready to shear stopped being ready.
- `PlayerVendor.NextPayTime`: an overdue payment was pushed back.
- `BaseCreature.SummonEnd`, the spell fields' `End`, `TransientItem.Expiration`, and the ChampionSpawn times could move the same way.

**Records were rewritten for nothing.** The first save after every restart rewrote:
- stamps of things that had already happened: VirtueContext's `LastHonorUse`, `LastSacrificeGain`, the four `Last*Loss` stamps, and `Corpse.TimeOfDeath`
- every deadline that had expired long ago.

## Change

- **`IGenericReader.AnchoredTimeOrigin`** is the loaded save's start time.
  - `GenericEntityPersistence` and `GenericPersistence` set it from `World.LoadedSaveStartTime`.
  - `BufferReader`, `UnmanagedDataReader` and `BinaryFileReader` expose it.
- **`ReadAnchoredTime` returns any value at or before the origin exactly as saved.**
  - A deadline still running at save start is shifted as before and keeps its remaining time.
  - Readers without an origin keep the old behaviour.
- **This covers every anchored field, including generated ones.** Generated `[AnchoredDateTime]` fields call `reader.ReadAnchoredTime()`, so the one change covers all 26 generated fields as well as the manual reads. `[DeserializeTimer]` only restores timers that were running at save time, so it is unaffected.
- **The `[AnchoredDateTime]` section of `dev-docs/serialization.md` and the `WriteAnchoredTime` doc comment describe the new rule.**

## Behaviour change

Downtime now counts toward windows measured from a past stamp:

| Window | Length |
|---|---|
| Honor use | 5 minutes |
| Sacrifice gain | 1 day |
| Virtue atrophy | 7 days |
| Instanced corpse looting | 3 minutes |

A 30-minute outage makes a 7-day atrophy fire about 0.3% sooner. No format change.

## Tests

New tests in `AnchoredTimeTests`:
- A deadline running at save start keeps its remaining time.
- A deadline that passed before save start stays passed.
- A past stamp is not shifted.
- A value exactly at save start is not shifted.

`Server.Tests`: 845 passed. `UOContent.Tests`: 1140 passed.
2026-10-25 16:44:33 -07:00

210 lines
9 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2026 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: BinaryFileReader.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.CompilerServices;
using System.Text;
namespace Server;
/// <summary>
/// Read bits of data from a serialized file in a managed environment.
/// <para>
/// Uses the following components collectively:
/// <br><see cref="MemoryMappedFile"/></br>
/// <br><see cref="MemoryMappedViewStream"/></br>
/// <br><see cref="UnmanagedDataReader"/></br>
/// </para>
/// </summary>
public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader
{
private readonly bool _usePrefixes;
private readonly MemoryMappedFile _mmf;
private readonly MemoryMappedViewStream _accessor;
private readonly UnmanagedDataReader _reader;
/// <summary>
/// Read bits of data from a serialized file in a managed environment.
/// <br>Encoding is UTF8 if left null.</br>
/// <para>
/// Uses the following components collectively:
/// <br><see cref="MemoryMappedFile"/></br>
/// <br><see cref="MemoryMappedViewStream"/></br>
/// <br><see cref="UnmanagedDataReader"/></br>
/// </para>
/// </summary>
/// <param name="path">Full file path of the file to be deserialized.</param>
/// <param name="usePrefixes">Sets if strings should be read with itern.</param>
/// <param name="encoding">Set an encoding. By default UTF8</param>
public BinaryFileReader(string path, bool usePrefixes = true, Encoding encoding = null)
{
_usePrefixes = usePrefixes;
var fi = new FileInfo(path);
if (fi.Length > 0)
{
_mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open);
_accessor = _mmf.CreateViewStream();
byte* ptr = null;
_accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
_reader = new UnmanagedDataReader(ptr, _accessor.Length, encoding: encoding);
}
else
{
_reader = new UnmanagedDataReader(null, 0, encoding: encoding);
}
}
/// <summary>
/// How many bits deep into the file is the reader at currently.
/// </summary>
public long Position => _reader.Position;
public TimeSpan AnchoredTimeShift
{
get => _reader.AnchoredTimeShift;
set => _reader.AnchoredTimeShift = value;
}
public DateTime AnchoredTimeOrigin
{
get => _reader.AnchoredTimeOrigin;
set => _reader.AnchoredTimeOrigin = value;
}
public void Dispose()
{
_accessor?.SafeMemoryMappedViewHandle.ReleasePointer();
_accessor?.Dispose();
_mmf?.Dispose();
}
/// <summary>
/// If usePrefixes is true, return a ReadString(intern) else ReadStringRaw(intern).
/// </summary>
/// <returns>A string value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(bool intern = false) => _usePrefixes ? _reader.ReadString(intern) : _reader.ReadStringRaw(intern);
/// <summary>
/// Returns the next set of bits that make up a string.
/// </summary>
/// <returns>Next string value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadStringRaw(bool intern = false) => _reader.ReadStringRaw(intern);
/// <summary>
/// Read the next 64 bits to make up a long (int64).
/// </summary>
/// <returns>Next long value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadLong() => _reader.ReadLong();
/// <summary>
/// Read the next 64 bits to make up an unsigned long (uint64).
/// </summary>
/// <returns>Next ulong value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReadULong() => _reader.ReadULong();
/// <summary>
/// Read the next 32 bits to make up an int (int32).
/// </summary>
/// <returns>Next int value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt() => _reader.ReadInt();
/// <summary>
/// Read the next 32 bits to make up an unsigned int (uint32).
/// </summary>
/// <returns>Next uint value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadUInt() => _reader.ReadUInt();
/// <summary>
/// Read the next 16 bits to make up a short (int16).
/// </summary>
/// <returns>Next short value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short ReadShort() => _reader.ReadShort();
/// <summary>
/// Read the next 16 bits to make up an unsigned short (int16).
/// </summary>
/// <returns>Next ushort value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort ReadUShort() => _reader.ReadUShort();
/// <summary>
/// Read the next 8 bits to make up a float point double.
/// </summary>
/// <returns>Next double value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double ReadDouble() => _reader.ReadDouble();
/// <summary>
/// Read the next 4 bits to make up a float point.
/// </summary>
/// <returns>Next float value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float ReadFloat() => _reader.ReadFloat();
/// <summary>
/// Read the next 8 bits to make up a byte.
/// </summary>
/// <returns>Next byte value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte ReadByte() => _reader.ReadByte();
/// <summary>
/// Read the next 8 bits to make up a signed byte.
/// </summary>
/// <returns>Next sbyte value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte ReadSByte() => _reader.ReadSByte();
/// <summary>
/// Read the next 1 bit to make up a boolean.
/// </summary>
/// <returns>Next bool value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ReadBool() => _reader.ReadBool();
/// <summary>
/// Read the next 32 bytes to make up a <see cref="Serial"/>.
/// </summary>
/// <returns>Next uint value cast as a <see cref="Serial"/> struct.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Serial ReadSerial() => _reader.ReadSerial();
/// <summary>
/// Reads the next Byte which helps determine how to read the following Type.
/// <br>If the byte returns 1 => <see cref="ReadStringRaw"/> and translate into a Type via the <see cref="AssemblyHandler"/></br>
/// <br>If the byte returns 2 => <see cref="UnmanagedDataReader.ReadTypeByHash"/></br>
/// <br>else return null</br>
/// </summary>
/// <returns>Next Type value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Type ReadType() => _reader.ReadType();
/// <summary>
/// Reads the next set of bytes to fill the buffer.
/// </summary>
/// <param name="buffer">A reference span that will be filled with the next set of bytes.</param>
/// <returns>The length of the buffer.</returns>
/// <exception cref="OutOfMemoryException">Thrown if the buffer is larger than the remaining data to read in the file.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Read(Span<byte> buffer) => _reader.Read(buffer);
/// <summary>
/// Sets the current position of the stream to a specified value.
/// </summary>
/// <param name="offset">The new position, relative to the <paramref name="origin"/> parameter.</param>
/// <param name="origin">The reference point for the <paramref name="offset"/> parameter. It can be one of the values of <see cref="SeekOrigin"/>.</param>
/// <returns>The new position in the stream, in bytes.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="offset"/> or the resulting position is out of the valid range.</exception>
/// <exception cref="InvalidOperationException">Thrown if the stream does not support seeking.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Seek(long offset, SeekOrigin origin) => _reader.Seek(offset, origin);
}