From 1108dea69689183d3b0671cd7aa73f79f555f0d8 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 6 Oct 2014 19:34:11 -0300 Subject: [PATCH 1/3] Updated the error message when invalid UTF-8 strings are used in love.graphics.print and friends to mention UTF-8. --- src/modules/font/GlyphData.cpp | 2 +- src/modules/font/Rasterizer.cpp | 4 ++-- src/modules/font/freetype/Font.cpp | 4 ++-- src/modules/graphics/opengl/Font.cpp | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modules/font/GlyphData.cpp b/src/modules/font/GlyphData.cpp index 7ed2b3db2..efaa7100d 100644 --- a/src/modules/font/GlyphData.cpp +++ b/src/modules/font/GlyphData.cpp @@ -106,7 +106,7 @@ std::string GlyphData::getGlyphString() const } catch (utf8::exception &e) { - throw love::Exception("Decoding error: %s", e.what()); + throw love::Exception("UTF-8 decoding error: %s", e.what()); } // Just in case... diff --git a/src/modules/font/Rasterizer.cpp b/src/modules/font/Rasterizer.cpp index cec9f0bef..de8e05328 100644 --- a/src/modules/font/Rasterizer.cpp +++ b/src/modules/font/Rasterizer.cpp @@ -63,7 +63,7 @@ GlyphData *Rasterizer::getGlyphData(const std::string &text) const } catch (utf8::exception &e) { - throw love::Exception("Decoding error: %s", e.what()); + throw love::Exception("UTF-8 decoding error: %s", e.what()); } return getGlyphData(codepoint); @@ -89,7 +89,7 @@ bool Rasterizer::hasGlyphs(const std::string &text) const } catch (utf8::exception &e) { - throw love::Exception("Decoding error: %s", e.what()); + throw love::Exception("UTF-8 decoding error: %s", e.what()); } return true; diff --git a/src/modules/font/freetype/Font.cpp b/src/modules/font/freetype/Font.cpp index ece8e8ddb..62b78d3b3 100644 --- a/src/modules/font/freetype/Font.cpp +++ b/src/modules/font/freetype/Font.cpp @@ -66,7 +66,7 @@ Rasterizer *Font::newRasterizer(love::image::ImageData *data, const std::string catch (utf8::exception &e) { delete [] glyphs; - throw love::Exception("Decoding error: %s", e.what()); + throw love::Exception("UTF-8 decoding error: %s", e.what()); } Rasterizer *r = newRasterizer(data, glyphs, numglyphs); @@ -90,7 +90,7 @@ GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text) } catch (utf8::exception &e) { - throw love::Exception("Decoding error: %s", e.what()); + throw love::Exception("UTF-8 decoding error: %s", e.what()); } return r->getGlyphData(codepoint); diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index df19f875d..0acf1572a 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -367,7 +367,7 @@ void Font::print(const std::string &text, float x, float y, float extra_spacing, } catch (utf8::exception &e) { - throw love::Exception("Decoding error: %s", e.what()); + throw love::Exception("UTF-8 decoding error: %s", e.what()); } if (vertexcount <= 0 || glyphinfolist.size() == 0) @@ -429,7 +429,7 @@ int Font::getWidth(const std::string &str) } catch(utf8::exception &e) { - throw love::Exception("Decoding error: %s", e.what()); + throw love::Exception("UTF-8 decoding error: %s", e.what()); } if (width > max_width) From 3bc28345aa1b9acc16e411864dd15463f209bcfa Mon Sep 17 00:00:00 2001 From: Dalton Nell Date: Fri, 10 Oct 2014 12:35:33 -0600 Subject: [PATCH 2/3] Added function host:destroy(), added error handling for trying to work with a destroyed host, and fixed awkward naming of socket_get_address (it now matches the rest of the lib and official documentation as get_socket_address now). --- src/libraries/enet/enet.cpp | 100 +++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 23 deletions(-) diff --git a/src/libraries/enet/enet.cpp b/src/libraries/enet/enet.cpp index 7d915cfce..a4b60489b 100644 --- a/src/libraries/enet/enet.cpp +++ b/src/libraries/enet/enet.cpp @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2011 by Leaf Corcoran + * Copyright (C) 2014 by Leaf Corcoran * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -40,9 +40,9 @@ extern "C" { /** * Parse address string, eg: - * *:5959 - * 127.0.0.1:* - * website.com:8080 + * *:5959 + * 127.0.0.1:* + * website.com:8080 */ static void parse_address(lua_State *l, const char *addr_str, ENetAddress *address) { int host_i = 0, port_i = 0; @@ -204,11 +204,11 @@ static ENetPacket *read_packet(lua_State *l, int idx, enet_uint8 *channel_id) { /** * Create a new host * Args: - * address (nil for client) - * [peer_count = 64] - * [channel_count = 1] - * [in_bandwidth = 0] - * [out_bandwidth = 0] + * address (nil for client) + * [peer_count = 64] + * [channel_count = 1] + * [in_bandwidth = 0] + * [out_bandwidth = 0] */ static int host_create(lua_State *l) { ENetHost *host; @@ -236,7 +236,7 @@ static int host_create(lua_State *l) { } // printf("host create, peers=%d, channels=%d, in=%d, out=%d\n", - // peer_count, channel_count, in_bandwidth, out_bandwidth); + // peer_count, channel_count, in_bandwidth, out_bandwidth); host = enet_host_create(have_address ? &address : NULL, peer_count, channel_count, in_bandwidth, out_bandwidth); @@ -264,14 +264,17 @@ static int linked_version(lua_State *l) { /** * Serice a host * Args: - * timeout + * timeout * * Return - * nil on no event - * an event table on event + * nil on no event + * an event table on event */ static int host_service(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } ENetEvent event; int timeout = 0, out; @@ -291,6 +294,9 @@ static int host_service(lua_State *l) { */ static int host_check_events(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } ENetEvent event; int out = enet_host_check_events(host, &event); if (out == 0) return 0; @@ -306,6 +312,9 @@ static int host_check_events(lua_State *l) { */ static int host_compress_with_range_coder(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } int result = enet_host_compress_with_range_coder (host); if (result == 0) { @@ -320,12 +329,15 @@ static int host_compress_with_range_coder(lua_State *l) { /** * Connect a host to an address * Args: - * the address - * [channel_count = 1] - * [data = 0] + * the address + * [channel_count = 1] + * [data = 0] */ static int host_connect(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } ENetAddress address; ENetPeer *peer; @@ -355,12 +367,18 @@ static int host_connect(lua_State *l) { static int host_flush(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } enet_host_flush(host); return 0; } static int host_broadcast(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } enet_uint8 channel_id; ENetPacket *packet = read_packet(l, 2, &channel_id); @@ -371,6 +389,9 @@ static int host_broadcast(lua_State *l) { // Args: limit:number static int host_channel_limit(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } int limit = luaL_checkint(l, 2); enet_host_channel_limit(host, limit); return 0; @@ -378,14 +399,20 @@ static int host_channel_limit(lua_State *l) { static int host_bandwidth_limit(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } enet_uint32 in_bandwidth = luaL_checkint(l, 2); enet_uint32 out_bandwidth = luaL_checkint(l, 2); enet_host_bandwidth_limit(host, in_bandwidth, out_bandwidth); return 0; } -static int host_socket_get_address(lua_State *l) { +static int host_get_socket_address(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } ENetAddress address; enet_socket_get_address (host->socket, &address); @@ -399,6 +426,9 @@ static int host_socket_get_address(lua_State *l) { } static int host_total_sent_data(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } lua_pushinteger (l, host->totalSentData); @@ -407,6 +437,9 @@ static int host_total_sent_data(lua_State *l) { static int host_total_received_data(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } lua_pushinteger (l, host->totalReceivedData); @@ -414,6 +447,9 @@ static int host_total_received_data(lua_State *l) { } static int host_service_time(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } lua_pushinteger (l, host->serviceTime); @@ -422,6 +458,9 @@ static int host_service_time(lua_State *l) { static int host_peer_count(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } lua_pushinteger (l, host->peerCount); @@ -430,6 +469,9 @@ static int host_peer_count(lua_State *l) { static int host_get_peer(lua_State *l) { ENetHost *host = check_host(l, 1); + if (!host) { + return luaL_error(l, "Tried to index a nil host!"); + } int peer_index = luaL_checkint(l, 2) - 1; @@ -444,8 +486,13 @@ static int host_get_peer(lua_State *l) { } static int host_gc(lua_State *l) { - ENetHost *host = check_host(l, 1); - enet_host_destroy(host); + // We have to manually grab the userdata so that we can set it to NULL. + ENetHost** host = luaL_checkudata(l, 1, "enet_host"); + // We don't want to crash by destroying a non-existant host. + if (*host) { + enet_host_destroy(*host); + } + *host = NULL; return 0; } @@ -653,9 +700,9 @@ static int peer_receive(lua_State *l) { /** * Send a lua string to a peer * Args: - * packet data, string - * channel id - * flags ["reliable", nil] + * packet data, string + * channel id + * flags ["reliable", nil] * */ static int peer_send(lua_State *l) { @@ -684,7 +731,14 @@ static const struct luaL_Reg enet_host_funcs [] = { {"broadcast", host_broadcast}, {"channel_limit", host_channel_limit}, {"bandwidth_limit", host_bandwidth_limit}, - {"socket_get_address", host_socket_get_address}, + // Since ENetSocket isn't part of enet-lua, we should try to keep + // naming conventions the same as the rest of the lib. + {"get_socket_address", host_get_socket_address}, + // TODO: Remove the line below in future versions, it's for backward + // compatibility only. + {"socket_get_address", host_get_socket_address}, + // We need this function to free up our ports when needed! + {"destroy", host_gc}, // additional convenience functions (mostly accessors) {"total_sent_data", host_total_sent_data}, From 7bcb48c562ee5c1d68301bf9201828cb8d11b553 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 13 Oct 2014 03:20:47 -0300 Subject: [PATCH 3/3] Fixed compilation (resolves issue #938.) --- src/libraries/enet/enet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/enet/enet.cpp b/src/libraries/enet/enet.cpp index a4b60489b..04b3b4a4c 100644 --- a/src/libraries/enet/enet.cpp +++ b/src/libraries/enet/enet.cpp @@ -487,7 +487,7 @@ static int host_get_peer(lua_State *l) { static int host_gc(lua_State *l) { // We have to manually grab the userdata so that we can set it to NULL. - ENetHost** host = luaL_checkudata(l, 1, "enet_host"); + ENetHost** host = (ENetHost**)luaL_checkudata(l, 1, "enet_host"); // We don't want to crash by destroying a non-existant host. if (*host) { enet_host_destroy(*host);