Introduce macro to assert and return the assertion condition

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle
2026-01-21 17:45:12 +05:00
parent 9ac0314d7a
commit 490df18fd5
3 changed files with 34 additions and 10 deletions

23
launcher/Assert.h Normal file
View File

@@ -0,0 +1,23 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2025 Octol1ttle <l1ttleofficial@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#if !defined(ASSERT_NEVER)
#define ASSERT_NEVER(cond) (Q_ASSERT(cond == false), cond)
#endif

View File

@@ -102,6 +102,9 @@ set(CORE_SOURCES
MMCTime.cpp
MTPixmapCache.h
# Assertion helper
Assert.h
)
if (UNIX AND NOT CYGWIN AND NOT APPLE)
set(CORE_SOURCES
@@ -1614,7 +1617,7 @@ if(WIN32 OR (UNIX AND APPLE))
)
# FIXME: remove this crap once we stop using msys2
if(MINGW)
# i've not found a solution better than injecting the config vars like this...
# i've not found a solution better than injecting the config vars like this...
# with install(CODE" for everything everything just breaks
install(CODE "
set(QT_PLUGINS_DIR \"${QT_PLUGINS_DIR}\")
@@ -1622,7 +1625,7 @@ if(WIN32 OR (UNIX AND APPLE))
set(QT_LIBEXECS_DIR \"${QT_LIBEXECS_DIR}\")
set(CMAKE_SYSTEM_LIBRARY_PATH \"${CMAKE_SYSTEM_LIBRARY_PATH}\")
set(CMAKE_INSTALL_PREFIX \"${CMAKE_INSTALL_PREFIX}\")
"
"
COMPONENT bundle)
install(CODE [[

View File

@@ -38,6 +38,8 @@
#include <QDebug>
#include "Assert.h"
Q_LOGGING_CATEGORY(taskLogC, "launcher.task")
Task::Task(bool show_debug) : m_show_debug(show_debug)
@@ -96,9 +98,8 @@ void Task::start()
break;
}
case State::Running: {
if (m_show_debug)
if (ASSERT_NEVER(isRunning()) && m_show_debug)
qCWarning(taskLogC) << "The launcher tried to start task" << describe() << "while it was already running!";
Q_ASSERT(!isRunning());
return;
}
}
@@ -111,9 +112,8 @@ void Task::start()
void Task::emitFailed(QString reason)
{
// Don't fail twice.
if (!isRunning()) {
if (ASSERT_NEVER(!isRunning())) {
qCCritical(taskLogC) << "Task" << describe() << "failed while not running!!!!: " << reason;
Q_ASSERT(!isRunning());
return;
}
m_state = State::Failed;
@@ -126,9 +126,8 @@ void Task::emitFailed(QString reason)
void Task::emitAborted()
{
// Don't abort twice.
if (!isRunning()) {
if (ASSERT_NEVER(!isRunning())) {
qCCritical(taskLogC) << "Task" << describe() << "aborted while not running!!!!";
Q_ASSERT(!isRunning());
return;
}
m_state = State::AbortedByUser;
@@ -142,9 +141,8 @@ void Task::emitAborted()
void Task::emitSucceeded()
{
// Don't succeed twice.
if (!isRunning()) {
if (ASSERT_NEVER(!isRunning())) {
qCCritical(taskLogC) << "Task" << describe() << "succeeded while not running!!!!";
Q_ASSERT(!isRunning());
return;
}
m_state = State::Succeeded;