forked from aly/qstbak
muffin v7.5.12
This commit is contained in:
parent
3102923ce2
commit
911ed68baa
65 changed files with 2356 additions and 1539 deletions
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Dalamud.Plugin.Services;
|
||||
using Lumina.Data.Files;
|
||||
|
|
@ -22,6 +23,8 @@ public sealed class NpcPositionCache
|
|||
|
||||
private const string FileName = "npc-position-cache.bin";
|
||||
|
||||
private const int MaxPreallocatedEntries = 131072;
|
||||
|
||||
private readonly IDataManager _dataManager;
|
||||
|
||||
private readonly ILogger<NpcPositionCache> _logger;
|
||||
|
|
@ -89,6 +92,23 @@ public sealed class NpcPositionCache
|
|||
}
|
||||
}
|
||||
|
||||
public bool TryLoadFromStream(Stream stream, bool acceptStaleVersion)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(stream, "stream");
|
||||
if (_cache != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
lock (_buildLock)
|
||||
{
|
||||
if (_cache == null)
|
||||
{
|
||||
_cache = ReadCache(stream, acceptStaleVersion, "stream");
|
||||
}
|
||||
return _cache != null;
|
||||
}
|
||||
}
|
||||
|
||||
public void WarmUp(CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
EnsureBuilt(cancellationToken);
|
||||
|
|
@ -149,36 +169,20 @@ public sealed class NpcPositionCache
|
|||
{
|
||||
return null;
|
||||
}
|
||||
Dictionary<uint, (ushort, Vector3, bool)> dictionary;
|
||||
try
|
||||
{
|
||||
using BinaryReader binaryReader = new BinaryReader(File.OpenRead(cacheFilePath));
|
||||
if (binaryReader.ReadUInt32() != 1129336401 || binaryReader.ReadInt32() != 1)
|
||||
{
|
||||
_logger.LogDebug("NPC position cache file has an unknown header, discarding");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(binaryReader.ReadString() != _options.GameVersion))
|
||||
{
|
||||
int num = binaryReader.ReadInt32();
|
||||
Dictionary<uint, (ushort, Vector3, bool)> dictionary = new Dictionary<uint, (ushort, Vector3, bool)>(num);
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
uint key = binaryReader.ReadUInt32();
|
||||
ushort item = binaryReader.ReadUInt16();
|
||||
Vector3 item2 = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
|
||||
bool item3 = binaryReader.ReadBoolean();
|
||||
dictionary[key] = (item, item2, item3);
|
||||
}
|
||||
_logger.LogDebug("NPC position cache loaded from disk with {Count} entries", dictionary.Count);
|
||||
return dictionary;
|
||||
}
|
||||
_logger.LogDebug("NPC position cache file is for a different game version, discarding");
|
||||
}
|
||||
using FileStream stream = File.OpenRead(cacheFilePath);
|
||||
dictionary = ReadCache(stream, acceptStaleVersion: false, "disk");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
_logger.LogWarning(exception, "Failed to load NPC position cache file, discarding");
|
||||
_logger.LogWarning(exception, "Failed to open NPC position cache file, discarding");
|
||||
dictionary = null;
|
||||
}
|
||||
if (dictionary != null)
|
||||
{
|
||||
return dictionary;
|
||||
}
|
||||
try
|
||||
{
|
||||
|
|
@ -190,6 +194,51 @@ public sealed class NpcPositionCache
|
|||
return null;
|
||||
}
|
||||
|
||||
private Dictionary<uint, (ushort TerritoryId, Vector3 Position, bool FestivalOnly)>? ReadCache(Stream stream, bool acceptStaleVersion, string source)
|
||||
{
|
||||
try
|
||||
{
|
||||
using BinaryReader binaryReader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true);
|
||||
if (binaryReader.ReadUInt32() != 1129336401 || binaryReader.ReadInt32() != 1)
|
||||
{
|
||||
_logger.LogDebug("NPC position cache data has an unknown header, discarding");
|
||||
return null;
|
||||
}
|
||||
string text = binaryReader.ReadString();
|
||||
if (text != _options.GameVersion)
|
||||
{
|
||||
if (!acceptStaleVersion)
|
||||
{
|
||||
_logger.LogDebug("NPC position cache data is for a different game version, discarding");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("NPC position cache data was built for game version {CachedVersion} instead of {ExpectedVersion}, using it anyway", text, _options.GameVersion);
|
||||
}
|
||||
int num = binaryReader.ReadInt32();
|
||||
if (num < 0)
|
||||
{
|
||||
_logger.LogDebug("NPC position cache data has a negative entry count, discarding");
|
||||
return null;
|
||||
}
|
||||
Dictionary<uint, (ushort, Vector3, bool)> dictionary = new Dictionary<uint, (ushort, Vector3, bool)>(Math.Min(num, 131072));
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
uint key = binaryReader.ReadUInt32();
|
||||
ushort item = binaryReader.ReadUInt16();
|
||||
Vector3 item2 = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
|
||||
bool item3 = binaryReader.ReadBoolean();
|
||||
dictionary[key] = (item, item2, item3);
|
||||
}
|
||||
_logger.LogDebug("NPC position cache loaded from {Source} with {Count} entries", source, dictionary.Count);
|
||||
return dictionary;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
_logger.LogWarning(exception, "Failed to read NPC position cache data, discarding");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveToDisk(Dictionary<uint, (ushort TerritoryId, Vector3 Position, bool FestivalOnly)> cache)
|
||||
{
|
||||
string cacheFilePath = CacheFilePath;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue