Skip to content

Commit

Permalink
add import/export/clear buttons in alarms tab
Browse files Browse the repository at this point in the history
  • Loading branch information
xorus committed Jan 12, 2024
1 parent f4a351c commit c9412e4
Show file tree
Hide file tree
Showing 9 changed files with 449 additions and 25 deletions.
10 changes: 4 additions & 6 deletions EngageTimer.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xml:space="preserve">
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve">
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">This file is part of EngageTimer
Copyright (C) $CURRENT_YEAR$ Xorus &lt;xorus@posteo.net&gt;
Copyright (C) ${CurrentDate.Year} Xorus &lt;xorus@posteo.net&gt;

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
Expand All @@ -15,4 +12,5 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see &lt;https://www.gnu.org/licenses/&gt;.</s:String></wpf:ResourceDictionary>
along with this program. If not, see &lt;https://www.gnu.org/licenses/&gt;.</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using System.Collections.Generic;
using System.Numerics;

namespace EngageTimer.Configuration.Legacy;
namespace EngageTimer.Configuration;

public class CombatAlarmsConfiguration
{
Expand Down
2 changes: 1 addition & 1 deletion Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public sealed class Plugin : IDalamudPlugin
private static FrameworkThings FrameworkThings { get; set; } = null!;
private static MainCommand MainCommand { get; set; } = null!;
private static SettingsCommand SettingsCommand { get; set; } = null!;
private static CombatAlarm CombatAlarm { get; set; } = null!;
public static CombatAlarm CombatAlarm { get; set; } = null!;
public static SfxPlay SfxPlay { get; set; } = null!;

public Plugin(DalamudPluginInterface pluginInterface)
Expand Down
153 changes: 153 additions & 0 deletions Plugin/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions Plugin/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,55 @@ help text for the /eg settings command</comment>
<data name="Settings_AlarmsTab_Line2" xml:space="preserve">
<value>A common use is to notify you of the potion window.</value>
</data>
<data name="AlarmEdit_Add" xml:space="preserve">
<value>Create new</value>
</data>
<data name="AlarmEdit_Clear" xml:space="preserve">
<value>Clear all</value>
</data>
<data name="AlarmEdit_Clear_Confirm" xml:space="preserve">
<value>Do you really want to remove all the alarms?</value>
</data>
<data name="AlarmEdit_Export" xml:space="preserve">
<value>Export</value>
</data>
<data name="AlarmEdit_Import" xml:space="preserve">
<value>Import</value>
</data>
<data name="AlarmEdit_Text_Test" xml:space="preserve">
<value>Test</value>
</data>
<data name="AlarmEdit_Export_Tooltip" xml:space="preserve">
<value>Exports the active alarms to a file</value>
</data>
<data name="AlarmEdit_Import_File" xml:space="preserve">
<value>Select file to import</value>
</data>
<data name="AlarmEdit_Export_File" xml:space="preserve">
<value>Select file to export</value>
</data>
<data name="Modal_Ok" xml:space="preserve">
<value>OK</value>
</data>
<data name="Modal_Cancel" xml:space="preserve">
<value>Cancel</value>
</data>
<data name="Modal_Confirm" xml:space="preserve">
<value>Confirm</value>
</data>
<data name="CombatAlarm_AccessDenied" xml:space="preserve">
<value>Could not save the file in this directory (access denied). Please try again with another directory.</value>
</data>
<data name="CombatAlarm_SaveGeneric" xml:space="preserve">
<value>An error occured while trying to save the alarms to {0}: {1}.</value>
</data>
<data name="CombatAlarm_ReadGeneric" xml:space="preserve">
<value>An error occured while trying to read the alarms to {0}: {1}.</value>
</data>
<data name="CombatAlarm_IncorrectFormat" xml:space="preserve">
<value>The alarms file format is incompatible or incorrect</value>
</data>
<data name="CombatAlarm_ImportedEmpty" xml:space="preserve">
<value>An empty or invalid alarm list was imported</value>
</data>
</root>
62 changes: 58 additions & 4 deletions Plugin/Status/CombatAlarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

using System;
using System.Collections.Generic;
using System.IO;
using Dalamud.Game.Text;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Plugin.Services;
using EngageTimer.Configuration.Legacy;
using EngageTimer.Configuration;
using EngageTimer.Game;
using EngageTimer.Ui;
using Newtonsoft.Json;

namespace EngageTimer.Status;

Expand Down Expand Up @@ -50,6 +53,57 @@ public CombatAlarm()
Plugin.State.InCombatChanged += InCombatChanged;
}

public static string? Import(string fileName)
{
try
{
var text = File.ReadAllText(fileName);
var data = JsonConvert.DeserializeObject<CombatAlarmsConfiguration>(text,
new JsonSerializerSettings
{
// using "TypeNameHandling.Objects" causes a "resolving to a collectible assembly is not supported"
TypeNameHandling = TypeNameHandling.None
});
if (data == null || data.Alarms.Count == 0) return Translator.Tr("CombatAlarm_ImportedEmpty");
Plugin.Config.CombatAlarms.Alarms.AddRange(data.Alarms);
}
catch (JsonSerializationException e)
{
Plugin.Logger.Error(e, $"Could not parse file {fileName}");
return Translator.Tr("CombatAlarm_IncorrectFormat");
}
catch (Exception e)
{
Plugin.Logger.Error(e, $"Could not read file {fileName}");
return Translator.Tr("CombatAlarm_ReadGeneric", fileName, e.Message);
}

return null;
}

public static string? Export(string fileName)
{
try
{
File.WriteAllText(fileName,
JsonConvert.SerializeObject(Plugin.Config.CombatAlarms, Formatting.Indented,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.None
}));
}
catch (UnauthorizedAccessException)
{
return Translator.Tr("CombatAlarm_AccessDenied");
}
catch (Exception e)
{
Plugin.Logger.Error(e, $"Could not save file {fileName}");
return Translator.Tr("CombatAlarm_SaveGeneric", fileName, e.Message);
}

return null;
}

private void ConfigurationChanged(object? sender, EventArgs e)
{
Expand Down Expand Up @@ -88,7 +142,7 @@ private void FrameworkUpdate(IFramework framework)
if (!Plugin.State.InCombat) return;

// only run once a second
var time = (int)Math.Floor(Plugin.State.CombatDuration.TotalSeconds);
var time = (int) Math.Floor(Plugin.State.CombatDuration.TotalSeconds);
if (_lastCheck == time) return;
_lastCheck = time;

Expand All @@ -107,14 +161,14 @@ public static void AlarmSfx(CombatAlarmsConfiguration.Alarm alarm)
{
if (alarm.Sfx != null)
{
Plugin.SfxPlay.SoundEffect((uint)(SfxPlay.FirstSeSfx + alarm.Sfx));
Plugin.SfxPlay.SoundEffect((uint) (SfxPlay.FirstSeSfx + alarm.Sfx));
}
}

public static void AlarmText(CombatAlarmsConfiguration.Alarm alarm)
{
var trimText = alarm.Text?.Trim();
if (trimText is not { Length: > 0 }) return;
if (trimText is not {Length: > 0}) return;
switch (alarm.TextType)
{
case CombatAlarmsConfiguration.TextType.DalamudNotification:
Expand Down
Loading

0 comments on commit c9412e4

Please sign in to comment.