add api to mare, change all to file scoped namespace

This commit is contained in:
Stanley Dimant
2022-09-29 15:52:33 +02:00
parent b10a02f228
commit ac6c46390c
27 changed files with 4436 additions and 4373 deletions

View File

@@ -6,83 +6,82 @@ using MareSynchronos.API;
using MareSynchronos.Utils;
using Lumina.Excel.GeneratedSheets;
namespace MareSynchronos.Models
namespace MareSynchronos.Models;
[JsonObject(MemberSerialization.OptIn)]
public class CharacterData
{
[JsonObject(MemberSerialization.OptIn)]
public class CharacterData
[JsonProperty]
public Dictionary<ObjectKind, List<FileReplacement>> FileReplacements { get; set; } = new();
[JsonProperty]
public Dictionary<ObjectKind, string> GlamourerString { get; set; } = new();
public bool IsReady => FileReplacements.SelectMany(k => k.Value).All(f => f.Computed);
[JsonProperty]
public string ManipulationString { get; set; } = string.Empty;
[JsonProperty]
public float HeelsOffset { get; set; } = 0f;
public void AddFileReplacement(ObjectKind objectKind, FileReplacement fileReplacement)
{
[JsonProperty]
public Dictionary<ObjectKind, List<FileReplacement>> FileReplacements { get; set; } = new();
if (!fileReplacement.HasFileReplacement) return;
[JsonProperty]
public Dictionary<ObjectKind, string> GlamourerString { get; set; } = new();
if (!FileReplacements.ContainsKey(objectKind)) FileReplacements.Add(objectKind, new List<FileReplacement>());
public bool IsReady => FileReplacements.SelectMany(k => k.Value).All(f => f.Computed);
[JsonProperty]
public string ManipulationString { get; set; } = string.Empty;
[JsonProperty]
public float HeelsOffset { get; set; } = 0f;
public void AddFileReplacement(ObjectKind objectKind, FileReplacement fileReplacement)
var existingReplacement = FileReplacements[objectKind].SingleOrDefault(f => f.ResolvedPath == fileReplacement.ResolvedPath);
if (existingReplacement != null)
{
if (!fileReplacement.HasFileReplacement) return;
if (!FileReplacements.ContainsKey(objectKind)) FileReplacements.Add(objectKind, new List<FileReplacement>());
var existingReplacement = FileReplacements[objectKind].SingleOrDefault(f => f.ResolvedPath == fileReplacement.ResolvedPath);
if (existingReplacement != null)
{
existingReplacement.GamePaths.AddRange(fileReplacement.GamePaths.Where(e => !existingReplacement.GamePaths.Contains(e)));
}
else
{
FileReplacements[objectKind].Add(fileReplacement);
}
existingReplacement.GamePaths.AddRange(fileReplacement.GamePaths.Where(e => !existingReplacement.GamePaths.Contains(e)));
}
public CharacterCacheDto ToCharacterCacheDto()
else
{
var fileReplacements = FileReplacements.ToDictionary(k => k.Key, k => k.Value.Where(f => f.HasFileReplacement && !f.IsFileSwap).GroupBy(f => f.Hash).Select(g =>
{
return new FileReplacementDto()
{
GamePaths = g.SelectMany(f => f.GamePaths).Distinct().ToArray(),
Hash = g.First().Hash,
};
}).ToList());
Logger.Debug("Adding fileSwaps");
foreach (var item in FileReplacements)
{
Logger.Debug("Checking fileSwaps for " + item.Key);
var fileSwapsToAdd = item.Value.Where(f => f.IsFileSwap).Select(f => f.ToFileReplacementDto());
Logger.Debug("Adding " + fileSwapsToAdd.Count() + " file swaps");
foreach (var swap in fileSwapsToAdd)
{
Logger.Debug("Adding: " + swap.GamePaths.First() + ":" + swap.FileSwapPath);
}
fileReplacements[item.Key].AddRange(fileSwapsToAdd);
}
return new CharacterCacheDto()
{
FileReplacements = fileReplacements,
GlamourerData = GlamourerString.ToDictionary(d => d.Key, d => d.Value),
ManipulationData = ManipulationString,
HeelsOffset = HeelsOffset
};
}
public override string ToString()
{
StringBuilder stringBuilder = new();
foreach (var fileReplacement in FileReplacements.SelectMany(k => k.Value).OrderBy(a => a.GamePaths[0]))
{
stringBuilder.AppendLine(fileReplacement.ToString());
}
return stringBuilder.ToString();
FileReplacements[objectKind].Add(fileReplacement);
}
}
public CharacterCacheDto ToCharacterCacheDto()
{
var fileReplacements = FileReplacements.ToDictionary(k => k.Key, k => k.Value.Where(f => f.HasFileReplacement && !f.IsFileSwap).GroupBy(f => f.Hash).Select(g =>
{
return new FileReplacementDto()
{
GamePaths = g.SelectMany(f => f.GamePaths).Distinct().ToArray(),
Hash = g.First().Hash,
};
}).ToList());
Logger.Debug("Adding fileSwaps");
foreach (var item in FileReplacements)
{
Logger.Debug("Checking fileSwaps for " + item.Key);
var fileSwapsToAdd = item.Value.Where(f => f.IsFileSwap).Select(f => f.ToFileReplacementDto());
Logger.Debug("Adding " + fileSwapsToAdd.Count() + " file swaps");
foreach (var swap in fileSwapsToAdd)
{
Logger.Debug("Adding: " + swap.GamePaths.First() + ":" + swap.FileSwapPath);
}
fileReplacements[item.Key].AddRange(fileSwapsToAdd);
}
return new CharacterCacheDto()
{
FileReplacements = fileReplacements,
GlamourerData = GlamourerString.ToDictionary(d => d.Key, d => d.Value),
ManipulationData = ManipulationString,
HeelsOffset = HeelsOffset
};
}
public override string ToString()
{
StringBuilder stringBuilder = new();
foreach (var fileReplacement in FileReplacements.SelectMany(k => k.Value).OrderBy(a => a.GamePaths[0]))
{
stringBuilder.AppendLine(fileReplacement.ToString());
}
return stringBuilder.ToString();
}
}

