From b8283b373caa2ac198497a8dd466bf494d81982a Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Wed, 20 Nov 2024 13:04:14 +0000 Subject: Prettified Code! --- webAO/utils/calculateApngLength.js | 52 ++++++++++++++------------- webAO/utils/calculateGifLength.js | 44 ++++++++++++----------- webAO/utils/calculateWebpLength.js | 34 +++++++++--------- webAO/utils/calculatorHandler.js | 12 +++---- webAO/utils/getAnimLength.js | 34 +++++++++--------- webAO/utils/getResources.js | 72 +++++++++++++++++++------------------- 6 files changed, 127 insertions(+), 121 deletions(-) (limited to 'webAO/utils') diff --git a/webAO/utils/calculateApngLength.js b/webAO/utils/calculateApngLength.js index ab8682a..5d4ea7e 100644 --- a/webAO/utils/calculateApngLength.js +++ b/webAO/utils/calculateApngLength.js @@ -1,30 +1,32 @@ /** - * Adds up the chunk delays to find out how long a APNG is - * @param {data} apngFile the APNG data - */ + * Adds up the chunk delays to find out how long a APNG is + * @param {data} apngFile the APNG data + */ const calculateApngLength = (apngFile) => { - const d = new Uint8Array(apngFile); - // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk - let duration = 0; - for (let i = 0; i < d.length; i++) { - // Find fcTL header (66 63 54 4C) - if (d[i] === 0x66 - && d[i + 1] === 0x63 - && d[i + 2] === 0x54 - && d[i + 3] === 0x4C) { - // numerator and denominator - const delayNum = Number(d[i + 23]); - const delayDen = Number(d[i + 25]); - let delay; - // minimum is 100ms - if (delayDen === 0) { - delay = delayNum / 100; - } else { - delay = delayNum / delayDen; - } - duration += delay; - } + const d = new Uint8Array(apngFile); + // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk + let duration = 0; + for (let i = 0; i < d.length; i++) { + // Find fcTL header (66 63 54 4C) + if ( + d[i] === 0x66 && + d[i + 1] === 0x63 && + d[i + 2] === 0x54 && + d[i + 3] === 0x4c + ) { + // numerator and denominator + const delayNum = Number(d[i + 23]); + const delayDen = Number(d[i + 25]); + let delay; + // minimum is 100ms + if (delayDen === 0) { + delay = delayNum / 100; + } else { + delay = delayNum / delayDen; + } + duration += delay; } - return duration * 10; + } + return duration * 10; }; export default calculateApngLength; diff --git a/webAO/utils/calculateGifLength.js b/webAO/utils/calculateGifLength.js index eccfc77..d8f64c1 100644 --- a/webAO/utils/calculateGifLength.js +++ b/webAO/utils/calculateGifLength.js @@ -1,29 +1,31 @@ /* eslint no-bitwise: "off" */ /** - * Adds up the frame delays to find out how long a GIF is - * I totally didn't steal this - * @param {data} gifFile the GIF data - */ + * Adds up the frame delays to find out how long a GIF is + * I totally didn't steal this + * @param {data} gifFile the GIF data + */ const calculateGifLength = (gifFile) => { - const d = new Uint8Array(gifFile); - // Thanks to http://justinsomnia.org/2006/10/gif-animation-duration-calculation/ - // And http://www.w3.org/Graphics/GIF/spec-gif89a.txt - let duration = 0; - for (let i = 0; i < d.length; i++) { - // Find a Graphic Control Extension hex(21F904__ ____ __00) - if (d[i] === 0x21 - && d[i + 1] === 0xF9 - && d[i + 2] === 0x04 - && d[i + 7] === 0x00) { - // Swap 5th and 6th bytes to get the delay per frame - const delay = (d[i + 5] << 8) | (d[i + 4] & 0xFF); + const d = new Uint8Array(gifFile); + // Thanks to http://justinsomnia.org/2006/10/gif-animation-duration-calculation/ + // And http://www.w3.org/Graphics/GIF/spec-gif89a.txt + let duration = 0; + for (let i = 0; i < d.length; i++) { + // Find a Graphic Control Extension hex(21F904__ ____ __00) + if ( + d[i] === 0x21 && + d[i + 1] === 0xf9 && + d[i + 2] === 0x04 && + d[i + 7] === 0x00 + ) { + // Swap 5th and 6th bytes to get the delay per frame + const delay = (d[i + 5] << 8) | (d[i + 4] & 0xff); - // Should be aware browsers have a minimum frame delay - // e.g. 6ms for IE, 2ms modern browsers (50fps) - duration += delay < 2 ? 10 : delay; - } + // Should be aware browsers have a minimum frame delay + // e.g. 6ms for IE, 2ms modern browsers (50fps) + duration += delay < 2 ? 10 : delay; } - return duration * 10; + } + return duration * 10; }; export default calculateGifLength; diff --git a/webAO/utils/calculateWebpLength.js b/webAO/utils/calculateWebpLength.js index e81a77b..76a69e3 100644 --- a/webAO/utils/calculateWebpLength.js +++ b/webAO/utils/calculateWebpLength.js @@ -1,24 +1,26 @@ /* eslint no-bitwise: "off" */ const calculateWebpLength = (webpFile) => { - const d = new Uint8Array(webpFile); - // https://developers.google.com/speed/webp/docs/riff_container#animation - let duration = 0; - for (let i = 0; i < d.length; i++) { - // Find ANMF header (41 4E 4D 46) - if (d[i] === 0x41 - && d[i + 1] === 0x4E - && d[i + 2] === 0x4D - && d[i + 3] === 0x46) { - // Swap 5th and 6th bytes to get the delay per frame - const delay = (d[i + 21] << 8) | (d[i + 20] & 0xFF); + const d = new Uint8Array(webpFile); + // https://developers.google.com/speed/webp/docs/riff_container#animation + let duration = 0; + for (let i = 0; i < d.length; i++) { + // Find ANMF header (41 4E 4D 46) + if ( + d[i] === 0x41 && + d[i + 1] === 0x4e && + d[i + 2] === 0x4d && + d[i + 3] === 0x46 + ) { + // Swap 5th and 6th bytes to get the delay per frame + const delay = (d[i + 21] << 8) | (d[i + 20] & 0xff); - // Should be aware browsers have a minimum frame delay - // e.g. 6ms for IE, 2ms modern browsers (50fps) - duration += delay < 2 ? 10 : delay; - } + // Should be aware browsers have a minimum frame delay + // e.g. 6ms for IE, 2ms modern browsers (50fps) + duration += delay < 2 ? 10 : delay; } - return duration; + } + return duration; }; export default calculateWebpLength; diff --git a/webAO/utils/calculatorHandler.js b/webAO/utils/calculatorHandler.js index 890b53c..301de0e 100644 --- a/webAO/utils/calculatorHandler.js +++ b/webAO/utils/calculatorHandler.js @@ -1,9 +1,9 @@ -import calculateGifLength from './calculateGifLength'; -import calculateWebpLength from './calculateWebpLength'; -import calculateApngLength from './calculateApngLength'; +import calculateGifLength from "./calculateGifLength"; +import calculateWebpLength from "./calculateWebpLength"; +import calculateApngLength from "./calculateApngLength"; export default { - '.gif': calculateGifLength, - '.webp': calculateWebpLength, - '.apng': calculateApngLength, + ".gif": calculateGifLength, + ".webp": calculateWebpLength, + ".apng": calculateApngLength, }; diff --git a/webAO/utils/getAnimLength.js b/webAO/utils/getAnimLength.js index 2d83844..ac673c2 100644 --- a/webAO/utils/getAnimLength.js +++ b/webAO/utils/getAnimLength.js @@ -2,26 +2,26 @@ /* eslint no-restricted-syntax: "off" */ /* TODO: use promises for this */ -import calculatorHandler from './calculatorHandler'; -import fileExists from './fileExists'; -import { requestBuffer } from '../services/request'; +import calculatorHandler from "./calculatorHandler"; +import fileExists from "./fileExists"; +import { requestBuffer } from "../services/request"; /** - * Gets animation length. If the animation cannot be found, it will - * silently fail and return 0 instead. - * @param {string} filename the animation file name - */ + * Gets animation length. If the animation cannot be found, it will + * silently fail and return 0 instead. + * @param {string} filename the animation file name + */ const getAnimLength = async (url) => { - const extensions = ['.gif', '.webp', '.apng']; - for (const extension of extensions) { - const urlWithExtension = url + extension; - const exists = await fileExists(urlWithExtension); - if (exists) { - const fileBuffer = await requestBuffer(urlWithExtension); - const length = calculatorHandler[extension](fileBuffer); - return length; - } + const extensions = [".gif", ".webp", ".apng"]; + for (const extension of extensions) { + const urlWithExtension = url + extension; + const exists = await fileExists(urlWithExtension); + if (exists) { + const fileBuffer = await requestBuffer(urlWithExtension); + const length = calculatorHandler[extension](fileBuffer); + return length; } - return 0; + } + return 0; }; export default getAnimLength; diff --git a/webAO/utils/getResources.js b/webAO/utils/getResources.js index a711452..9837c77 100644 --- a/webAO/utils/getResources.js +++ b/webAO/utils/getResources.js @@ -1,39 +1,39 @@ const getResources = (AO_HOST, THEME) => ({ - holdit: { - src: `${AO_HOST}misc/default/holdit_bubble.png`, - duration: 720, - }, - objection: { - src: `${AO_HOST}misc/default/objection_bubble.png`, - duration: 720, - }, - takethat: { - src: `${AO_HOST}misc/default/takethat_bubble.png`, - duration: 840, - }, - custom: { - src: '', - duration: 840, - }, - witnesstestimony: { - src: `${AO_HOST}themes/${THEME}/witnesstestimony_bubble.gif`, - duration: 1560, - sfx: `${AO_HOST}sounds/general/sfx-testimony.opus`, - }, - crossexamination: { - src: `${AO_HOST}themes/${THEME}/crossexamination_bubble.gif`, - duration: 1600, - sfx: `${AO_HOST}sounds/general/sfx-testimony2.opus`, - }, - guilty: { - src: `${AO_HOST}themes/${THEME}/guilty_bubble.gif`, - duration: 2870, - sfx: `${AO_HOST}sounds/general/sfx-guilty.opus`, - }, - notguilty: { - src: `${AO_HOST}themes/${THEME}/notguilty_bubble.gif`, - duration: 2440, - sfx: `${AO_HOST}sounds/general/sfx-notguilty.opus`, - }, + holdit: { + src: `${AO_HOST}misc/default/holdit_bubble.png`, + duration: 720, + }, + objection: { + src: `${AO_HOST}misc/default/objection_bubble.png`, + duration: 720, + }, + takethat: { + src: `${AO_HOST}misc/default/takethat_bubble.png`, + duration: 840, + }, + custom: { + src: "", + duration: 840, + }, + witnesstestimony: { + src: `${AO_HOST}themes/${THEME}/witnesstestimony_bubble.gif`, + duration: 1560, + sfx: `${AO_HOST}sounds/general/sfx-testimony.opus`, + }, + crossexamination: { + src: `${AO_HOST}themes/${THEME}/crossexamination_bubble.gif`, + duration: 1600, + sfx: `${AO_HOST}sounds/general/sfx-testimony2.opus`, + }, + guilty: { + src: `${AO_HOST}themes/${THEME}/guilty_bubble.gif`, + duration: 2870, + sfx: `${AO_HOST}sounds/general/sfx-guilty.opus`, + }, + notguilty: { + src: `${AO_HOST}themes/${THEME}/notguilty_bubble.gif`, + duration: 2440, + sfx: `${AO_HOST}sounds/general/sfx-notguilty.opus`, + }, }); export default getResources; -- cgit