update auto message feed plugin to IPluginV2
This commit is contained in:
parent
8fc47ec6c4
commit
2688790736
17
Plugins/AutomessageFeed/AutoMessageFeedConfiguration.cs
Normal file
17
Plugins/AutomessageFeed/AutoMessageFeedConfiguration.cs
Normal 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";
|
||||
}
|
@ -10,7 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
@ -1,88 +1,77 @@
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SyndicationFeed.Rss;
|
||||
using SharedLibraryCore.Configuration;
|
||||
using System.Xml;
|
||||
using Microsoft.SyndicationFeed;
|
||||
using System.Collections.Generic;
|
||||
using SharedLibraryCore.Helpers;
|
||||
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";
|
||||
|
||||
private int _currentFeedItem;
|
||||
private readonly IConfigurationHandler<Configuration> _configurationHandler;
|
||||
|
||||
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
|
||||
if (configuration.EnableFeed)
|
||||
{
|
||||
_configurationHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("AutomessageFeedPluginSettings");
|
||||
}
|
||||
|
||||
private async Task<string> GetNextFeedItem(Server server)
|
||||
{
|
||||
var items = new List<string>();
|
||||
|
||||
using (var reader = XmlReader.Create(_configurationHandler.Configuration().FeedUrl, new XmlReaderSettings() { Async = true }))
|
||||
IManagementEventSubscriptions.Load += (manager, _) =>
|
||||
{
|
||||
var feedReader = new RssFeedReader(reader);
|
||||
|
||||
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;
|
||||
manager.GetMessageTokens().Add(new MessageToken("FEED", GetNextFeedItem));
|
||||
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"];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user