-
Notifications
You must be signed in to change notification settings - Fork 158
New Plugin (Full Pull Request) #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1188e29
f1cf935
ef24733
9563f8c
36f57ac
521941e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please be consistent about your usage of single quotes ( |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| (function(Scratch) { | ||
| 'use strict'; | ||
|
|
||
| class HexDatePlugin { | ||
| getInfo() { | ||
| return { | ||
| id: 'hexDate', | ||
| name: 'Date to Hex', | ||
| blocks: [ | ||
| { | ||
| opcode: 'dateToHex', | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: 'convert date [DATE] to hex bytes', | ||
| arguments: { | ||
| DATE: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "2024-01-01" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| opcode: 'hexToDate', | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: 'convert hex [HEX] to date string', | ||
| arguments: { | ||
| HEX: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "0000018CD05D2000" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| }; | ||
| } | ||
|
|
||
| dateToHex(args) { | ||
| const date = new Date(args.DATE); | ||
| const timestamp = date.getTime(); | ||
|
|
||
| if (isNaN(timestamp)) return "Invalid Date"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's an issue that cannot be continued from, you should throw, not return. Throw will render an error message on the GUI and highlight the script that had an issue, returning doesn't. |
||
|
|
||
| // Create an 8-byte buffer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is pointless; comments should be used exceedingly sparingly, generally. |
||
| const buffer = new ArrayBuffer(8); | ||
| const view = new DataView(buffer); | ||
|
|
||
| // Write as Big-Endian 64-bit integer | ||
| // Note: BigInt is used because JS numbers lose precision above 53 bits | ||
| view.setBigUint64(0, BigInt(timestamp), false); | ||
|
Comment on lines
+46
to
+48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Big endian is the default mode of this method, anyway. Specifying it to be written in big endian in a bit pointless: |
||
|
|
||
| // Convert buffer to Hex string | ||
| return Array.from(new Uint8Array(buffer)) | ||
| .map(b => b.toString(16).padStart(2, '0')) | ||
|
Comment on lines
+42
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a lot of work for something that is seemingly replicated perfectly fine with |
||
| .join('') | ||
| .toUpperCase(); | ||
| } | ||
|
|
||
| hexToDate(args) { | ||
| try { | ||
| const hex = args.HEX.replace(/\s/g, ''); | ||
| const bytes = new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); | ||
| const view = new DataView(bytes.buffer); | ||
|
|
||
| // Read as Big-Endian 64-bit integer | ||
| const timestamp = Number(view.getBigUint64(0, false)); | ||
|
Comment on lines
+59
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yet again, I think this entire area could fairly well replaced with a singular call to |
||
| return new Date(timestamp).toString(); | ||
| } catch (e) { | ||
| return "Invalid Hex"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once more, throw here. I really dislike seeing areas where random bugs can occur for no reason without even being able to track down where it's going on. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| Scratch.extensions.register(new HexDatePlugin()); | ||
| })(Scratch); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if it matters for this PR (it can always be changed later) but AVIF is generally expected for thumbnails. You should probably either convert it on your own machine or look for an online converter to convert it for you, then upload it with the same basename but with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd put this at least a little further down in the list. Maybe next to format numbers and date format.