More Stuff.

This commit is contained in:
Ottermandias 2023-03-26 18:02:32 +02:00
parent ef9022a746
commit ccdafcf85d
8 changed files with 49 additions and 52 deletions

View file

@ -4,8 +4,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OtterGui; using OtterGui;
using OtterGui.Classes;
using Penumbra.Services;
namespace Penumbra.Collections; namespace Penumbra.Collections;

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using FFXIVClientStructs.FFXIV.Client.Game.Object; using FFXIVClientStructs.FFXIV.Client.Game.Object;
using Penumbra.String;
namespace Penumbra.Collections; namespace Penumbra.Collections;
@ -25,14 +24,14 @@ public readonly struct ResolveData
AssociatedGameObject = IntPtr.Zero; AssociatedGameObject = IntPtr.Zero;
} }
public ResolveData( ModCollection collection, IntPtr gameObject ) public ResolveData(ModCollection collection, IntPtr gameObject)
{ {
_modCollection = collection; _modCollection = collection;
AssociatedGameObject = gameObject; AssociatedGameObject = gameObject;
} }
public ResolveData( ModCollection collection ) public ResolveData(ModCollection collection)
: this( collection, IntPtr.Zero ) : this(collection, IntPtr.Zero)
{ } { }
public override string ToString() public override string ToString()
@ -40,19 +39,18 @@ public readonly struct ResolveData
public unsafe string AssociatedName() public unsafe string AssociatedName()
{ {
if( AssociatedGameObject == IntPtr.Zero ) if (AssociatedGameObject == IntPtr.Zero)
{
return "no associated object."; return "no associated object.";
}
try try
{ {
var id = Penumbra.Actors.FromObject( ( GameObject* )AssociatedGameObject, out _, false, true, true ); var id = Penumbra.Actors.FromObject((GameObject*)AssociatedGameObject, out _, false, true, true);
if( id.IsValid ) if (id.IsValid)
{ {
var name = id.ToString(); var name = id.ToString();
var parts = name.Split( ' ', 3 ); var parts = name.Split(' ', 3);
return string.Join( " ", parts.Length != 3 ? parts.Select( n => $"{n[ 0 ]}." ) : parts[ ..2 ].Select( n => $"{n[ 0 ]}." ).Append( parts[ 2 ] ) ); return string.Join(" ",
parts.Length != 3 ? parts.Select(n => $"{n[0]}.") : parts[..2].Select(n => $"{n[0]}.").Append(parts[2]));
} }
} }
catch catch
@ -66,12 +64,12 @@ public readonly struct ResolveData
public static class ResolveDataExtensions public static class ResolveDataExtensions
{ {
public static ResolveData ToResolveData( this ModCollection collection ) public static ResolveData ToResolveData(this ModCollection collection)
=> new(collection); => new(collection);
public static ResolveData ToResolveData( this ModCollection collection, IntPtr ptr ) public static ResolveData ToResolveData(this ModCollection collection, IntPtr ptr)
=> new(collection, ptr); => new(collection, ptr);
public static unsafe ResolveData ToResolveData( this ModCollection collection, void* ptr ) public static unsafe ResolveData ToResolveData(this ModCollection collection, void* ptr)
=> new(collection, ( IntPtr )ptr); => new(collection, (IntPtr)ptr);
} }

View file

@ -120,7 +120,7 @@ public class ModDataEditor
var newDescription = json[nameof(Mod.Description)]?.Value<string>() ?? string.Empty; var newDescription = json[nameof(Mod.Description)]?.Value<string>() ?? string.Empty;
var newVersion = json[nameof(Mod.Version)]?.Value<string>() ?? string.Empty; var newVersion = json[nameof(Mod.Version)]?.Value<string>() ?? string.Empty;
var newWebsite = json[nameof(Mod.Website)]?.Value<string>() ?? string.Empty; var newWebsite = json[nameof(Mod.Website)]?.Value<string>() ?? string.Empty;
var newFileVersion = json[nameof(Mod.FileVersion)]?.Value<uint>() ?? 0; var newFileVersion = json[nameof(Mod.ModMeta.FileVersion)]?.Value<uint>() ?? 0;
var importDate = json[nameof(Mod.ImportDate)]?.Value<long>(); var importDate = json[nameof(Mod.ImportDate)]?.Value<long>();
var modTags = json[nameof(Mod.ModTags)]?.Values<string>().OfType<string>(); var modTags = json[nameof(Mod.ModTags)]?.Values<string>().OfType<string>();
@ -155,10 +155,9 @@ public class ModDataEditor
mod.Website = newWebsite; mod.Website = newWebsite;
} }
if (mod.FileVersion != newFileVersion) if (newFileVersion != Mod.ModMeta.FileVersion)
{ {
mod.FileVersion = newFileVersion; if (Mod.Migration.Migrate(mod, json, ref newFileVersion))
if (Mod.Migration.Migrate(mod, json))
{ {
changes |= ModDataChangeType.Migration; changes |= ModDataChangeType.Migration;
_saveService.ImmediateSave(new Mod.ModMeta(mod)); _saveService.ImmediateSave(new Mod.ModMeta(mod));

View file

@ -54,6 +54,8 @@ public sealed partial class Mod
internal readonly struct ModData : ISavable internal readonly struct ModData : ISavable
{ {
public const int FileVersion = 3;
private readonly Mod _mod; private readonly Mod _mod;
public ModData(Mod mod) public ModData(Mod mod)
@ -66,7 +68,7 @@ public sealed partial class Mod
{ {
var jObject = new JObject var jObject = new JObject
{ {
{ nameof(FileVersion), JToken.FromObject(_mod.FileVersion) }, { nameof(FileVersion), JToken.FromObject(FileVersion) },
{ nameof(ImportDate), JToken.FromObject(_mod.ImportDate) }, { nameof(ImportDate), JToken.FromObject(_mod.ImportDate) },
{ nameof(LocalTags), JToken.FromObject(_mod.LocalTags) }, { nameof(LocalTags), JToken.FromObject(_mod.LocalTags) },
{ nameof(Note), JToken.FromObject(_mod.Note) }, { nameof(Note), JToken.FromObject(_mod.Note) },

View file

@ -15,31 +15,34 @@ public sealed partial class Mod
{ {
public static partial class Migration public static partial class Migration
{ {
public static bool Migrate(Mod mod, JObject json)
=> MigrateV0ToV1(mod, json) || MigrateV1ToV2(mod) || MigrateV2ToV3(mod);
private static bool MigrateV2ToV3(Mod mod)
{
if (mod.FileVersion > 2)
return false;
// Remove import time.
mod.FileVersion = 3;
return true;
}
[GeneratedRegex(@"group_\d{3}_", RegexOptions.Compiled | RegexOptions.NonBacktracking | RegexOptions.ExplicitCapture)] [GeneratedRegex(@"group_\d{3}_", RegexOptions.Compiled | RegexOptions.NonBacktracking | RegexOptions.ExplicitCapture)]
private static partial Regex GroupRegex(); private static partial Regex GroupRegex();
private static bool MigrateV1ToV2(Mod mod) [GeneratedRegex("^group_", RegexOptions.Compiled)]
private static partial Regex GroupStartRegex();
public static bool Migrate(Mod mod, JObject json, ref uint fileVersion)
=> MigrateV0ToV1(mod, json, ref fileVersion) || MigrateV1ToV2(mod, ref fileVersion) || MigrateV2ToV3(mod, ref fileVersion);
private static bool MigrateV2ToV3(Mod _, ref uint fileVersion)
{ {
if (mod.FileVersion > 1) if (fileVersion > 2)
return false;
// Remove import time.
fileVersion = 3;
return true;
}
private static bool MigrateV1ToV2(Mod mod, ref uint fileVersion)
{
if (fileVersion > 1)
return false; return false;
if (!mod.GroupFiles.All(g => GroupRegex().IsMatch(g.Name))) if (!mod.GroupFiles.All(g => GroupRegex().IsMatch(g.Name)))
foreach (var (group, index) in mod.GroupFiles.WithIndex().ToArray()) foreach (var (group, index) in mod.GroupFiles.WithIndex().ToArray())
{ {
var newName = Regex.Replace(group.Name, "^group_", $"group_{index + 1:D3}_", RegexOptions.Compiled); var newName = GroupStartRegex().Replace(group.Name, $"group_{index + 1:D3}_");
try try
{ {
if (newName != group.Name) if (newName != group.Name)
@ -51,14 +54,14 @@ public sealed partial class Mod
} }
} }
mod.FileVersion = 2; fileVersion = 2;
return true; return true;
} }
private static bool MigrateV0ToV1(Mod mod, JObject json) private static bool MigrateV0ToV1(Mod mod, JObject json, ref uint fileVersion)
{ {
if (mod.FileVersion > 0) if (fileVersion > 0)
return false; return false;
var swaps = json["FileSwaps"]?.ToObject<Dictionary<Utf8GamePath, FullPath>>() var swaps = json["FileSwaps"]?.ToObject<Dictionary<Utf8GamePath, FullPath>>()
@ -110,7 +113,7 @@ public sealed partial class Mod
Penumbra.Log.Warning($"Could not delete old meta file {oldMetaFile} during migration:\n{e}"); Penumbra.Log.Warning($"Could not delete old meta file {oldMetaFile} during migration:\n{e}");
} }
mod.FileVersion = 1; fileVersion = 1;
mod.SaveDefaultMod(); mod.SaveDefaultMod();
return true; return true;

View file

@ -18,8 +18,6 @@ public sealed partial class Mod : IMod
Priority = int.MaxValue, Priority = int.MaxValue,
}; };
public const uint CurrentFileVersion = 3;
public uint FileVersion { get; internal set; } = CurrentFileVersion;
public LowerString Name { get; internal set; } = "New Mod"; public LowerString Name { get; internal set; } = "New Mod";
public LowerString Author { get; internal set; } = LowerString.Empty; public LowerString Author { get; internal set; } = LowerString.Empty;
public string Description { get; internal set; } = string.Empty; public string Description { get; internal set; } = string.Empty;
@ -32,6 +30,8 @@ public sealed partial class Mod : IMod
internal readonly struct ModMeta : ISavable internal readonly struct ModMeta : ISavable
{ {
public const uint FileVersion = 3;
private readonly Mod _mod; private readonly Mod _mod;
public ModMeta(Mod mod) public ModMeta(Mod mod)
@ -44,7 +44,7 @@ public sealed partial class Mod : IMod
{ {
var jObject = new JObject var jObject = new JObject
{ {
{ nameof(FileVersion), JToken.FromObject(_mod.FileVersion) }, { nameof(FileVersion), JToken.FromObject(FileVersion) },
{ nameof(Name), JToken.FromObject(_mod.Name) }, { nameof(Name), JToken.FromObject(_mod.Name) },
{ nameof(Author), JToken.FromObject(_mod.Author) }, { nameof(Author), JToken.FromObject(_mod.Author) },
{ nameof(Description), JToken.FromObject(_mod.Description) }, { nameof(Description), JToken.FromObject(_mod.Description) },

View file

@ -72,9 +72,6 @@ public class Penumbra : IDalamudPlugin
private bool _disposed; private bool _disposed;
private readonly PenumbraNew _tmp; private readonly PenumbraNew _tmp;
// TODO
public static ResourceManagerService ResourceManagerService { get; private set; } = null!;
public static ResourceService ResourceService { get; private set; } = null!;
public Penumbra(DalamudPluginInterface pluginInterface) public Penumbra(DalamudPluginInterface pluginInterface)
{ {
@ -98,13 +95,13 @@ public class Penumbra : IDalamudPlugin
StainService = _tmp.Services.GetRequiredService<StainService>(); StainService = _tmp.Services.GetRequiredService<StainService>();
TempMods = _tmp.Services.GetRequiredService<TempModManager>(); TempMods = _tmp.Services.GetRequiredService<TempModManager>();
ResidentResources = _tmp.Services.GetRequiredService<ResidentResourceManager>(); ResidentResources = _tmp.Services.GetRequiredService<ResidentResourceManager>();
ResourceManagerService = _tmp.Services.GetRequiredService<ResourceManagerService>(); _tmp.Services.GetRequiredService<ResourceManagerService>();
ModManager = _tmp.Services.GetRequiredService<Mod.Manager>(); ModManager = _tmp.Services.GetRequiredService<Mod.Manager>();
CollectionManager = _tmp.Services.GetRequiredService<ModCollection.Manager>(); CollectionManager = _tmp.Services.GetRequiredService<ModCollection.Manager>();
TempCollections = _tmp.Services.GetRequiredService<TempCollectionManager>(); TempCollections = _tmp.Services.GetRequiredService<TempCollectionManager>();
ModFileSystem = _tmp.Services.GetRequiredService<ModFileSystem>(); ModFileSystem = _tmp.Services.GetRequiredService<ModFileSystem>();
RedrawService = _tmp.Services.GetRequiredService<RedrawService>(); RedrawService = _tmp.Services.GetRequiredService<RedrawService>();
ResourceService = _tmp.Services.GetRequiredService<ResourceService>(); _tmp.Services.GetRequiredService<ResourceService>();
ResourceLoader = _tmp.Services.GetRequiredService<ResourceLoader>(); ResourceLoader = _tmp.Services.GetRequiredService<ResourceLoader>();
using (var t = _tmp.Services.GetRequiredService<StartTracker>().Measure(StartTimeType.PathResolver)) using (var t = _tmp.Services.GetRequiredService<StartTracker>().Measure(StartTimeType.PathResolver))
{ {

View file

@ -48,7 +48,7 @@ public class ResourceTab : ITab
unsafe unsafe
{ {
Penumbra.ResourceManagerService.IterateGraphs(DrawCategoryContainer); _resourceManager.IterateGraphs(DrawCategoryContainer);
} }
ImGui.NewLine(); ImGui.NewLine();