Prevent random.burp Sound From Crashing Game

This commit is contained in:
TheBrokenRail 2021-09-12 16:38:41 -04:00
parent d851a8f3e1
commit d18afddf1b
10 changed files with 73 additions and 19 deletions

View File

@ -1 +1 @@
2.2.0 2.2.1

View File

@ -1,5 +1,10 @@
# Changelog # Changelog
**2.2.1**
* Prevent `random.burp` Sound From Crashing Game
* Always Cleanup Media Layer, Even On Crash
* Resolve All Sounds On Startup
**2.2.0** **2.2.0**
* Sound Support * Sound Support
* Split Off "Allow Joining Survival Servers" From Game-Mode Mod * Split Off "Allow Joining Survival Servers" From Game-Mode Mod

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View File

@ -43,17 +43,27 @@ void media_audio_update(float volume, float x, float y, float z, float yaw) {
std::vector<ALuint>::iterator it = get_sources().begin(); std::vector<ALuint>::iterator it = get_sources().begin();
while (it != get_sources().end()) { while (it != get_sources().end()) {
ALuint source = *it; ALuint source = *it;
bool remove = false;
// Check // Check
ALint source_state; if (source && alIsSource(source)) {
alGetSourcei(source, AL_SOURCE_STATE, &source_state); // Is Valid Source
AL_ERROR_CHECK(); ALint source_state;
if (source_state != AL_PLAYING) { alGetSourcei(source, AL_SOURCE_STATE, &source_state);
// Finished
it = get_sources().erase(it);
alDeleteSources(1, &source);
AL_ERROR_CHECK(); AL_ERROR_CHECK();
if (source_state != AL_PLAYING) {
// Finished Playing
remove = true;
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
} else {
// Not A Source
remove = true;
}
// Remove If Needed
if (remove) {
it = get_sources().erase(it);
} else { } else {
// Still Playing
++it; ++it;
} }
} }
@ -62,7 +72,7 @@ void media_audio_update(float volume, float x, float y, float z, float yaw) {
void media_audio_play(const char *source, const char *name, float x, float y, float z, float pitch, float volume, int is_ui) { void media_audio_play(const char *source, const char *name, float x, float y, float z, float pitch, float volume, int is_ui) {
// Load Sound // Load Sound
ALuint buffer = _media_audio_get_buffer(source, name); ALuint buffer = _media_audio_get_buffer(source, name);
if (buffer) { if (volume > 0.0f && buffer) {
// Create Source // Create Source
ALuint al_source; ALuint al_source;
alGenSources(1, &al_source); alGenSources(1, &al_source);

View File

@ -224,7 +224,7 @@ ALuint _media_audio_get_buffer(const char *source, const char *name) {
return _media_audio_get_buffer(source, name); return _media_audio_get_buffer(source, name);
} }
} else { } else {
return 0; ERR("%s", "Audio Engine Isn't Loaded");
} }
} }

View File

@ -310,6 +310,9 @@ void media_cleanup() {
if (is_running) { if (is_running) {
// GLFW And Audio Are Disabled In Headless Mode // GLFW And Audio Are Disabled In Headless Mode
#ifndef MCPI_HEADLESS_MODE #ifndef MCPI_HEADLESS_MODE
// Ignore GLFW Errors During Termination
glfwSetErrorCallback(NULL);
// Terminate GLFW // Terminate GLFW
glfwDestroyWindow(glfw_window); glfwDestroyWindow(glfw_window);
glfwTerminate(); glfwTerminate();
@ -322,6 +325,10 @@ void media_cleanup() {
is_running = 0; is_running = 0;
} }
} }
// Always Cleanup Media Layer
__attribute__((destructor)) static void always_cleanup() {
media_cleanup();
}
// Store Cursor State // Store Cursor State
static int cursor_grabbed = 0; static int cursor_grabbed = 0;

View File

@ -5,6 +5,7 @@
#include <ctime> #include <ctime>
#include <libreborn/libreborn.h> #include <libreborn/libreborn.h>
#include <media-layer/audio.h>
#include "sound.h" #include "sound.h"
@ -314,6 +315,12 @@ static std::unordered_map<std::string, std::vector<std::string>> repository = {
{ {
"PCM_fuse" "PCM_fuse"
} }
},
{
"random.burp",
{
"PCM_burp"
}
} }
} }
}; };
@ -333,3 +340,16 @@ std::string _sound_pick(std::string sound) {
ERR("Invalid Sound: %s", sound.c_str()); ERR("Invalid Sound: %s", sound.c_str());
} }
} }
// Resolve All Sounds
void _sound_resolve_all() {
std::string source = _sound_get_source_file();
if (source.size() > 0) {
for (auto it : repository) {
for (std::string name : it.second) {
// Zero Volume Prevents An OpenAL Source From Being Allocated While Still Resolving The Sound
media_audio_play(source.c_str(), name.c_str(), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);
}
}
}
}

View File

