From 761d119d64a1386c6a8ee7663f81ac58243b1f62 Mon Sep 17 00:00:00 2001 From: Muffin Date: Wed, 23 Aug 2023 10:26:11 -0500 Subject: [PATCH] Fix errors caused by variable or list values that are not Scratch-compatible https://github.com/TurboWarp/sb3fix/issues/8 --- index.html | 1 + samples/invalid-variable-values.sb3 | Bin 0 -> 2156 bytes sb3fix.js | 41 ++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 samples/invalid-variable-values.sb3 diff --git a/index.html b/index.html index 35f497a..09cb627 100644 --- a/index.html +++ b/index.html @@ -108,6 +108,7 @@

Which errors does this fix?

  • Could not load project: {"validationError":"Could not parse as a valid SB2 or SB3 project.","sb3Errors":[{"keyword":"enum","dataPath":".targets[0].name","schemaPath":"sb3_definitions.json#/definitions/stage/properties/name/enum","params":{"allowedValues":["Stage"]},"message":"should be equal to one of the allowed values"}],"sb2Errors":[{"keyword":"required","dataPath":"","schemaPath":"#/required","params":{"missingProperty":"objName"},"message":"should have required property 'objName'"}]}
  • Could not load project: {"validationError":"Could not parse as a valid SB2 or SB3 project.","sb3Errors":[{"keyword":"minItems","dataPath":".targets[0].costumes","schemaPath":"#/properties/costumes/minItems","params":{"limit":1},"message":"should NOT have less than 1 items"}],"sb2Errors":[{"keyword":"required","dataPath":"","schemaPath":"#/required","params":{"missingProperty":"objName"},"message":"should have required property 'objName'"}]}
  • Could not load project: {"validationError":"Could not parse as a valid SB2 or SB3 project.","sb3Errors":[{"keyword":"required","dataPath":".targets[0].costumes[0]","schemaPath":"#/required","params":{"missingProperty":"assetId"},"message":"should have required property 'assetId'"}],"sb2Errors":[{"keyword":"required","dataPath":"","schemaPath":"#/required","params":{"missingProperty":"objName"},"message":"should have required property 'objName'"}]}
  • +
  • Could not load project: {"validationError":"Could not parse as a valid SB2 or SB3 project.","sb3Errors":[{"keyword":"type","dataPath":".targets[0].variables['`jEk@4|i[#Fk?(8x)AV.-my variable'][1]","schemaPath":"#/definitions/stringOrNumber/oneOf/0/type","params":{"type":"string"},"message":"should be string"},{"keyword":"type","dataPath":".targets[0].variables['`jEk@4|i[#Fk?(8x)AV.-my variable'][1]","schemaPath":"#/definitions/stringOrNumber/oneOf/1/type","params":{"type":"number"},"message":"should be number"},{"keyword":"oneOf","dataPath":".targets[0].variables['`jEk@4|i[#Fk?(8x)AV.-my variable'][1]","schemaPath":"#/definitions/stringOrNumber/oneOf","params":{"passingSchemas":null},"message":"should match exactly one schema in oneOf"}...}
  • The best way to see if you're in luck is to just try it.

    diff --git a/samples/invalid-variable-values.sb3 b/samples/invalid-variable-values.sb3 new file mode 100644 index 0000000000000000000000000000000000000000..3ff11fcd9bdc53ac6ae4a83e2b0a15b662ce26a2 GIT binary patch literal 2156 zcma)-do&aLAIFDLw5`!JemoQ_mpwF^nJz4sTo&e@TV=OIc;=FOn?)q|ToQ{7i#kaz zAu@$Zp0H;wB@s%~iP3WDiT309=l6R$=Q-#3e9rfL&gXo-=Y7uSkJtM{z{>y?006*t zz)&3At8;bF97^V|7J%3+CT7OO01AeRB@)d@STYuiMi~)J&;ca$K_jXu5n~u};p|-x z$Lu#4=%ZDyI(@CeYUFb$B8oW%LvFgEGTt-2qeBx3W<7fJ#l9KKyr!F7pf)c2}LnT zadx!oa!*Zgf>qW_z7yhl)XM4r{bL)7Gj+omgb3c*sxe+RpZ1E1UZ+20wPrH^$vbGE4*I zrYjhofEl3O+7o+DnZeIch$A{pfe6v_Oc9)2En`;Xs*uuktzfU7#}|ib!c~4x)kEdA zgbwkc?==UX2IYP*_0f$@llwEJ@&?ko5u|pz3L~w!20uohs$(_~`F?kSO=QXeaP}>} z7_S?P6LBnIcBw)+iB=ATwB|ME7c}ueE%d|78!7B|GD>9}<)gxms^(Ou*LSk7?E8p9 zwwKEyt?jbh{PR6J5y}oUDYUQyHdtGbO#;GRV{LG`8Ey>BC!5yF!tOlJy!6&7-2&q- zd;NWvD8KvsvX0tIDQL*E%$uLY|cWGusR>;1vNq(h&h+ z#)0Up{$X`AMW?C5`>UkR9E+%GQ7C%k^Kscle}kC#>vOM>XUd0vtITa4v3120Cm)zw zQ%-%}sXH4-2P`&i(8@%G*b&Z1I;>acMz1Qm4OvgR});6Uu@LaN; z-csbOIf$d*=X$=&wz9XPq)fuBuMNK6R=tC<(yihro|Y-r&v%GsL!0GgXk zbLsMDPQd3^@pQn}Qm{?q*baE-3ik>))D51I1^~i#005xPrG5(!3!;!B4TB=WLf?8i zI={g|ABmB_Hg9v~4`DC}I}f~`do=;UBp{Q8uMX1+U$rePjM)bYCMTM#lGh8MHJ{jb zN@tG_WKk#DKMxpKX}TqCcd-|Dw(ygBdfB%~u4V)%hpU@A?W9VEeoSjEckcuNWi2dO z9eIrm-lS&sWE)TT^6Isvw)u(1h?uvDzYJu2I9tWhzfu7M>w^oAyIc(3fDyng1~#(= zaoYMG_`By@66v249tz8xPsx5~O%@3TyyFuykTM6!a>6nwD!7J!M0j0oM7RTjU_7%7 z4(z@}(^gu<9_QY;@4h|r7h269#8S_KI@}B6n!YL*RdXOoD~8y-%wRBg84?L*{?q_9 z#Y*cahj34v`kMxSVr<(=4k)!_23vS_x12`a%^e|n#OW#bqdHd6HhY;!LVklbS)s>^ z)la*7mc~0*`?euyC{{1r_Z;uNp0H~x6@Urx`x1Gd=w#t_9QCdo&q_4 z4^OF+EG_Mg5+Ls7q6y{cQW4aVdgozfO+?ybA?W1w-778=&%gD@@l-7lzF-|iclM({ z!ZEKzZ$mMvVZ4OzGs`94uS7@B9EmJl*??f(aIf_xHFTe7?%1qEbZT)eu=uW#ijT-qzXvxC>%Z!&WB)$b;LtF2sijb!}b z=dls|CtCv%S+cG%?!5u3* { if (!Array.isArray(variable)) { - throw new Error(`variable or list ${id} is not an array`); + throw new Error(`variable object ${id} is not an array`); } + const name = variable[0]; if (typeof name !== 'string') { log(`variable or list ${id} name was not a string`); variable[0] = String(variable[0]); } + + const value = variable[1]; + if (typeof value !== 'number' && typeof value !== 'string' && typeof value !== 'boolean') { + log(`variable ${id} value was not a Scratch-compatible value`); + variable[1] = String(variable[1]); + } + }; + + /** + * @param {string} id + * @param {unknown} list + */ + const fixListInPlace = (id, list) => { + if (!Array.isArray(list)) { + throw new Error(`list object ${id} is not an array`); + } + + const name = list[0]; + if (typeof name !== 'string') { + log(`list ${id} name was not a string`); + list[0] = String(list[0]); + } + + if (!Array.isArray(list[1])) { + log(`list ${id} value was not an array`); + list[1] = []; + } + + const listValue = list[1]; + for (let i = 0; i < listValue.length; i++) { + const value = listValue[i]; + if (typeof value !== 'number' && typeof value !== 'string' && typeof value !== 'boolean') { + log(`list ${id} index ${i} was not a Scratch-compatible value`); + listValue[i] = String(value); + } + } }; /**