Experimenting with source generators
This commit is contained in:
parent
4d80885168
commit
b4540af9db
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,6 +3,8 @@
|
||||
##
|
||||
## Get latest from `dotnet new gitignore`
|
||||
|
||||
Generated/
|
||||
|
||||
# dotenv files
|
||||
.env
|
||||
|
||||
|
||||
20
Discord.API/DataTypes/ChannelData.cs
Normal file
20
Discord.API/DataTypes/ChannelData.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
public class ChannelData
|
||||
{
|
||||
[JsonRequired]
|
||||
public ulong Id;
|
||||
[JsonRequired]
|
||||
public int Type;
|
||||
public ulong? GuildId;
|
||||
public int? Position;
|
||||
public string? Name;
|
||||
public string? Topic;
|
||||
public bool? Nsfw;
|
||||
public ulong? LastMessageId;
|
||||
public int? Bitrate;
|
||||
public ulong? ParentId;
|
||||
//TODO: Missing fields
|
||||
}
|
||||
12
Discord.API/DataTypes/PartialApplicationData.cs
Normal file
12
Discord.API/DataTypes/PartialApplicationData.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
public sealed class PartialApplicationData
|
||||
{
|
||||
[JsonRequired]
|
||||
public ulong Id;
|
||||
|
||||
[JsonRequired]
|
||||
public ulong Flags;
|
||||
}
|
||||
11
Discord.API/DataTypes/UnavailableGuildData.cs
Normal file
11
Discord.API/DataTypes/UnavailableGuildData.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
public sealed class UnavailableGuildData
|
||||
{
|
||||
[JsonRequired]
|
||||
public ulong Id;
|
||||
[JsonRequired]
|
||||
public bool Unavailable;
|
||||
}
|
||||
16
Discord.API/DataTypes/UserData.cs
Normal file
16
Discord.API/DataTypes/UserData.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
public sealed class UserData
|
||||
{
|
||||
[JsonRequired]
|
||||
public ulong Id;
|
||||
[JsonRequired]
|
||||
public string Username;
|
||||
[JsonRequired]
|
||||
public string Discriminator;
|
||||
public string? GlobalName;
|
||||
//TODO More fields
|
||||
}
|
||||
36
Discord.API/Discord.API.csproj
Normal file
36
Discord.API/Discord.API.csproj
Normal file
@ -0,0 +1,36 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>api</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
|
||||
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Serilog" Version="4.0.0" />
|
||||
<PackageReference Include="Websocket.Client" Version="5.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Generated\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Remove="Generated\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Generated\**" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
[JsonDerivedType(typeof(DispatchPacket), "CHANNEL_CREATE")]
|
||||
public class ChannelCreatePacket : DispatchPacket
|
||||
{
|
||||
[JsonRequired]
|
||||
[JsonPropertyName("d")]
|
||||
public ChannelData Data;
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
[JsonDerivedType(typeof(DispatchPacket), "CHANNEL_DELETE")]
|
||||
public class ChannelDeletePacket : DispatchPacket
|
||||
{
|
||||
[JsonRequired]
|
||||
[JsonPropertyName("d")]
|
||||
public ChannelData Data;
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
[JsonDerivedType(typeof(DispatchPacket), "CHANNEL_UPDATE")]
|
||||
public class ChannelUpdatePacket : DispatchPacket
|
||||
{
|
||||
[JsonRequired]
|
||||
[JsonPropertyName("d")]
|
||||
public ChannelData Data;
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
[JsonDerivedType(typeof(GatewayPacket), (int)Opcode.Dispatch)]
|
||||
[JsonPolymorphic(IgnoreUnrecognizedTypeDiscriminators = true, TypeDiscriminatorPropertyName = "t", UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToBaseType)]
|
||||
public class DispatchPacket : GatewayPacket
|
||||
{
|
||||
[JsonPropertyName("t")]
|
||||
[JsonRequired]
|
||||
public string Event;
|
||||
|
||||
[JsonPropertyName("s")]
|
||||
public ulong? Sequence;
|
||||
}
|
||||
33
Discord.API/GatewayPacketTypes/DispatchPacket/ReadyPacket.cs
Normal file
33
Discord.API/GatewayPacketTypes/DispatchPacket/ReadyPacket.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
[JsonDerivedType(typeof(DispatchPacket), "READY")]
|
||||
public class ReadyPacket : DispatchPacket
|
||||
{
|
||||
public sealed class ReadyData
|
||||
{
|
||||
[JsonPropertyName("v")]
|
||||
public int Version;
|
||||
|
||||
[JsonRequired]
|
||||
public UserData User;
|
||||
|
||||
[JsonRequired]
|
||||
public UnavailableGuildData Guilds;
|
||||
|
||||
[JsonRequired]
|
||||
public string SessionId;
|
||||
|
||||
[JsonRequired]
|
||||
public string ResumeUrl;
|
||||
|
||||
[JsonRequired]
|
||||
public PartialApplicationData Application;
|
||||
}
|
||||
|
||||
[JsonRequired]
|
||||
[JsonPropertyName("d")]
|
||||
public ReadyData Data;
|
||||
}
|
||||
26
Discord.API/GatewayPacketTypes/GatewayPacket.cs
Normal file
26
Discord.API/GatewayPacketTypes/GatewayPacket.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
[JsonPolymorphic(IgnoreUnrecognizedTypeDiscriminators = false, TypeDiscriminatorPropertyName = "op", UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToBaseType)]
|
||||
public class GatewayPacket
|
||||
{
|
||||
public enum Opcode
|
||||
{
|
||||
Dispatch = 0,
|
||||
Heartbeat = 1,
|
||||
Identify = 2,
|
||||
PresenceUpdate = 3,
|
||||
VoiceStateUpdate = 4,
|
||||
Resume = 6,
|
||||
Reconnect = 7,
|
||||
RequestGuildMembers = 8,
|
||||
InvalidSession = 9,
|
||||
Hello = 10,
|
||||
HeartbeatAck = 11
|
||||
}
|
||||
|
||||
[JsonRequired]
|
||||
[JsonPropertyName("op")]
|
||||
public Opcode Op;
|
||||
}
|
||||
36
Discord.API/GatewayPacketTypes/IdentifyPacket.cs
Normal file
36
Discord.API/GatewayPacketTypes/IdentifyPacket.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
//[JsonDerivedType(typeof(GatewayPacket), (int)Opcode.Identify)]
|
||||
public class IdentifyPacket : GatewayPacket
|
||||
{
|
||||
public sealed class IdentifyData
|
||||
{
|
||||
public class PropertiesClass
|
||||
{
|
||||
public string Os => Environment.OSVersion.ToString();
|
||||
public string Browser => StaticProperties.LibraryName;
|
||||
public string Device => StaticProperties.LibraryName;
|
||||
private PropertiesClass() { }
|
||||
public static PropertiesClass Instance { get; } = new PropertiesClass();
|
||||
}
|
||||
|
||||
[JsonInclude]
|
||||
[JsonRequired]
|
||||
[JsonPropertyName("token")]
|
||||
public required string Token { get; set; }
|
||||
[JsonInclude]
|
||||
public PropertiesClass Properties => PropertiesClass.Instance;
|
||||
[JsonInclude]
|
||||
public bool Compress => false;
|
||||
public int LargeThreshold = 250;
|
||||
public int[]? Shard = null;
|
||||
//presence
|
||||
public ulong Intents { get; init; }
|
||||
|
||||
}
|
||||
|
||||
[JsonPropertyName("d")]
|
||||
public IdentifyData Data;
|
||||
}
|
||||
20
Discord.API/IDiscordApi.cs
Normal file
20
Discord.API/IDiscordApi.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace Discord.API;
|
||||
|
||||
public interface IDiscordApi
|
||||
{
|
||||
public void OpenConnection();
|
||||
public void Close();
|
||||
|
||||
public delegate void DiscordEventHandler<in T>(IDiscordApi sender, T data, LifeTime lifetime);
|
||||
|
||||
/*public event DiscordEventHandler<JObject>? OnReady;
|
||||
public event DiscordEventHandler<JObject>? OnChannelCreate;
|
||||
public event DiscordEventHandler<JObject>? OnChannelUpdate;
|
||||
public event DiscordEventHandler<JObject>? OnChannelDelete;
|
||||
|
||||
public event DiscordEventHandler<JObject>? OnGuildCreate;
|
||||
public event DiscordEventHandler<JObject>? OnGuildUpdate;
|
||||
public event DiscordEventHandler<JObject>? OnGuildDelete;*/
|
||||
|
||||
|
||||
}
|
||||
13
Discord.API/LifeTime.cs
Normal file
13
Discord.API/LifeTime.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace Discord.API;
|
||||
|
||||
public class LifeTime
|
||||
{
|
||||
public virtual bool IsValid => true;
|
||||
public virtual bool IsInfinite => true;
|
||||
|
||||
protected LifeTime()
|
||||
{}
|
||||
|
||||
internal static LifeTime Infinite { get; } = new LifeTime();
|
||||
internal static SingleEventLifeTime SingleEvent => new SingleEventLifeTime();
|
||||
}
|
||||
13
Discord.API/SingleEventLifeTime.cs
Normal file
13
Discord.API/SingleEventLifeTime.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace Discord.API;
|
||||
|
||||
public class SingleEventLifeTime : LifeTime
|
||||
{
|
||||
private bool _valid = true;
|
||||
public override bool IsValid => _valid;
|
||||
public override bool IsInfinite => false;
|
||||
|
||||
internal void Terminate()
|
||||
{
|
||||
_valid = false;
|
||||
}
|
||||
}
|
||||
13
Discord.API/SourceGenerationContext.cs
Normal file
13
Discord.API/SourceGenerationContext.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
|
||||
namespace Discord.API;
|
||||
|
||||
using System.Text.Json;
|
||||
|
||||
[JsonSourceGenerationOptions(IgnoreReadOnlyFields = false, IgnoreReadOnlyProperties = false, IncludeFields = true, PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
|
||||
[JsonSerializable(typeof(GatewayPacket))]
|
||||
[JsonSerializable(typeof(IdentifyPacket))]
|
||||
internal partial class SourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
8
Discord.API/StaticProperties.cs
Normal file
8
Discord.API/StaticProperties.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Discord.API;
|
||||
|
||||
public class StaticProperties
|
||||
{
|
||||
public static string LibraryName => "Custom C# Discord Library";
|
||||
public static string LibraryVersion => "0.1 alpha";
|
||||
public static string LibraryWebsite => "https://laci0503.duckdns.org/laci0503/DiscordApi";
|
||||
}
|
||||
@ -3,6 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "model", "model\model.csproj", "{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.API", "Discord.API\Discord.API.csproj", "{5C77661B-670F-400B-AF77-E3EC062B673D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example_bot", "example_bot\example_bot.csproj", "{331F5928-8E65-4782-8311-BC1E80F1C002}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -11,4 +17,18 @@ Global
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5C77661B-670F-400B-AF77-E3EC062B673D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5C77661B-670F-400B-AF77-E3EC062B673D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5C77661B-670F-400B-AF77-E3EC062B673D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5C77661B-670F-400B-AF77-E3EC062B673D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{331F5928-8E65-4782-8311-BC1E80F1C002}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{331F5928-8E65-4782-8311-BC1E80F1C002}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{331F5928-8E65-4782-8311-BC1E80F1C002}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{331F5928-8E65-4782-8311-BC1E80F1C002}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
2
example_bot/Program.cs
Normal file
2
example_bot/Program.cs
Normal file
@ -0,0 +1,2 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
10
example_bot/example_bot.csproj
Normal file
10
example_bot/example_bot.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
6
model/Class1.cs
Normal file
6
model/Class1.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace model;
|
||||
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
9
model/model.csproj
Normal file
9
model/model.csproj
Normal file
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Reference in New Issue
Block a user