Skip to content

Commit

Permalink
rbxthumb 2.0.0
Browse files Browse the repository at this point in the history
* Methods now return content objects
* Remove avatar_item_type_to_thumbnail_type
* Remove urlformats
* Remove format
* Removed subtables for formatters, ie `rbxthumb.avatar.headshot()` is now `rbxthumb.avatar_headshot()`
  • Loading branch information
gaymeowing committed Nov 18, 2024
1 parent 11876cb commit ff4615f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
2 changes: 1 addition & 1 deletion libs/rbxthumb/LIBRARY.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tags = [ "thumbnail", "roblox", "rbxthumb" ]
version = "1.3.0"
version = "2.0.0"

[runtime]
main = "roblox"
126 changes: 64 additions & 62 deletions libs/rbxthumb/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
-- module for easily creating roblox thumbnail urls, based on nevermores rbxthumbutil
-- although with types

type PlayerThumbnailFormatter<sizes> = (player: Player, size: sizes?, circular: boolean?) -> string
type ThumbnailFormatter<sizes> = (id: number, size: sizes?, circular: boolean?) -> string
type PlayerThumbnailFormatter<S> = (player: Player, size: S?, circular: boolean?) -> Content
type ThumbnailFormatter<S> = (id: number, size: S?, circular: boolean?) -> Content

export type ThumbnailType = "Asset" | "Avatar" | "AvatarHeadShot" | "BadgeIcon" |
"BundleThumbnail" | "GameIcon" | "GamePass" | "GroupIcon" | "Outfit" | "FontFamily" |
Expand All @@ -22,109 +22,111 @@ type AvatarBustThumbnailSize = "100x100" | "150x15" | "75×75" | "100×100" | "1
type FullAvatarThumbnailSize = "50×50" | "60×60" | "75×75" | "100×100" | "150×150" | "180×180" |
"352×352" | "420×420"


-- making an enum var rq so no errors occur in lune
local Enum = Enum or table.freeze({
AvatarItemType = table.freeze({
Asset = 1,
})
})

local BASE_URL = "rbxthumb://type=%s&id=%d&w=%d&h=%d"
local CIRCULAR_URL = `{BASE_URL}&filters=circular`
local BASE_URI = "rbxthumb://type=%*&id=%*&w=%*&h=%*"
local CIRCULAR_URI = `{BASE_URI}&filters=circular`
local ASSET_ENUM = Enum.AvatarItemType.Asset
local CREATE_CONTENT = Content.fromUri
local BUNDLE_STR = "BundleThumbnail"
local SPLIT_PATTERN = "(%*)x(%*)"
local FORMAT = string.format
local DEFUALT_SIZE_NUM = 150
local MATCH = string.match
local ASSET_STR = "Asset"
local X = "x"

local function RAW_FORMAT(id: number, type: ThumbnailType, width: number, height: number, circular: boolean?): string
return string.format(
if circular then CIRCULAR_URL else BASE_URL,
local function RAW_FORMAT(
id: number,
type: ThumbnailType,
width: number | string,
height: number | string,
circular: boolean?
): Content
return CREATE_CONTENT(FORMAT(
if circular then CIRCULAR_URI else BASE_URI,
type,
id,
width,
height
)
))
end

local function SPLIT_SIZE(size: ThumbnailSize?, default_width: number, default_height: number): (number, number)
local function SPLIT_SIZE(
size: ThumbnailSize?,
default_width: number,
default_height: number
): (number | string, number | string)
if size then
local splitted = string.split(size, X)
return tonumber(splitted[1]) or default_width, tonumber(splitted[2]) or default_height
local width, height = MATCH(size, SPLIT_PATTERN)
return width or default_width, height or default_height
else
return default_width, default_height
end
end

local function FORMAT_RBXTHUMB_URL(id: number, type: ThumbnailType, size: ThumbnailSize?, circular: boolean?): string
local function FORMAT_RBXTHUMB_URL(
id: number,
type: ThumbnailType,
size: ThumbnailSize?,
circular: boolean?
): Content
local width, height = SPLIT_SIZE(size, DEFUALT_SIZE_NUM, DEFUALT_SIZE_NUM)
return RAW_FORMAT(id, type, width, height, circular)
end

local function CREATE_PLAYER_FORMATTER<sizes>(type: ThumbnailType, default_width: number?, default_height: number?): PlayerThumbnailFormatter<sizes>
local function CREATE_PLAYER_FORMATTER<S>(
type: ThumbnailType,
default_width: number?,
default_height: number?
): PlayerThumbnailFormatter<S>
local defualt_height = default_height or DEFUALT_SIZE_NUM
local defualt_width = default_width or DEFUALT_SIZE_NUM

return function(player: Player, size: sizes?, circular: boolean?): string
return function(player: Player, size: S?, circular: boolean?): Content
local width, height = SPLIT_SIZE(size :: any, defualt_width, defualt_height)
return RAW_FORMAT(player.UserId, type, width, height, circular)
return ( RAW_FORMAT(player.UserId, type, width, height, circular) )
end
end

local function CREATE_FORMATTER<sizes>(type: ThumbnailType, default_width: number?, default_height: number?): ThumbnailFormatter<sizes>
local function CREATE_FORMATTER<S>(
type: ThumbnailType,
default_width: number?,
default_height: number?
): ThumbnailFormatter<S>
local defualt_height = default_height or DEFUALT_SIZE_NUM
local defualt_width = default_width or DEFUALT_SIZE_NUM

