Skip to content

Commit

Permalink
detect when config defaults change, and automatically update them
Browse files Browse the repository at this point in the history
  • Loading branch information
dit-zy committed Aug 9, 2024
1 parent bd4623e commit 6350802
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 20 deletions.
58 changes: 49 additions & 9 deletions ScoutHelper/Config/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Dalamud.Configuration;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using ScoutHelper.Models;
using static ScoutHelper.Utils.Utils;

namespace ScoutHelper.Config;

[Serializable]
public class Configuration : IPluginConfiguration {

// the below exists just to make saving less cumbersome
[NonSerialized]
private IDalamudPluginInterface _pluginInterface = null!;
[NonSerialized, NotManaged] private IDalamudPluginInterface _pluginInterface = null!;

public int Version { get; set; } = 0;

Expand All @@ -21,7 +23,7 @@ public class Configuration : IPluginConfiguration {
public string BearTrainName { get; set; } = "Scout Helper Train";

public string SirenBaseUrl { get; set; } = "https://sirenhunts.com/scouting/";

public string TurtleBaseUrl { get; set; } = "https://scout.wobbuffet.net";
public string TurtleTrainPath { get; set; } = "/scout";
public string TurtleApiBaseUrl { get; set; } = "https://scout.wobbuffet.net";
Expand All @@ -31,13 +33,51 @@ public class Configuration : IPluginConfiguration {
public string CopyTemplate { get; set; } = Constants.DefaultCopyTemplate;
public bool IsCopyModeFullText { get; set; } = false;

public DateTime LastPluginUpdate = DateTime.UnixEpoch;
public DateTime LastNoticeAcknowledged = DateTime.UnixEpoch;
public DateTime LastInstancePatchUpdate = DateTime.UnixEpoch;
public Dictionary<uint, uint> Instances { get; set; } = new();
[NotManaged] public DateTime LastPluginUpdate { get; set; } = DateTime.UnixEpoch;
[NotManaged] public DateTime LastNoticeAcknowledged { get; set; } = DateTime.UnixEpoch;

[NotManaged]
[Obsolete("field no longer needed, but must remain for backwards compatibility")]
public DateTime LastInstancePatchUpdate { get; set; } = DateTime.UnixEpoch;

public void Initialize(IDalamudPluginInterface pluginInterface) {
[NotManaged] public Dictionary<uint, uint> Instances { get; set; } = new();
[NotManaged] public (Territory, uint)[] LatestPatchInstances { get; set; } = Constants.LatestPatchInstances;

[NotManaged] public Dictionary<string, string?> ConfigDefaults { get; set; } = new();

public void Initialize(IPluginLog log, IDalamudPluginInterface pluginInterface) {
_pluginInterface = pluginInterface;
ManageDefaults(log);
}

private void ManageDefaults(IPluginLog log) {
log.Debug("checking configs for new defaults...");

var defaultConf = new Configuration();

typeof(Configuration)
.GetProperties()
.Where(propInfo => propInfo.CustomAttributes.All(attrData => attrData.AttributeType != typeof(NotManaged)))
.ForEach(
propInfo => {
var currentDefault = propInfo.GetValue(defaultConf);
var currentDefaultStr = currentDefault?.ToString();
var propName = propInfo.Name;

if (ConfigDefaults.TryGetValue(propName, out var prevDefault)
&& ActualValuesEqualBecauseMicrosoftHasBrainDamage(prevDefault, currentDefaultStr)
) {
log.Debug("config [{0:l}] does not need to be updated.", propName);
return;
}

log.Debug("updating config [{0:l}] to the new default.", propName);
ConfigDefaults[propName] = currentDefaultStr;
propInfo.SetValue(this, currentDefault);
}
);

Save();
}

public void Save() {
Expand Down
6 changes: 6 additions & 0 deletions ScoutHelper/Config/NotManaged.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System;

namespace ScoutHelper.Config;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false)]
public class NotManaged : Attribute;
2 changes: 0 additions & 2 deletions ScoutHelper/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public static class Constants {
// "Something importanter we extra don't want the user forgetting for even longer.",
}.AsList();

public static readonly DateTime LatestPatchUpdate = DateTime.Parse("2024-07-18T00:00:00Z");

public static readonly (Territory, uint)[] LatestPatchInstances = {
(Territory.Urqopacha, 3),
(Territory.Kozamauka, 3),
Expand Down
19 changes: 11 additions & 8 deletions ScoutHelper/Managers/InitializationManager.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
using System.Linq;
using CSharpFunctionalExtensions;
using Dalamud.Plugin.Services;
using ScoutHelper.Config;
using ScoutHelper.Models;
using ScoutHelper.Utils.Functional;
using static ScoutHelper.Utils.Utils;

namespace ScoutHelper.Managers;

/**
* A manager for initializing system components that require it *after*
* dependency injection is stood up. It is safe to assume that this manager will
* dependency injection is stood up. It is safe to assume that this manager
* was run before any other components are used.
*/
public class InitializationManager {
private readonly IPluginLog _log;
private readonly Configuration _conf;
private readonly TerritoryManager _territoryManager;

// mobManager is unused, but including it in the constructor forces it to be
// initialized right away, rather than waiting for a dependant to be used.
public InitializationManager(
IPluginLog log,
Configuration conf,
TerritoryManager territoryManager
TerritoryManager territoryManager,
// mobManager is unused, but including it in the constructor forces it to be
// initialized right away, rather than waiting for a dependant to be used.
MobManager mobManager
) {
_log = log;
_conf = conf;
Expand All @@ -36,13 +35,17 @@ public void InitializeNecessaryComponents() {
}

private void InitializeInstanceMap() {
var patchUpdateNotYetApplied = _conf.LastInstancePatchUpdate < Constants.LatestPatchUpdate;
var patchUpdateNotYetApplied = !ActualValuesEqualBecauseMicrosoftHasBrainDamage(
_conf.LatestPatchInstances,
Constants.LatestPatchInstances
);
_log.Debug("initializing instance map. patch instances updated since last update: {0}", patchUpdateNotYetApplied);

_territoryManager.GetDefaultInstancesForIds()
.Where(map => patchUpdateNotYetApplied || !_conf.Instances.ContainsKey(map.territoryId))
.UseToUpdate(_conf.Instances);

if (patchUpdateNotYetApplied) _conf.LastInstancePatchUpdate = Constants.LatestPatchUpdate;
if (patchUpdateNotYetApplied) _conf.LatestPatchInstances = Constants.LatestPatchInstances;
_conf.Save();
}

Expand Down
2 changes: 1 addition & 1 deletion ScoutHelper/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ IDataManager dataManager
_log = log;

var conf = pluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
conf.Initialize(pluginInterface);
conf.Initialize(_log, pluginInterface);

var serviceProvider = new ServiceCollection()
.AddSingleton(pluginInterface)
Expand Down
6 changes: 6 additions & 0 deletions ScoutHelper/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using CSharpFunctionalExtensions;
using ImGuiNET;
using Lumina.Text;
using Newtonsoft.Json;
using ScoutHelper.Models;

namespace ScoutHelper.Utils;
Expand Down Expand Up @@ -147,5 +148,10 @@ private static IEnumerable<string> Tokenize(string s) {

public static Vector2 Transpose(this Vector2 vec) => V2(vec.Y, vec.X);

public static bool NotEquals<T>(this T objA, T objB) => !Equals(objA, objB);

public static bool ActualValuesEqualBecauseMicrosoftHasBrainDamage(object? objA, object? objB) =>
Equals(objA, objB) || Equals(JsonConvert.SerializeObject(objA), JsonConvert.SerializeObject(objB));

#endregion
}

0 comments on commit 6350802

Please sign in to comment.