From b50bf6ceb8307fa0465f1e129557bfb651882536 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Wed, 31 Jan 2024 03:02:19 -0800 Subject: [PATCH] Add SVG support to alt styles Just moving our hacks around and adapting them to the SVG case lol! --- app/controllers/alt_styles_controller.rb | 7 ++++- .../wardrobe-2020/loaders/alt-styles.js | 5 ++-- app/models/alt_style.rb | 10 +------ app/models/swf_asset.rb | 27 +++++++++++++++++++ 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/app/controllers/alt_styles_controller.rb b/app/controllers/alt_styles_controller.rb index 1e27b9f5..d6a8fa03 100644 --- a/app/controllers/alt_styles_controller.rb +++ b/app/controllers/alt_styles_controller.rb @@ -12,7 +12,12 @@ class AltStylesController < ApplicationController format.html { render } format.json { render json: @alt_styles.includes(swf_assets: [:zone]).as_json( - include: {swf_assets: {include: [:zone], methods: [:image_url]}}, + include: { + swf_assets: { + include: [:zone], + methods: [:html5_image_url, :html5_svg_url], + } + }, methods: [:series_name, :adjective_name, :thumbnail_url], ) } diff --git a/app/javascript/wardrobe-2020/loaders/alt-styles.js b/app/javascript/wardrobe-2020/loaders/alt-styles.js index 222646ef..86dbca08 100644 --- a/app/javascript/wardrobe-2020/loaders/alt-styles.js +++ b/app/javascript/wardrobe-2020/loaders/alt-styles.js @@ -73,10 +73,9 @@ function normalizeSwfAssetToLayer(swfAssetData) { // HACK: We're just simplifying this adapter, but it would be better to // actually check what file formats the manifest says! - // TODO: For example, these do generally have SVGs, we could use them! - svgUrl: null, + svgUrl: swfAssetData.html5_svg_url, canvasMovieLibraryUrl: null, - imageUrl: swfAssetData.image_url, + imageUrl: swfAssetData.html5_image_url, swfUrl: swfAssetData.url, }; } diff --git a/app/models/alt_style.rb b/app/models/alt_style.rb index 0f2d285c..3115c6d8 100644 --- a/app/models/alt_style.rb +++ b/app/models/alt_style.rb @@ -34,19 +34,11 @@ class AltStyle < ApplicationRecord "#{color.name.gsub(/\s+/, '').downcase}_#{species.name.downcase}.gif" end - MANIFEST_PATTERN = %r{^https://images.neopets.com/(?.+)/(?[0-9]+)(?_[^/]+)?/manifest\.json} def preview_image_url swf_asset = swf_assets.first return nil if swf_asset.nil? - # HACK: Just assuming all of these were well-formed by the same process, - # and infer the image URL from the manifest URL! But strictly speaking we - # should be reading the manifest to check! - match = swf_asset.manifest_url.match(MANIFEST_PATTERN) - return nil if match.nil? - - "https://images.neopets.com/#{match[:prefix]}/" + - "#{match[:id]}#{match[:hash_part]}/#{match[:id]}.png" + swf_asset.html5_image_url end def biology=(biology) diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index 225332d8..07f5728c 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -110,6 +110,33 @@ class SwfAsset < ApplicationRecord } end + MANIFEST_PATTERN = %r{^https://images.neopets.com/(?.+)/(?[0-9]+)(?_[^/]+)?/manifest\.json} + def html5_image_url + return nil if manifest_url.nil? + + # HACK: Just assuming all of these were well-formed by the same process, + # and infer the image URL from the manifest URL! But strictly speaking we + # should be reading the manifest to check! + match = manifest_url.match(MANIFEST_PATTERN) + return nil if match.nil? + + "https://images.neopets.com/#{match[:prefix]}/" + + "#{match[:id]}#{match[:hash_part]}/#{match[:id]}.png" + end + + def html5_svg_url + return nil if manifest_url.nil? + + # HACK: Just assuming all of these were well-formed by the same process, + # and infer the image URL from the manifest URL! But strictly speaking we + # should be reading the manifest to check! + match = manifest_url.match(MANIFEST_PATTERN) + return nil if match.nil? + + "https://images.neopets.com/#{match[:prefix]}/" + + "#{match[:id]}#{match[:hash_part]}/#{match[:id]}.svg" + end + def known_glitches self[:known_glitches].split(',') end