2023-07-22 13:46:40 +01:00
|
|
|
--!nocheck
|
2023-09-25 21:48:02 +01:00
|
|
|
--!nolint UnknownGlobal
|
2023-07-22 13:46:40 +01:00
|
|
|
|
2023-03-17 09:44:21 +00:00
|
|
|
-- NOTE: This must be ran in Roblox Studio to get up-to-date font values
|
|
|
|
|
|
|
|
local contents = ""
|
|
|
|
|
2023-03-17 14:05:10 +00:00
|
|
|
contents ..= "\ntype FontData = (&'static str, FontWeight, FontStyle);\n"
|
|
|
|
|
2023-03-17 15:54:13 +00:00
|
|
|
local ENUM_IMPLEMENTATION = [[
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub(crate) enum <<ENUM_NAME>> {
|
|
|
|
<<ENUM_NAMES>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <<ENUM_NAME>> {
|
2023-03-18 07:39:46 +00:00
|
|
|
pub(crate) fn as_<<NUMBER_TYPE>>(&self) -> <<NUMBER_TYPE>> {
|
2023-03-17 15:54:13 +00:00
|
|
|
match self {
|
|
|
|
<<ENUM_TO_NUMBERS>>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 07:39:46 +00:00
|
|
|
pub(crate) fn from_<<NUMBER_TYPE>>(n: <<NUMBER_TYPE>>) -> Option<Self> {
|
2023-03-17 15:54:13 +00:00
|
|
|
match n {
|
|
|
|
<<NUMBERS_TO_ENUM>>
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 07:39:46 +00:00
|
|
|
impl Default for <<ENUM_NAME>> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::<<DEFAULT_NAME>>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 15:54:13 +00:00
|
|
|
impl std::str::FromStr for <<ENUM_NAME>> {
|
2023-03-18 07:39:46 +00:00
|
|
|
type Err = &'static str;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
2023-03-17 15:54:13 +00:00
|
|
|
<<STRINGS_TO_ENUM>>
|
2023-03-18 07:39:46 +00:00
|
|
|
_ => Err("Unknown <<ENUM_NAME>>"),
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 15:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for <<ENUM_NAME>> {
|
2023-03-18 07:39:46 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(
|
2023-03-17 15:54:13 +00:00
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
match self {
|
|
|
|
<<ENUM_TO_STRINGS>>
|
|
|
|
}
|
|
|
|
)
|
2023-03-18 07:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lua> FromLua<'lua> for <<ENUM_NAME>> {
|
|
|
|
fn from_lua(lua_value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> {
|
|
|
|
let mut message = None;
|
|
|
|
if let LuaValue::UserData(ud) = &lua_value {
|
|
|
|
let value = ud.borrow::<EnumItem>()?;
|
|
|
|
if value.parent.desc.name == "<<ENUM_NAME>>" {
|
|
|
|
if let Ok(value) = <<ENUM_NAME>>::from_str(&value.name) {
|
|
|
|
return Ok(value);
|
|
|
|
} else {
|
|
|
|
message = Some(format!(
|
|
|
|
"Found unknown Enum.<<ENUM_NAME>> value '{}'",
|
|
|
|
value.name
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message = Some(format!(
|
|
|
|
"Expected Enum.<<ENUM_NAME>>, got Enum.{}",
|
|
|
|
value.parent.desc.name
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(LuaError::FromLuaConversionError {
|
|
|
|
from: lua_value.type_name(),
|
|
|
|
to: "Enum.<<ENUM_NAME>>",
|
|
|
|
message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lua> ToLua<'lua> for <<ENUM_NAME>> {
|
|
|
|
fn to_lua(self, lua: &'lua Lua) -> LuaResult<LuaValue<'lua>> {
|
|
|
|
match EnumItem::from_enum_name_and_name("<<ENUM_NAME>>", self.to_string()) {
|
|
|
|
Some(enum_item) => Ok(LuaValue::UserData(lua.create_userdata(enum_item)?)),
|
|
|
|
None => Err(LuaError::ToLuaConversionError {
|
|
|
|
from: "<<ENUM_NAME>>",
|
|
|
|
to: "EnumItem",
|
|
|
|
message: Some(format!("Found unknown Enum.<<ENUM_NAME>> value '{}'", self)),
|
|
|
|
}),
|
|
|
|
}
|
2023-03-17 15:54:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
2023-03-17 14:05:10 +00:00
|
|
|
-- FontWeight enum and implementation
|
|
|
|
|
2023-03-18 07:39:46 +00:00
|
|
|
local function makeRustEnum(enum, default, numType: string)
|
|
|
|
local name = tostring(enum)
|
|
|
|
name = string.gsub(name, "^Enum.", "")
|
|
|
|
|
|
|
|
local defaultName = tostring(default)
|
|
|
|
defaultName = string.gsub(defaultName, "^Enum.", "")
|
|
|
|
defaultName = string.gsub(defaultName, "^" .. name .. ".", "")
|
2023-03-17 14:05:10 +00:00
|
|
|
|
2023-03-17 15:54:13 +00:00
|
|
|
local enumNames = ""
|
|
|
|
local enumToNumbers = ""
|
|
|
|
local numbersToEnum = ""
|
|
|
|
local stringsToEnum = ""
|
|
|
|
local enumToStrings = ""
|
2023-03-17 14:05:10 +00:00
|
|
|
|
|
|
|
for _, enum in enum:GetEnumItems() do
|
2023-03-17 15:54:13 +00:00
|
|
|
enumNames ..= `\n{enum.Name},`
|
|
|
|
enumToNumbers ..= `\nSelf::{enum.Name} => {enum.Value},`
|
|
|
|
numbersToEnum ..= `\n{enum.Value} => Some(Self::{enum.Name}),`
|
|
|
|
stringsToEnum ..= `\n"{enum.Name}" => Ok(Self::{enum.Name}),`
|
|
|
|
enumToStrings ..= `\nSelf::{enum.Name} => "{enum.Name}",`
|
2023-03-17 14:05:10 +00:00
|
|
|
end
|
|
|
|
|
2023-03-17 15:54:13 +00:00
|
|
|
local mappings: { [string]: string } = {
|
|
|
|
["<<ENUM_NAME>>"] = name,
|
2023-03-18 07:39:46 +00:00
|
|
|
["<<DEFAULT_NAME>>"] = defaultName,
|
2023-03-17 15:54:13 +00:00
|
|
|
["<<NUMBER_TYPE>>"] = numType,
|
|
|
|
["<<ENUM_NAMES>>"] = enumNames,
|
|
|
|
["<<ENUM_TO_NUMBERS>>"] = enumToNumbers,
|
|
|
|
["<<ENUM_TO_STRINGS>>"] = enumToStrings,
|
|
|
|
["<<NUMBERS_TO_ENUM>>"] = numbersToEnum,
|
|
|
|
["<<STRINGS_TO_ENUM>>"] = stringsToEnum,
|
|
|
|
}
|
|
|
|
|
|
|
|
local result = ENUM_IMPLEMENTATION
|
|
|
|
for key, replacement in mappings do
|
|
|
|
result = string.gsub(result, "(\t*)" .. key, function(tabbing)
|
|
|
|
local spacing = string.gsub(tabbing, "\t", " ")
|
|
|
|
local inner = string.gsub(replacement, "\n", "\n" .. spacing)
|
|
|
|
inner = string.gsub(inner, "^\n+", "")
|
|
|
|
return inner
|
|
|
|
end)
|
2023-03-17 14:05:10 +00:00
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2023-03-18 07:39:46 +00:00
|
|
|
contents ..= makeRustEnum(Enum.FontWeight, Enum.FontWeight.Regular, "u16")
|
2023-03-17 14:05:10 +00:00
|
|
|
contents ..= "\n"
|
|
|
|
|
2023-03-18 07:39:46 +00:00
|
|
|
contents ..= makeRustEnum(Enum.FontStyle, Enum.FontStyle.Normal, "u8")
|
2023-03-17 14:05:10 +00:00
|
|
|
contents ..= "\n"
|
|
|
|
|
|
|
|
-- Font constant map from enum to font data
|
|
|
|
|
|
|
|
local longestNameLen = 0
|
2023-03-17 15:54:13 +00:00
|
|
|
local longestFamilyLen = 0
|
|
|
|
local longestWeightLen = 0
|
2023-03-17 14:05:10 +00:00
|
|
|
for _, enum in Enum.Font:GetEnumItems() do
|
|
|
|
longestNameLen = math.max(longestNameLen, #enum.Name)
|
2023-03-17 15:54:13 +00:00
|
|
|
if enum ~= Enum.Font.Unknown then
|
|
|
|
local font = Font.fromEnum(enum)
|
|
|
|
longestFamilyLen = math.max(longestFamilyLen, #font.Family)
|
|
|
|
longestWeightLen = math.max(longestWeightLen, #font.Weight.Name)
|
|
|
|
end
|
2023-03-17 14:05:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
contents ..= "\n#[rustfmt::skip]\nconst FONT_ENUM_MAP: &[(&str, Option<FontData>)] = &[\n"
|
2023-03-17 09:44:21 +00:00
|
|
|
for _, enum in Enum.Font:GetEnumItems() do
|
|
|
|
if enum == Enum.Font.Unknown then
|
2023-03-17 15:54:13 +00:00
|
|
|
contents ..= string.format(
|
|
|
|
' ("Unknown",%s None),\n',
|
|
|
|
string.rep(" ", longestNameLen - #enum.Name)
|
|
|
|
)
|
2023-03-17 09:44:21 +00:00
|
|
|
else
|
|
|
|
local font = Font.fromEnum(enum)
|
|
|
|
contents ..= string.format(
|
2023-03-17 15:54:13 +00:00
|
|
|
' ("%s",%s Some(("%s",%s FontWeight::%s,%s FontStyle::%s))),\n',
|
2023-03-17 09:44:21 +00:00
|
|
|
enum.Name,
|
2023-03-17 14:05:10 +00:00
|
|
|
string.rep(" ", longestNameLen - #enum.Name),
|
2023-03-17 09:44:21 +00:00
|
|
|
font.Family,
|
2023-03-17 15:54:13 +00:00
|
|
|
string.rep(" ", longestFamilyLen - #font.Family),
|
2023-03-17 09:44:21 +00:00
|
|
|
font.Weight.Name,
|
2023-03-17 15:54:13 +00:00
|
|
|
string.rep(" ", longestWeightLen - #font.Weight.Name),
|
2023-03-17 09:44:21 +00:00
|
|
|
font.Style.Name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
contents ..= "];\n"
|
|
|
|
|
|
|
|
print(contents)
|