@ -11,7 +11,7 @@
// Resolve Source File Path // Resolve Source File Path
#define SOURCE_FILE_BASE "data/libminecraftpe.so" #define SOURCE_FILE_BASE "data/libminecraftpe.so"
static std::string get_source_file() { std::string _sound_get_source_file() {
static bool source_loaded = false; static bool source_loaded = false;
static std::string source; static std::string source;
@ -56,24 +56,20 @@ static std::string get_source_file() {
source_loaded = true; source_loaded = true;
// Return // Return
return get_source_file(); return _sound_get_source_file();
} }
} }
// Resolve On Startup
__attribute__((constructor)) static void resolve_source_file() {
get_source_file();
}
// Play Sound // Play Sound
// The pitch value is unsued because it causes glitchy sounds, it is seemingly unused in MCPE as well. // The pitch value is unsued because it causes glitchy sounds, it is seemingly unused in MCPE as well.
static void SoundEngine_playUI_injection(__attribute__((unused)) unsigned char *sound_engine, std::string const& name, __attribute__((unused)) float pitch, float volume) { static void SoundEngine_playUI_injection(__attribute__((unused)) unsigned char *sound_engine, std::string const& name, __attribute__((unused)) float pitch, float volume) {
std::string source = get_source_file(); std::string source = _sound_get_source_file();
if (source.size() > 0) { if (source.size() > 0) {
media_audio_play(source.c_str(), _sound_pick(name).c_str(), 0.0f, 0.0f, 0.0f, 1.0f, volume, 1); media_audio_play(source.c_str(), _sound_pick(name).c_str(), 0.0f, 0.0f, 0.0f, 1.0f, volume, 1);
} }
} }
static void SoundEngine_play_injection(__attribute__((unused)) unsigned char *sound_engine, std::string const& name, float x, float y, float z, __attribute__((unused)) float pitch, float volume) { static void SoundEngine_play_injection(__attribute__((unused)) unsigned char *sound_engine, std::string const& name, float x, float y, float z, __attribute__((unused)) float pitch, float volume) {
std::string source = get_source_file(); std::string source = _sound_get_source_file();
if (source.size() > 0) { if (source.size() > 0) {
media_audio_play(source.c_str(), _sound_pick(name).c_str(), x, y, z, 1.0f, volume, 0); media_audio_play(source.c_str(), _sound_pick(name).c_str(), x, y, z, 1.0f, volume, 0);
} }
@ -108,6 +104,16 @@ static void SoundEngine_update_injection(unsigned char *sound_engine, unsigned c
media_audio_update(volume, x, y, z, yaw); media_audio_update(volume, x, y, z, yaw);
} }
// Resolve All Sounds On Init
// SoundEngine::init Is Called After The Audio Engine Has Been Loaded
static void SoundEngine_init_injection(unsigned char *sound_engine, unsigned char *minecraft, unsigned char *options) {
// Call Original Method
(*SoundEngine_init)(sound_engine, minecraft, options);
// Resolve Sounds
_sound_resolve_all();
}
// Init // Init
void init_sound() { void init_sound() {
// Implement Sound Engine // Implement Sound Engine
@ -115,5 +121,6 @@ void init_sound() {
overwrite_calls((void *) SoundEngine_playUI, (void *) SoundEngine_playUI_injection); overwrite_calls((void *) SoundEngine_playUI, (void *) SoundEngine_playUI_injection);
overwrite_calls((void *) SoundEngine_play, (void *) SoundEngine_play_injection); overwrite_calls((void *) SoundEngine_play, (void *) SoundEngine_play_injection);
overwrite_calls((void *) SoundEngine_update, (void *) SoundEngine_update_injection); overwrite_calls((void *) SoundEngine_update, (void *) SoundEngine_update_injection);
overwrite_calls((void *) SoundEngine_init, (void *) SoundEngine_init_injection);
} }
} }

View File

@ -2,4 +2,6 @@
#include <string> #include <string>
__attribute__((visibility("internal"))) std::string _sound_get_source_file();
__attribute__((visibility("internal"))) void _sound_resolve_all();
__attribute__((visibility("internal"))) std::string _sound_pick(std::string sound); __attribute__((visibility("internal"))) std::string _sound_pick(std::string sound);

View File

@ -499,6 +499,9 @@ static Tesselator_color_t Tesselator_color = (Tesselator_color_t) 0x52a48;
// SoundEngine // SoundEngine
typedef void (*SoundEngine_init_t)(unsigned char *sound_engine, unsigned char *minecraft, unsigned char *options);
static SoundEngine_init_t SoundEngine_init = (SoundEngine_init_t) 0x67760;
typedef void (*SoundEngine_enable_t)(unsigned char *sound_engine, bool state); typedef void (*SoundEngine_enable_t)(unsigned char *sound_engine, bool state);
static SoundEngine_enable_t SoundEngine_enable = (SoundEngine_enable_t) 0x6776c; static SoundEngine_enable_t SoundEngine_enable = (SoundEngine_enable_t) 0x6776c;