PPML

Pixel Place Map Loader

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/443803/1050258/PPML.js

  1. // ==UserScript==
  2. // @name PPML
  3. // @description Pixel Place Map Loader
  4. // @version 1.6.2
  5. // @author 0vC4
  6. // @namespace https://greasyfork.org/users/670183
  7. // @match https://pixelplace.io/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=pixelplace.io
  9. // @license MIT
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13. (() => {
  14. const PPClient = window.PPClient || {modules:{}};
  15. window.PPClient = PPClient;
  16. if ('MapLoader' in PPClient.modules) return;
  17.  
  18. const module = {};
  19. module.map = {};
  20. module.maps = {};
  21.  
  22. module.args = {};
  23. module.config = ({colors}) => Object.assign(module.args, {colors});
  24.  
  25. module.callbacks = [];
  26. module.subscribe = func => module.callbacks.push(func);
  27.  
  28. const Img = window.Image;
  29. window.Image = function() {
  30. const img = new Img(...arguments);
  31. Object.defineProperty(img, 'src', {
  32. enumerable: true,
  33. configurable: true,
  34. set(val) {
  35. this.setAttribute('src', val);
  36. if (!val.match(/canvas\/\d+\.png\?/)) return;
  37. const serverId = +val.match(/canvas\/(\d+)\.png\?/)[1];
  38. this.addEventListener('load', () => {
  39. const canvas = document.createElement('canvas');
  40. canvas.width = this.width;
  41. canvas.height = this.height;
  42. const ctx = canvas.getContext('2d');
  43. ctx.drawImage(this, 0, 0);
  44. const rgba = ctx.getImageData(0, 0, this.width, this.height).data;
  45. const pixels = new Uint8Array(rgba.length>>2);
  46. for (let i = 0; i < rgba.length; i += 4)
  47. pixels[i>>2] = module.args.colors.indexOf((rgba[i]<<16) + (rgba[i+1]<<8) + (rgba[i+2]));
  48. const {width, height} = this;
  49. const map = {
  50. pixels, width, height, serverId,
  51. get(x,y) {
  52. return this.pixels[x+y*this.width];
  53. },
  54. set(x,y,pixel) {
  55. const offset = x+y*this.width;
  56. if (this.pixels[offset] == null) return;
  57. this.pixels[offset] = pixel;
  58. }
  59. };
  60. if (!('serverId' in module.map)) Object.assign(module.map, map);
  61. if (!PPClient.map) PPClient.map = map;
  62.  
  63. if (!PPClient.maps) PPClient.maps = {};
  64. PPClient.maps[serverId] = map;
  65. module.maps[serverId] = map;
  66.  
  67. module.callbacks.map(f => f(module, map));
  68. });
  69. }
  70. });
  71. return img;
  72. };
  73. Object.assign(Image, Img);
  74. for (let k in Img.prototype) try {Image.prototype[k] = Img.prototype[k];} catch (e) {};
  75.  
  76. PPClient.modules.MapLoader = module;
  77. })();
  78. // 0vC4#7152