View File

@@ -6,55 +6,54 @@ using MareSynchronos.API;
using System.Text.RegularExpressions;
using MareSynchronos.FileCache;
namespace MareSynchronos.Models
namespace MareSynchronos.Models;
public class FileReplacement
{
public class FileReplacement
private readonly FileCacheManager fileDbManager;
public FileReplacement(FileCacheManager fileDbManager)
{
private readonly FileCacheManager fileDbManager;
this.fileDbManager = fileDbManager;
}
public FileReplacement(FileCacheManager fileDbManager)
public bool Computed => IsFileSwap || !HasFileReplacement || !string.IsNullOrEmpty(Hash);
public List<string> GamePaths { get; set; } = new();
public bool HasFileReplacement => GamePaths.Count >= 1 && GamePaths.Any(p => p != ResolvedPath);
public bool IsFileSwap => !Regex.IsMatch(ResolvedPath, @"^[a-zA-Z]:(/|\\)", RegexOptions.ECMAScript) && GamePaths.First() != ResolvedPath;
public string Hash { get; set; } = string.Empty;
public string ResolvedPath { get; set; } = string.Empty;
public void SetResolvedPath(string path)
{
ResolvedPath = path.ToLowerInvariant().Replace('\\', '/');
if (!HasFileReplacement || IsFileSwap) return;
_ = Task.Run(() =>
{
this.fileDbManager = fileDbManager;
}
var cache = fileDbManager.GetFileCacheByPath(ResolvedPath);
Hash = cache.Hash;
});
}
public bool Computed => IsFileSwap || !HasFileReplacement || !string.IsNullOrEmpty(Hash);
public List<string> GamePaths { get; set; } = new();
public bool HasFileReplacement => GamePaths.Count >= 1 && GamePaths.Any(p => p != ResolvedPath);
public bool IsFileSwap => !Regex.IsMatch(ResolvedPath, @"^[a-zA-Z]:(/|\\)", RegexOptions.ECMAScript) && GamePaths.First() != ResolvedPath;
public string Hash { get; set; } = string.Empty;
public string ResolvedPath { get; set; } = string.Empty;
public void SetResolvedPath(string path)
public FileReplacementDto ToFileReplacementDto()
{
return new FileReplacementDto
{
ResolvedPath = path.ToLowerInvariant().Replace('\\', '/');
if (!HasFileReplacement || IsFileSwap) return;
_ = Task.Run(() =>
{
var cache = fileDbManager.GetFileCacheByPath(ResolvedPath);
Hash = cache.Hash;
});
}
public FileReplacementDto ToFileReplacementDto()
{
return new FileReplacementDto
{
GamePaths = GamePaths.ToArray(),
Hash = Hash,
FileSwapPath = IsFileSwap ? ResolvedPath : string.Empty
};
}
public override string ToString()
{
StringBuilder builder = new();
builder.AppendLine($"Modded: {HasFileReplacement} - {string.Join(",", GamePaths)} => {ResolvedPath}");
return builder.ToString();
}
GamePaths = GamePaths.ToArray(),
Hash = Hash,
FileSwapPath = IsFileSwap ? ResolvedPath : string.Empty
};
}
public override string ToString()
{
StringBuilder builder = new();
builder.AppendLine($"Modded: {HasFileReplacement} - {string.Join(",", GamePaths)} => {ResolvedPath}");
return builder.ToString();
}
}

