Skip to content

Commit

Permalink
#48 General tidying, null-safety, better error reporting #49 union-ap…
Browse files Browse the repository at this point in the history
…i & gothic-api included using FetchContent, /W4 /WX flags set (#52)

* #48 Add additional null-checks and perform general tidying of the project

* #49 Include union-api and gothic-api using FetchContent, enable /W4 /WX flags

* Ignore warning C4740: flow in or out of inline asm code suppresses global optimization
  • Loading branch information
piotrmacha authored Oct 19, 2024
1 parent 5f4fca5 commit 3d59b19
Show file tree
Hide file tree
Showing 41 changed files with 425 additions and 425 deletions.
8 changes: 0 additions & 8 deletions .gitmodules

This file was deleted.

38 changes: 28 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ project(zBassMusic VERSION ${PROJECT_VERSION_CMAKE})

option(BUILD_VDF "Build .VDF file with plugin" ON)

set(UNION_API_DIR "${CMAKE_SOURCE_DIR}/dependencies/union-api")
set(GOTHIC_API_DIR "${CMAKE_SOURCE_DIR}/dependencies/gothic-api")
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
set(BASS_DIR "${CMAKE_SOURCE_DIR}/dependencies/bass")
set(VDF_DIR "${CMAKE_SOURCE_DIR}/vdf")
set(GOTHIC_USERAPI_DIR "${CMAKE_SOURCE_DIR}/gothic-userapi")
Expand All @@ -17,8 +16,24 @@ if (${CMAKE_BUILD_RELEASE_PDB})
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
endif ()

include(cmake/union-api.cmake)
include(cmake/gothic-api.cmake)
include(FetchContent)
set(GOTHIC_API_G1 ON)
set(GOTHIC_API_G1A ON)
set(GOTHIC_API_G2 ON)
set(GOTHIC_API_G2A ON)
FetchContent_Declare(
union-api
GIT_REPOSITORY https://gitlab.com/union-framework/union-api.git
GIT_TAG ede619be94cb6a7349c43b6678748ad1113a8c56
)
FetchContent_Declare(
gothic-api
GIT_REPOSITORY https://gitlab.com/union-framework/gothic-api.git
GIT_TAG ba0894b136eda163209ef58a7dad07103cb2ed10
)
FetchContent_MakeAvailable(union-api)
FetchContent_MakeAvailable(gothic-api)

include(cmake/bass.cmake)

add_library(plugin SHARED)
Expand All @@ -35,16 +50,20 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.rc.in" "${CMAKE_CURRENT_BINA
configure_file("${CMAKE_SOURCE_DIR}/vdf/script.vdfs.in" "${CMAKE_BINARY_DIR}/script.vdfs")
configure_file("${CMAKE_SOURCE_DIR}/src/BuildInfo.h.in" "${CMAKE_BINARY_DIR}/src/BuildInfo.h" @ONLY)

file(GLOB_RECURSE PLUGIN_SOURCES "src/**.cpp" "${UNION_API_DIR}/union-api/Union/Memory.cpp")
file(GLOB_RECURSE PLUGIN_SOURCES "src/**.cpp")
target_sources(plugin PRIVATE ${PLUGIN_SOURCES} "${CMAKE_CURRENT_BINARY_DIR}/version.rc")

add_compile_options(plugin PRIVATE /W4 /WX)
target_compile_definitions(plugin PRIVATE _UNION_API_DLL __G1 __G1A __G2 __G2A)
target_compile_options(plugin PRIVATE
/W4 /WX /permissive- /Zc:__cplusplus /Zc:preprocessor /utf-8
/experimental:external /external:W0 /external:anglebrackets /external:templates-
/external:I ${CMAKE_BINARY_DIR}/_deps
/wd4740 # disable warning C4740: flow in or out of inline asm code suppresses global optimization
)

target_include_directories(plugin PRIVATE "src/" "${CMAKE_BINARY_DIR}/src/")
target_link_libraries(plugin PRIVATE union-api gothic-api bass_all)
target_link_libraries(plugin PRIVATE union_api_lib gothic_api bass_all)

install(FILES $<TARGET_RUNTIME_DLLS:plugin> "${CMAKE_BINARY_DIR}/zBassMusic.dll" TYPE BIN)
install(FILES $<TARGET_RUNTIME_DLLS:plugin> "${CMAKE_BINARY_DIR}/UnionAPI.dll" TYPE BIN)
if (${BUILD_VDF})
install(FILES "${CMAKE_BINARY_DIR}/script.vdfs" DESTINATION "${CMAKE_INSTALL_PREFIX}")
install(SCRIPT "${VDF_DIR}/vdf.cmake")
Expand All @@ -70,7 +89,6 @@ endif ()
if (DEFINED ENV{COPY_DLL_TARGET})
add_custom_target(copy_dll ALL
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/zBassMusic.dll" "$ENV{COPY_DLL_TARGET}/Autorun/zBassMusic.dll"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/UnionAPI.dll" "$ENV{COPY_DLL_TARGET}/Autorun/UnionAPI.dll"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/dependencies/bass/lib/bass.dll" "$ENV{COPY_DLL_TARGET}/bass.dll"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/dependencies/bass/lib/bassmidi.dll" "$ENV{COPY_DLL_TARGET}/bassmidi.dll"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/dependencies/bass/lib/bassopus.dll" "$ENV{COPY_DLL_TARGET}/bassopus.dll"
Expand Down
8 changes: 0 additions & 8 deletions cmake/gothic-api.cmake

This file was deleted.

20 changes: 0 additions & 20 deletions cmake/union-api.cmake

This file was deleted.

1 change: 0 additions & 1 deletion dependencies/gothic-api
Submodule gothic-api deleted from 102f42
1 change: 0 additions & 1 deletion dependencies/union-api
Submodule union-api deleted from f492eb
79 changes: 43 additions & 36 deletions src/Gothic/BassLoader.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "NH/Bass/MidiFile.h"

#include <functional>
#include <NH/Bass/MidiFile.h>

namespace GOTHIC_NAMESPACE
{
struct GothicMusicTheme
{
struct GothicMusicTheme {
zSTRING fileName;
float vol;
bool loop;
Expand All @@ -14,17 +14,18 @@ namespace GOTHIC_NAMESPACE
zTMus_TransSubType trSubType;
};

enum class BassMusicThemeType : size_t { NORMAL = 0 };

struct BassMusicTheme
enum class BassMusicThemeType : size_t
{
NORMAL = 0
};

struct BassMusicTheme {
zSTRING Name;
zSTRING Zones;
BassMusicThemeType Type = BassMusicThemeType::NORMAL;
};

struct BassMusicThemeAudio
{
struct BassMusicThemeAudio {
zSTRING Theme;
zSTRING Type;
zSTRING Filename;
Expand Down Expand Up @@ -52,19 +53,24 @@ namespace GOTHIC_NAMESPACE

~BassLoader()
{
for (auto theme: m_GothicThemeInstances) { delete theme; }
for (auto theme: m_BassThemeInstances) { delete theme; }
for (auto theme: m_BassThemeAudioInstances) { delete theme; }
for (const auto theme: m_GothicThemeInstances) {
delete theme;
}
for (const auto theme: m_BassThemeInstances) {
delete theme;
}
for (const auto theme: m_BassThemeAudioInstances) {
delete theme;
}
}

void Load()
{
LoadGothic();
LoadBass();

zSTRING initFuncName("BassMusic_Init");
zCPar_Symbol* symbol = m_Parser->GetSymbol(initFuncName);
if (symbol && symbol->type == zPAR_TYPE_FUNC)
const zSTRING initFuncName("BassMusic_Init");
if (const zCPar_Symbol* symbol = m_Parser->GetSymbol(initFuncName); symbol && symbol->type == zPAR_TYPE_FUNC)
{
m_Parser->CallFunc(initFuncName);
}
Expand All @@ -76,8 +82,8 @@ namespace GOTHIC_NAMESPACE
ForEachClass<GothicMusicTheme>(
"C_MUSICTHEME",
[&]() { return m_GothicThemeInstances.emplace_back(new GothicMusicTheme{}); },
[&](GothicMusicTheme* input, zCPar_Symbol* symbol) {
std::shared_ptr<NH::Bass::MusicTheme> theme = std::make_shared<NH::Bass::MusicTheme>(symbol->name.ToChar());
[&](const GothicMusicTheme* input, const zCPar_Symbol* symbol) {
const auto theme = std::make_shared<NH::Bass::MusicTheme>(symbol->name.ToChar());
theme->SetAudioFile(NH::Bass::AudioFile::DEFAULT, input->fileName.ToChar());
theme->SetAudioEffects(NH::Bass::AudioFile::DEFAULT, [&](NH::Bass::AudioEffects& effects) {
effects.Loop.Active = input->loop;
Expand All @@ -89,7 +95,7 @@ namespace GOTHIC_NAMESPACE
effects.ReverbDX8.Mix = input->reverbMix;
effects.ReverbDX8.Time = input->reverbTime;
}
bool forceFade = NH::Bass::Options->ForceFadeTransition;
const bool forceFade = NH::Bass::Options->ForceFadeTransition;
if (forceFade || input->trType == zMUS_TR_INTRO || input->trType == zMUS_TR_ENDANDINTRO)
{
effects.FadeIn.Active = true;
Expand All @@ -114,21 +120,23 @@ namespace GOTHIC_NAMESPACE

ForEachClass<BassMusicTheme>(
Globals->BassMusicThemeClassName,
[&]() { return m_BassThemeInstances.emplace_back(new BassMusicTheme{}); },
[&](BassMusicTheme* input, zCPar_Symbol* symbol) {
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(std::string(zone.MakeUpper().ToChar())); }
[&] { return m_BassThemeInstances.emplace_back(new BassMusicTheme{}); },
[&](const BassMusicTheme* input, [[maybe_unused]] const zCPar_Symbol* symbol) {
const auto theme = std::make_shared<NH::Bass::MusicTheme>(input->Name.ToChar());
theme->SetAudioEffects(NH::Bass::AudioFile::DEFAULT, []([[maybe_unused]] const NH::Bass::AudioEffects& effects) {});
for (auto zones = NH::String(input->Zones.ToChar()).Split(","); 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);
});

ForEachClass<BassMusicThemeAudio>(
Globals->BassMusicThemeAudioClassName,
[&]() { 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());
[&] { return m_BassThemeAudioInstances.emplace_back(new BassMusicThemeAudio{}); },
[&](const BassMusicThemeAudio* input, const zCPar_Symbol* symbol) {
const std::shared_ptr<NH::Bass::MusicTheme> theme = NH::Bass::Engine::GetInstance()->GetMusicManager().GetTheme(input->Theme.ToChar());
if (!theme) {
log->Error("Could not find theme '{0}' referenced by {1}(BassMusic_ThemeAudio). Skipping...", input->Theme.ToChar(), symbol->name.ToChar());
log->Error("Make sure that {0}.theme == BassMusic_Theme.name.", symbol->name.ToChar());
Expand Down Expand Up @@ -161,7 +169,7 @@ namespace GOTHIC_NAMESPACE
});
if (!input->MidiFile.IsEmpty())
{
auto midiFile = std::make_shared<NH::Bass::MidiFile>(theme->GetName(), NH::String(input->MidiFile.ToChar()));
const 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 All @@ -171,14 +179,13 @@ namespace GOTHIC_NAMESPACE
template<typename T>
void ForEachClass(const zSTRING& className, const std::function<T*()>& classFactory, const std::function<void(T*, zCPar_Symbol*)>& instanceFunc)
{
ForEachPrototype(className, [&](int index) {
T* theme = classFactory();
if (theme)
ForEachPrototype(className, [&](const int index) {
if (T* theme = classFactory())
{
m_Parser->CreatePrototype(index, theme);
}
});
ForEachInstance(className, [&](int index, zCPar_Symbol* symbol) {
ForEachInstance(className, [&](const int index, zCPar_Symbol* symbol) {
T* theme = classFactory();
if (theme)
{
Expand All @@ -188,9 +195,9 @@ namespace GOTHIC_NAMESPACE
});
}

void ForEachPrototype(const zSTRING& className, const std::function<void(int)>& func)
void ForEachPrototype(const zSTRING& className, const std::function<void(int)>& func) const
{
int classIndex = m_Parser->GetIndex(className);
const int classIndex = m_Parser->GetIndex(className);
if (classIndex < 0)
{
return;
Expand All @@ -204,9 +211,9 @@ namespace GOTHIC_NAMESPACE
}
}

void ForEachInstance(const zSTRING& className, const std::function<void(int, zCPar_Symbol*)>& func)
void ForEachInstance(const zSTRING& className, const std::function<void(int, zCPar_Symbol*)>& func) const
{
int classIndex = m_Parser->GetIndex(className);
const int classIndex = m_Parser->GetIndex(className);
if (classIndex < 0)
{
return;
Expand All @@ -221,4 +228,4 @@ namespace GOTHIC_NAMESPACE
}
}
};
}
}// namespace GOTHIC_NAMESPACE
25 changes: 15 additions & 10 deletions src/Gothic/CMusicSys_Bass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace GOTHIC_NAMESPACE
{
namespace BassEvent
{
inline void Event_OnEnd(const NH::Bass::Event& event, void* userData)
inline void Event_OnEnd(const NH::Bass::Event& event, [[maybe_unused]] void* userData)
{
static NH::Logger* log = NH::CreateLogger("zBassMusic::Event_OnEnd");

Expand All @@ -20,7 +20,7 @@ namespace GOTHIC_NAMESPACE
}
}

inline void Event_OnTransition(const NH::Bass::Event& event, void* userData)
inline void Event_OnTransition(const NH::Bass::Event& event, [[maybe_unused]] void* userData)
{
static NH::Logger* log = NH::CreateLogger("zBassMusic::Event_OnTransition");

Expand All @@ -38,7 +38,7 @@ namespace GOTHIC_NAMESPACE
}
}

inline void Event_OnChange(const NH::Bass::Event& event, void* userData)
inline void Event_OnChange(const NH::Bass::Event& event, [[maybe_unused]] void* userData)
{
static NH::Logger* log = NH::CreateLogger("zBassMusic::Event_OnChange");

Expand Down Expand Up @@ -72,14 +72,14 @@ namespace GOTHIC_NAMESPACE
{
static constexpr std::string_view validExt{ ".sgt" };

if (file.Length() < validExt.size())
if (static_cast<uint32_t>(file.Length()) < validExt.size())
{
return false;
}

const auto fileLen = static_cast<size_t>(file.Length());
const auto extStart = fileLen - validExt.size();
const std::string_view fileExt{ std::next(file.ToChar(), static_cast<uint32_t>(extStart)), validExt.size() };
const std::string_view fileExt{ std::next(file.ToChar(), extStart), validExt.size() };

auto toLowerSimple = [](const char t_char)
{
Expand Down Expand Up @@ -177,15 +177,20 @@ namespace GOTHIC_NAMESPACE
}
}

void PlayTheme(zCMusicTheme* theme, float const& volume, zTMus_TransType const& transition, zTMus_TransSubType const& subTransition) override
void PlayTheme(zCMusicTheme* theme, float const& themeVolume, zTMus_TransType const& transition, zTMus_TransSubType const& subTransition) override
{
if (theme == nullptr) {
log->Error("PlayTheme illegal argument: theme == nullptr");
return;
}

log->Trace("PlayTheme: {0}", theme->fileName.ToChar());

if (IsDirectMusicFormat(theme->fileName))
{
m_BassEngine->StopMusic();
m_ActiveTheme = theme;
m_DirectMusic->PlayTheme(theme, volume, transition, subTransition);
m_DirectMusic->PlayTheme(theme, themeVolume, transition, subTransition);
return;
}

Expand Down Expand Up @@ -226,10 +231,10 @@ namespace GOTHIC_NAMESPACE
return m_BassEngine->GetVolume();
}

void SetVolume(const float volume) override
void SetVolume(const float masterVolume) override
{
m_BassEngine->SetVolume(volume);
m_DirectMusic->SetVolume(volume);
m_BassEngine->SetVolume(masterVolume);
m_DirectMusic->SetVolume(masterVolume);
}

int IsAvailable(zSTRING const& id) override
Expand Down
Loading

0 comments on commit 3d59b19

Please sign in to comment.