Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: NH::HashString Memory Violation #44

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Gothic/BassLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ namespace GOTHIC_NAMESPACE
std::shared_ptr<NH::Bass::MusicTheme> theme = std::make_shared<NH::Bass::MusicTheme>(input->Name.ToChar());
theme->SetAudioEffects(NH::Bass::AudioFile::DEFAULT, [](NH::Bass::AudioEffects& effects){});
auto zones = NH::String(input->Zones.ToChar()).Split(",");
for (auto& zone: zones) { theme->AddZone(zone.MakeUpper()); }
for (auto& zone: zones) { theme->AddZone(std::string(zone.MakeUpper().ToChar())); }
// input->Type ignored for now
NH::Bass::Engine::GetInstance()->GetMusicManager().AddTheme(input->Name.ToChar(), theme);
});
Expand All @@ -127,8 +127,8 @@ namespace GOTHIC_NAMESPACE
[&]() { return m_BassThemeAudioInstances.emplace_back(new BassMusicThemeAudio{}); },
[&](BassMusicThemeAudio* input, zCPar_Symbol* symbol) {
std::shared_ptr<NH::Bass::MusicTheme> theme = NH::Bass::Engine::GetInstance()->GetMusicManager().GetTheme(input->Theme.ToChar());
NH::String type = NH::String(input->Type.ToChar());
NH::HashString id = type == "DEFAULT" ? NH::Bass::AudioFile::DEFAULT : NH::HashString(type);
auto type = NH::String(input->Type.ToChar());
const std::string id = type == "DEFAULT" ? NH::Bass::AudioFile::DEFAULT : type.ToChar();
theme->SetAudioFile(id, input->Filename.ToChar());
theme->SetAudioEffects(id, [&](NH::Bass::AudioEffects& effects) {
effects.Loop.Active = input->Loop;
Expand All @@ -154,8 +154,8 @@ namespace GOTHIC_NAMESPACE
});
if (!input->MidiFile.IsEmpty())
{
auto midiFile = std::make_shared<NH::Bass::MidiFile>(theme->GetName(), NH::HashString(NH::String(input->MidiFile.ToChar())));
theme->AddMidiFile(NH::HashString(""), midiFile);
auto midiFile = std::make_shared<NH::Bass::MidiFile>(theme->GetName(), NH::String(input->MidiFile.ToChar()));
theme->AddMidiFile("", midiFile);
}
NH::Bass::Engine::GetInstance()->GetMusicManager().RefreshTheme(theme->GetName());
});
Expand Down
14 changes: 7 additions & 7 deletions src/Gothic/CMusicSys_Bass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace GOTHIC_NAMESPACE
static NH::Logger* log = NH::CreateLogger("zBassMusic::Event_OnEnd");

NH::Bass::Event::MusicEnd data = std::get<NH::Bass::Event::MusicEnd>(event.Data);
zSTRING filename{ data.Theme->GetAudioFile(data.AudioId).Filename.ToChar() };
zSTRING name{ data.Theme->GetName() };
zSTRING filename{ data.Theme->GetAudioFile(data.AudioId).Filename.c_str() };
zSTRING name{ data.Theme->GetName().c_str() };
log->Trace("{0}, {1}", name.ToChar(), filename.ToChar());

for (int i = 0; i < Globals->Event_OnEnd_Functions.GetNumInList(); i++)
Expand All @@ -25,8 +25,8 @@ namespace GOTHIC_NAMESPACE
static NH::Logger* log = NH::CreateLogger("zBassMusic::Event_OnTransition");

NH::Bass::Event::MusicTransition data = std::get<NH::Bass::Event::MusicTransition>(event.Data);
zSTRING filename{ data.Theme->GetAudioFile(data.AudioId).Filename.ToChar() };
zSTRING name{ data.Theme->GetName() };
zSTRING filename{ data.Theme->GetAudioFile(data.AudioId).Filename.c_str() };
zSTRING name{ data.Theme->GetName().c_str() };
float timeLeft = data.TimeLeft;
log->Trace("{0}, {1}", name.ToChar(), filename.ToChar());

Expand All @@ -44,8 +44,8 @@ namespace GOTHIC_NAMESPACE
static NH::Logger* log = NH::CreateLogger("zBassMusic::Event_OnChange");

