generated from goatcorp/SamplePlugin
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
723 additions
and
207 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
ScoutHelper/Configuration.cs → ScoutHelper/Config/Configuration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace ScoutHelper.Config; | ||
|
||
public record ScoutHelperOptions( | ||
string BearDataFile | ||
) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,108 @@ | ||
using Dalamud.Game.Command; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Dalamud.Game.Command; | ||
using Dalamud.Interface.Windowing; | ||
using Dalamud.IoC; | ||
using Dalamud.Plugin; | ||
using Dalamud.Plugin.Services; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using ScoutHelper.Config; | ||
using ScoutHelper.Localization; | ||
using ScoutHelper.Managers; | ||
using ScoutHelper.Windows; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
|
||
namespace ScoutHelper; | ||
|
||
// ReSharper disable once ClassNeverInstantiated.Global | ||
public sealed class Plugin : IDalamudPlugin { | ||
|
||
public const string Name = Constants.PluginName; | ||
|
||
private static readonly List<string> CommandNames = new List<string>() {"/scouth", "/sch"}; | ||
|
||
public static Configuration Conf { get; private set; } = null!; | ||
|
||
[RequiredVersion("1.0"), PluginService] | ||
public static DalamudPluginInterface PluginInterface { get; private set; } = null!; | ||
[RequiredVersion("1.0"), PluginService] | ||
public static IPluginLog Log { get; private set; } = null!; | ||
[RequiredVersion("1.0"), PluginService] | ||
public static IChatGui ChatGui { get; private set; } = null!; | ||
[RequiredVersion("1.0"), PluginService] | ||
public static ICommandManager CommandManager { get; private set; } = null!; | ||
[RequiredVersion("1.0"), PluginService] | ||
public static IClientState ClientState { get; private set; } = null!; | ||
|
||
private WindowSystem WindowSystem { get; } = new WindowSystem(Constants.PluginNamespace); | ||
private HuntHelperManager HuntHelperManager { get; init; } | ||
private BearManager BearManager { get; init; } | ||
|
||
private ConfigWindow ConfigWindow { get; init; } | ||
private MainWindow MainWindow { get; init; } | ||
|
||
public Plugin() { | ||
Conf = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration(); | ||
Conf.Initialize(PluginInterface); | ||
|
||
HuntHelperManager = new HuntHelperManager(); | ||
BearManager = new BearManager(Utils.PluginFilePath(@"Data\Bear.json")); | ||
|
||
ConfigWindow = new ConfigWindow(); | ||
MainWindow = new MainWindow(HuntHelperManager, BearManager, ConfigWindow); | ||
|
||
PluginInterface.LanguageChanged += OnLanguageChanged; | ||
OnLanguageChanged(PluginInterface.UiLanguage); | ||
|
||
WindowSystem.AddWindow(ConfigWindow); | ||
WindowSystem.AddWindow(MainWindow); | ||
|
||
CommandNames.ForEach(commandName => | ||
CommandManager.AddHandler(commandName, new CommandInfo(OnCommand) {HelpMessage = "Opens the main window."}) | ||
private static readonly List<string> CommandNames = new() { "/scouth", "/sch" }; | ||
|
||
private readonly IPluginLog _log; | ||
|
||
private readonly WindowSystem _windowSystem = new WindowSystem(Constants.PluginNamespace); | ||
private readonly ConfigWindow _configWindow; | ||
private readonly MainWindow _mainWindow; | ||
private readonly Action _dispose; | ||
|
||
public Plugin( | ||
[RequiredVersion("1.0")] DalamudPluginInterface pluginInterface, | ||
[RequiredVersion("1.0")] IPluginLog log, | ||
[RequiredVersion("1.0")] IChatGui chatGui, | ||
[RequiredVersion("1.0")] ICommandManager commandManager, | ||
[RequiredVersion("1.0")] IClientState clientState | ||
) { | ||
_log = log; | ||
|
||
var conf = pluginInterface.GetPluginConfig() as Configuration ?? new Configuration(); | ||
conf.Initialize(pluginInterface); | ||
|
||
var serviceProvider = new ServiceCollection() | ||
.AddSingleton(pluginInterface) | ||
.AddSingleton(_log) | ||
.AddSingleton(chatGui) | ||
.AddSingleton(commandManager) | ||
.AddSingleton(clientState) | ||
.AddSingleton(conf) | ||
.AddSingleton(new ScoutHelperOptions(pluginInterface.PluginFilePath(Constants.BearDataFile))) | ||
.AddSingleton<ConfigWindow>() | ||
.AddSingleton<MainWindow>() | ||
.AddSingleton<BearManager>() | ||
.AddSingleton<HuntHelperManager>() | ||
.BuildServiceProvider(); | ||
|
||
pluginInterface.LanguageChanged += OnLanguageChanged; | ||
OnLanguageChanged(pluginInterface.UiLanguage); | ||
|
||
_configWindow = serviceProvider.GetRequiredService<ConfigWindow>(); | ||
_mainWindow = serviceProvider.GetRequiredService<MainWindow>(); | ||
_windowSystem.AddWindow(_configWindow); | ||
_windowSystem.AddWindow(_mainWindow); | ||
|
||
CommandNames.ForEach( | ||
commandName => | ||
commandManager.AddHandler(commandName, new CommandInfo(OnCommand) { HelpMessage = "Opens the main window." }) | ||
); | ||
|
||
PluginInterface.UiBuilder.Draw += DrawUi; | ||
PluginInterface.UiBuilder.OpenConfigUi += DrawConfigUi; | ||
} | ||
pluginInterface.UiBuilder.Draw += DrawUi; | ||
pluginInterface.UiBuilder.OpenConfigUi += DrawConfigUi; | ||
|
||
public void Dispose() { | ||
PluginInterface.LanguageChanged -= OnLanguageChanged; | ||
CommandNames.ForEach(commandName => CommandManager.RemoveHandler(commandName)); | ||
// create the dispose action here, so fields don't need to be made just for disposal | ||
_dispose = () => { | ||
pluginInterface.LanguageChanged -= OnLanguageChanged; | ||
CommandNames.ForEach(commandName => commandManager.RemoveHandler(commandName)); | ||
WindowSystem.RemoveAllWindows(); | ||
_windowSystem.RemoveAllWindows(); | ||
ConfigWindow.Dispose(); | ||
MainWindow.Dispose(); | ||
conf.Save(); | ||
HuntHelperManager.Dispose(); | ||
|
||
Conf.Save(); | ||
serviceProvider.Dispose(); | ||
}; | ||
} | ||
|
||
private static void OnLanguageChanged(string languageCode) { | ||
public void Dispose() => _dispose.Invoke(); | ||
|
||
private void OnLanguageChanged(string languageCode) { | ||
try { | ||
Log.Information($"Loading localization for {languageCode}"); | ||
_log.Information($"Loading localization for {languageCode}"); | ||
Strings.Culture = new CultureInfo(languageCode); | ||
} | ||
catch (Exception e) { | ||
Log.Error(e, "Unable to load localization for language code: {0}", languageCode); | ||
} catch (Exception e) { | ||
_log.Error(e, "Unable to load localization for language code: {0}", languageCode); | ||
} | ||
} | ||
|
||
private void OnCommand(string command, string args) { | ||
MainWindow.IsOpen = true; | ||
_mainWindow.IsOpen = true; | ||
} | ||
|
||
private void DrawUi() { | ||
WindowSystem.Draw(); | ||
_windowSystem.Draw(); | ||
} | ||
|
||
private void DrawConfigUi() { | ||
ConfigWindow.IsOpen = true; | ||
_configWindow.IsOpen = true; | ||
} | ||
} |
Oops, something went wrong.