mirror of
https://github.com/isledecomp/isle-portable.git
synced 2026-05-01 18:13:57 +00:00
* Add multiplayer extension * Fix animation system to work when host is outside ISLE world - Move TickHostSessions outside m_inIsleWorld gate so the host can coordinate animations from any world - Load animation catalog early in HandleCreate so the host can coordinate before entering the ISLE world - Use network-reported positions for remote player location detection instead of requiring spawned ROIs - Always erase sessions at launch — the host's job ends when the animation starts; clients play and complete independently - Replace BroadcastAnimComplete with locally-driven completion callbacks: host generates eventId at launch, clients cache completion JSON at start time, fire it when ScenePlayer finishes - Make StopAnimation only do local cleanup (stop playback, cancel own interest, reset coordinator) without destroying the session host, so other players' sessions survive world transitions - Broadcast state=0 in ResetAnimationState for full teardown paths (shutdown, reconnect, host migration) so clients aren't left with stale session state * Fix use-after-free crash in ScenePlayer when remote player disconnects mid-animation When a remote player's ROI is destroyed (disconnect, timeout, or respawn), notify all active ScenePlayer instances to null out dangling references before the ROI is freed. The animation engine already handles null ROI map entries gracefully, so playback continues for remaining participants. * Fix crash when performer's child ROIs are left dangling in ScenePlayer NotifyROIDestroyed now walks the parent chain to also invalidate child ROIs of the destroyed performer (head, limbs, etc.) that were placed into the roiMap by BuildROIMap. The ancestor walk happens once; all other fields are cleaned with simple pointer equality. * Allow spectator to play click animation during scene playback * Make PTATCAM track spectator ROI instead of camera in ScenePlayer * Only regenerate emscripten version files when git state changes Replace add_custom_target(ALL) with add_custom_command(OUTPUT) so the version script only runs when .git/HEAD or the current branch ref file changes, instead of on every build. * Fix ROI name collision causing dangling pointers in NPC locomotion roiMaps When ScenePlayer created cloned NPC ROIs for cooperative animations, it renamed them to match the original character name and added them to the ViewManager. This created a name collision: two ROIs with the same name. The original game's AppendROIToScene searches by name and stops at the first match, so if a locomotion BuildROIMap ran while the clone existed, it could capture pointers to the clone's child ROIs. When the clone was later destroyed (CleanupProps), those roiMap entries became dangling pointers, crashing in AnimateWithTransform at roi.h:151 (SetVisibility). Fix: use the alias mechanism (already supported by AnimUtils::BuildROIMap) instead of renaming clones. Also unify all ROI name generation behind a shared counter to prevent character manager key collisions.
219 lines
5.8 KiB
C++
219 lines
5.8 KiB
C++
#include "extensions/multiplayer/animation/phonemeplayer.h"
|
|
|
|
#include "extensions/multiplayer/animation/loader.h"
|
|
#include "flic.h"
|
|
#include "legocharactermanager.h"
|
|
#include "misc.h"
|
|
#include "misc/legocontainer.h"
|
|
#include "mxbitmap.h"
|
|
#include "roi/legoroi.h"
|
|
|
|
#include <SDL3/SDL_stdinc.h>
|
|
|
|
using namespace Multiplayer::Animation;
|
|
|
|
// Find the ROI matching a phoneme track's roiName.
|
|
// Check actor aliases first (participant ROIs whose names differ from animation actor names),
|
|
// then fall back to a direct name search in the roiMap.
|
|
static LegoROI* FindTrackROI(
|
|
const std::string& p_roiName,
|
|
LegoROI** p_roiMap,
|
|
MxU32 p_roiMapSize,
|
|
const std::vector<std::pair<std::string, LegoROI*>>& p_actorAliases
|
|
)
|
|
{
|
|
if (p_roiName.empty() || !p_roiMap) {
|
|
return nullptr;
|
|
}
|
|
|
|
for (const auto& alias : p_actorAliases) {
|
|
if (!SDL_strcasecmp(p_roiName.c_str(), alias.first.c_str())) {
|
|
return alias.second;
|
|
}
|
|
}
|
|
|
|
for (MxU32 i = 1; i < p_roiMapSize; i++) {
|
|
if (p_roiMap[i] && p_roiMap[i]->GetName() && !SDL_strcasecmp(p_roiName.c_str(), p_roiMap[i]->GetName())) {
|
|
return p_roiMap[i];
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void PhonemePlayer::Init(
|
|
const std::vector<SceneAnimData::PhonemeTrack>& p_tracks,
|
|
LegoROI** p_roiMap,
|
|
MxU32 p_roiMapSize,
|
|
const std::vector<std::pair<std::string, LegoROI*>>& p_actorAliases
|
|
)
|
|
{
|
|
for (size_t trackIdx = 0; trackIdx < p_tracks.size(); trackIdx++) {
|
|
auto& track = p_tracks[trackIdx];
|
|
PhonemeState state;
|
|
state.targetROI = nullptr;
|
|
state.originalTexture = nullptr;
|
|
state.cachedTexture = nullptr;
|
|
state.bitmap = nullptr;
|
|
state.currentFrame = -1;
|
|
|
|
// Resolve the target ROI from the track's roiName via aliases or roiMap
|
|
LegoROI* targetROI = FindTrackROI(track.roiName, p_roiMap, p_roiMapSize, p_actorAliases);
|
|
if (!targetROI) {
|
|
m_states.push_back(state);
|
|
continue;
|
|
}
|
|
state.targetROI = targetROI;
|
|
|
|
// If a previous track already set up a cached texture for this ROI, reuse it.
|
|
// Otherwise the second track's "original" would be the first track's cached texture,
|
|
// causing a use-after-free during cleanup.
|
|
PhonemeState* existing = nullptr;
|
|
for (size_t j = 0; j < m_states.size(); j++) {
|
|
if (m_states[j].targetROI == targetROI && m_states[j].cachedTexture) {
|
|
existing = &m_states[j];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (existing) {
|
|
state.cachedTexture = existing->cachedTexture;
|
|
state.bitmap = new MxBitmap();
|
|
state.bitmap->SetSize(track.width, track.height, nullptr, FALSE);
|
|
m_states.push_back(state);
|
|
continue;
|
|
}
|
|
|
|
LegoROI* head = targetROI->FindChildROI("head", targetROI);
|
|
if (!head) {
|
|
m_states.push_back(state);
|
|
continue;
|
|
}
|
|
|
|
LegoTextureInfo* originalInfo = nullptr;
|
|
head->GetTextureInfo(originalInfo);
|
|
if (!originalInfo) {
|
|
m_states.push_back(state);
|
|
continue;
|
|
}
|
|
state.originalTexture = originalInfo;
|
|
|
|
LegoTextureInfo* cached = TextureContainer()->GetCached(originalInfo);
|
|
if (!cached) {
|
|
m_states.push_back(state);
|
|
continue;
|
|
}
|
|
state.cachedTexture = cached;
|
|
|
|
CharacterManager()->SetHeadTexture(targetROI, cached);
|
|
|
|
state.bitmap = new MxBitmap();
|
|
state.bitmap->SetSize(track.width, track.height, nullptr, FALSE);
|
|
|
|
m_states.push_back(state);
|
|
}
|
|
}
|
|
|
|
void PhonemePlayer::Tick(float p_elapsedMs, const std::vector<SceneAnimData::PhonemeTrack>& p_tracks)
|
|
{
|
|
for (size_t i = 0; i < p_tracks.size() && i < m_states.size(); i++) {
|
|
auto& track = p_tracks[i];
|
|
auto& state = m_states[i];
|
|
|
|
if (!state.bitmap || !state.cachedTexture) {
|
|
continue;
|
|
}
|
|
|
|
float trackElapsed = p_elapsedMs - (float) track.timeOffset;
|
|
if (trackElapsed < 0.0f) {
|
|
continue;
|
|
}
|
|
|
|
if (track.flcHeader->speed == 0) {
|
|
continue;
|
|
}
|
|
|
|
int targetFrame = (int) (trackElapsed / (float) track.flcHeader->speed);
|
|
if (targetFrame == state.currentFrame) {
|
|
continue;
|
|
}
|
|
if (targetFrame >= (int) track.frameData.size()) {
|
|
continue;
|
|
}
|
|
|
|
int startFrame = state.currentFrame + 1;
|
|
if (startFrame < 0) {
|
|
startFrame = 0;
|
|
}
|
|
|
|
for (int f = startFrame; f <= targetFrame; f++) {
|
|
const auto& data = track.frameData[f];
|
|
if (data.size() < sizeof(MxS32)) {
|
|
continue;
|
|
}
|
|
|
|
MxS32 rectCount;
|
|
SDL_memcpy(&rectCount, data.data(), sizeof(MxS32));
|
|
size_t headerSize = sizeof(MxS32) + rectCount * sizeof(MxRect32);
|
|
if (data.size() <= headerSize) {
|
|
continue;
|
|
}
|
|
|
|
FLIC_FRAME* flcFrame = (FLIC_FRAME*) (data.data() + headerSize);
|
|
|
|
BYTE decodedColorMap;
|
|
DecodeFLCFrame(
|
|
&state.bitmap->GetBitmapInfo()->m_bmiHeader,
|
|
state.bitmap->GetImage(),
|
|
track.flcHeader,
|
|
flcFrame,
|
|
&decodedColorMap
|
|
);
|
|
|
|
// When the FLC frame updates the palette, apply it to the texture surface
|
|
if (decodedColorMap && state.cachedTexture->m_palette) {
|
|
PALETTEENTRY entries[256];
|
|
RGBQUAD* colors = state.bitmap->GetBitmapInfo()->m_bmiColors;
|
|
for (int c = 0; c < 256; c++) {
|
|
entries[c].peRed = colors[c].rgbRed;
|
|
entries[c].peGreen = colors[c].rgbGreen;
|
|
entries[c].peBlue = colors[c].rgbBlue;
|
|
entries[c].peFlags = PC_NONE;
|
|
}
|
|
state.cachedTexture->m_palette->SetEntries(0, 0, 256, entries);
|
|
}
|
|
}
|
|
|
|
state.cachedTexture->LoadBits(state.bitmap->GetImage());
|
|
state.currentFrame = targetFrame;
|
|
}
|
|
}
|
|
|
|
void PhonemePlayer::NotifyROIDestroyed(LegoROI* p_roi)
|
|
{
|
|
for (auto& state : m_states) {
|
|
if (state.targetROI == p_roi) {
|
|
state.targetROI = nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
void PhonemePlayer::Cleanup()
|
|
{
|
|
for (size_t i = 0; i < m_states.size(); i++) {
|
|
auto& state = m_states[i];
|
|
|
|
// Only the state that owns the original texture (i.e. performed the initial setup)
|
|
// should restore and erase. Other states sharing the same cachedTexture are secondary.
|
|
if (state.targetROI && state.originalTexture) {
|
|
CharacterManager()->SetHeadTexture(state.targetROI, state.originalTexture);
|
|
}
|
|
|
|
if (state.originalTexture && state.cachedTexture) {
|
|
TextureContainer()->EraseCached(state.cachedTexture);
|
|
}
|
|
|
|
delete state.bitmap;
|
|
}
|
|
m_states.clear();
|
|
}
|