NH::Bass::Event::MusicChange data = std::get<NH::Bass::Event::MusicChange>(event.Data);
zSTRING filename{ data.Theme->GetAudioFile(data.AudioId).Filename.ToChar() };
zSTRING name{ data.Theme->GetName() };
zSTRING filename{ data.Theme->GetAudioFile(data.AudioId).Filename.c_str() };
zSTRING name{ data.Theme->GetName().c_str() };
log->Trace("{0}, {1}", name.ToChar(), filename.ToChar());

Globals->BassMusic_ActiveThemeFilename = filename;
Expand Down Expand Up @@ -169,7 +169,7 @@ namespace GOTHIC_NAMESPACE

nowPlaying = identifier;
m_ActiveTheme = nullptr;
m_BassEngine->GetCommandQueue().AddCommand(std::make_shared<NH::Bass::ChangeZoneCommand>(NH::String(identifier.ToChar())));
m_BassEngine->GetCommandQueue().AddCommand(std::make_shared<NH::Bass::ChangeZoneCommand>(identifier.ToChar()));

if (done)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Gothic/Externals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ namespace GOTHIC_NAMESPACE
parser->GetParameter(filter);
parser->GetParameter(theme);

auto target = NH::Bass::Engine::GetInstance()->GetMusicManager().GetTheme(NH::String(theme));
auto target = NH::Bass::Engine::GetInstance()->GetMusicManager().GetTheme(theme.ToChar());
if (!target)
{
log->Error("Theme {0} not found", theme.ToChar());
return 0;
}

target->AddMidiFile(NH::String(filter), std::make_shared<NH::Bass::MidiFile>(target->GetName(), NH::String(midiFilename.ToChar())));
target->AddMidiFile(filter.ToChar(), std::make_shared<NH::Bass::MidiFile>(target->GetName(), NH::String(midiFilename.ToChar())));
return 0;
}

Expand All @@ -120,15 +120,15 @@ namespace GOTHIC_NAMESPACE
parser->GetParameter(filter);
parser->GetParameter(theme);

auto target = NH::Bass::Engine::GetInstance()->GetMusicManager().GetTheme(NH::String(theme.ToChar()));
auto target = NH::Bass::Engine::GetInstance()->GetMusicManager().GetTheme(theme.ToChar());
if (!target)
{
log->Error("Theme {0} not found", theme.ToChar());
return 0;
}

NH::Bass::Transition::TimePoint tp { start, duration, (NH::Bass::TransitionEffect)effect, nextStart, nextDuration, (NH::Bass::TransitionEffect)nextEffect };
target->GetTransitionInfo().AddTimePoint(tp, NH::String(filter.ToChar()));
target->GetTransitionInfo().AddTimePoint(tp, filter.ToChar());

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/NH/Bass/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ namespace NH::Bass
if (!m_Stream)
{
int code = BASS_ErrorGetCode();
log->Error("Could not create stream: {0}\n error: {1}\n at {2}:{3}", audioFile.Filename, Engine::ErrorCodeToString(code), __FILE__, __LINE__);
return std::unexpected(Error{ IChannel::ErrorType::INVALID_BUFFER, code, Engine::ErrorCodeToString(code) });
log->Error("Could not create stream: {0}\n error: {1}\n at {2}:{3}", audioFile.Filename.c_str(), Engine::ErrorCodeToString(code), __FILE__, __LINE__);
return std::unexpected(Error{ IChannel::ErrorType::INVALID_BUFFER, code, Engine::ErrorCodeToString(code).ToChar() });
}

BASS_ChannelStart(m_Stream);
Expand Down
7 changes: 1 addition & 6 deletions src/NH/Bass/Engine.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#pragma once

#include <bass.h>
#include "EventManager.h"
#include "Channel.h"
#include "NH/Logger.h"
#include "NH/HashString.h"
#include <NH/Bass/IEngine.h>
#include <NH/Bass/MusicManager.h>
#include <NH/Bass/Command.h>
#include <vector>
#include <mutex>
#include <chrono>
#include <unordered_map>

