PPML

Pixel Place Map Loader

Versión del día 22/4/2022. Echa un vistazo a la versión más reciente.

Este script no debería instalarse directamente. Es una biblioteca que utilizan otros scripts mediante la meta-directiva de inclusión // @require https://update.greasyfork.org/scripts/443803/1042792/PPML.js

// ==UserScript==
// @name         PPML
// @description  Pixel Place Map Loader
// @version      1.1
// @author       0vC4
// @namespace    https://greasyfork.org/users/670183
// @match        https://pixelplace.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pixelplace.io
// @license      MIT
// @grant        none
// @run-at       document-start
// ==/UserScript==





const PPML = (() => {
    if (window.PPML) return window.PPML;



    const colors = new Uint32Array([
        0xCCCCCC,
        0xFFFFFF,
        0xC4C4C4,
        0x888888,
        0x555555,
        0x222222,
        0x000000,
        0x006600,
        0x22B14C,
        0x02BE01,
        0x51E119,
        0x94E044,
        0xFBFF5B,
        0xE5D900,
        0xE6BE0C,
        0xE59500,
        0xA06A42,
        0x99530D,
        0x633C1F,
        0x6B0000,
        0x9F0000,
        0xE50000,
        0xFF3904,
        0xBB4F00,
        0xFF755F,
        0xFFC49F,
        0xFFDFCC,
        0xFFA7D1,
        0xCF6EE4,
        0xEC08EC,
        0x820080,
        0x5100FF,
        0x020763,
        0x0000EA,
        0x044BFF,
        0x6583CF,
        0x36BAFF,
        0x0083C7,
        0x00D3DD,
        0x45FFC8
    ]);



    const PPML = {
        map: {},
        ws: null,

        hooks: [],
        setHook(hook) {
            PPML.hooks.push(hook);
        },
        setHooks(...hooks) {
            PPML.hooks.push(...hooks.flat());
        },

        hook: {
            priority: 0,

            init(){
                if (PPML.ws) return arguments;
                PPML.ws = this;
                return arguments;
            },

            message({data}) {
                if (PPML.ws != this) return arguments;

                const message = JSON.parse(data.split(/(?<=^\d+)(?=[^\d])/)[1] || '[]');
                if (!message.length) return arguments;

                const [event, json] = message;
                if (event == 'canvas' || event == 'p') json.map(p => PPML.map.set(...p));

                return arguments;
            },
        },

        emit(x,y,pixel) {
            if (!PPML.ws) return;
            if (PPML.map.get(x,y) === 255) return;
            CWSS.send.call(PPML.ws, `42["p",[${x},${y},${pixel},1]]`);
        }
    };
    CWSS.setHook(PPML.hook);



    const Img = window.Image;
    window.Image = function() {
        const img = new Img(...arguments);

        Object.defineProperty(img, 'src', {
            enumerable: true,
            configurable: true,
            set(val) {
                this.setAttribute('src', val);
                if (!val.match(/canvas\/\d+\.png\?/)) return;

                this.addEventListener('load', () => {
                    const canvas = document.createElement('canvas');
                    canvas.width = this.width;
                    canvas.height = this.height;

                    const ctx = canvas.getContext('2d');
                    ctx.drawImage(this, 0, 0);

                    const rgba = ctx.getImageData(0, 0, this.width, this.height).data;
                    const pixels = new Uint8Array(rgba.length>>2);
                    for (let i = 0; i < rgba.length; i += 4)
                        pixels[i>>2] = colors.indexOf((rgba[i]<<16) + (rgba[i+1]<<8) + (rgba[i+2]))-1;

                    const {width, height} = this;
                    const map = {
                        pixels, colors,
                        width, height,
                        find(pixel) {
                            const pos = this.pixels.indexOf(pixel);
                            if (pos > -1) return pos;
                            return null;
                        },
                        get(x,y) {
                            return this.pixels[x+y*this.width];
                        },
                        set(x,y,pixel) {
                            const offset = x+y*this.width;
                            if (this.pixels[offset] == null) return;
                            this.pixels[offset] = pixel;
                        }
                    };

                    Object.assign(PPML.map, map);
                    PPML.hooks.sort((a,b) => b.priority - a.priority).map(h => h.init(map));
                });
            }
        });

        return img;
    };
    Object.assign(Image, Img);
    for (let k in Img.prototype) try {Image.prototype[k] = Img.prototype[k];} catch (e) {};



    window.PPML = PPML;
    return PPML;
})();
// 0vC4#7152