update auto message feed plugin to IPluginV2

This commit is contained in:
RaidMax 2023-02-11 20:44:04 -06:00
parent 8fc47ec6c4
commit 2688790736
4 changed files with 76 additions and 88 deletions

View File

@ -0,0 +1,17 @@
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Plugins.AutoMessageFeed;
public class AutoMessageFeedConfiguration : IBaseConfiguration
{
public bool EnableFeed { get; set; }
public string FeedUrl { get; set; }
public int MaxFeedItems { get; set; }
public IBaseConfiguration Generate()
{
return this;
}
public string Name() => "AutomessageFeedConfiguration";
}

View File

@ -10,7 +10,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" /> <PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.10.13.1" PrivateAssets="All" /> <PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2023.2.11.1" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent"> <Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -1,18 +0,0 @@
using SharedLibraryCore.Interfaces;
namespace AutomessageFeed
{
class Configuration : IBaseConfiguration
{
public bool EnableFeed { get; set; }
public string FeedUrl { get; set; }
public int MaxFeedItems { get; set; }
public IBaseConfiguration Generate()
{
return this;
}
public string Name() => "AutomessageFeedConfiguration";
}
}

View File

@ -1,54 +1,71 @@
using SharedLibraryCore; using SharedLibraryCore;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.SyndicationFeed.Rss; using Microsoft.SyndicationFeed.Rss;
using SharedLibraryCore.Configuration;
using System.Xml; using System.Xml;
using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed;
using System.Collections.Generic; using System.Collections.Generic;
using SharedLibraryCore.Helpers; using SharedLibraryCore.Helpers;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyInjection;
using SharedLibraryCore.Interfaces.Events;
namespace AutomessageFeed namespace IW4MAdmin.Plugins.AutoMessageFeed;
{
public class Plugin : IPlugin public class Plugin : IPluginV2
{ {
public string Name => "Automessage Feed"; public string Name => "Automessage Feed";
public string Version => Utilities.GetVersionAsString();
public float Version => (float)Utilities.GetVersionAsDouble();
public string Author => "RaidMax"; public string Author => "RaidMax";
private int _currentFeedItem; private int _currentFeedItem;
private readonly IConfigurationHandler<Configuration> _configurationHandler; private readonly AutoMessageFeedConfiguration _configuration;
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory) public static void RegisterDependencies(IServiceCollection serviceCollection)
{ {
_configurationHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("AutomessageFeedPluginSettings"); serviceCollection.AddConfiguration<AutoMessageFeedConfiguration>("AutomessageFeedPluginSettings");
}
public Plugin(AutoMessageFeedConfiguration configuration)
{
_configuration = configuration;
if (configuration.EnableFeed)
{
IManagementEventSubscriptions.Load += (manager, _) =>
{
manager.GetMessageTokens().Add(new MessageToken("FEED", GetNextFeedItem));
return Task.CompletedTask;
};
}
} }
private async Task<string> GetNextFeedItem(Server server) private async Task<string> GetNextFeedItem(Server server)
{ {
if (!_configuration.EnableFeed)
{
return null;
}
var items = new List<string>(); var items = new List<string>();
using (var reader = XmlReader.Create(_configurationHandler.Configuration().FeedUrl, new XmlReaderSettings() { Async = true })) using (var reader = XmlReader.Create(_configuration.FeedUrl, new XmlReaderSettings { Async = true }))
{ {
var feedReader = new RssFeedReader(reader); var feedReader = new RssFeedReader(reader);
while (await feedReader.Read()) while (await feedReader.Read())
{ {
switch (feedReader.ElementType) if (feedReader.ElementType != SyndicationElementType.Item)
{ {
case SyndicationElementType.Item: continue;
}
var item = await feedReader.ReadItem(); var item = await feedReader.ReadItem();
items.Add(Regex.Replace(item.Title, @"\<.+\>.*\</.+\>", "")); items.Add(Regex.Replace(item.Title, @"\<.+\>.*\</.+\>", ""));
break;
}
} }
} }
if (_currentFeedItem < items.Count && (_configurationHandler.Configuration().MaxFeedItems == 0 || _currentFeedItem < _configurationHandler.Configuration().MaxFeedItems)) if (_currentFeedItem < items.Count && (_configuration.MaxFeedItems == 0 || _currentFeedItem < _configuration.MaxFeedItems))
{ {
_currentFeedItem++; _currentFeedItem++;
return items[_currentFeedItem - 1]; return items[_currentFeedItem - 1];
@ -57,32 +74,4 @@ namespace AutomessageFeed
_currentFeedItem = 0; _currentFeedItem = 0;
return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_AUTOMESSAGEFEED_NO_ITEMS"]; return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_AUTOMESSAGEFEED_NO_ITEMS"];
} }
public Task OnEventAsync(GameEvent E, Server S)
{
return Task.CompletedTask;
}
public async Task OnLoadAsync(IManager manager)
{
await _configurationHandler.BuildAsync();
if (_configurationHandler.Configuration() == null)
{
_configurationHandler.Set((Configuration)new Configuration().Generate());
await _configurationHandler.Save();
}
manager.GetMessageTokens().Add(new MessageToken("FEED", GetNextFeedItem));
}
public Task OnTickAsync(Server S)
{
throw new NotImplementedException();
}
public Task OnUnloadAsync()
{
return Task.CompletedTask;
}
}
} }