Refactor folder stucture

This commit is contained in:
BlossomiShymae
2024-08-18 20:08:25 -05:00
parent baf189e6a9
commit d26180dce5
17 changed files with 51 additions and 133 deletions

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace Needlework.Net.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSingletonsFromAssemblies<T>(this ServiceCollection services)
{
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => !p.IsAbstract && typeof(T).IsAssignableFrom(p));
foreach (var type in types) services.AddSingleton(typeof(T), type);
return services;
}
}
}

View File

@@ -1,9 +1,9 @@
using CommunityToolkit.Mvvm.Messaging.Messages;
using Needlework.Net.Core;
using Needlework.Net.Models;
namespace Needlework.Net.Messages
{
public class DataReadyMessage(LcuSchemaHandler handler) : ValueChangedMessage<LcuSchemaHandler>(handler)
public class DataReadyMessage(OpenApiDocumentWrapper wrapper) : ValueChangedMessage<OpenApiDocumentWrapper>(wrapper)
{
}
}

View File

@@ -1,9 +1,9 @@
using CommunityToolkit.Mvvm.Messaging.Messages;
using Needlework.Net.Core;
using Needlework.Net.Models;
namespace Needlework.Net.Messages
{
public class DataRequestMessage : RequestMessage<LcuSchemaHandler>
public class DataRequestMessage : RequestMessage<OpenApiDocumentWrapper>
{
}
}

View File

@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
namespace Needlework.Net
namespace Needlework.Net.Models
{
public class GithubRelease
{

View File

@@ -0,0 +1,71 @@
using System.Collections.Generic;
using Microsoft.OpenApi.Models;
namespace Needlework.Net.Models;
public class OpenApiDocumentWrapper
{
internal OpenApiDocument OpenApiDocument { get; }
public SortedDictionary<string, List<PathOperation>> Plugins { get; }
public OpenApiInfo Info => OpenApiDocument.Info;
public List<string> Paths => [.. OpenApiDocument.Paths.Keys];
public OpenApiDocumentWrapper(OpenApiDocument openApiDocument)
{
OpenApiDocument = openApiDocument;
var plugins = new SortedDictionary<string, List<PathOperation>>();
foreach ((var path, var pathItem) in openApiDocument.Paths)
{
foreach ((var method, var operation) in pathItem.Operations)
{
var operations = new List<PathOperation>();
var pluginsKey = "_unknown";
// Process and group endpoints into the following formats:
// "_unknown" - group that should not be possible
// "default" - no tags
// "builtin" - 'builtin' not associated with an endpoint
// "lol-summoner" etc. - 'plugin' associated with an endpoint
// "performance", "tracing", etc.
if (operation.Tags.Count == 0)
{
pluginsKey = "default";
if (plugins.TryGetValue(pluginsKey, out var p))
p.Add(new(method.ToString(), path, operation));
else
{
operations.Add(new(method.ToString(), path, operation));
plugins[pluginsKey] = operations;
}
}
else
{
foreach (var tag in operation.Tags)
{
var lowercaseTag = tag.Name.ToLower();
if (lowercaseTag == "plugins")
continue;
else if (lowercaseTag.Contains("plugin "))
pluginsKey = lowercaseTag.Replace("plugin ", "");
else
pluginsKey = lowercaseTag;
if (plugins.TryGetValue(pluginsKey, out var p))
p.Add(new(method.ToString(), path, operation));
else
{
operations.Add(new(method.ToString(), path, operation));
plugins[pluginsKey] = operations;
}
}
}
}
}
Plugins = plugins;
}
}

View File

@@ -0,0 +1,5 @@
using Microsoft.OpenApi.Models;
namespace Needlework.Net.Models;
public record PathOperation(string Method, string Path, OpenApiOperation Operation);

View File

@@ -0,0 +1,23 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
namespace Needlework.Net.Models;
public static class Resources
{
/// <summary>
/// Get the OpenApi document of the LCU schema. Provided by dysolix.
/// </summary>
/// <param name="httpClient"></param>
/// <returns></returns>
public static async Task<OpenApiDocument> GetOpenApiDocumentAsync(HttpClient httpClient)
{
var stream = await httpClient.GetStreamAsync("https://raw.githubusercontent.com/dysolix/hasagi-types/main/swagger.json");
var document = new OpenApiStreamReader().Read(stream, out var _);
return document;
}
}

View File

@@ -33,6 +33,8 @@
<PackageReference Include="Material.Icons.Avalonia" Version="2.1.10" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.17" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.17" />
<PackageReference Include="Projektanker.Icons.Avalonia" Version="9.4.0" />
<PackageReference Include="Projektanker.Icons.Avalonia.FontAwesome" Version="9.4.0" />
<PackageReference Include="TextMateSharp.Grammars" Version="1.0.62" />
@@ -42,10 +44,6 @@
<AvaloniaResource Include="Assets\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Needlework.Net.Core\Needlework.Net.Core.csproj" />
</ItemGroup>
<ItemGroup>
<UpToDateCheckInput Remove="Views\AboutView.axaml" />
</ItemGroup>

View File

@@ -1,5 +1,6 @@
using Avalonia;
using Microsoft.Extensions.DependencyInjection;
using Needlework.Net.Extensions;
using Needlework.Net.Services;
using Needlework.Net.ViewModels;
using Projektanker.Icons.Avalonia;
@@ -36,13 +37,8 @@ class Program
builder.AddSingleton<MainWindowViewModel>();
builder.AddSingleton<WindowService>();
// Dynamically add ViewModels
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => !p.IsAbstract && typeof(PageBase).IsAssignableFrom(p));
foreach (var type in types)
builder.AddSingleton(typeof(PageBase), type);
builder.AddSingletonsFromAssemblies<PageBase>();
builder.AddHttpClient();
var services = builder.BuildServiceProvider();

View File

@@ -3,7 +3,6 @@ using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Needlework.Net.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Needlework.Net.ViewModels

View File

@@ -4,8 +4,8 @@ using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using FluentAvalonia.UI.Controls;
using Microsoft.OpenApi.Models;
using Needlework.Net.Core;
using Needlework.Net.Messages;
using Needlework.Net.Models;
using Needlework.Net.Services;
using System;
using System.Collections.Generic;
@@ -32,7 +32,7 @@ namespace Needlework.Net.ViewModels
public HttpClient HttpClient { get; }
public WindowService WindowService { get; }
public LcuSchemaHandler? LcuSchemaHandler { get; set; }
public OpenApiDocumentWrapper? OpenApiDocumentWrapper { get; set; }
public OpenApiDocument? HostDocument { get; set; }
[ObservableProperty] private bool _isBusy = true;
@@ -105,8 +105,8 @@ namespace Needlework.Net.ViewModels
{
var document = await Resources.GetOpenApiDocumentAsync(HttpClient);
HostDocument = document;
var handler = new LcuSchemaHandler(document);
LcuSchemaHandler = handler;
var handler = new OpenApiDocumentWrapper(document);
OpenApiDocumentWrapper = handler;
WeakReferenceMessenger.Default.Send(new DataReadyMessage(handler));
IsBusy = false;
@@ -114,7 +114,7 @@ namespace Needlework.Net.ViewModels
public void Receive(DataRequestMessage message)
{
message.Reply(LcuSchemaHandler!);
message.Reply(OpenApiDocumentWrapper!);
}
public void Receive(HostDocumentRequestMessage message)

View File

@@ -3,8 +3,8 @@ using BlossomiShymae.GrrrLCU;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Needlework.Net.Core;
using Needlework.Net.Messages;
using Needlework.Net.Models;
using System;
using System.Net.Http;
using System.Text;