View File

@@ -5,132 +5,131 @@ using System.Runtime.InteropServices;
using MareSynchronos.Utils;
using Penumbra.GameData.ByteString;
namespace MareSynchronos.Models
namespace MareSynchronos.Models;
public class PlayerRelatedObject
{
public class PlayerRelatedObject
private readonly Func<IntPtr> getAddress;
public unsafe Character* Character => (Character*)Address;
private string _name;
public ObjectKind ObjectKind { get; }
public IntPtr Address { get; set; }
public IntPtr DrawObjectAddress { get; set; }
public IntPtr CurrentAddress
{
private readonly Func<IntPtr> getAddress;
public unsafe Character* Character => (Character*)Address;
private string _name;
public ObjectKind ObjectKind { get; }
public IntPtr Address { get; set; }
public IntPtr DrawObjectAddress { get; set; }
public IntPtr CurrentAddress
get
{
get
try
{
try
{
return getAddress.Invoke();
}
catch
{ return IntPtr.Zero; }
return getAddress.Invoke();
}
}
public PlayerRelatedObject(ObjectKind objectKind, IntPtr address, IntPtr drawObjectAddress, Func<IntPtr> getAddress)
{
ObjectKind = objectKind;
Address = address;
DrawObjectAddress = drawObjectAddress;
this.getAddress = getAddress;
_name = string.Empty;
}
public byte[] EquipSlotData { get; set; } = new byte[40];
public byte[] CustomizeData { get; set; } = new byte[26];
public byte? HatState { get; set; }
public byte? VisorWeaponState { get; set; }
public bool HasTransientsUpdate { get; set; } = false;
public bool HasUnprocessedUpdate { get; set; } = false;
public bool DoNotSendUpdate { get; set; } = false;
public bool IsProcessing { get; set; } = false;
public unsafe void CheckAndUpdateObject()
{
var curPtr = CurrentAddress;
if (curPtr != IntPtr.Zero)
{
var chara = (Character*)curPtr;
bool addr = Address == IntPtr.Zero || Address != curPtr;
bool equip = CompareAndUpdateByteData(chara->EquipSlotData, chara->CustomizeData);
bool drawObj = (IntPtr)chara->GameObject.DrawObject != DrawObjectAddress;
var name = new Utf8String(chara->GameObject.Name).ToString();
bool nameChange = (name != _name);
if (addr || equip || drawObj || nameChange)
{
_name = name;
Logger.Verbose($"{ObjectKind} changed: {_name}, now: {curPtr:X}, {(IntPtr)chara->GameObject.DrawObject:X}");
Address = curPtr;
DrawObjectAddress = (IntPtr)chara->GameObject.DrawObject;
HasUnprocessedUpdate = true;
}
}
else if (Address != IntPtr.Zero || DrawObjectAddress != IntPtr.Zero)
{
Address = IntPtr.Zero;
DrawObjectAddress = IntPtr.Zero;
Logger.Verbose(ObjectKind + " Changed: " + _name + ", now: " + Address + ", " + DrawObjectAddress);
}
}
private unsafe bool CompareAndUpdateByteData(byte* equipSlotData, byte* customizeData)
{
bool hasChanges = false;
DoNotSendUpdate = false;
for (int i = 0; i < EquipSlotData.Length; i++)
{
var data = Marshal.ReadByte((IntPtr)equipSlotData, i);
if (EquipSlotData[i] != data)
{
EquipSlotData[i] = data;
hasChanges = true;
}
}
for (int i = 0; i < CustomizeData.Length; i++)
{
var data = Marshal.ReadByte((IntPtr)customizeData, i);
if (CustomizeData[i] != data)
{
CustomizeData[i] = data;
hasChanges = true;
}
}
var newHatState = Marshal.ReadByte((IntPtr)customizeData + 30, 0);
var newWeaponOrVisorState = Marshal.ReadByte((IntPtr)customizeData + 31, 0);
if (newHatState != HatState)
{
if (HatState != null && !hasChanges && !HasUnprocessedUpdate)
{
Logger.Debug("Not Sending Update, only Hat changed");
DoNotSendUpdate = true;
}
HatState = newHatState;
hasChanges = true;
}
newWeaponOrVisorState &= 0b1101; // ignore drawing weapon
if (newWeaponOrVisorState != VisorWeaponState)
{
if (VisorWeaponState != null && !hasChanges && !HasUnprocessedUpdate)
{
Logger.Debug("Not Sending Update, only Visor/Weapon changed");
DoNotSendUpdate = true;
}
VisorWeaponState = newWeaponOrVisorState;
hasChanges = true;
}
return hasChanges;
catch
{ return IntPtr.Zero; }
}
}
public PlayerRelatedObject(ObjectKind objectKind, IntPtr address, IntPtr drawObjectAddress, Func<IntPtr> getAddress)
{
ObjectKind = objectKind;
Address = address;
DrawObjectAddress = drawObjectAddress;
this.getAddress = getAddress;
_name = string.Empty;
}
public byte[] EquipSlotData { get; set; } = new byte[40];
public byte[] CustomizeData { get; set; } = new byte[26];
public byte? HatState { get; set; }
public byte? VisorWeaponState { get; set; }
public bool HasTransientsUpdate { get; set; } = false;
public bool HasUnprocessedUpdate { get; set; } = false;
public bool DoNotSendUpdate { get; set; } = false;
public bool IsProcessing { get; set; } = false;
public unsafe void CheckAndUpdateObject()
{
var curPtr = CurrentAddress;
if (curPtr != IntPtr.Zero)
{
var chara = (Character*)curPtr;
bool addr = Address == IntPtr.Zero || Address != curPtr;
bool equip = CompareAndUpdateByteData(chara->EquipSlotData, chara->CustomizeData);
bool drawObj = (IntPtr)chara->GameObject.DrawObject != DrawObjectAddress;
var name = new Utf8String(chara->GameObject.Name).ToString();
bool nameChange = (name != _name);
if (addr || equip || drawObj || nameChange)
{
_name = name;
Logger.Verbose($"{ObjectKind} changed: {_name}, now: {curPtr:X}, {(IntPtr)chara->GameObject.DrawObject:X}");
Address = curPtr;
DrawObjectAddress = (IntPtr)chara->GameObject.DrawObject;
HasUnprocessedUpdate = true;
}
}
else if (Address != IntPtr.Zero || DrawObjectAddress != IntPtr.Zero)
{
Address = IntPtr.Zero;
DrawObjectAddress = IntPtr.Zero;
Logger.Verbose(ObjectKind + " Changed: " + _name + ", now: " + Address + ", " + DrawObjectAddress);
}
}
private unsafe bool CompareAndUpdateByteData(byte* equipSlotData, byte* customizeData)
{
bool hasChanges = false;
DoNotSendUpdate = false;
for (int i = 0; i < EquipSlotData.Length; i++)
{
var data = Marshal.ReadByte((IntPtr)equipSlotData, i);
if (EquipSlotData[i] != data)
{
EquipSlotData[i] = data;
hasChanges = true;
}
}
for (int i = 0; i < CustomizeData.Length; i++)
{
var data = Marshal.ReadByte((IntPtr)customizeData, i);
if (CustomizeData[i] != data)
{
CustomizeData[i] = data;
hasChanges = true;
}
}
var newHatState = Marshal.ReadByte((IntPtr)customizeData + 30, 0);
var newWeaponOrVisorState = Marshal.ReadByte((IntPtr)customizeData + 31, 0);
if (newHatState != HatState)
{
if (HatState != null && !hasChanges && !HasUnprocessedUpdate)
{
Logger.Debug("Not Sending Update, only Hat changed");
DoNotSendUpdate = true;
}
HatState = newHatState;
hasChanges = true;
}
newWeaponOrVisorState &= 0b1101; // ignore drawing weapon
if (newWeaponOrVisorState != VisorWeaponState)
{
if (VisorWeaponState != null && !hasChanges && !HasUnprocessedUpdate)
{
Logger.Debug("Not Sending Update, only Visor/Weapon changed");
DoNotSendUpdate = true;
}
VisorWeaponState = newWeaponOrVisorState;
hasChanges = true;
}
return hasChanges;
}
}