FIXME: the way this is written, it can't ever do any sort of validation and can accept total junk
| 19 | |
| 20 | // FIXME: the way this is written, it can't ever do any sort of validation and can accept total junk |
| 21 | MinecraftServerTarget MinecraftServerTarget::parse(const QString &fullAddress) { |
| 22 | QStringList split = fullAddress.split(":"); |
| 23 | |
| 24 | // The logic below replicates the exact logic minecraft uses for parsing server addresses. |
| 25 | // While the conversion is not lossless and eats errors, it ensures the same behavior |
| 26 | // within Minecraft and PolyMC when entering server addresses. |
| 27 | if (fullAddress.startsWith("[")) |
| 28 | { |
| 29 | int bracket = fullAddress.indexOf("]"); |
| 30 | if (bracket > 0) |
| 31 | { |
| 32 | QString ipv6 = fullAddress.mid(1, bracket - 1); |
| 33 | QString port = fullAddress.mid(bracket + 1).trimmed(); |
| 34 | |
| 35 | if (port.startsWith(":") && !ipv6.isEmpty()) |
| 36 | { |
| 37 | port = port.mid(1); |
| 38 | split = QStringList({ ipv6, port }); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | split = QStringList({ipv6}); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if (split.size() > 2) |
| 48 | { |
| 49 | split = QStringList({fullAddress}); |
| 50 | } |
| 51 | |
| 52 | QString realAddress = split[0]; |
| 53 | |
| 54 | quint16 realPort = 25565; |
| 55 | if (split.size() > 1) |
| 56 | { |
| 57 | bool ok; |
| 58 | realPort = split[1].toUInt(&ok); |
| 59 | |
| 60 | if (!ok) |
| 61 | { |
| 62 | realPort = 25565; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return MinecraftServerTarget { realAddress, realPort }; |
| 67 | } |