diff --git a/code/__DEFINES/text.dm b/code/__DEFINES/text.dm index 26567be26255..dc58682d9483 100644 --- a/code/__DEFINES/text.dm +++ b/code/__DEFINES/text.dm @@ -24,3 +24,5 @@ #define MAX_PAPER_MESSAGE_LEN 3072 #define MAX_BOOK_MESSAGE_LEN 9216 #define MAX_NAME_LEN 28 + +#define MAX_MESSAGE_CHUNKS 20 diff --git a/code/modules/tgui/tgui.dm b/code/modules/tgui/tgui.dm index c52508e01a93..09618180e671 100644 --- a/code/modules/tgui/tgui.dm +++ b/code/modules/tgui/tgui.dm @@ -43,6 +43,9 @@ /// If the window should be closed with other windows when requested var/closeable = TRUE + /// Any partial packets that we have received from TGUI, waiting to be sent + var/partial_packets + /** * public * @@ -345,6 +348,30 @@ // Pass act type messages to ui_act if(type && copytext(type, 1, 5) == "act/") var/act_type = copytext(type, 5) + + var/id = href_list["packetId"] + if(!isnull(id)) + id = text2num(id) + + var/total = text2num(href_list["totalPackets"]) + if(id == 1) + if(total > MAX_MESSAGE_CHUNKS) + return + + partial_packets = new /list(total) + + partial_packets[id] = href_list["packet"] + + if(id != total) + return + + var/assembled_payload = "" + for(var/packet in partial_packets) + assembled_payload += packet + + payload = json_decode(assembled_payload) + partial_packets = null + log_tgui(user, "Action: [act_type] [href_list["payload"]]", window = window, src_object = src_object) diff --git a/tgui/public/tgui.html b/tgui/public/tgui.html index 70ef812c69f9..780868653936 100644 --- a/tgui/public/tgui.html +++ b/tgui/public/tgui.html @@ -205,9 +205,28 @@ ? { type: type, payload: payload } : type; // JSON-encode the payload + if (message.payload !== null && message.payload !== undefined) { message.payload = JSON.stringify(message.payload); + + if(!Byond.TRIDENT && message.payload.length > 512) { + var chunks = []; + + for(var i = 0, charsLength = message.payload.length; i < charsLength; i += 512) { + chunks.push(message.payload.substring(i, i + 512)) + } + + for(var i = 0; i < chunks.length; i++) { + var to_send = chunks[i] + + message = { type: type, packet: to_send, packetId: i + 1, totalPackets: chunks.length, tgui: 1, window_id: Byond.windowId }; + Byond.topic(message); + } + + return; + } } + // Append an identifying header assign(message, { tgui: 1, diff --git a/tools/build/build.js b/tools/build/build.js index 9efee60ebd31..0df8bb67c9b0 100644 --- a/tools/build/build.js +++ b/tools/build/build.js @@ -68,6 +68,7 @@ export const DmTarget = new Juke.Target({ "html/**", "icons/**", "interface/**", + "tgui/public/tgui.html", `${DME_NAME}.dme`, NamedVersionFile, ],