Initial Build

Multiarch docker build
Unified logging (replaced some crashes with error logs)
Replace all console writes with log formatted writes.

Changelog: added
This commit is contained in:
Michael Chen 2022-01-12 13:03:30 +01:00
parent 1579430f76
commit d8c1f81023
No known key found for this signature in database
GPG Key ID: 1CBC7AA5671437BB
6 changed files with 202 additions and 72 deletions

View File

@ -15,12 +15,11 @@ public class ConnectedComputer {
} }
private void OnMessage(string message) { private void OnMessage(string message) {
var msg = JsonConvert.DeserializeObject<ReplyMessage>(message); if (JsonConvert.DeserializeObject<ReplyMessage>(message) is not ReplyMessage msg) return;
if (msg is null) throw new InvalidProgramException("Unexpected Message!");
IChunkWaiter? waiter; IChunkWaiter? waiter;
lock (_syncRoot) { lock (_syncRoot) {
if (!_waits.TryGetValue(msg.AnswerId, out waiter)) { if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
Console.WriteLine($"Invalid wait id '{msg.AnswerId}'!"); Program.LogWarning("Socket", $"Invalid wait id '{msg.AnswerId}'!");
return; return;
} }
} }
@ -62,9 +61,16 @@ public class ConnectedComputer {
public void AddChunk(int chunkId, int totalChunks, string value) { public void AddChunk(int chunkId, int totalChunks, string value) {
lock (_syncRoot) { lock (_syncRoot) {
if (_chunks is null) _chunks = new string[totalChunks]; if (_chunks is null) _chunks = new string[totalChunks];
else if (_chunks.Length != totalChunks) throw new InvalidOperationException("Different numbers of chunks in same message ID!"); else if (_chunks.Length != totalChunks) {
Program.LogError(Program.WebSocketSource, new InvalidOperationException("Different numbers of chunks in same message ID!"));
return;
}
ref string? chunk = ref _chunks[chunkId - 1]; // Lua 1-indexed ref string? chunk = ref _chunks[chunkId - 1]; // Lua 1-indexed
chunk = chunk is null ? value : throw new InvalidOperationException($"Chunk with ID {chunkId} was already received!"); if (chunk is not null) {
Program.LogError(Program.WebSocketSource, new InvalidOperationException($"Chunk with ID {chunkId} was already received!"));
return;
}
chunk = value;
} }
if (++_receivedChunks == totalChunks) FinalizeResult(_chunks); if (++_receivedChunks == totalChunks) FinalizeResult(_chunks);
} }
@ -75,13 +81,13 @@ public class ConnectedComputer {
} }
protected int GetFreeId() { protected int GetFreeId() {
int i = 10; var attempts = 0;
while (i-- >= 0) { while (true) {
var id = _rnd.Next(); var id = _rnd.Next();
if (!_waits.ContainsKey(id)) if (!_waits.ContainsKey(id))
return id; return id;
Program.LogWarning(Program.WebSocketSource, $"Could not get a free ID after {++attempts} attempts!");
} }
throw new InvalidOperationException("Could not get a free ID after many attempts!");
} }
protected ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct) { protected ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct) {
@ -285,9 +291,7 @@ public class ModItemId {
if (colon < 0) throw new ArgumentException("Invalid mod item id!", nameof(name)); if (colon < 0) throw new ArgumentException("Invalid mod item id!", nameof(name));
ModName = name[..colon]; ModName = name[..colon];
ModItem = name[(colon + 1)..]; ModItem = name[(colon + 1)..];
#if DEBUG
if (ToString() != name) throw new InvalidProgramException("Bad Parsing!"); if (ToString() != name) throw new InvalidProgramException("Bad Parsing!");
#endif
} }
public override string ToString() => $"{ModName}:{ModItem}"; public override string ToString() => $"{ModName}:{ModItem}";
public string ModName { get; } public string ModName { get; }

View File

