~ruther/NosSmooth

ccdf2299f74571291fdc4fd5df5830b2ce085e6d — František Boháček 3 years ago d5ff025
feat: add simple chat sample
M NosSmooth.sln => NosSmooth.sln +15 -0
@@ 34,6 34,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".metadata", ".metadata", "{
		Directory.build.props = Directory.build.props
	EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleChat", "Samples\SimpleChat\SimpleChat.csproj", "{4017A4F4-5E59-48AA-A7D0-A8518148933A}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU


@@ 150,6 152,18 @@ Global
		{945E9248-C150-4617-AB0F-1450561859E3}.Release|x64.Build.0 = Release|Any CPU
		{945E9248-C150-4617-AB0F-1450561859E3}.Release|x86.ActiveCfg = Release|Any CPU
		{945E9248-C150-4617-AB0F-1450561859E3}.Release|x86.Build.0 = Release|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|x64.ActiveCfg = Debug|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|x64.Build.0 = Debug|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|x86.ActiveCfg = Debug|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Debug|x86.Build.0 = Debug|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Release|Any CPU.Build.0 = Release|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Release|x64.ActiveCfg = Release|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Release|x64.Build.0 = Release|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Release|x86.ActiveCfg = Release|Any CPU
		{4017A4F4-5E59-48AA-A7D0-A8518148933A}.Release|x86.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE


@@ 163,6 177,7 @@ Global
		{63E97FF3-7E40-44DE-9E91-F5DEE79AF95F} = {6078AE6E-7CD0-48E4-84E0-EB164D8881DA}
		{27DF38DF-AC58-4039-A91C-824D829ECECD} = {01B5E872-271F-4D30-A1AA-AD48D81840C5}
		{945E9248-C150-4617-AB0F-1450561859E3} = {01B5E872-271F-4D30-A1AA-AD48D81840C5}
		{4017A4F4-5E59-48AA-A7D0-A8518148933A} = {F20FE754-FDEA-4F3A-93D4-0750CB9EBB33}
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {C5F46653-4DEC-429B-8580-4ED18ED9B4CA}

A Samples/SimpleChat/DllMain.cs => Samples/SimpleChat/DllMain.cs +33 -0
@@ 0,0 1,33 @@
//
//  DllMain.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.InteropServices;

namespace SimpleChat;

/// <summary>
/// The main entrypoint class of the dll.
/// </summary>
public class DllMain
{
    [DllImport("kernel32")]
#pragma warning disable SA1600
    public static extern bool AllocConsole();
#pragma warning restore SA1600

    /// <summary>
    /// The main entrypoint method of the dll.
    /// </summary>
    /// <param name="handle">The handle of the module.</param>
    [DllExport]
    public static void Main(IntPtr handle)
    {
        AllocConsole();
        Console.WriteLine("Hello from SimpleChat DllMain entry point.");

        new Thread(() => new SimpleChat().RunAsync().GetAwaiter().GetResult()).Start();
    }
}
\ No newline at end of file

A Samples/SimpleChat/FodyWeavers.xml => Samples/SimpleChat/FodyWeavers.xml +4 -0
@@ 0,0 1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
  <Costura />
</Weavers>
\ No newline at end of file

A Samples/SimpleChat/SayResponder.cs => Samples/SimpleChat/SayResponder.cs +57 -0
@@ 0,0 1,57 @@
//
//  SayResponder.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using NosCore.Packets.Enumerations;
using NosCore.Packets.ServerPackets.Chats;
using NosCore.Packets.ServerPackets.UI;
using NosCore.Shared.Enumerations;
using NosSmooth.Core.Client;
using NosSmooth.Core.Packets;
using Remora.Results;

namespace SimpleChat;

/// <summary>
/// Responds to <see cref="SayPacket"/>.
/// </summary>
public class SayResponder : IPacketResponder<SayPacket>, IPacketResponder<MsgPacket>
{
    private readonly INostaleClient _client;

    /// <summary>
    /// Initializes a new instance of the <see cref="SayResponder"/> class.
    /// </summary>
    /// <param name="client">The nostale client.</param>
    public SayResponder(INostaleClient client)
    {
        _client = client;
    }

    /// <inheritdoc />
    public Task<Result> Respond(SayPacket packet, CancellationToken ct = default)
    {
        return _client.ReceivePacketAsync(
            new SayPacket()
            {
                Message = "Hello world from NosSmooth!", VisualType = VisualType.Player, Type = SayColorType.Red, VisualId = 1,
            },
            ct);
    }

    /// <inheritdoc/>
    public Task<Result> Respond(MsgPacket packet, CancellationToken ct = default)
    {
        return _client.ReceivePacketAsync(
            new SayPacket()
            {
                Message = "Hello world from NosSmooth!",
                VisualType = VisualType.Player,
                Type = SayColorType.Red,
                VisualId = 1,
            },
            ct);
    }
}
\ No newline at end of file

A Samples/SimpleChat/SimpleChat.cs => Samples/SimpleChat/SimpleChat.cs +59 -0
@@ 0,0 1,59 @@
//
//  SimpleChat.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NosCore.Packets.Enumerations;
using NosCore.Packets.ServerPackets.Chats;
using NosCore.Shared.Enumerations;
using NosSmooth.Core.Client;
using NosSmooth.Core.Packets;
using NosSmooth.LocalClient.Extensions;

namespace SimpleChat;

