Skip to content

Commit

Permalink
Added animation usage check
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanV2 committed Nov 9, 2021
1 parent 4a52f75 commit ff69cf6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Lib/Diagnostics/BehaviorPack/Animation/usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Internal } from "bc-minecraft-bedrock-project";
import { Types } from "bc-minecraft-bedrock-types";
import { DiagnosticsBuilder } from "../../../Types/DiagnosticsBuilder/DiagnosticsBuilder";
import { minecraft_animation_used } from "../../Minecraft/Animation";

export function behaviorpack_animation_used(animations: Types.Definition | undefined, diagnoser: DiagnosticsBuilder, script?: Internal.Script): void {
if (animations === undefined) return;

minecraft_animation_used(animations, diagnoser, diagnoser.context.getCache().BehaviorPacks.animation_controllers, script);
}
4 changes: 4 additions & 0 deletions src/Lib/Diagnostics/BehaviorPack/Entity/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Types } from "bc-minecraft-bedrock-types";
import { diagnose_molang } from "../../Molang/diagnostics";
import { diagnose_script } from "../../Minecraft/Script";
import { behaviorpack_entity_check_events } from "./events";
import { behaviorpack_animation_used } from "../Animation/usage";

/**Diagnoses the given document as an bp entity
* @param doc The text document to diagnose
Expand Down Expand Up @@ -49,4 +50,7 @@ export function Diagnose(doc: TextDocument, diagnoser: DiagnosticsBuilder): void

//Check events
if (container.events) behaviorpack_entity_check_events(container.events, diagnoser, container.component_groups);

//Check used animations
behaviorpack_animation_used(container.description.animations, diagnoser, container.description.scripts);
}
39 changes: 39 additions & 0 deletions src/Lib/Diagnostics/Minecraft/Animation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Internal, ResourcePack, BehaviorPack } from "bc-minecraft-bedrock-project";
import { DataSetConnector } from "bc-minecraft-bedrock-project/lib/src/Lib/Types/DataSet/DataSetConnector";
import { Types } from "bc-minecraft-bedrock-types";
import { DiagnosticsBuilder } from "../../Types/DiagnosticsBuilder/DiagnosticsBuilder";
import { DiagnosticSeverity } from "../../Types/DiagnosticsBuilder/Severity";

export function minecraft_animation_used(
animations: Types.Definition,
diagnoser: DiagnosticsBuilder,
controllers: DataSetConnector<
ResourcePack.AnimationController.AnimationController | BehaviorPack.AnimationController.AnimationController,
ResourcePack.ResourcePack
>,
script?: Internal.Script
): void {
const used: string[] = [];

if (script) {
Types.Conditional.forEach(script.animate, (ref, condition) => used.push(ref));
}

//Extract animations from controllers
Types.Definition.forEach(animations, (ref, id) => {
const controller = controllers.get(id);

//If it is a controller, then add used animations
if (controller) {
used.push(...controller.animations.using);
}
});

//
Types.Definition.forEach(animations, (ref, id) => {
//If the used animations does not contain the referenced animation, then its unused
if (!used.includes(ref)) {
diagnoser.Add(`${ref}/${id}`, `Animation: ${id} is not being used, could be removed`, DiagnosticSeverity.info, `minecraft.animation.unused`);
}
});
}
10 changes: 10 additions & 0 deletions src/Lib/Diagnostics/ResourcePack/Animation/usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Internal } from "bc-minecraft-bedrock-project";
import { Types } from "bc-minecraft-bedrock-types";
import { DiagnosticsBuilder } from "../../../Types/DiagnosticsBuilder/DiagnosticsBuilder";
import { minecraft_animation_used } from "../../Minecraft/Animation";

export function resourcepack_animation_used(animations: Types.Definition | undefined, diagnoser: DiagnosticsBuilder, script?: Internal.Script): void {
if (animations === undefined) return;

minecraft_animation_used(animations, diagnoser, diagnoser.context.getCache().ResourcePacks.animation_controllers, script);
}
Binary file modified src/Lib/Diagnostics/ResourcePack/Entity/entry.ts
Binary file not shown.

0 comments on commit ff69cf6

Please sign in to comment.