Skip to content

Commit

Permalink
Deal with the fallout of hsStream::close() being de-virtualzed.
Browse files Browse the repository at this point in the history
This also adds some more debug information for explosions mentioned
in #378 and #173.
  • Loading branch information
Hoikas committed Sep 22, 2023
1 parent 11e8b2b commit 17afd0e
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions korman/properties/modifiers/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from bpy.app.handlers import persistent
from contextlib import contextmanager
import math
import os.path
from PyHSPlasma import *
from typing import *

Expand Down Expand Up @@ -429,34 +430,22 @@ def _convert_sound(self, exporter, so, pClass, wavHeader, dataSize, channel=None

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
Expand Down Expand Up @@ -494,6 +483,22 @@ def _idprop_sources(self):
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))
Expand Down

0 comments on commit 17afd0e

Please sign in to comment.