Skip to main content

vrm_runtime/
meta.rs

1use vrm_spec::vrmc_vrm_1_0::{
2    AvatarPermissionType, CommercialUsageType, CreditNotationType, Meta, ModificationType,
3};
4
5/// The metadata of an avatar: name, authors, licenses and usage permissions.
6#[derive(Debug, Clone, Default)]
7pub struct AvatarMeta {
8    pub(crate) name: String,
9    pub(crate) version: Option<String>,
10    pub(crate) authors: Vec<String>,
11    pub(crate) copyright_information: Option<String>,
12    pub(crate) contact_information: Option<String>,
13    pub(crate) references: Option<Vec<String>>,
14    pub(crate) third_party_licenses: Option<String>,
15    pub(crate) thumbnail_image: Option<u32>,
16    pub(crate) license_url: Option<String>,
17    pub(crate) other_license_url: Option<String>,
18    pub(crate) permissions: AvatarPermissions,
19}
20
21/// The usage permissions of an avatar, as agreed by the author.
22#[derive(Debug, Clone, Default)]
23pub struct AvatarPermissions {
24    /// Who may use the avatar as their character.
25    pub avatar_permission: AvatarPermission,
26    /// The maximum permitted class of commercial usage.
27    pub commercial_usage: CommercialUsage,
28    /// Whether users must credit the author.
29    pub credit_notation: CreditNotation,
30    /// Whether modification and redistribution of modifications are permitted.
31    pub modification: ModificationPermission,
32
33    /// Whether redistribution of the original avatar is permitted.
34    pub allow_redistribution: bool,
35    /// Whether excessively violent usage is permitted.
36    pub allow_excessively_violent_usage: bool,
37    /// Whether excessively sexual usage is permitted.
38    pub allow_excessively_sexual_usage: bool,
39    /// Whether political or religious usage is permitted.
40    pub allow_political_or_religious_usage: bool,
41    /// Whether antisocial or hate-related usage is permitted.
42    pub allow_antisocial_or_hate_usage: bool,
43}
44
45/// Identifies who may use the avatar as their character.
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
47pub enum AvatarPermission {
48    /// Only the author may use the avatar.
49    #[default]
50    OnlyAuthor,
51    /// Only people granted a separate license may use the avatar.
52    OnlySeparatelyLicensedPerson,
53    /// Anyone may use the avatar, subject to the remaining permissions.
54    Everyone,
55}
56
57/// Maximum commercial-use category permitted by the author.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
59pub enum CommercialUsage {
60    /// Only personal, non-profit use is permitted.
61    #[default]
62    PersonalNonProfit,
63    /// Personal for-profit use is also permitted.
64    PersonalProfit,
65    /// Corporate commercial use is also permitted.
66    Corporation,
67}
68
69/// Whether users must show author credit.
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
71pub enum CreditNotation {
72    /// Author credit is required.
73    #[default]
74    Required,
75    /// Author credit is not required.
76    Unnecessary,
77}
78
79/// Modification and modified-work redistribution permission.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
81pub enum ModificationPermission {
82    /// Modification is prohibited.
83    #[default]
84    Prohibited,
85    /// Modification is permitted, but redistribution of modifications is not.
86    AllowModification,
87    /// Both modification and redistribution of modified works are permitted.
88    AllowModificationRedistribution,
89}
90
91impl From<AvatarPermissionType> for AvatarPermission {
92    fn from(val: AvatarPermissionType) -> Self {
93        match val {
94            AvatarPermissionType::OnlyAuthor => Self::OnlyAuthor,
95            AvatarPermissionType::OnlySeparatelyLicensedPerson => {
96                Self::OnlySeparatelyLicensedPerson
97            }
98            AvatarPermissionType::Everyone => Self::Everyone,
99        }
100    }
101}
102
103impl From<CommercialUsageType> for CommercialUsage {
104    fn from(val: CommercialUsageType) -> Self {
105        match val {
106            CommercialUsageType::PersonalNonProfit => Self::PersonalNonProfit,
107            CommercialUsageType::PersonalProfit => Self::PersonalProfit,
108            CommercialUsageType::Corporation => Self::Corporation,
109        }
110    }
111}
112
113impl From<CreditNotationType> for CreditNotation {
114    fn from(val: CreditNotationType) -> Self {
115        match val {
116            CreditNotationType::Required => Self::Required,
117            CreditNotationType::Unnecessary => Self::Unnecessary,
118        }
119    }
120}
121
122impl From<ModificationType> for ModificationPermission {
123    fn from(val: ModificationType) -> Self {
124        match val {
125            ModificationType::Prohibited => Self::Prohibited,
126            ModificationType::AllowModification => Self::AllowModification,
127            ModificationType::AllowModificationRedistribution => {
128                Self::AllowModificationRedistribution
129            }
130        }
131    }
132}
133
134impl AvatarPermissions {
135    pub(crate) fn load_meta(meta: &vrm_spec::vrmc_vrm_1_0::Meta) -> Self {
136        Self {
137            avatar_permission: meta.avatar_permission.map(Into::into).unwrap_or_default(),
138            commercial_usage: meta.commercial_usage.map(Into::into).unwrap_or_default(),
139            credit_notation: meta.credit_notation.map(Into::into).unwrap_or_default(),
140            modification: meta.modification.map(Into::into).unwrap_or_default(),
141            allow_redistribution: meta.allow_redistribution.unwrap_or(false),
142            allow_excessively_violent_usage: meta.allow_excessively_violent_usage.unwrap_or(false),
143            allow_excessively_sexual_usage: meta.allow_excessively_sexual_usage.unwrap_or(false),
144            allow_political_or_religious_usage: meta
145                .allow_political_or_religious_usage
146                .unwrap_or(false),
147            allow_antisocial_or_hate_usage: meta.allow_antisocial_or_hate_usage.unwrap_or(false),
148        }
149    }
150}
151
152impl AvatarMeta {
153    /// Creates the required metadata fields for a programmatic avatar.
154    pub fn new(
155        name: impl Into<String>,
156        authors: impl IntoIterator<Item = impl Into<String>>,
157        license_url: impl Into<String>,
158    ) -> Self {
159        Self {
160            name: name.into(),
161            authors: authors.into_iter().map(Into::into).collect(),
162            license_url: Some(license_url.into()),
163            ..Self::default()
164        }
165    }
166
167    pub(crate) fn load_meta(meta: &Meta) -> Self {
168        Self {
169            name: meta.name.clone(),
170            version: meta.version.clone(),
171            authors: meta.authors.clone(),
172            copyright_information: meta.copyright_information.clone(),
173            contact_information: meta.contact_information.clone(),
174            references: meta.references.clone(),
175            third_party_licenses: meta.third_party_licenses.clone(),
176            thumbnail_image: meta.thumbnail_image.as_ref().map(|i| i.value() as u32),
177            license_url: Some(meta.license_url.clone()),
178            other_license_url: meta.other_license_url.clone(),
179            permissions: AvatarPermissions::load_meta(meta),
180        }
181    }
182
183    /// Returns the avatar title.
184    pub fn name(&self) -> &str {
185        &self.name
186    }
187
188    /// Returns the author-provided avatar version.
189    pub fn version(&self) -> Option<&str> {
190        self.version.as_deref()
191    }
192
193    /// Returns author names in declaration order.
194    pub fn authors(&self) -> &[String] {
195        &self.authors
196    }
197
198    /// Returns free-form copyright information.
199    pub fn copyright_information(&self) -> Option<&str> {
200        self.copyright_information.as_deref()
201    }
202
203    /// Returns author contact information.
204    pub fn contact_information(&self) -> Option<&str> {
205        self.contact_information.as_deref()
206    }
207
208    /// Returns source or reference URLs supplied by the author.
209    pub fn references(&self) -> Option<&[String]> {
210        self.references.as_deref()
211    }
212
213    /// Returns third-party license notices bundled with the avatar.
214    pub fn third_party_licenses(&self) -> Option<&str> {
215        self.third_party_licenses.as_deref()
216    }
217
218    /// Returns the glTF image index of the square thumbnail, if present.
219    pub fn thumbnail_image(&self) -> Option<u32> {
220        self.thumbnail_image
221    }
222
223    /// Returns the required license URL.
224    pub fn license_url(&self) -> Option<&str> {
225        self.license_url.as_deref()
226    }
227
228    /// Returns an additional license URL, if supplied.
229    pub fn other_license_url(&self) -> Option<&str> {
230        self.other_license_url.as_deref()
231    }
232
233    /// Returns the structured usage permissions declared by the author.
234    pub fn permissions(&self) -> &AvatarPermissions {
235        &self.permissions
236    }
237}