TurboWarp / extensions

User-contributed unsandboxed extension gallery for TurboWarp
https://extensions.turbowarp.org/
MIT License
115 stars 230 forks source link

UPDATE: NUMETRICAL ENCODING #1121

Open MaxttcYT opened 10 months ago

MaxttcYT commented 10 months ago

I have created a new improved version of the numerical encoding extension:

// Name: Numerical Encoding // ID: cs2627883NumericalEncoding // Description: Encode strings as numbers for cloud variables. // By: cs2627883 https://scratch.mit.edu/users/cs2627883/

// https://github.com/CS2627883/Turbowarp-Encoding-Extension/blob/main/Encoding.js

(function (Scratch) { "use strict"; class NumericalEncodingExtension { maxcharlength = 6; // There are 149,186 unicode characters, so the maximum character code length is 6 encoded = 0; decoded = 0; getInfo() { return { id: "cs2627883NumericalEncoding", name: "Numerical Encoding", blocks: [ { opcode: "NumericalEncode", blockType: Scratch.BlockType.REPORTER, text: "Encode [DATA] to numbers", arguments: { DATA: { type: Scratch.ArgumentType.STRING, defaultValue: "Hello!", }, }, }, { opcode: "NumericalDecode", blockType: Scratch.BlockType.REPORTER, text: "Decode [ENCODED] back to text", arguments: { ENCODED: { type: Scratch.ArgumentType.STRING, defaultValue: "000072000101000108000108000111000033", //Encoded "Hello!" }, }, }, ], }; } NumericalEncode(args) { const toencode = String(args.DATA); var encoded = ""; for (let i = 0; i < toencode.length; ++i) { // Get char code of character var encodedchar = String(toencode.charCodeAt(i)); // Pad encodedchar with 0s to ensure all encodedchars are the same length encodedchar = "0".repeat(this.maxcharlength - encodedchar.length) + encodedchar; encoded += encodedchar; } return encoded; } NumericalDecode(args) { const todecode = String(args.ENCODED); if (todecode == "") { this.decoded = ""; return; } var decoded = ""; // Create regex to split by char length const regex = new RegExp(".{1," + this.maxcharlength + "}", "g"); // Split into array of characters var encodedchars = todecode.match(regex); for (let i = 0; i < encodedchars.length; i++) { // Get character from char code var decodedchar = String.fromCharCode(encodedchars[i]); decoded += decodedchar; } return decoded; }

GetNumericalEncoded(args) {
  return this.encoded;
}
GetNumericalDecoded(args) {
  return this.decoded;
}

}

// Test Code / encoding = new NumericalEncodingExtension(); encodingNumericalEncode({"DATA": 'Hello!'}); console.log(encoding.GetNumericalEncoded()) encoding.NumericalDecode({"ENCODED": encoding.GetNumericalEncoded()}); console.log(encoding.GetNumericalDecoded()); /

Scratch.extensions.register(new NumericalEncodingExtension()); })(Scratch);

CS2627883 commented 10 months ago

It looks like you've simplified the extension by moving the encode and decode blocks to reporter blocks - this is a good change and I like it. However, it looks like the whole code has been completely refactored (https://github.com/TurboWarp/extensions/commit/cec16cb77c5f9330909fcbc35beb5aa54d36171b), so this change is no longer valid. Would you mind looking at the new code (https://github.com/TurboWarp/extensions/blob/master/extensions/cs2627883/numericalencoding.js) and making your change to that? If you do, it would be great if you could submit a pull request.

In the meantime, since this is a good change and it deserves attention, I will ping @GarboMuffin.

CS2627883 commented 10 months ago

I don't have the time to implement this change myself right now, but maybe I will remember to come back to it.