rbx-reader-rts
Version:
A modern TypeScript library for parsing Roblox binary files (.rbxm, .rbxl) in Node.js and the browser. Provides utilities to extract Animation and Sound asset IDs and is easily extensible.
53 lines (52 loc) • 2.03 kB
text/typescript
import Instance, { InstanceRoot } from './Instance';
/**
* Update all AnimationId properties in the given instance tree according to
* the provided mapping. Each key in the mapping corresponds to an old
* animation ID (as a string or number), and its value is the new ID. Any
* instance that has an AnimationId property matching one of the keys will
* have its AnimationId replaced with `rbxassetid://<newId>`.
*
* @param root The root instance tree to traverse.
* @param mapping A map of old IDs to new IDs (string or number).
*/
export function updateAnimationIds(root: InstanceRoot, mapping: Record<string, string | number>): void {
function traverse(inst: Instance) {
// Check if this instance has a property that looks like an animation ID.
for (const prop in inst.Properties) {
const lower = prop.toLowerCase();
if (lower.includes('animationid')) {
const desc = inst.Properties[prop];
const value = desc?.value;
if (typeof value === 'string') {
// Extract numeric part
const match = value.match(/\d{3,}/);
if (match) {
const oldId = match[0];
const newId = mapping[oldId];
if (newId !== undefined) {
// Update property value via descriptor
inst.Properties[prop].value = `rbxassetid://${newId}`;
}
}
}
}
}
// Special case: Animation class with direct AnimationId property
if (inst.ClassName.toLowerCase() === 'animation') {
const val: any = (inst as any).AnimationId;
if (typeof val === 'string') {
const match = val.match(/\d{3,}/);
if (match) {
const oldId = match[0];
const newId = mapping[oldId];
if (newId !== undefined) {
(inst as any).AnimationId = `rbxassetid://${newId}`;
}
}
}
}
// Traverse children
inst.Children.forEach(child => traverse(child));
}
root.getChildren().forEach(child => traverse(child));
}