Browse Source

Deal with the fallout of `hsStream::close()` being de-virtualzed.

This also adds some more debug information for explosions mentioned
in #378 and #173.
pull/214/merge
Adam Johnson 7 months ago
parent
commit
17afd0e614
Signed by: Hoikas
GPG Key ID: 0B6515D6FF6F271E
  1. 55
      korman/properties/modifiers/sound.py

55
korman/properties/modifiers/sound.py

@ -20,6 +20,7 @@ from bpy.props import *
from bpy.app.handlers import persistent
from contextlib import contextmanager
import math
import os.path
from PyHSPlasma import *
from typing import *
@ -429,34 +430,22 @@ class PlasmaSound(idprops.IDPropMixin, bpy.types.PropertyGroup):
def _get_sound_info(self):
"""Generates a tuple (plWAVHeader, PCMsize) from the current sound"""
sound = self._sound
if sound.packed_file is None:
stream = hsFileStream()
try:
stream.open(bpy.path.abspath(sound.filepath), fmRead)
except IOError:
self._raise_error("failed to open file")
else:
stream = hsRAMStream()
stream.buffer = sound.packed_file.data
try:
magic = stream.read(4)
stream.rewind()
header = plWAVHeader()
if magic == b"RIFF":
size = korlib.inspect_wavefile(stream, header)
return (header, size)
elif magic == b"OggS":
size = korlib.inspect_vorbisfile(stream, header)
return (header, size)
else:
raise NotSupportedError("unsupported audio format")
with self._open_sound_stream() as stream:
magic = stream.read(4)
stream.rewind()
header = plWAVHeader()
if magic == b"RIFF":
size = korlib.inspect_wavefile(stream, header)
return (header, size)
elif magic == b"OggS":
size = korlib.inspect_vorbisfile(stream, header)
return (header, size)
else:
raise NotImplementedError("unsupported audio format")
except Exception as e:
self._raise_error(str(e))
finally:
stream.close()
def _find_sound_buffer(self, exporter, so, wavHeader, dataSize, channel):
# First, cleanup the file path to not have directories
@ -494,6 +483,22 @@ class PlasmaSound(idprops.IDPropMixin, bpy.types.PropertyGroup):
def is_3d_stereo(self):
return self.sfx_type == "kSoundFX" and self.channel == {"L", "R"} and self.is_stereo
@contextmanager
def _open_sound_stream(self):
sound = self._sound
if sound.packed_file is None:
filepath = sound.filepath
if not os.path.exists(filepath):
filepath = bpy.path.abspath(filepath)
if not os.path.exists(filepath):
self._raise_error(f"Sound file not found! Requested '{sound.filepath}' - resolved to '{filepath}'")
with hsFileStream().open(filepath, fmRead) as fs:
yield fs
else:
stream = hsRAMStream()
stream.buffer = sound.packed_file.data
yield stream
def _raise_error(self, msg):
if self.sound:
raise ExportError("SoundEmitter '{}': Sound '{}' {}".format(self.id_data.name, self.sound.name, msg))

Loading…
Cancel
Save