Skip to content

Commit

Permalink
cloudlink: Update to v0.1.2 (#1134)
Browse files Browse the repository at this point in the history
This is a critical bugfix that restores functionality for projects that
rely upon the username block for obtaining user objects. This also fixes
a connection timing bug.
  • Loading branch information
MikeDev101 committed Nov 8, 2023
1 parent 8f8f433 commit 4335062
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions extensions/cloudlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(function (Scratch) {

/*
CloudLink Extension for TurboWarp v0.1.1.
CloudLink Extension for TurboWarp v0.1.2.
This extension should be fully compatible with projects developed using
extensions S4.1, S4.0, and B3.0.
Expand Down Expand Up @@ -74,8 +74,8 @@
*/
const version = {
editorType: "TurboWarp",
versionNumber: 1,
versionString: "0.1.1",
versionNumber: 2,
versionString: "0.1.2",
};

// Store extension state
Expand Down Expand Up @@ -324,7 +324,7 @@
}

// CL-specific netcode needed for sending messages
async function sendMessage(message) {
function sendMessage(message) {
// Prevent running this while disconnected
if (clVars.socket == null) {
console.warn("[CloudLink] Ignoring attempt to send a packet while disconnected.");
Expand Down Expand Up @@ -434,6 +434,12 @@
// Log configured spec version
console.log(`[CloudLink] Configured protocol spec to v${clVars.linkState.identifiedProtocol}.`);

// Fix timing bug
clVars.linkState.status = 2;

// Fire event hats (only one not broken)
runtime.startHats('cloudlink_onConnect');

// Don't nag user if they already trusted this server
if (clVars.currentServerUrl === clVars.lastServerUrl) return;

Expand Down Expand Up @@ -510,7 +516,7 @@
// Server 0.1.5 (at least)
case "vers":
window.clearTimeout(clVars.handshakeTimeout);
setServerVersion(packet.val.val);
await setServerVersion(packet.val.val);
return;

// Server 0.1.7 (at least)
Expand Down Expand Up @@ -668,7 +674,7 @@

case "server_version":
window.clearTimeout(clVars.handshakeTimeout);
setServerVersion(packet.val);
await setServerVersion(packet.val);
break;

case "client_ip":
Expand Down Expand Up @@ -734,17 +740,12 @@
// Set the link state to connected.
console.log("[CloudLink] Connected.");

clVars.linkState.status = 2;

// If a server_version message hasn't been received in over half a second, try to broadcast a handshake
clVars.handshakeTimeout = window.setTimeout(function() {
console.log("[CloudLink] Hmm... This server hasn't sent us it's server info. Going to attempt a handshake.");
sendHandshake();
}, 500);

// Fire event hats (only one not broken)
runtime.startHats('cloudlink_onConnect');

// Return promise (during setup)
return;
};
Expand Down Expand Up @@ -792,7 +793,7 @@
// GET the serverList
try {
Scratch.fetch(
"https://mikedev101.github.io/cloudlink/serverlist.json"
"https://raw.githubusercontent.com/MikeDev101/cloudlink/master/serverlist.json"
)
.then((response) => {
return response.text();
Expand Down Expand Up @@ -861,11 +862,18 @@
},

{
opcode: "returnUsernameData",
opcode: "returnUsernameDataNew",
blockType: Scratch.BlockType.REPORTER,
text: "My username"
},

{
opcode: "returnUsernameData",
blockType: Scratch.BlockType.REPORTER,
hideFromPalette: clVars.hideCLDeprecatedBlocks,
text: "(OLD - DO NOT USE IN NEW PROJECTS) My username"
},

"---",

{
Expand Down Expand Up @@ -1564,10 +1572,20 @@
}

// Reporter - Returns currently set username.
returnUsernameData() {
returnUsernameDataNew() {
return makeValueScratchSafe(clVars.username.value);
}

// Reporter - (OLD) Returns currently set username (returns user object to retain compatibility with old projects).
returnUsernameData() {
return makeValueScratchSafe(clVars.myUserObject);
}

// Reporter - Returns the reported user object of the client (Snowflake ID, UUID, Username) - Intended replacement for the old username reporter block.
returnUserObject() {
return makeValueScratchSafe(clVars.myUserObject);
}

// Reporter - Returns current client version.
returnVersionData() {
return generateVersionString();
Expand All @@ -1593,11 +1611,6 @@
return makeValueScratchSafe(clVars.client_ip);
}

// Reporter - Returns the reported user object of the client (Snowflake ID, UUID, Username)
returnUserObject() {
return makeValueScratchSafe(clVars.myUserObject);
}

// Reporter - Returns data for a specific listener ID.
// ID - String (listener ID)
returnListenerData(args) {
Expand Down Expand Up @@ -2049,7 +2062,7 @@
clVars.username.temp = args.NAME;

// Send the command
return sendMessage({ cmd: "setid", val: args.NAME, listener: "username_cfg" });
sendMessage({ cmd: "setid", val: args.NAME, listener: "username_cfg" });
}

// Command - Prepares the next transmitted message to have a listener ID attached to it.
Expand Down Expand Up @@ -2114,7 +2127,7 @@
};

clVars.rooms.isAttemptingLink = true;
return sendMessage({ cmd: "link", val: args.ROOMS, listener: "link" });
sendMessage({ cmd: "link", val: args.ROOMS, listener: "link" });
}

// Command - Specifies specific subscribed rooms to transmit messages to.
Expand Down Expand Up @@ -2183,7 +2196,7 @@
};

clVars.rooms.isAttemptingUnlink = true;
return sendMessage({ cmd: "unlink", val: "", listener: "unlink" });
sendMessage({ cmd: "unlink", val: "", listener: "unlink" });
}

// Command - Sends a gmsg value.
Expand All @@ -2193,7 +2206,7 @@
// Must be connected.
if (clVars.socket == null) return;

return sendMessage({ cmd: "gmsg", val: args.DATA });
sendMessage({ cmd: "gmsg", val: args.DATA });
}

// Command - Sends a pmsg value.
Expand All @@ -2209,7 +2222,7 @@
return;
};

return sendMessage({ cmd: "pmsg", val: args.DATA, id: args.ID });
sendMessage({ cmd: "pmsg", val: args.DATA, id: args.ID });
}

// Command - Sends a gvar value.
Expand All @@ -2219,7 +2232,7 @@
// Must be connected.
if (clVars.socket == null) return;

return sendMessage({ cmd: "gvar", val: args.DATA, name: args.VAR });
sendMessage({ cmd: "gvar", val: args.DATA, name: args.VAR });
}

// Command - Sends a pvar value.
Expand All @@ -2235,7 +2248,7 @@
return;
};

return sendMessage({ cmd: "pvar", val: args.DATA, name: args.VAR, id: args.ID });
sendMessage({ cmd: "pvar", val: args.DATA, name: args.VAR, id: args.ID });
}

// Command - Sends a raw-format command without specifying an ID.
Expand All @@ -2245,7 +2258,7 @@
// Must be connected.
if (clVars.socket == null) return;

return sendMessage({ cmd: args.CMD, val: args.DATA });
sendMessage({ cmd: args.CMD, val: args.DATA });
}

// Command - Sends a raw-format command with an ID.
Expand All @@ -2261,7 +2274,7 @@
return;
};

return sendMessage({ cmd: args.CMD, val: args.DATA, id: args.ID });
sendMessage({ cmd: args.CMD, val: args.DATA, id: args.ID });
}

// Command - Resets the "returnIsNewData" boolean state.
Expand Down

0 comments on commit 4335062

Please sign in to comment.