// ==UserScript==
// @name bonk editor
// @version e1.3_t1.2
// @description Makes editor a bit better
// @author Apx
// @match https://bonk.io/gameframe-release.html
// @match https://bonkisback.io/gameframe-release.html
// @run-at document-end
// @namespace https://gf.qytechs.cn/users/1272759
// @grant none
// ==/UserScript==
const scriptName = "editer";
const guiSettings = {
noWindow: true,
settingsContent: null,
bonkLIBVersion: "1.1.3",
modVersion: "e1.3_t1.2",
}
window.bonkEditor = {
rangeView: 100 * 1024,
isCrashed: false,
crashTime: 1000,
crashTimeEnabled: true,
visibilityStateChanged: false,
degree: 1,
waitTime: -1,
}
function injector(src){
let newSrc = src;
let CSS = document.createElement('style');
CSS.innerHTML = `
#mapeditor_colorpicker_transparency_slider{
margin-left: 0px;
background-color: transparent;
margin-top: -2px;
}
#mapeditor_colorpicker_alphalabel{
color:#ffffff;
}
`;
document.getElementsByTagName('head')[0].appendChild(CSS);
let label = document.createElement('span');
document.getElementById('mapeditor_colorpicker').insertBefore(label, document.getElementById("mapeditor_colorpicker_existingcontainer"));
label.outerHTML = `<span id="mapeditor_colorpicker_alphalabel">Alpha (0)</span>`;
let slider = document.createElement('input');
document.getElementById('mapeditor_colorpicker').insertBefore(slider, document.getElementById("mapeditor_colorpicker_existingcontainer"));
slider.outerHTML = `<input type="range" class="compactSlider compactSlider_classic" min="0" max="1" value="0.5" step="0.01" id="mapeditor_colorpicker_transparency_slider">`;
function patch (src, newsrc) {
newSrc = newSrc.replace(src, newsrc);
};
function log(regex){
console.log(typeof(regex) == "string"?regex:(Array.isArray(regex)?regex:newSrc.match(regex)));
};
// bonk alpha (only v49 support)
let r1 = newSrc.match(/E80\[5]\[E80\[9]\[116]] <= 0xffffff/);
let r2 = newSrc.match(/this\[m\$J\[1571]]\[m\$J\[430]]\(I80\[m\$J\[116]]\);/);
let r3 = newSrc.match(/x6Z\[840]\[x6Z\[8]\[430]]\(x6Z\[519]\[x6Z\[8]\[116]]\);/); // editor
let r4 = newSrc.match(/T_U\[8]\[T_U\[6]\[1017]]/g);
let r5 = newSrc.match(/\(1,T_U\[89]\)\(a2T\(T_U\[14],T_U\[77],T_U\[47]\)\);/);
let r6 = newSrc.match(/function\(x6s\){var H\$J=\[arguments];H\$J\[1]=M\$QCc;R3M\[9]\[H\$J\[1]\[116]]=H\$J\[0]\[0];N_A\(true\);k7V\.t8H\(\);R3M\[31]\[H\$J\[1]\[461]]\[H\$J\[1]\[1025]]=t\$e\[61]\[H\$J\[1]\[1029]]\(H\$J\[0]\[0]\);}/);
let r7 = newSrc.match(/var J22=\[arguments];/);
let r8 = newSrc.match(/\(1,T_U\[89]\)\(T_U\[71]\);/);
// editor
const widthRoundingRegex = newSrc.match(/[a-zA-Z0-9\$_]{3}\[[0-9]{1,3}]]\[[a-zA-Z0-9\$_]{3}\[[0-9]{1,3}]\[[0-9]{1,3}]]=Math\[[a-zA-Z0-9\$_]{3}\[[0-9]{1,3}]\[[0-9]{1,3}]]\([a-zA-Z0-9\$_]{3}\[[0-9]{2,3}]\);[a-zA-Z0-9\$_]{3}\(true\)/)[0];
const rectPosRegex = newSrc.match(/[a-zA-Z0-9\$_]{3}\[[0-9]{1,3}]\[[a-zA-Z0-9\$_]{3}\[[0-9]{1,3}]\[[0-9]{1,3}]]\[[0-1]]=Math\[[a-zA-Z0-9\$_]{3}\[[0-9]{1,3}]\[[0-9]{1,3}]]\([a-zA-Z0-9\$_\[\]]+\);/g).map((x) => {return x.split("=")});
const platZindex = newSrc.match(/function [a-zA-Z0-9\$_]{3}\(\){[a-zA-Z0-9-+=_ \$;\(\)[\]{}\.,!]*?}}[a-zA-Z0-9\$_]{3}\(\);[a-zA-Z0-9\$_]{3}\(true\);}/g)[0];
const arrayBufferRegex = newSrc.match(/(?<=new ArrayBuffer\()k7V\.Q5\$\(100,1024\)/)[0];
const spawnId = newSrc.match(/[a-zA-Z0-9\$_]{3}\[[0-9]{1,3}](?=--;}else {(?!if))/)[0];
patch(r5, `(1,T_U[89])(a2T(T_U[14],T_U[77],T_U[47]),document.getElementById("mapeditor_colorpicker_transparency_slider").value);`);
patch(r6, `function(x6s,alph){var H$J=[arguments];H$J[1]=M$QCc;R3M[9][H$J[1][116]]=H$J[0][0]+(alph==1?0:(Math.round((alph*100+1))*0x1000000));let textValue = alph;if(alph==1){textValue = "Opaque";}else if(alph==0){textValue = "Invisible";}document.getElementById("mapeditor_colorpicker_alphalabel").textContent = \`Transparency (\${textValue})\`;N_A(true);k7V.t8H();R3M[31][H$J[1][461]][H$J[1][1025]]=t$e[61][H$J[1][1029]](H$J[0][0]);}`);
patch(r7, r7+`document.getElementById("mapeditor_colorpicker_transparency_slider").value = ((J22[0][0] >> 24 & 255)-1)/100<0?1:((J22[0][0] >> 24 & 255)-1)/100;`);
patch(r8, `(1,T_U[89])(T_U[71],document.getElementById("mapeditor_colorpicker_transparency_slider").value);`);
patch(r4, `document.getElementById("mapeditor_colorpicker_transparency_slider").onchange = function () {h1N(false)};document.getElementById("mapeditor_colorpicker_transparency_slider").oninput = function () {h1N(false)};`+r4);
patch(r1, r1+`ff`);
patch(r2, `let alpha = ((I80[m$J[116]] >> 24 & 255)-1)/100;this[m$J[1571]][m$J[430]](parseInt(t$e[61].numToHex(I80[m$J[116]]).substring(1),16),(alpha<0?1:Math.min(alpha,1)));`);
patch(r3, `let alpha = ((x6Z[519][x6Z[8][116]] >> 24 & 255)-1)/100;x6Z[840][x6Z[8][430]](parseInt(t$e[61].numToHex(x6Z[519][x6Z[8][116]]).substring(1),16),(alpha<0?1:Math.min(alpha,1)));`);
// move up / down spawns
const vars = platZindex.match(/(?<![\.a-zA-Z])(?:[a-zA-Z0-9\$_]{3})(?=[\[=]{1})(?:\[[0-9]{1,3}])?/g);
const modifiedPlatZindex = platZindex
.replace(/}}(?!else)/, `
}
}
}
else{
if(this==${vars[10]}){
if(${vars[4]}.spawns[${spawnId}-1]!=undefined){
${vars[15]}=${vars[4]}.spawns[${spawnId}-1];
${vars[4]}.spawns[${spawnId}-1]=${vars[0]}[100];
${vars[4]}.spawns[${spawnId}]=${vars[15]};
${spawnId}--;
}
}
else if(this==${vars[33]}){
if(${vars[4]}.spawns[${spawnId}+1]!=undefined){
${vars[38]}=${vars[4]}.spawns[${spawnId}+1];
${vars[4]}.spawns[${spawnId}+1]=${vars[0]}[100];
${vars[4]}.spawns[${spawnId}]=${vars[38]};
${spawnId}++;
}
}
}`)
.replace(/if\(.*?\){.*?}/, "")
.replace(/;if\(.*?\){.*?}/, `;${vars[0]}[100]=${vars[4]}.spawns[${spawnId}];if(${vars[3]} == -1 && ${vars[4]}.spawns.indexOf(${vars[0]}[100]) == -1){return;}if(${vars[3]}!=-1){`);
patch(platZindex,modifiedPlatZindex);
// disable width rounding to integers
patch(widthRoundingRegex.split(";")[0] + ";", `${widthRoundingRegex.split("=")[0]}=${widthRoundingRegex.split("=")[1].split(";")[0].match(/.{7}(?=\))/)};`);
// disable rectangle position rounding to integers
for(let i = 0; i < 4; i++) patch(rectPosRegex[i].join("="), `${rectPosRegex[i][0]}=${rectPosRegex[i][1].match(/.{6}(?=\))/)};`);
// replace the minimum number of width, height and radius with MIN_VALUE (values lower than 1e-100 are not recommended)
const precission = newSrc.match(/function [a-zA-Z0-9\$_]{3}\([a-zA-Z0-9\$_]{3}\){[a-zA-Z0-9\$_\[\]= ]+;[a-zA-Z0-9\$_\[\]=]+;[a-zA-Z0-9\$_\[\]=]+\*=10000;[a-zA-Z0-9\$_\[\]=]+\([a-zA-Z0-9\$_\[\]=]+\);[a-zA-Z0-9\$_\[\]=]+\/=10000;return [a-zA-Z0-9\$_\[\]=]+;}/)[0];
patch(`min:1,`, `min:0.0001,`);
patch(precission ,`function ${precission.split(" ")[1].substring(0,3)}(arg_){return arg_;}`);
// make range view changeable
patch(arrayBufferRegex ,`window.bonkEditor.rangeView`);
// anti-crash type 1
const anticrash1 = newSrc.match(/(?<=null)\){var [a-zA-Z0-9\$_]{3}=[a-zA-Z0-9\$_]{3}\..*?;/g);
for(let i = 1; i < anticrash1.length; i++) {
patch(anticrash1[i], `&&this.shapes[${anticrash1[i].split("=")[1].replace(";","")}]!=undefined${anticrash1[i]}`);
}
// anti-crash type 2
const anticrash2 = newSrc.match(/(?<=0xccbbaa;)if\(this...../);
patch(anticrash2,`if(this.capFill==null){return;}${anticrash2}`);
// 0fps fix
const gameExit = newSrc.match(/\(\);[a-zA-Z0-9\$_]{3}\[[0-9]{1,3}]=false;}else {.*?}/);
const frame = newSrc.match(/requestAnimationFrame\([a-zA-Z0-9\$_]{1,3}\);(?=})/);
const frameDispatch = newSrc.match(/[a-zA-Z0-9\$_]{1,3}\(\){}(;window.*?;(?=func))?function [a-zA-Z0-9\$_]{1,3}\(\){.*?;.*?;.*?(?=;)/)[0].split(";").reverse()[0];
const visibilityChange = newSrc.match(/}\);}document.*?{/);
patch(visibilityChange, visibilityChange + `window.bonkEditor.visibilityStateChanged = true;`);
patch(frame, `
if(!window.bonkEditor.isCrashed){
if(window.bonkEditor.waitTime == -1) window.bonkEditor.waitTime = Date.now();
if(Date.now() - window.bonkEditor.waitTime > window.bonkEditor.crashTime && !window.bonkEditor.visibilityStateChanged){
window.bonkEditor.isCrashed = true;
throw new Error("The game crashed after reaching limit of waiting time (" + (Date.now() - window.bonkEditor.waitTime) + "ms)");
}
window.bonkEditor.visibilityStateChanged = false;
if(window.bonkEditor.crashTimeEnabled) window.bonkEditor.waitTime = Date.now();
}` + frame);
patch(frameDispatch, `try{${frameDispatch}}catch(e){window.bonkEditor.isCrashed = true;throw e;}`);
patch(gameExit, gameExit + `if(window.bonkEditor.isCrashed){window.bonkEditor.isCrashed = false;window.bonkEditor.waitTime = Date.now();${frame}}`);
// menu
const menuRegex = newSrc.match(/== 13\){...\(\);}}/)[0];
patch(menuRegex, menuRegex + "window.bonkEditor.menu = this;");
// anti lobby kick
const ws = window.WebSocket.prototype.send;
window.WebSocket.prototype.send = function(args){
if(this.url.includes("socket.io/?EIO=3&transport=websocket&sid=") && typeof(args) == "string" && args.length > 250000){
window.bonkEditor.menu.showStatusMessage("* Protected from being kicked out of the room.","#b53030",false);
return;
}
ws.call(this,args);
}
if(src === newSrc) throw "Injection failed!";
console.log(`${scriptName} injector run`);
return newSrc;
}
if(!window.bonkCodeInjectors) window.bonkCodeInjectors = [];
window.bonkCodeInjectors.push(bonkCode => {
try {
return injector(bonkCode);
} catch (error) {
alert(`Whoops! ${scriptName} was unable to load.`);
throw error;
}
});
if (window.bonkHUD) {
let storage = localStorage.getItem("bonkEditor");
if(storage != null){
storage = JSON.parse(storage);
} else storage = {};
const label = (target, ...elements) => {
let div = document.createElement("div");
div.margin = "5px";
target.appendChild(div);
for(let element in elements) {
if(typeof(elements[element]) == "string"){
let labelElement = document.createElement("label");
labelElement.classList.add("bonkhud-settings-label");
labelElement.textContent = elements[element];
labelElement.style.padding = "0 5px";
labelElement.style.display = "inline-block";
div.appendChild(labelElement);
} else div.appendChild(elements[element]);
div.lastChild.style.verticalAlign = "middle";
}
}
// functions
const notAppliedText = target => {
if(target.lastChild.className == "bonkeditor_not_applied_text") return;
let span = document.createElement("span");
span.className = "bonkeditor_not_applied_text";
span.textContent = "* not applied";
span.style["margin-left"] = "5px";
span.style.color = "#000000aa";
span.style.verticalAlign = "middle";
target.appendChild(span);
}
const checkbox = value => {
let checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = value;
checkbox.style["vertical-align"] = "middle";
return checkbox;
}
const inputText = value => {
let text = document.createElement("input");
text.style.width = "40px";
text.style.height = "19px";
text.style["vertical-align"] = "middle";
text.value = value;
return text;
}
let settings = window.bonkHUD.generateSection();
guiSettings.settingsContent = settings;
const ind = window.bonkHUD.createMod("bonk editor & transparency", guiSettings);
// section
let rangeView = inputText(storage.rangeView/(1024**storage.degree) || window.bonkEditor.rangeView/(1024**window.bonkEditor.degree));
rangeView.oninput = (event) => {
event.target.value = event.target.value.replaceAll(/[^0-9]+/g, '');
event.target.value = String(Math.min(Math.max(parseInt(event.target.value) || 0, 0), 1024));
notAppliedText(event.target.parentNode);
}
let unit = document.createElement("select");
let units = ["KB","MB"];
units.forEach((text,key) => {
unit.appendChild(new Option(text, key, false, (storage.degree || window.bonkEditor.degree) == key + 1));
});
unit.onchange = (event) => {
window.bonkEditor.degree = parseInt(event.target.value);
notAppliedText(event.target.parentNode);
}
let crashTime = inputText(storage.crashTime || window.bonkEditor.crashTime);
crashTime.oninput = (event) => {
event.target.value = event.target.value.replaceAll(/[^0-9]+/g, '');
event.target.value = String(Math.max(parseInt(event.target.value) || 0, 0));
notAppliedText(event.target.parentNode);
}
let crashTimeEnabled = checkbox(storage.crashTimeEnabled || window.bonkEditor.crashTimeEnabled);
crashTimeEnabled.onchange = event => {
notAppliedText(event.target.parentNode);
};
// apply button
let applyButton = window.bonkHUD.generateButton("Apply");
applyButton.style.display = "inline-block";
applyButton.style.padding = "0 5px";
applyButton.onclick = () => {
window.bonkEditor.rangeView = parseInt(rangeView.value) * (1024 ** (parseInt(unit.selectedIndex) + 1));
window.bonkEditor.degree = (parseInt(unit.selectedIndex) + 1);
window.bonkEditor.crashTime = parseInt(crashTime.value);
window.bonkEditor.crashTimeEnabled = crashTimeEnabled.checked;
[...settings.getElementsByClassName("bonkeditor_not_applied_text")].forEach(x => x.parentNode.removeChild(x));
localStorage.setItem("bonkEditor", JSON.stringify(window.bonkEditor));
}
// apply button
let clearStorageButton = window.bonkHUD.generateButton("Clear Storage");
clearStorageButton.style.display = "inline-block";
clearStorageButton.style.padding = "0 5px";
clearStorageButton.onclick = () => {
localStorage.removeItem("bonkEditor");
}
label(settings, "Array buffer range view", rangeView, unit);
label(settings, "Stop the game if the time between frames reaches", crashTime, "milliseconds", crashTimeEnabled);
label(settings, applyButton);
label(settings, clearStorageButton);
window.bonkHUD.updateStyleSettings();
}