From db44be0d009a3889153e2895f884cfd06f798979 Mon Sep 17 00:00:00 2001 From: RisaDev <151885272+RisaDev@users.noreply.github.com> Date: Sat, 16 Nov 2024 03:55:21 +0300 Subject: [PATCH] Dalamud branch detection --- CustomizePlus/Core/Helpers/VersionHelper.cs | 3 + CustomizePlus/Core/ServiceManagerBuilder.cs | 3 +- .../Services/Dalamud/DalamudBranchService.cs | 51 ++++++++++++++ .../Services/Dalamud/DalamudConfigService.cs | 68 +++++++++++++++++++ .../Services/{ => Dalamud}/DalamudServices.cs | 2 +- CustomizePlus/Core/Services/HookingService.cs | 7 ++ .../Core/Services/SupportLogBuilderService.cs | 7 +- ...ifierService.cs => UserNotifierService.cs} | 16 ++++- CustomizePlus/Plugin.cs | 2 +- .../UI/Windows/Controls/PluginStateBlock.cs | 20 ++++-- 10 files changed, 168 insertions(+), 11 deletions(-) create mode 100644 CustomizePlus/Core/Services/Dalamud/DalamudBranchService.cs create mode 100644 CustomizePlus/Core/Services/Dalamud/DalamudConfigService.cs rename CustomizePlus/Core/Services/{ => Dalamud}/DalamudServices.cs (95%) rename CustomizePlus/Core/Services/{TestingVersionNotifierService.cs => UserNotifierService.cs} (52%) diff --git a/CustomizePlus/Core/Helpers/VersionHelper.cs b/CustomizePlus/Core/Helpers/VersionHelper.cs index 7c8e64f..5696b25 100644 --- a/CustomizePlus/Core/Helpers/VersionHelper.cs +++ b/CustomizePlus/Core/Helpers/VersionHelper.cs @@ -10,10 +10,13 @@ internal static class VersionHelper public static bool IsTesting { get; private set; } = false; + public static bool IsDebug { get; private set; } = false; + static VersionHelper() { #if DEBUG Version = $"{ThisAssembly.Git.Commit}+{ThisAssembly.Git.Sha} [DEBUG]"; + IsDebug = true; #else Version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty; #endif diff --git a/CustomizePlus/Core/ServiceManagerBuilder.cs b/CustomizePlus/Core/ServiceManagerBuilder.cs index 98dd064..f18f770 100644 --- a/CustomizePlus/Core/ServiceManagerBuilder.cs +++ b/CustomizePlus/Core/ServiceManagerBuilder.cs @@ -6,6 +6,7 @@ using CustomizePlus.Configuration.Data; using CustomizePlus.Configuration.Services; using CustomizePlus.Core.Events; using CustomizePlus.Core.Services; +using CustomizePlus.Core.Services.Dalamud; using CustomizePlus.Game.Events; using CustomizePlus.Game.Services; using CustomizePlus.Game.Services.GPose; @@ -139,7 +140,7 @@ public static class ServiceManagerBuilder .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton(); + .AddSingleton(); return services; } diff --git a/CustomizePlus/Core/Services/Dalamud/DalamudBranchService.cs b/CustomizePlus/Core/Services/Dalamud/DalamudBranchService.cs new file mode 100644 index 0000000..1a329cd --- /dev/null +++ b/CustomizePlus/Core/Services/Dalamud/DalamudBranchService.cs @@ -0,0 +1,51 @@ +using OtterGui.Log; +using OtterGui.Services; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CustomizePlus.Core.Services.Dalamud; + +public class DalamudBranchService : IService +{ + /// + /// Current Dalamud branch + /// + public DalamudBranch CurrentBranch { get; private set; } + + /// + /// Current Dalamud branch name + /// + public string CurrentBranchName { get; private set; } + + public DalamudBranchService(DalamudConfigService dalamudConfigService, Logger logger) + { + dalamudConfigService.GetDalamudConfig(DalamudConfigService.BetaKindOption, out var betaOption); + + CurrentBranchName = betaOption?.ToLowerInvariant() ?? "release"; + switch (CurrentBranchName) + { + case "release": + CurrentBranch = DalamudBranch.Release; + break; + case "stg": + CurrentBranch = DalamudBranch.Staging; + break; + default: + CurrentBranch = DalamudBranch.Other; + break; + } + + logger.Information($"Current Dalamud branch is: {CurrentBranchName} ({CurrentBranch})"); + } + + public enum DalamudBranch + { + //For our purposes we want to default to Release + Release, + Staging, + Other + } +} diff --git a/CustomizePlus/Core/Services/Dalamud/DalamudConfigService.cs b/CustomizePlus/Core/Services/Dalamud/DalamudConfigService.cs new file mode 100644 index 0000000..00a6d38 --- /dev/null +++ b/CustomizePlus/Core/Services/Dalamud/DalamudConfigService.cs @@ -0,0 +1,68 @@ +using Dalamud.Plugin; +using OtterGui.Services; +using System.Linq; +using System.Reflection; +using System; + +namespace CustomizePlus.Core.Services.Dalamud; + +public class DalamudConfigService : IService +{ + public DalamudConfigService() + { + try + { + var serviceType = + typeof(IDalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "Service`1" && t.IsGenericType); + var configType = typeof(IDalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "DalamudConfiguration"); + var interfaceType = typeof(IDalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "DalamudInterface"); + if (serviceType == null || configType == null || interfaceType == null) + return; + + var configService = serviceType.MakeGenericType(configType); + var interfaceService = serviceType.MakeGenericType(interfaceType); + var configGetter = configService.GetMethod("Get", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + _interfaceGetter = interfaceService.GetMethod("Get", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + if (configGetter == null || _interfaceGetter == null) + return; + + _dalamudConfig = configGetter.Invoke(null, null); + } + catch + { + _dalamudConfig = null; + _interfaceGetter = null; + } + } + + public const string BetaKindOption = "DalamudBetaKind"; + + private readonly object? _dalamudConfig; + private readonly MethodInfo? _interfaceGetter; + + public bool GetDalamudConfig(string fieldName, out T? value) + { + value = default; + try + { + if (_dalamudConfig == null) + return false; + + var getter = _dalamudConfig.GetType().GetProperty(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + if (getter == null) + return false; + + var result = getter.GetValue(_dalamudConfig); + if (result is not T v) + return false; + + value = v; + return true; + } + catch (Exception e) + { + Plugin.Logger.Error($"Error while fetching Dalamud Config {fieldName}:\n{e}"); + return false; + } + } +} diff --git a/CustomizePlus/Core/Services/DalamudServices.cs b/CustomizePlus/Core/Services/Dalamud/DalamudServices.cs similarity index 95% rename from CustomizePlus/Core/Services/DalamudServices.cs rename to CustomizePlus/Core/Services/Dalamud/DalamudServices.cs index 308b9ea..f658844 100644 --- a/CustomizePlus/Core/Services/DalamudServices.cs +++ b/CustomizePlus/Core/Services/Dalamud/DalamudServices.cs @@ -6,7 +6,7 @@ using Dalamud.Plugin.Services; using Microsoft.Extensions.DependencyInjection; using OtterGui.Services; -namespace CustomizePlus.Core.Services; +namespace CustomizePlus.Core.Services.Dalamud; public class DalamudServices { diff --git a/CustomizePlus/Core/Services/HookingService.cs b/CustomizePlus/Core/Services/HookingService.cs index 363478f..788b8e0 100644 --- a/CustomizePlus/Core/Services/HookingService.cs +++ b/CustomizePlus/Core/Services/HookingService.cs @@ -13,6 +13,7 @@ using CustomizePlus.GameData.Data; using Penumbra.GameData.Interop; using Dalamud.Plugin; using CustomizePlus.Core.Helpers; +using CustomizePlus.Core.Services.Dalamud; namespace CustomizePlus.Core.Services; @@ -24,6 +25,7 @@ public class HookingService : IDisposable private readonly ProfileManager _profileManager; private readonly ArmatureManager _armatureManager; private readonly GameStateService _gameStateService; + //private readonly DalamudBranchService _dalamudBranchService; private readonly Logger _logger; private Hook? _renderManagerHook; @@ -44,6 +46,7 @@ public class HookingService : IDisposable ProfileManager profileManager, ArmatureManager armatureManager, GameStateService gameStateService, + //DalamudBranchService dalamudBranchService, Logger logger) { _configuration = configuration; @@ -52,6 +55,7 @@ public class HookingService : IDisposable _profileManager = profileManager; _armatureManager = armatureManager; _gameStateService = gameStateService; + //_dalamudBranchService = dalamudBranchService; _logger = logger; ReloadHooks(); @@ -71,6 +75,9 @@ public class HookingService : IDisposable RenderHookFailed = false; MovementHookFailed = false; + /*if(_dalamudBranchService.CurrentBranch != DalamudBranchService.DalamudBranch.Release) + return;*/ + try { if (_configuration.PluginEnabled) diff --git a/CustomizePlus/Core/Services/SupportLogBuilderService.cs b/CustomizePlus/Core/Services/SupportLogBuilderService.cs index 8c7dead..20db39b 100644 --- a/CustomizePlus/Core/Services/SupportLogBuilderService.cs +++ b/CustomizePlus/Core/Services/SupportLogBuilderService.cs @@ -6,6 +6,7 @@ using CustomizePlus.Configuration.Data; using CustomizePlus.Core.Data; using CustomizePlus.Core.Extensions; using CustomizePlus.Core.Helpers; +using CustomizePlus.Core.Services.Dalamud; using CustomizePlus.Profiles; using CustomizePlus.Templates; using Dalamud.Plugin; @@ -20,19 +21,22 @@ public class SupportLogBuilderService private readonly ProfileManager _profileManager; private readonly ArmatureManager _armatureManager; private readonly IDalamudPluginInterface _dalamudPluginInterface; + private readonly DalamudBranchService _dalamudBranchService; public SupportLogBuilderService( PluginConfiguration configuration, TemplateManager templateManager, ProfileManager profileManager, ArmatureManager armatureManager, - IDalamudPluginInterface dalamudPluginInterface) + IDalamudPluginInterface dalamudPluginInterface, + DalamudBranchService dalamudBranchService) { _configuration = configuration; _templateManager = templateManager; _profileManager = profileManager; _armatureManager = armatureManager; _dalamudPluginInterface = dalamudPluginInterface; + _dalamudBranchService = dalamudBranchService; } public string BuildSupportLog() @@ -41,6 +45,7 @@ public class SupportLogBuilderService sb.AppendLine("**Settings**"); sb.Append($"> **`Plugin Version: `** {VersionHelper.Version}\n"); sb.Append($"> **`Commit Hash: `** {ThisAssembly.Git.Commit}+{ThisAssembly.Git.Sha}\n"); + sb.Append($"> **`Dalamud Branch: `** {_dalamudBranchService.CurrentBranchName} ({_dalamudBranchService.CurrentBranch})\n"); sb.Append($"> **`Plugin enabled: `** {_configuration.PluginEnabled}\n"); sb.AppendLine("**Settings -> Editor Settings**"); sb.Append($"> **`Preview character (editor): `** {_configuration.EditorConfiguration.PreviewCharacter.Incognito(null)}\n"); diff --git a/CustomizePlus/Core/Services/TestingVersionNotifierService.cs b/CustomizePlus/Core/Services/UserNotifierService.cs similarity index 52% rename from CustomizePlus/Core/Services/TestingVersionNotifierService.cs rename to CustomizePlus/Core/Services/UserNotifierService.cs index 14c8228..bc0e6be 100644 --- a/CustomizePlus/Core/Services/TestingVersionNotifierService.cs +++ b/CustomizePlus/Core/Services/UserNotifierService.cs @@ -1,19 +1,27 @@ using System; using CustomizePlus.Core.Helpers; +using CustomizePlus.Core.Services.Dalamud; using CustomizePlus.Game.Services; using Dalamud.Plugin.Services; namespace CustomizePlus.Core.Services; -public class TestingVersionNotifierService : IDisposable +public class UserNotifierService : IDisposable { private readonly IClientState _clientState; private readonly ChatService _chatService; + private readonly DalamudBranchService _dalamudBranchService; - public TestingVersionNotifierService(IClientState clientState, ChatService chatService) + public UserNotifierService( + IClientState clientState, + ChatService chatService, + DalamudBranchService dalamudBranchService) { _clientState = clientState; _chatService = chatService; + _dalamudBranchService = dalamudBranchService; + + OnLogin(); _clientState.Login += OnLogin; } @@ -28,5 +36,9 @@ public class TestingVersionNotifierService : IDisposable if (VersionHelper.IsTesting) _chatService.PrintInChat($"You are running testing version of Customize+! Some features like integration with other plugins might not function correctly.", ChatService.ChatMessageColor.Warning); + + if (_dalamudBranchService.CurrentBranch != DalamudBranchService.DalamudBranch.Release) + _chatService.PrintInChat($"You are running development or testing version of Dalamud. This is not supported and might be actively prevented in the future.", + ChatService.ChatMessageColor.Error); } } diff --git a/CustomizePlus/Plugin.cs b/CustomizePlus/Plugin.cs index 3ea8407..e9fe524 100644 --- a/CustomizePlus/Plugin.cs +++ b/CustomizePlus/Plugin.cs @@ -28,7 +28,7 @@ public sealed class Plugin : IDalamudPlugin _services.GetService(); //needs to be initialized early for config to be read properly - _services.GetService(); + _services.GetService(); _services.GetService(); _services.GetService(); _services.GetService(); diff --git a/CustomizePlus/UI/Windows/Controls/PluginStateBlock.cs b/CustomizePlus/UI/Windows/Controls/PluginStateBlock.cs index e989db8..71addbc 100644 --- a/CustomizePlus/UI/Windows/Controls/PluginStateBlock.cs +++ b/CustomizePlus/UI/Windows/Controls/PluginStateBlock.cs @@ -9,6 +9,7 @@ using CustomizePlus.UI.Windows.MainWindow.Tabs.Templates; using CustomizePlus.Core.Helpers; using CustomizePlus.Api; using CustomizePlus.Core.Data; +using CustomizePlus.Core.Services.Dalamud; namespace CustomizePlus.UI.Windows.Controls; @@ -19,19 +20,22 @@ public class PluginStateBlock private readonly GameStateService _gameStateService; private readonly HookingService _hookingService; private readonly CustomizePlusIpc _ipcService; + private readonly DalamudBranchService _dalamudBranchService; public PluginStateBlock( BoneEditorPanel boneEditorPanel, PluginConfiguration configuration, GameStateService gameStateService, HookingService hookingService, - CustomizePlusIpc ipcService) + CustomizePlusIpc ipcService, + DalamudBranchService dalamudBranchService) { _boneEditorPanel = boneEditorPanel; _configuration = configuration; _gameStateService = gameStateService; _hookingService = hookingService; _ipcService = ipcService; + _dalamudBranchService = dalamudBranchService; } public void Draw(float yPos) @@ -43,7 +47,7 @@ public class PluginStateBlock if(_hookingService.RenderHookFailed || _hookingService.MovementHookFailed) { severity = PluginStateSeverity.Error; - message = $"Detected failure in game hooks. Customize+ disabled."; + message = "Detected failure in game hooks. Customize+ disabled."; } else if (!_configuration.PluginEnabled) { @@ -68,17 +72,23 @@ public class PluginStateBlock else if (_gameStateService.GameInPosingMode()) { severity = PluginStateSeverity.Warning; - message = $"GPose active. Compatibility with posing tools is limited."; + message = "GPose active. Compatibility with posing tools is limited."; } else if (_ipcService.IPCFailed) //this is a low priority error { severity = PluginStateSeverity.Error; - message = $"Detected failure in IPC. Integrations with other plugins will not function."; + message = "Detected failure in IPC. Integrations with other plugins will not function."; + } + else if (_dalamudBranchService.CurrentBranch != DalamudBranchService.DalamudBranch.Release) + { + severity = PluginStateSeverity.Error; + message = "You are running unsupported version of Dalamud, hover for more information."; + hoverInfo = "Regular users are not supposed to run Customize+ on development or testing versions of Dalamud.\nThis is not supported and might be actively prevented in the future."; } else if(VersionHelper.IsTesting) { severity = PluginStateSeverity.Warning; - message = $"You are running testing version of Customize+, hover for more information."; + message = "You are running testing version of Customize+, hover for more information."; hoverInfo = "This is a testing build of Customize+. Some features like integration with other plugins might not function correctly."; }