Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the port optional #239

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 60 additions & 7 deletions CSharp/MineStat/MineStat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Threading.Tasks;
using System.Net;

#if NETSTANDARD2_0
smellilac marked this conversation as resolved.
Show resolved Hide resolved
using DnsClient;
#elif NET46
smellilac marked this conversation as resolved.
Show resolved Hide resolved
using ARSoft.Tools.Net;
using ARSoft.Tools.Net.Dns;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we want to handle all other cases with an #else here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how #else can be used logically here. The only thing that comes to mind is a construction like this:

#else
#error This target framework is not supported by this code. Please ensure the target is .NET Standard 2.0 or greater, or .NET Framework 4.6 or greater.


namespace MineStatLib
{
Expand Down Expand Up @@ -158,10 +167,10 @@ public class MineStat
/// <param name="port">Port to connect to on the address</param>
/// <param name="timeout">(Optional) Timeout in seconds</param>
/// <param name="protocol">(Optional) SLP protocol to use, defaults to automatic detection</param>
public MineStat(string address, ushort port, int timeout = DefaultTimeout, SlpProtocol protocol = SlpProtocol.Automatic)
public MineStat(string address, ushort? port = null, int timeout = DefaultTimeout, SlpProtocol protocol = SlpProtocol.Automatic)
{
Address = address;
Port = port;
Port = port ?? GetDefaultPort(address, timeout).GetAwaiter().GetResult();
Timeout = timeout;

// If the user manually selected a protocol, use that
Expand Down Expand Up @@ -231,11 +240,55 @@ public MineStat(string address, ushort port, int timeout = DefaultTimeout, SlpPr
}
}

/// <summary>
/// Function for stripping all formatting codes from a motd.
/// </summary>
/// <returns>string with the stripped motd</returns>
static private string strip_motd_formatting(string rawmotd)
private async Task<ushort> GetDefaultPort(string address, int timeout)
{
try
{
#if NETSTANDARD2_0
var lookup = new LookupClient();
var result = await lookup.QueryAsync($"_minecraft._tcp.{address}", QueryType.SRV);

var srvRecords = result.Answers.SrvRecords();

if (srvRecords.Any())
{
var srvRecord = result.Answers.SrvRecords().First();
return (ushort)srvRecord.Port;
}
#elif NET46
var dnsClient = new DnsClient(IPAddress.Parse(address), timeout);


var dnsMessage = await Task<DnsMessage>.Factory.FromAsync(
dnsClient.BeginResolve,
dnsClient.EndResolve,
$"_minecraft._tcp.{address}",
RecordType.Srv,
RecordClass.INet,
null);

var srvRecords = dnsMessage.AnswerRecords.OfType<SrvRecord>();

if (srvRecords.Any())
{
var srvRecord = srvRecords.First();
return (ushort)srvRecord.Port;
}
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any conditions that should be covered under an #else?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I would not use #else, as I already suggested adding it above. That way, if the wrong framework is used. The compiler will not even get to this code.

}
catch (Exception)
{
return 25565;
}

return 25565;
}

/// <summary>
/// Function for stripping all formatting codes from a motd.
/// </summary>
/// <returns>string with the stripped motd</returns>
static private string strip_motd_formatting(string rawmotd)
{
return Regex.Replace(rawmotd, @"\u00A7+[a-zA-Z0-9]", string.Empty);
}
Expand Down
89 changes: 50 additions & 39 deletions CSharp/MineStat/MineStat.csproj
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- General Information about an assembly is controlled through the following set of attributes. Change these attribute values to modify the information associated with an assembly. -->
<AssemblyName>MineStat</AssemblyName>
<Description>A Minecraft server status checker</Description>
<Company>Frag Land</Company>
<Product>MineStat</Product>
<Copyright>Copyright © 2014-2023 Lloyd Dilley, 2021-2023 Felix Ern (MindSolve), 2022-2023 Ajoro</Copyright>
<Authors>Lloyd Dilley, Felix Ern, Ajoro</Authors>
<PropertyGroup>
<!-- General Information about an assembly is controlled through the following set of attributes. Change these attribute values to modify the information associated with an assembly. -->
<AssemblyName>MineStat</AssemblyName>
<Description>A Minecraft server status checker</Description>
<Company>Frag Land</Company>
<Product>MineStat</Product>
<Copyright>Copyright © 2014-2023 Lloyd Dilley, 2021-2023 Felix Ern (MindSolve), 2022-2023 Ajoro</Copyright>
<Authors>Lloyd Dilley, Felix Ern, Ajoro</Authors>

<!--The following GUID is for the ID of the typelib if this project is exposed to COM-->
<ProjectGuid>{11EDD760-FC1E-4BC0-8ED4-8F6D8FF97779}</ProjectGuid>
<!--The following GUID is for the ID of the typelib if this project is exposed to COM-->
<ProjectGuid>{11EDD760-FC1E-4BC0-8ED4-8F6D8FF97779}</ProjectGuid>

<!-- Version information for an assembly consists of the following four values: Major.Minor.Build.Revision -->
<AssemblyVersion>3.1.2.0</AssemblyVersion>
<FileVersion>3.1.2.0</FileVersion>
<Version>3.1.2.0</Version>
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>net46;netstandard20</TargetFrameworks>
<Deterministic>true</Deterministic>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\MineStat.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\MineStat.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard20'">
<PackageReference Include="DnsClient" Version="1.3.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net46'">
<PackageReference Include="ARSoft.Tools.Net" Version="1.4.0" />
</ItemGroup>

<!-- Version information for an assembly consists of the following four values: Major.Minor.Build.Revision -->
<AssemblyVersion>3.1.2.0</AssemblyVersion>
<FileVersion>3.1.2.0</FileVersion>
<Version>3.1.2.0</Version>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net46;netstandard20</TargetFrameworks>
<Deterministic>true</Deterministic>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\MineStat.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\MineStat.xml</DocumentationFile>
</PropertyGroup>
</Project>