From be6af511bd6cf1d57fc56bca136119e1240bbde7 Mon Sep 17 00:00:00 2001 From: izzy Date: Sat, 7 Mar 2026 15:00:07 -0500 Subject: [PATCH] feat: cleanup exceptions a bit --- Minecraft.Client/PlayerConnection.cpp | 15 +++++++++++++++ Minecraft.World/Exceptions.h | 21 +++++++++++---------- Minecraft.World/Packet.cpp | 23 ++++++++--------------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/Minecraft.Client/PlayerConnection.cpp b/Minecraft.Client/PlayerConnection.cpp index 9404a5d68..cd19a1ef7 100644 --- a/Minecraft.Client/PlayerConnection.cpp +++ b/Minecraft.Client/PlayerConnection.cpp @@ -35,6 +35,9 @@ #include "..\Minecraft.World\net.minecraft.world.item.crafting.h" #include "Options.h" +// MinecraftConsoles Added +#include "..\Minecraft.World\Exceptions.h" + Random PlayerConnection::random; PlayerConnection::PlayerConnection(MinecraftServer *server, Connection *connection, shared_ptr player) @@ -88,6 +91,18 @@ void PlayerConnection::tick() didTick = false; tickCount++; + + try + { + connection->tick(); + } + catch (const IOException& e) + { + app.DebugPrintF("IOException -- %ls\n", e.information); + disconnect(DisconnectPacket::eDisconnect_None); + return; + } + connection->tick(); if(done) return; diff --git a/Minecraft.World/Exceptions.h b/Minecraft.World/Exceptions.h index 6f3ba75e6..df113ce90 100644 --- a/Minecraft.World/Exceptions.h +++ b/Minecraft.World/Exceptions.h @@ -1,29 +1,30 @@ #pragma once -using namespace std; -class EOFException : public std::exception +// izzint - TODO: these other exceptions should really be implemented + +class EOFException : public std::runtime_error { }; -class IllegalArgumentException : public std::exception +class IllegalArgumentException : public std::runtime_error { public: - wstring information; + std::wstring information; - IllegalArgumentException(const wstring& information); + IllegalArgumentException(const std::wstring& information); }; -class IOException : public std::exception +class IOException : public std::runtime_error { public: - wstring information; + std::wstring information; - IOException(const wstring& information); + IOException(const std::wstring& information); }; -class RuntimeException : public std::exception +class RuntimeException : public std::runtime_error { public: - RuntimeException(const wstring& information); + RuntimeException(const std::wstring& information); }; \ No newline at end of file diff --git a/Minecraft.World/Packet.cpp b/Minecraft.World/Packet.cpp index 61e3cc63c..0a080f0cf 100644 --- a/Minecraft.World/Packet.cpp +++ b/Minecraft.World/Packet.cpp @@ -157,9 +157,9 @@ IllegalArgumentException::IllegalArgumentException(const wstring& information) this->information = information; } -IOException::IOException(const wstring& information) +IOException::IOException(const wstring &info) : std::runtime_error("IOException") { - this->information = information; + this->information = info; } Packet::Packet() : createTime( System::currentTimeMillis() ) @@ -379,12 +379,10 @@ void Packet::writePacket(shared_ptr packet, DataOutputStream *dos) // th void Packet::writeUtf(const wstring& value, DataOutputStream *dos) // throws IOException TODO 4J JEV, should this declare a throws? { -#if 0 if (value.length() > Short::MAX_VALUE) { - throw new IOException(L"String too big"); + throw IOException(L"Packet::writeUtf - String too big"); } -#endif dos->writeShort((short)value.length()); dos->writeChars(value); @@ -394,20 +392,15 @@ wstring Packet::readUtf(DataInputStream *dis, int maxLength) // throws IOExcepti { short stringLength = dis->readShort(); - if (stringLength > maxLength) + + if (stringLength > maxLength || stringLength < 0) { - wstringstream stream; - stream << L"Received string length longer than maximum allowed (" << stringLength << " > " << maxLength << ")"; - assert(false); - // throw new IOException( stream.str() ); - } - if (stringLength < 0) - { - assert(false); - // throw new IOException(L"Received string length is less than zero! Weird string!"); + throw IOException(L"Packet::readUtf - Invalid string passed"); } wstring builder = L""; + builder.reserve(stringLength); + for (int i = 0; i < stringLength; i++) { wchar_t rc = dis->readChar();