1 /* 2 * Copyright (c) 2017-2020 sel-project 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in all 12 * copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * SOFTWARE. 21 * 22 */ 23 /** 24 * Copyright: 2017-2020 sel-project 25 * License: MIT 26 * Authors: Kripth 27 * Source: $(HTTP github.com/sel-project/sel-client/sel/client/util.d, sel/client/util.d) 28 */ 29 module sel.client.util; 30 31 import std.conv : to; 32 import std.regex : ctRegex, replaceAll; 33 import std.string : strip; 34 import std.traits : Parameters; 35 36 /** 37 * Server's informations retrieved by a client's ping. 38 */ 39 struct Server { 40 41 bool valid = false; 42 43 string motd; 44 string rawMotd; 45 46 uint protocol; 47 int online, max; 48 49 string favicon; 50 51 ulong ping; 52 53 public this(string motd, uint protocol, int online, int max, ulong ping) { 54 this.valid = true; 55 this.motd = motd.replaceAll(ctRegex!"§[0-9a-fk-or]", "").strip; 56 this.rawMotd = motd; 57 this.protocol = protocol; 58 this.online = online; 59 this.max = max; 60 this.ping = ping; 61 } 62 63 public inout string toString() { 64 if(this.valid) { 65 return "Server(motd: " ~ this.motd ~ ", protocol: " ~ to!string(this.protocol) ~ ", players: " ~ to!string(this.online) ~ "/" ~ to!string(this.max) ~ ", ping: " ~ to!string(this.ping) ~ " ms)"; 66 } else { 67 return "Server()"; 68 } 69 } 70 71 alias valid this; 72 73 } 74 75 interface IHandler { 76 77 public void handle(ubyte[] buffer); 78 79 } 80 81 class Handler(E...) : IHandler { //TODO validate packets 82 83 private E handlers; 84 85 public this(E handlers) { 86 this.handlers = handlers; 87 } 88 89 public override void handle(ubyte[] buffer) { 90 foreach(i, F; E) { 91 static if(is(typeof(Parameters!F[0].ID))) { 92 if(Parameters!F[0].ID == buffer[0]) { 93 this.handlers[i](Parameters!F[0].fromBuffer(buffer)); 94 } 95 } else static if(is(Parameters!F[0] : ubyte[])) { 96 this.handlers[i](buffer); 97 } 98 } 99 } 100 101 } 102 103 Handler!E handler(E...)(E handlers) { 104 return new Handler!E(handlers); 105 }