@ -3,16 +3,16 @@
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim-amd64 AS build
WORKDIR /src WORKDIR /src
COPY ["MinecraftDiscordBot/MinecraftDiscordBot.csproj", "MinecraftDiscordBot/"] COPY ["MinecraftDiscordBot/MinecraftDiscordBot.csproj", "MinecraftDiscordBot/"]
RUN dotnet restore "MinecraftDiscordBot/MinecraftDiscordBot.csproj" RUN dotnet restore "MinecraftDiscordBot/MinecraftDiscordBot.csproj"
COPY . . COPY . .
WORKDIR "/src/MinecraftDiscordBot" WORKDIR "/src/MinecraftDiscordBot"
RUN dotnet build "MinecraftDiscordBot.csproj" -c Release -o /app/build RUN dotnet build "MinecraftDiscordBot.csproj" -c Release -o /app/build /p:UseAppHost=false
FROM build AS publish FROM build AS publish
RUN dotnet publish "MinecraftDiscordBot.csproj" -c Release -o /app/publish RUN dotnet publish "MinecraftDiscordBot.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final FROM base AS final
WORKDIR /app WORKDIR /app

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
@ -9,10 +9,15 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Discord.Net" Version="3.1.0" /> <PackageReference Include="Discord.Net" Version="3.1.0" />
<PackageReference Include="Fleck" Version="1.2.0" /> <PackageReference Include="Fleck" Version="1.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project> </Project>

View File

