Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
[STABLE] feat: update for api v9
Browse files Browse the repository at this point in the history
  • Loading branch information
kalilistic committed Oct 3, 2023
1 parent 7c9254d commit bb22579
Show file tree
Hide file tree
Showing 13 changed files with 883 additions and 485 deletions.
2 changes: 1 addition & 1 deletion src/NeatNoter/DalamudPackager.targets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<Target Name="Package" AfterTargets="Build" Condition="'$(Configuration)' == 'Release'">
<Target Name="Package" AfterTargets="Build">
<DalamudPackager
ProjectDir="$(ProjectDir)"
OutputPath="$(OutputPath)"
Expand Down
7 changes: 4 additions & 3 deletions src/NeatNoter/NeatNoter.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.4.0.0</Version>
<Version>2.5.0.0</Version>
<Title>NeatNoter</Title>
<Authors>kalilistic, karashiiro</Authors>
<TargetFramework>net7.0-windows</TargetFramework>
Expand All @@ -19,6 +19,7 @@
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<UseWindowsForms>false</UseWindowsForms>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<NoWarn>SA1600,CS1591,CS0618</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -93,8 +94,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="DalamudPackager" Version="2.1.10" />
<PackageReference Include="Dalamud.DrunkenToad" Version="1.2.1" />
<PackageReference Include="DalamudPackager" Version="2.1.12" />
<PackageReference Include="Dalamud.DrunkenToad" Version="1.7.2" />
<PackageReference Include="LiteDB" Version="5.0.12" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
Expand Down
3 changes: 1 addition & 2 deletions src/NeatNoter/NeatNoter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ tags:
- tool
- overlay
repo_url: https://github.com/kalilistic/NeatNoter
icon_url: https://github.com/goatcorp/DalamudPlugins/blob/api6/plugins/NeatNoter/images/icon.png
dalamud_api_level: 8
icon_url: https://github.com/goatcorp/DalamudPlugins/blob/api6/plugins/NeatNoter/images/icon.png
77 changes: 77 additions & 0 deletions src/NeatNoter/NeatNoter/Localization/LegacyLoc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Reflection;

using CheapLoc;
using Dalamud.Game.Command;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;

namespace NeatNoter.Localization;

public class LegacyLoc
{
private readonly DalamudPluginInterface pluginInterface;
private readonly ICommandManager commandManager;
private readonly string pluginName;
private readonly Assembly assembly;

public LegacyLoc(DalamudPluginInterface pluginInterface, ICommandManager commandManager)
{
this.pluginInterface = pluginInterface;
this.commandManager = commandManager;
this.assembly = Assembly.GetCallingAssembly();
this.pluginName = this.assembly.GetName().Name ?? string.Empty;
this.SetLanguage(this.pluginInterface.UiLanguage);
this.pluginInterface.LanguageChanged += this.LanguageChanged;
this.commandManager.AddHandler(
"/" + this.pluginName.ToLower() + "exloc",
new CommandInfo(this.ExportLocalizable)
{
ShowInHelp = false,
});
}

public void SetLanguage(string languageCode)
{
if (!string.IsNullOrEmpty(languageCode) && languageCode != "en")
{
try
{
string locData;
var resourceFile = $"{this.pluginName}.{this.pluginName}.Resource.translation.{languageCode}.json";
var resourceStream = this.assembly.GetManifestResourceStream(resourceFile);
using (var reader = new StreamReader(resourceStream ?? throw new InvalidOperationException()))
{
locData = reader.ReadToEnd();
}

Loc.Setup(locData, this.assembly);
}
catch (Exception)
{
Loc.SetupWithFallbacks(this.assembly);
}
}
else
{
Loc.SetupWithFallbacks(this.assembly);
}
}

public void Dispose()
{
this.pluginInterface.LanguageChanged -= this.LanguageChanged;
this.commandManager.RemoveHandler("/" + this.pluginName.ToLower() + "exloc");
}

private void ExportLocalizable(string command, string args)
{
Loc.ExportLocalizableForAssembly(this.assembly);
}

private void LanguageChanged(string langCode)
{
this.SetLanguage(langCode);
}
}
3 changes: 2 additions & 1 deletion src/NeatNoter/NeatNoter/Migration/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;

using Dalamud.DrunkenToad;
using Dalamud.Logging;

#pragma warning disable 618

Expand Down Expand Up @@ -86,7 +87,7 @@ public static bool Migrate(NeatNoterPlugin plugin)
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to migrate.");
PluginLog.Error(ex, "Failed to migrate.");
return false;
}

Expand Down
30 changes: 15 additions & 15 deletions src/NeatNoter/NeatNoter/Plugin/NeatNoterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
using System.Timers;

using Dalamud.DrunkenToad;
using Dalamud.DrunkenToad.Extensions;
using Dalamud.DrunkenToad.Helpers;
using Dalamud.Game.ClientState;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
using Dalamud.IoC;
using Dalamud.Logging;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using NeatNoter.Localization;

namespace NeatNoter
{
Expand Down Expand Up @@ -36,7 +40,7 @@ public class NeatNoterPlugin : IDalamudPlugin
public BackupManager BackupManager;

private readonly Timer backupTimer;
private readonly Localization localization;
private readonly LegacyLoc localization;

/// <summary>
/// Initializes a new instance of the <see cref="NeatNoterPlugin"/> class.
Expand All @@ -51,13 +55,13 @@ public NeatNoterPlugin()
}
catch (Exception ex)
{
Logger.LogError("Failed to load config so creating new one.", ex);
PluginLog.Error("Failed to load config so creating new one.", ex);
this.Configuration = new NeatNoterConfiguration();
this.SaveConfig();
}