return function(id: number, size: sizes?, circular: boolean?): string
return function(id: number, size: S?, circular: boolean?): Content
local width, height = SPLIT_SIZE(size :: any, defualt_width, defualt_height)
return RAW_FORMAT(id, type, width, height, circular)
return ( RAW_FORMAT(id, type, width, height, circular) )
end
end

local function avatar_item_type_to_thumbnail_type(avatar_item_type: Enum.AvatarItemType): "Asset" | "BundleThumbnail"
return if avatar_item_type == ASSET_ENUM then ASSET_STR :: any else BUNDLE_STR :: any
end
local rbxthumb = {
player_headshot = CREATE_PLAYER_FORMATTER("AvatarHeadShot") :: PlayerThumbnailFormatter<AvatarHeadshotThumbnailSize>,
player_bust = CREATE_PLAYER_FORMATTER("AvatarBust") :: PlayerThumbnailFormatter<AvatarBustThumbnailSize>,
avatar_headshot = CREATE_FORMATTER("AvatarHeadShot") :: ThumbnailFormatter<AvatarHeadshotThumbnailSize>,
player_full = CREATE_PLAYER_FORMATTER("Avatar") :: PlayerThumbnailFormatter<FullAvatarThumbnailSize>,
avatar_bust = CREATE_FORMATTER("AvatarBust") :: ThumbnailFormatter<AvatarBustThumbnailSize>,
bundle = CREATE_FORMATTER("BundleThumbnail") :: ThumbnailFormatter<"150x150" | "420x420">,
avatar_full = CREATE_FORMATTER("Avatar") :: ThumbnailFormatter<FullAvatarThumbnailSize>,
fontfamily = CREATE_FORMATTER("FontFamily", 1200, 80) :: ThumbnailFormatter<"1200×80">,
group = CREATE_FORMATTER("GroupIcon") :: ThumbnailFormatter<"150x150" | "420x420">,
outfit = CREATE_FORMATTER("Outfit") :: ThumbnailFormatter<"150x150" | "420x420">,
asset = CREATE_FORMATTER("Asset") :: ThumbnailFormatter<"150x150" | "420x420">,
experience = CREATE_FORMATTER("GameIcon"):: ThumbnailFormatter<"150x150">,
gamepass = CREATE_FORMATTER("GamePass") :: ThumbnailFormatter<"150x150">,
badge = CREATE_FORMATTER("BadgeIcon") :: ThumbnailFormatter<"150x150">,
}

local function avatar_item_to_thumbnail(
function rbxthumb.avatar_item(
avatar_item_type: Enum.AvatarItemType,
id: number,
size: ("150x150" | "420x420")?,
circular: boolean?
): string
): Content
return if avatar_item_type == ASSET_ENUM then
FORMAT_RBXTHUMB_URL(id, ASSET_STR :: any, size, circular)
else
FORMAT_RBXTHUMB_URL(id, BUNDLE_STR :: any, size, circular)
end

local exports = table.freeze {
avatar = table.freeze {
headshot = CREATE_FORMATTER("AvatarHeadShot") :: ThumbnailFormatter<AvatarHeadshotThumbnailSize>,
bust = CREATE_FORMATTER("AvatarBust") :: ThumbnailFormatter<AvatarBustThumbnailSize>,
full = CREATE_FORMATTER("Avatar") :: ThumbnailFormatter<FullAvatarThumbnailSize>,
item = avatar_item_to_thumbnail,
},
player = table.freeze {
headshot = CREATE_PLAYER_FORMATTER("AvatarHeadShot") :: PlayerThumbnailFormatter<AvatarHeadshotThumbnailSize>,
bust = CREATE_PLAYER_FORMATTER("AvatarBust") :: PlayerThumbnailFormatter<AvatarBustThumbnailSize>,
full = CREATE_PLAYER_FORMATTER("Avatar") :: PlayerThumbnailFormatter<FullAvatarThumbnailSize>,
},
bundle = CREATE_FORMATTER("BundleThumbnail") :: ThumbnailFormatter<"150x150" | "420x420">,
fontfamily = CREATE_FORMATTER("FontFamily", 1200, 80) :: ThumbnailFormatter<"1200×80">,
group = CREATE_FORMATTER("GroupIcon") :: ThumbnailFormatter<"150x150" | "420x420">,
outfit = CREATE_FORMATTER("Outfit") :: ThumbnailFormatter<"150x150" | "420x420">,
asset = CREATE_FORMATTER("Asset") :: ThumbnailFormatter<"150x150" | "420x420">,
experience = CREATE_FORMATTER("GameIcon"):: ThumbnailFormatter<"150x150">,
gamepass = CREATE_FORMATTER("GamePass") :: ThumbnailFormatter<"150x150">,
badge = CREATE_FORMATTER("BadgeIcon") :: ThumbnailFormatter<"150x150">,
urlformats = table.freeze {
circular = CIRCULAR_URL,
base = BASE_URL,
},

avatar_item_type_to_thumbnail_type = avatar_item_type_to_thumbnail_type,
format = FORMAT_RBXTHUMB_URL,
}

return exports
return table.freeze(rbxthumb)

0 comments on commit ff4615f

Please sign in to comment.