@ -6,103 +6,138 @@ using Fleck;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
namespace MinecraftDiscordBot; namespace MinecraftDiscordBot;
public class Program { public class Program : IDisposable {
private const string WebSocketSource = "WebSocket"; public const string WebSocketSource = "WebSocket";
private readonly object _logLock = new(); public const string BotSource = "Bot";
private static readonly object LogLock = new();
private readonly DiscordSocketClient _client = new(new() { private readonly DiscordSocketClient _client = new(new() {
LogLevel = LogSeverity.Verbose LogLevel = LogSeverity.Verbose,
GatewayIntents = GatewayIntents.AllUnprivileged & ~(GatewayIntents.GuildScheduledEvents | GatewayIntents.GuildInvites)
}); });
private readonly WebSocketServer _wssv; private readonly WebSocketServer _wssv;
private readonly BotConfiguration _config; private readonly BotConfiguration _config;
private readonly HashSet<ulong> _whitelistedChannels; private readonly HashSet<ulong> _whitelistedChannels;
private readonly ConcurrentDictionary<Guid, ConnectedComputer> _connections = new(); private readonly ConcurrentDictionary<Guid, ConnectedComputer> _connections = new();
private static readonly char[] WhiteSpace = new char[] { '\t', '\n', ' ', '\r' }; private static readonly char[] WhiteSpace = new char[] { '\t', '\n', ' ', '\r' };
public ITextChannel[] _channels = Array.Empty<ITextChannel>();
private RefinedStorageComputer? _rsSystem = null; private RefinedStorageComputer? _rsSystem = null;
private bool disposedValue;
public RefinedStorageComputer? RsSystem {
get => _rsSystem; set {
if (_rsSystem != value) {
_rsSystem = value;
_ = Task.Run(() => Broadcast(i => i.SendMessageAsync(value is null
? $"The Refined Storage went offline. Please check the server!"
: $"The Refined Storage is back online!")));
}
}
}
private async Task Broadcast(Func<ITextChannel, Task<IUserMessage>> message) => _ = await Task.WhenAll(_channels.Select(message));
public Program(BotConfiguration config) { public Program(BotConfiguration config) {
_config = config; _config = config;
_client.Log += Log; _client.Log += LogAsync;
_client.MessageReceived += (msg) => DiscordMessageReceived(msg); _client.MessageReceived += (msg) => DiscordMessageReceived(msg);
_wssv = new WebSocketServer($"ws://0.0.0.0:{config.Port}") { _wssv = new WebSocketServer($"ws://0.0.0.0:{config.Port}") {
RestartAfterListenError = true RestartAfterListenError = true
}; };
FleckLog.LogAction = LogWebSocket;
_whitelistedChannels = config.Channels.ToHashSet(); _whitelistedChannels = config.Channels.ToHashSet();
} }
private void LogWebSocket(LogLevel level, string message, Exception exception) => Log(new(level switch {
LogLevel.Debug => LogSeverity.Debug,
LogLevel.Info => LogSeverity.Info,
LogLevel.Warn => LogSeverity.Warning,
LogLevel.Error => LogSeverity.Error,
_ => LogSeverity.Critical // Unknown logging states should behave critical
}, WebSocketSource, message, exception));
public static Task<int> Main(string[] args) public static Task<int> Main(string[] args)
=> JsonConvert.DeserializeObject<BotConfiguration>(File.ReadAllText("config.json")) is BotConfiguration config => JsonConvert.DeserializeObject<BotConfiguration>(File.ReadAllText("config.json")) is BotConfiguration config
? new Program(config).RunAsync() ? new Program(config).RunAsync()
: throw new InvalidProgramException("Configuration file missing!"); : throw new InvalidProgramException("Configuration file missing!");
public async Task<int> RunAsync() { public async Task<int> RunAsync() {
_wssv.Start(socket => {
socket.OnOpen = async () => await SocketOpened(socket);
socket.OnClose = async () => await SocketClosed(socket);
socket.OnMessage = async message => await SocketReceived(socket, message);
});
await _client.LoginAsync(TokenType.Bot, _config.Token); await _client.LoginAsync(TokenType.Bot, _config.Token);
await _client.StartAsync(); await _client.StartAsync();
#if !DEBUG
await VerifyTextChannels(); await VerifyTextChannels();
#endif StartWebSocketServer();
// Block this task until the program is closed. // Block this task until the program is closed.
await Task.Delay(-1); await Task.Delay(-1);
return 0; return 0;
} }
private async Task VerifyTextChannels() { private void StartWebSocketServer() => _wssv.Start(socket => {
var channels = await Task.WhenAll(_whitelistedChannels.Select(id => _client.GetChannelAsync(id).AsTask()).ToArray()); socket.OnOpen = async () => await SocketOpened(socket);
await Task.WhenAll(channels.Where(i => i is ITextChannel { Guild: RestGuild }).Select(i => ((RestGuild)((ITextChannel)i).Guild).UpdateAsync())); socket.OnClose = async () => await SocketClosed(socket);
foreach (var channel in channels) { socket.OnMessage = async message => await SocketReceived(socket, message);
if (channel is ITextChannel tchannel) Console.WriteLine($"Whitelisted in channel: {channel.Name} [{channel.Id}] on server {tchannel.Guild.Name} [{tchannel.Guild.Id}]"); });
else throw new InvalidProgramException($"Cannot use this bot on non-text channel {channel.Name} [{channel.Id}]!"); private async IAsyncEnumerable<ITextChannel> GetValidChannels(IEnumerable<ulong> ids) {
foreach (var channelId in ids) {
var channel = await _client.GetChannelAsync(channelId);
if (channel is not ITextChannel textChannel) {
if (channel is null) await LogWarning(BotSource, $"Channel with id [{channelId}] does not exist!");
else await LogWarning(BotSource, $"Channel is not a text channels and will not be used: {channel.Name} [{channel.Id}]!");
continue;
}
if (textChannel.Guild is RestGuild guild) {
await guild.UpdateAsync();
await LogInfo(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on server {guild.Name} [{guild.Id}]");
} else {
await LogWarning(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on unknown server!");
}
yield return textChannel;
} }
} }
private async Task SocketReceived(IWebSocketConnection socket, string message) { private async Task VerifyTextChannels() => _channels = await GetValidChannels(_whitelistedChannels).ToArrayAsync();
var capability = JsonConvert.DeserializeObject<CapabilityMessage>(message);
if (capability is null) return; private async Task SocketReceived(IWebSocketConnection socket, string message) {
var pc = capability.Role switch { if (JsonConvert.DeserializeObject<CapabilityMessage>(message) is not CapabilityMessage capability) return;
RefinedStorageComputer.Role => new RefinedStorageComputer(socket),
string role => throw new ArgumentException($"Invalid role '{role}'!") try {
}; var pc = capability.Role switch {
AddComputerSocket(socket, pc); RefinedStorageComputer.Role => new RefinedStorageComputer(socket),
await Log(new LogMessage(LogSeverity.Info, WebSocketSource, $"[{socket.ConnectionInfo.Id}] Presented capability as {pc.GetType().Name}")).ConfigureAwait(false); string role => throw new ArgumentException($"Invalid role '{role}'!")
};
AddComputerSocket(socket, pc);
await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Presented capability as {pc.GetType().Name}");
} catch (ArgumentException e) {
await LogError(WebSocketSource, e);
}
} }
private void AddComputerSocket(IWebSocketConnection socket, RefinedStorageComputer pc) { private void AddComputerSocket(IWebSocketConnection socket, ConnectedComputer pc) {
_connections[socket.ConnectionInfo.Id] = pc; if (pc is RefinedStorageComputer rs) RsSystem = rs;
if (pc is not null) _rsSystem = pc;
} }
private void RemoveComputerSocket(IWebSocketConnection socket) { private void RemoveComputerSocket(IWebSocketConnection socket) {
if (!_connections.TryRemove(socket.ConnectionInfo.Id, out _)) if (RsSystem?.ConnectionInfo.Id == socket.ConnectionInfo.Id) RsSystem = null;
throw new InvalidProgramException("Could not remove non-existing client!");
if (_rsSystem?.ConnectionInfo.Id == socket.ConnectionInfo.Id) _rsSystem = null;
} }
private async Task SocketClosed(IWebSocketConnection socket) { private async Task SocketClosed(IWebSocketConnection socket) {
RemoveComputerSocket(socket); RemoveComputerSocket(socket);
await Log(new LogMessage(LogSeverity.Info, WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client disconnected!")).ConfigureAwait(false); await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client disconnected!");
} }
private async Task SocketOpened(IWebSocketConnection socket) => await Log(new LogMessage(LogSeverity.Info, WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client connected from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}!")).ConfigureAwait(false); private static async Task SocketOpened(IWebSocketConnection socket)
=> await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client connected from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}!");
private async Task DiscordMessageReceived(SocketMessage arg, int timeout = 10000) { private async Task DiscordMessageReceived(SocketMessage arg, int timeout = 10000) {
if (arg is not SocketUserMessage message) return; if (arg is not SocketUserMessage message) return;
if (message.Author.IsBot) return; if (message.Author.IsBot) return;
if (!IsChannelWhitelisted(arg.Channel)) return; if (!IsChannelWhitelisted(arg.Channel)) return;
var cts = new CancellationTokenSource(timeout var cts = new CancellationTokenSource(timeout);
#if DEBUG
* 1000
#endif
);
if (IsCommand(message, out var argPos)) { if (IsCommand(message, out var argPos)) {
var parameters = message.Content[argPos..].Split(WhiteSpace, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); var parameters = message.Content[argPos..].Split(WhiteSpace, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
@ -110,8 +145,8 @@ public class Program {
return; return;
} }
await Log(new LogMessage(LogSeverity.Info, "Discord", $"[{arg.Author.Username}] {arg.Content}")).ConfigureAwait(false); await LogInfo("Discord", $"[{arg.Author.Username}] {arg.Content}");
await SendToAll(JsonConvert.SerializeObject(new TextMessage(arg))); // TODO: Relay Message to Chat Receiver
} }
private Task HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct) private Task HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct)
@ -123,9 +158,9 @@ public class Program {
: message.ReplyAsync($"You really think an empty command works?"); : message.ReplyAsync($"You really think an empty command works?");
private Task HandleRefinedStorageCommand(SocketUserMessage message, string[] parameters, CancellationToken ct) private Task HandleRefinedStorageCommand(SocketUserMessage message, string[] parameters, CancellationToken ct)
=> _rsSystem is null => RsSystem is RefinedStorageComputer rs
? message.ReplyAsync("The Refined Storage system is currently unavailable!") ? rs.HandleCommand(message, parameters, ct)
: _rsSystem.HandleCommand(message, parameters, ct); : message.ReplyAsync("The Refined Storage system is currently unavailable!");
private bool IsCommand(SocketUserMessage message, out int argPos) { private bool IsCommand(SocketUserMessage message, out int argPos) {
argPos = 0; argPos = 0;
@ -134,20 +169,44 @@ public class Program {
private bool IsChannelWhitelisted(ISocketMessageChannel channel) private bool IsChannelWhitelisted(ISocketMessageChannel channel)
=> _whitelistedChannels.Contains(channel.Id); => _whitelistedChannels.Contains(channel.Id);
private async Task SendToAll(string message) { public static ConfiguredTaskAwaitable LogInfo(string source, string message) => LogAsync(new(LogSeverity.Info, source, message)).ConfigureAwait(false);
async Task SendToClient(KeyValuePair<Guid, ConnectedComputer> cp) { public static ConfiguredTaskAwaitable LogWarning(string source, string message) => LogAsync(new(LogSeverity.Warning, source, message)).ConfigureAwait(false);
try { public static ConfiguredTaskAwaitable LogError(string source, Exception exception) => LogAsync(new(LogSeverity.Error, source, exception?.Message, exception)).ConfigureAwait(false);
await cp.Value.Send(message);
} catch (Exception e) {
await Log(new LogMessage(LogSeverity.Warning, WebSocketSource, $"[{cp.Key}] Sending message failed!", e)).ConfigureAwait(false);
}
}
await Task.WhenAll(_connections.Select(SendToClient).ToArray());
}
private async Task Log(LogMessage msg) { private static async Task LogAsync(LogMessage msg) {
lock (_logLock) Log(msg);
Console.WriteLine(msg.ToString());
await Task.CompletedTask; await Task.CompletedTask;
} }
private static void Log(LogMessage msg) {
lock (LogLock)
Console.WriteLine(msg.ToString());
}
protected virtual void Dispose(bool disposing) {
if (!disposedValue) {
if (disposing) {
// TODO: dispose managed state (managed objects)
_wssv.Dispose();
_client.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~Program()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose() {
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
} }

34
build.py Normal file
View File

@ -0,0 +1,34 @@
#!python
import subprocess
import argparse
from itertools import chain
dockercmd = 'docker'
parser = argparse.ArgumentParser(description='Create custom recumock images.')
parser.add_argument('tags', metavar='TAG', nargs='+', help='Version tags to build.')
args = parser.parse_args()
platforms = ['linux/amd64', 'linux/arm64', 'linux/arm/v7']
def pull(image):
subprocess.run([dockercmd, 'pull', baseimage], check=True)
def build(image, directory, platforms, build_args = None):
if build_args is None:
build_args = []
build_args = list(chain.from_iterable(['--build-arg', f'{arg}={val}'] for (arg, val) in build_args))
platformlist = ','.join(platforms)
subprocess.run([dockercmd, 'buildx', 'build', '-f', 'MinecraftDiscordBot/Dockerfile', '--platform', platformlist, '-t', image] + build_args + ['--push', directory], check=True)
for tag in args.tags:
targetimage = f'chenio/mcdiscordbot:{tag}'
baseimage = f'mcr.microsoft.com/dotnet/runtime:6.0'
#print(f'Pulling base image {baseimage}')
#pull(baseimage)
print(f'Building image {targetimage} from {baseimage}.')
build(targetimage, '.', platforms, [('TAG', tag)])

28
checkcompat.py Normal file
View File

@ -0,0 +1,28 @@
#!python
import subprocess
import argparse
parser = argparse.ArgumentParser(description='Check platform compatibility with Python.')
parser.add_argument('tag', metavar='TAG', help='Version tag to build.')
args = parser.parse_args()
tag = args.tag
sourcetag = tag
baseimage = f'mcr.microsoft.com/dotnet/runtime:6.0'
targetimage = f'chenio/mcdiscordbot:{tag}'
platforms = ['linux/amd64', 'linux/arm64', 'linux/riscv64', 'linux/ppc64le', 'linux/s390x', 'linux/386', 'linux/mips64le', 'linux/mips64', 'linux/arm/v7', 'linux/arm/v6']
compatible_archs = []
print(f'Pulling base image {baseimage}')
subprocess.run(['docker', 'pull', baseimage], check=True)
for platform in platforms:
print(f'Try building image {targetimage} for architecture {platform}.')
proc = subprocess.run(['docker', 'buildx', 'build', '-f', 'MinecraftDiscordBot/Dockerfile', '--platform', platform, '-t', targetimage, '.'])
if proc.returncode == 0:
compatible_archs.append(platform)
print(f'Successful platforms for {baseimage}:')
for platform in compatible_archs:
print(f'\t- {platform}')