// load services
this.localization = new Localization(PluginInterface, CommandManager);
this.localization = new LegacyLoc(PluginInterface, CommandManager);
this.BackupManager = new BackupManager(PluginInterface.GetPluginConfigDirectory());
this.NotebookService = new NotebookService(this);

Expand All @@ -67,7 +71,7 @@ public NeatNoterPlugin()
var pluginVersion = Assembly.GetExecutingAssembly().VersionNumber();
if (this.Configuration.PluginVersion < pluginVersion)
{
Logger.LogInfo("Running backup since new version detected.");
PluginLog.Log("Running backup since new version detected.");
this.RunUpgradeBackup();
this.Configuration.PluginVersion = pluginVersion;
this.SaveConfig();
Expand Down Expand Up @@ -102,30 +106,27 @@ public NeatNoterPlugin()
/// </summary>
[PluginService]
[RequiredVersion("1.0")]
public static ChatGui Chat { get; private set; } = null!;
public static IChatGui Chat { get; private set; } = null!;

/// <summary>
/// Gets command manager.
/// </summary>
[PluginService]
[RequiredVersion("1.0")]
public static CommandManager CommandManager { get; private set; } = null!;
public static ICommandManager CommandManager { get; private set; } = null!;

/// <summary>
/// Gets client state.
/// </summary>
[PluginService]
[RequiredVersion("1.0")]
public static ClientState ClientState { get; private set; } = null!;
public static IClientState ClientState { get; private set; } = null!;

/// <summary>
/// Gets or sets command manager to handle user commands.
/// </summary>
public PluginCommandManager PluginCommandManager { get; set; } = null!;

/// <inheritdoc />
public string Name => "NeatNoter";

/// <summary>
/// Get plugin folder.
/// </summary>
Expand Down Expand Up @@ -183,25 +184,24 @@ private void HandleJustInstalled()
}

this.NotebookService.SetVersion(2);
Chat.PluginPrintNotice("NoteNoter has been installed! Type /notebook to open the notebook.");
this.Configuration.JustInstalled = false;
this.SaveConfig();
}

private void BackupTimerOnElapsed(object? sender, ElapsedEventArgs? e)
{
if (DateUtil.CurrentTime() > this.Configuration.LastBackup + this.Configuration.BackupFrequency)
if (UnixTimestampHelper.CurrentTime() > this.Configuration.LastBackup + this.Configuration.BackupFrequency)
{
Logger.LogInfo("Running backup due to frequency timer.");
this.Configuration.LastBackup = DateUtil.CurrentTime();
PluginLog.Log("Running backup due to frequency timer.");
this.Configuration.LastBackup = UnixTimestampHelper.CurrentTime();
this.BackupManager.CreateBackup();
this.BackupManager.DeleteBackups(this.Configuration.BackupRetention);
}
}

private void RunUpgradeBackup()
{
this.Configuration.LastBackup = DateUtil.CurrentTime();
this.Configuration.LastBackup = UnixTimestampHelper.CurrentTime();
this.BackupManager.CreateBackup("upgrade/v" + this.Configuration.PluginVersion + "_");
this.BackupManager.DeleteBackups(this.Configuration.BackupRetention);
}
Expand Down
85 changes: 85 additions & 0 deletions src/NeatNoter/NeatNoter/Service/BackupManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Dalamud.DrunkenToad.Helpers;
using Dalamud.Logging;

namespace NeatNoter;

public class BackupManager
{
private readonly string dataPath;

public BackupManager(string pluginFolder)
{
this.dataPath = pluginFolder + "/data/";
}

public void CreateBackup(string prefix = "")
{
try
{
var backupDir = $"{this.dataPath}{prefix}{UnixTimestampHelper.CurrentTime()}/";
Directory.CreateDirectory(backupDir);
var files = Directory.GetFiles(this.dataPath);
foreach (var file in files)
{
var fileName = Path.GetFileName(file);
File.Copy(this.dataPath + fileName, backupDir + fileName, true);
}
}
catch (Exception ex)
{
PluginLog.LogError(ex, "Failed to create backup.");
}
}

public void DeleteBackups(int max)
{
// don't delete everything
if (max == 0)
{
return;
}

try
{
// loop through directories and get those without prefix
var dirs = Directory.GetDirectories(this.dataPath);
var dirNames = new List<long>();
foreach (var dir in dirs)
{
try
{
var dirName = new DirectoryInfo(dir).Name;
if (dirName.Any(char.IsLetter))
{
continue;
}

dirNames.Add(Convert.ToInt64(dirName));
}
catch (Exception)
{
// ignored
}
}

// if don't exceed max then out
if (dirs.Length <= max)
{
return;
}

dirNames.Sort();
Directory.Delete(this.dataPath + dirNames.First(), true);
this.DeleteBackups(max);
}
catch (Exception ex)
{
PluginLog.LogError(ex, "Failed to delete backup.");
}
}
}
19 changes: 19 additions & 0 deletions src/NeatNoter/NeatNoter/Service/BaseRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace NeatNoter;

public class BaseRepository : Repository
{
protected BaseRepository(string pluginFolder)
: base(pluginFolder)
{
}

public int GetSchemaVersion()
{
return this.GetVersion();
}

public void SetSchemaVersion(int version)
{
this.SetVersion(version);
}
}
Loading

0 comments on commit bb22579

Please sign in to comment.