namespace NH::Bass
{
Expand All @@ -23,7 +18,7 @@ namespace NH::Bass
friend class ChangeZoneCommand;
friend class ScheduleThemeChangeCommand;

static NH::Logger* log;
static Logger* log;
static Engine* s_Instance;
bool m_Initialized = false;
float m_MasterVolume = 1.0f;
Expand Down
12 changes: 6 additions & 6 deletions src/NH/Bass/EngineCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace NH::Bass

CommandResult ChangeZoneCommand::Execute(Engine& engine)
{
log->Info("Music zone changed: {0}", m_Zone);
log->Info("Music zone changed: {0}", m_Zone.c_str());
const auto themes = engine.GetMusicManager().GetThemesForZone(m_Zone);
if (themes.empty())
{
log->Warning("No themes found for zone {0}", m_Zone);
log->Warning("No themes found for zone {0}", m_Zone.c_str());
return CommandResult::DONE;
}
engine.GetCommandQueue().AddCommand(std::make_shared<ScheduleThemeChangeCommand>(themes[0].first));
Expand All @@ -24,11 +24,11 @@ namespace NH::Bass

CommandResult ScheduleThemeChangeCommand::Execute(Engine& engine)
{
log->Info("Scheduling theme: {0}", m_ThemeId);
log->Info("Scheduling theme: {0}", m_ThemeId.c_str());
auto theme = engine.GetMusicManager().GetTheme(m_ThemeId);
if (!theme)
{
log->Error("Theme {0} doesn't exist", m_ThemeId);
log->Error("Theme {0} doesn't exist", m_ThemeId.c_str());
return CommandResult::DONE;
}

Expand All @@ -48,11 +48,11 @@ namespace NH::Bass

CommandResult PlayThemeInstantCommand::Execute(Engine& engine)
{
log->Info("Playing theme: {0} instantly, because PlayThemeInstantCommand forced it.", m_ThemeId);
log->Info("Playing theme: {0} instantly, because PlayThemeInstantCommand forced it.", m_ThemeId.c_str());
auto theme = engine.GetMusicManager().GetTheme(m_ThemeId);
if (!theme)
{
log->Error("Theme {0} doesn't exist", m_ThemeId);
log->Error("Theme {0} doesn't exist", m_ThemeId.c_str());
return CommandResult::DONE;
}

Expand Down
15 changes: 8 additions & 7 deletions src/NH/Bass/EngineCommands.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
#pragma once

#include <NH/HashString.h>
#include <NH/Logger.h>
#include <NH/Bass/Command.h>
#include <NH/Bass/IEngine.h>

#include <utility>

namespace NH::Bass
{
class ChangeZoneCommand : public Command
{
static Logger* log;
String m_Zone;
std::string m_Zone;
public:
explicit ChangeZoneCommand(const String& zone) : m_Zone(zone) {};
explicit ChangeZoneCommand(std::string zone) : m_Zone(std::move(zone)) {};
CommandResult Execute(Engine& engine) override;
};

class ScheduleThemeChangeCommand : public Command
{
static Logger* log;
HashString m_ThemeId;
std::string m_ThemeId;
public:
explicit ScheduleThemeChangeCommand(HashString themeId) : m_ThemeId(themeId) {};
explicit ScheduleThemeChangeCommand(std::string themeId) : m_ThemeId(std::move(themeId)) {};
CommandResult Execute(Engine& engine) override;
};

class PlayThemeInstantCommand : public Command
{
static Logger* log;
HashString m_ThemeId;
std::string m_ThemeId;
public:
explicit PlayThemeInstantCommand(HashString themeId) : m_ThemeId(themeId) {};
explicit PlayThemeInstantCommand(std::string themeId) : m_ThemeId(std::move(themeId)) {};
CommandResult Execute(Engine& engine) override;
};
}
15 changes: 7 additions & 8 deletions src/NH/Bass/EventManager.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "NH/Logger.h"
#include <NH/HashString.h>

#include <utility>
#include <vector>
Expand All @@ -22,16 +21,16 @@ namespace NH::Bass

struct Event
{
struct MusicEnd { MusicTheme const* Theme = nullptr; HashString AudioId; };
struct MusicTransition { MusicTheme const* Theme = nullptr; HashString AudioId; float TimeLeft = 0; };
struct MusicChange { MusicTheme const* Theme = nullptr; HashString AudioId; };
struct MusicEnd { MusicTheme const* Theme = nullptr; std::string AudioId; };
struct MusicTransition { MusicTheme const* Theme = nullptr; std::string AudioId; float TimeLeft = 0; };
struct MusicChange { MusicTheme const* Theme = nullptr; std::string AudioId; };
using DataType = std::variant<MusicEnd, MusicTransition, MusicChange>;

EventType Type = EventType::UNKNOWN;
DataType Data;

Event() = delete;
Event(EventType type, DataType data) : Type(type), Data(data) {}
Event(EventType type, DataType data) : Type(type), Data(std::move(data)) {}
virtual ~Event() = default;
};

Expand Down Expand Up @@ -78,19 +77,19 @@ namespace NH::Bass
struct MusicEndEvent : public Event
{
MusicEnd Data;
MusicEndEvent(MusicTheme* theme, HashString audioId) : Event(EventType::MUSIC_END, MusicEnd{theme, audioId}) {}
MusicEndEvent(const MusicTheme* theme, const std::string& audioId) : Event(EventType::MUSIC_END, MusicEnd{theme, audioId}) {}
};

struct MusicTransitionEvent : public Event
{
MusicTransition Data;
MusicTransitionEvent(MusicTheme* theme, HashString audioId, float timeLeft)
MusicTransitionEvent(const MusicTheme* theme, const std::string& audioId, const float timeLeft)
: Event(EventType::MUSIC_TRANSITION, MusicTransition{theme, audioId, timeLeft}) {}
};

struct MusicChangeEvent : public Event
{
MusicChange Data;
MusicChangeEvent(MusicTheme* theme, HashString audioId) : Event(EventType::MUSIC_CHANGE, MusicChange{theme, audioId}) {}
MusicChangeEvent(const MusicTheme* theme, const std::string& audioId) : Event(EventType::MUSIC_CHANGE, MusicChange{theme, audioId}) {}
};
}
15 changes: 9 additions & 6 deletions src/NH/Bass/IChannel.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include "NH/Commons.h"

#include <functional>
#include <expected>
#include <stdexcept>

namespace NH::Bass
{
Expand All @@ -16,17 +19,17 @@ namespace NH::Bass
{
ErrorType Type;
int32_t Code;
String Details;
std::string Details;
public:
Error(ErrorType type, int32_t code, const String& details)
: std::runtime_error(String::Format("{0} error: {1}, message: %s", ErrorTypeStrings[static_cast<int>(type)], code, details)),
Error(ErrorType type, const int32_t code, const std::string& details)
: std::runtime_error(String::Format("{0} error: {1}, message: %s", ErrorTypeStrings[static_cast<int>(type)], code, details.c_str()).ToChar()),
Type(type), Code(code), Details(details) {};
Error(ErrorType type, int32_t code, const String&& details)
: std::runtime_error(String::Format("{0} error: {1}, message: %s", ErrorTypeStrings[static_cast<int>(type)], code, details)),
Error(ErrorType type, const int32_t code, const std::string&& details)
: std::runtime_error(String::Format("{0} error: {1}, message: %s", ErrorTypeStrings[static_cast<int>(type)], code, details.c_str()).ToChar()),
Type(type), Code(code), Details(details) {};
[[nodiscard]] ErrorType GetType() const { return Type; }
[[nodiscard]] int32_t GetCode() const { return Code; }
[[nodiscard]] const char* GetDetails() const { return Details; }
[[nodiscard]] const char* GetDetails() const { return Details.c_str(); }
};

template<typename T>
Expand Down
7 changes: 3 additions & 4 deletions src/NH/Bass/MidiFile.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <NH/Logger.h>
#include <NH/HashString.h>
#include <NH/Executor.h>

#include <bass.h>
Expand Down Expand Up @@ -34,7 +33,7 @@ namespace NH::Bass

private:
static Logger* log;
HashString m_ThemeId;
std::string m_ThemeId;
String m_Filename;
String m_TargetFilter;
std::vector<char> m_Buffer{};
Expand All @@ -45,12 +44,12 @@ namespace NH::Bass
std::vector<Tone> m_Tones{};

public:
MidiFile(HashString themeId, const String& filename, const String& targetFilter = "")
MidiFile(const std::string& themeId, const String& filename, const String& targetFilter = "")
: m_ThemeId(themeId), m_Filename(filename), m_TargetFilter(targetFilter) {}

void LoadMidiFile(Executor& executor, const std::function<void(MidiFile&)>& onReady = nullptr);

[[nodiscard]] HashString GetThemeId() const { return m_ThemeId; }
[[nodiscard]] std::string GetThemeId() const { return m_ThemeId; }

[[nodiscard]] const String& GetFilename() const { return m_Filename; }

Expand Down
Loading