/// <summary>
/// The main simple chat class.
/// </summary>
public class SimpleChat
{
    /// <summary>
    /// Run the client.
    /// </summary>
    /// <returns>The task that may or may not have succeeded.</returns>
    public async Task RunAsync()
    {
        var provider = new ServiceCollection()
            .AddLocalClient()

            // .AddPacketResponder<SayResponder>()
            .AddLogging(b =>
            {
                b.ClearProviders();
                b.AddConsole();
                b.SetMinimumLevel(LogLevel.Debug);
            })
            .BuildServiceProvider();

        var dummy1 = provider.GetRequiredService<PacketSerializerProvider>().ServerSerializer;
        var dummy2 = provider.GetRequiredService<PacketSerializerProvider>().ClientSerializer;

        var logger = provider.GetRequiredService<ILogger<SimpleChat>>();
        logger.LogInformation("Hello world from SimpleChat!");

        var client = provider.GetRequiredService<INostaleClient>();

        await client.ReceivePacketAsync(new SayPacket()
        {
            Message = "Hello world from NosSmooth!",
            VisualType = VisualType.Player,
            Type = SayColorType.Red,
            VisualId = 1,
        });

        await client.RunAsync();
    }
}
\ No newline at end of file

A Samples/SimpleChat/SimpleChat.csproj => Samples/SimpleChat/SimpleChat.csproj +67 -0
@@ 0,0 1,67 @@
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>10</LangVersion>
  </PropertyGroup>
  <PropertyGroup>
    <DllExportIdent>89E4EE92-5848-4390-A6A7-34972FE923F5</DllExportIdent>
    <DllExportMetaLibName>DllExport.dll</DllExportMetaLibName>
    <DllExportNamespace>SimpleChat</DllExportNamespace>
    <DllExportDDNSCecil>true</DllExportDDNSCecil>
    <PlatformTarget>x86</PlatformTarget>
    <DllExportOrdinalsBase>7</DllExportOrdinalsBase>
    <DllExportGenExpLib>false</DllExportGenExpLib>
    <DllExportOurILAsm>false</DllExportOurILAsm>
    <DllExportSysObjRebase>false</DllExportSysObjRebase>
    <DllExportLeaveIntermediateFiles>false</DllExportLeaveIntermediateFiles>
    <DllExportTimeout>30000</DllExportTimeout>
    <DllExportPeCheck>2</DllExportPeCheck>
    <DllExportPatches>0</DllExportPatches>
    <DllExportPreProcType>0</DllExportPreProcType>
    <DllExportPostProcType>0</DllExportPostProcType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Costura.Fody" Version="5.7.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="DllExport">
      <Version>1.7.4</Version>
      <Visible>false</Visible>
      <Wz>1</Wz>
    </PackageReference>
    <PackageReference Include="Fody" Version="6.6.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\Core\NosSmooth.Core\NosSmooth.Core.csproj" />
    <ProjectReference Include="..\..\Local\NosSmooth.LocalClient\NosSmooth.LocalClient.csproj" />
  </ItemGroup>
  <ImportGroup Label=".NET DllExport">
    <Import Project="$(SolutionDir)packages\DllExport.1.7.4\tools\net.r_eg.DllExport.targets" Condition="Exists($([MSBuild]::Escape('$(SolutionDir)packages\DllExport.1.7.4\tools\net.r_eg.DllExport.targets')))" Label="8337224c9ad9e356" />
  </ImportGroup>
  <Target Name="DllExportRestorePkg" BeforeTargets="PrepareForBuild">
    <Error Condition="!Exists('$(SolutionDir)DllExport.bat')" Text="DllExport.bat is not found. Path: '$(SolutionDir)' - https://github.com/3F/DllExport" />
    <Exec Condition="('$(DllExportModImported)' != 'true' Or !Exists('$(SolutionDir)packages\DllExport.1.7.4\tools\net.r_eg.DllExport.targets')) And Exists('$(SolutionDir)DllExport.bat')" Command=".\DllExport.bat  -action Restore" WorkingDirectory="$(SolutionDir)" />
    <MSBuild Condition="'$(DllExportModImported)' != 'true'" Projects="$(SolutionDir)packages\DllExport.1.7.4\tools\net.r_eg.DllExport.targets" Targets="DllExportMetaXBaseTarget" Properties="TargetFramework=$(TargetFramework)">
      <Output TaskParameter="TargetOutputs" PropertyName="DllExportMetaXBase" />
    </MSBuild>
    <ItemGroup>
      <Reference Include="DllExport, PublicKeyToken=8337224c9ad9e356">
        <HintPath>$(SolutionDir)packages\DllExport.1.7.4\gcache\$(DllExportMetaXBase)\$(DllExportNamespace)\$(DllExportMetaLibName)</HintPath>
        <Private>False</Private>
        <SpecificVersion>False</SpecificVersion>
      </Reference>
    </ItemGroup>
  </Target>
  <Target Name="DllExportRPkgDynamicImport" BeforeTargets="PostBuildEvent" DependsOnTargets="GetFrameworkPaths" Condition="'$(DllExportModImported)' != 'true' And '$(DllExportRPkgDyn)' != 'false'">
    <MSBuild BuildInParallel="true" UseResultsCache="true" Projects="$(MSBuildProjectFullPath)" Properties="DllExportRPkgDyn=true" Targets="Build" />
  </Target>
</Project>
\ No newline at end of file

Do not follow this link