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,88 +1,77 @@
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 : IPluginV2
{ {
public class Plugin : IPlugin public string Name => "Automessage Feed";
public string Version => Utilities.GetVersionAsString();
public string Author => "RaidMax";
private int _currentFeedItem;
private readonly AutoMessageFeedConfiguration _configuration;
public static void RegisterDependencies(IServiceCollection serviceCollection)
{ {
public string Name => "Automessage Feed"; serviceCollection.AddConfiguration<AutoMessageFeedConfiguration>("AutomessageFeedPluginSettings");
}
public float Version => (float)Utilities.GetVersionAsDouble(); public Plugin(AutoMessageFeedConfiguration configuration)
{
_configuration = configuration;
public string Author => "RaidMax"; if (configuration.EnableFeed)
private int _currentFeedItem;
private readonly IConfigurationHandler<Configuration> _configurationHandler;
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
{ {
_configurationHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("AutomessageFeedPluginSettings"); IManagementEventSubscriptions.Load += (manager, _) =>
}
private async Task<string> GetNextFeedItem(Server server)
{
var items = new List<string>();
using (var reader = XmlReader.Create(_configurationHandler.Configuration().FeedUrl, new XmlReaderSettings() { Async = true }))
{ {
var feedReader = new RssFeedReader(reader); manager.GetMessageTokens().Add(new MessageToken("FEED", GetNextFeedItem));
return Task.CompletedTask;
while (await feedReader.Read()) };
{
switch (feedReader.ElementType)
{
case SyndicationElementType.Item:
var item = await feedReader.ReadItem();
items.Add(Regex.Replace(item.Title, @"\<.+\>.*\</.+\>", ""));
break;
}
}
}
if (_currentFeedItem < items.Count && (_configurationHandler.Configuration().MaxFeedItems == 0 || _currentFeedItem < _configurationHandler.Configuration().MaxFeedItems))
{
_currentFeedItem++;
return items[_currentFeedItem - 1];
}
_currentFeedItem = 0;
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;
} }
} }
private async Task<string> GetNextFeedItem(Server server)
{
if (!_configuration.EnableFeed)
{
return null;
}
var items = new List<string>();
using (var reader = XmlReader.Create(_configuration.FeedUrl, new XmlReaderSettings { Async = true }))
{
var feedReader = new RssFeedReader(reader);
while (await feedReader.Read())
{
if (feedReader.ElementType != SyndicationElementType.Item)
{
continue;
}
var item = await feedReader.ReadItem();
items.Add(Regex.Replace(item.Title, @"\<.+\>.*\</.+\>", ""));
}
}
if (_currentFeedItem < items.Count && (_configuration.MaxFeedItems == 0 || _currentFeedItem < _configuration.MaxFeedItems))
{
_currentFeedItem++;
return items[_currentFeedItem - 1];
}
_currentFeedItem = 0;
return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_AUTOMESSAGEFEED_NO_ITEMS"];
}
} }