simplify build code

This commit is contained in:
Garux 2024-01-24 17:09:04 +06:00
parent ecdad801b9
commit 9a13f9ed55
13 changed files with 40 additions and 95 deletions

View File

@ -23,7 +23,6 @@
#include "LoadPortalFileDialog.h" #include "LoadPortalFileDialog.h"
#include "stream/stringstream.h" #include "stream/stringstream.h"
#include "convert.h"
#include "os/path.h" #include "os/path.h"
#include "qerplugin.h" #include "qerplugin.h"

View File

@ -25,7 +25,7 @@
#include <cstring> #include <cstring>
#include "ixml.h" #include "ixml.h"
#include "libxml/parser.h" #include "libxml/parser.h"
#include "convert.h" #include "stream/textstream.h"
class TextInputStream; class TextInputStream;

View File

@ -21,9 +21,9 @@
#pragma once #pragma once
#include "convert.h"
#include <vector> #include <vector>
#include "xml/ixml.h" #include "xml/ixml.h"
#include "stream/textstream.h"
class XMLEntityOutputStream class XMLEntityOutputStream
{ {

View File

@ -468,7 +468,7 @@ void project_verify( Project& project, Tools& tools ){
#endif #endif
} }
void build_run( size_t buildIdx, CommandListener& listener ){ std::vector<CopiedString> build_construct_commands( size_t buildIdx ){
for ( const auto& [ name, tool ] : g_build_tools ) for ( const auto& [ name, tool ] : g_build_tools )
{ {
StringBuffer output; StringBuffer output;
@ -476,6 +476,8 @@ void build_run( size_t buildIdx, CommandListener& listener ){
build_set_variable( name.c_str(), output.c_str() ); build_set_variable( name.c_str(), output.c_str() );
} }
std::vector<CopiedString> commands;
if ( const auto buildIt = Project_find( g_build_project, buildIdx ); buildIt != g_build_project.end() ) if ( const auto buildIt = Project_find( g_build_project, buildIdx ); buildIt != g_build_project.end() )
{ {
g_lastExecutedBuild = buildIt; g_lastExecutedBuild = buildIt;
@ -484,13 +486,15 @@ void build_run( size_t buildIdx, CommandListener& listener ){
StringBuffer output; StringBuffer output;
command.evaluate( output ); command.evaluate( output );
if ( !output.empty() ) if ( !output.empty() )
listener.execute( output.c_str() ); commands.emplace_back( output.c_str() );
} }
} }
else else
{ {
globalErrorStream() << "build #" << buildIdx << " not defined"; globalErrorStream() << "build #" << buildIdx << " not defined";
} }
return commands;
} }

View File

@ -21,16 +21,13 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <vector>
#include "string/string.h"
void build_set_variable( const char* name, const char* value ); void build_set_variable( const char* name, const char* value );
void build_clear_variables(); void build_clear_variables();
class CommandListener std::vector<CopiedString> build_construct_commands( size_t buildIdx );
{
public:
virtual void execute( const char* command ) = 0;
};
void build_run( size_t buildIdx, CommandListener& listener );
void DoBuildMenu(); void DoBuildMenu();

View File

@ -36,7 +36,6 @@
#include <cstdlib> #include <cstdlib>
#include "stream/stringstream.h" #include "stream/stringstream.h"
#include "convert.h"
#include "gtkutil/dialog.h" #include "gtkutil/dialog.h"
#include "gtkutil/entry.h" #include "gtkutil/entry.h"
#include "gtkutil/image.h" #include "gtkutil/image.h"

View File

@ -60,7 +60,6 @@
#include "os/file.h" #include "os/file.h"
#include "stream/stringstream.h" #include "stream/stringstream.h"
#include "moduleobserver.h" #include "moduleobserver.h"
#include "convert.h"
#include "stringio.h" #include "stringio.h"
#include "gtkutil/accelerator.h" #include "gtkutil/accelerator.h"

View File

@ -63,7 +63,6 @@
#include "container/array.h" #include "container/array.h"
#include "generic/static.h" #include "generic/static.h"
#include "stream/stringstream.h" #include "stream/stringstream.h"
#include "convert.h"
#include "gtkutil/messagebox.h" #include "gtkutil/messagebox.h"
#include "gtkutil/image.h" #include "gtkutil/image.h"

View File

@ -27,7 +27,6 @@
#include "os/file.h" #include "os/file.h"
#include "generic/callback.h" #include "generic/callback.h"
#include "stream/stringstream.h" #include "stream/stringstream.h"
#include "convert.h"
#include "gtkutil/menu.h" #include "gtkutil/menu.h"
#include "map.h" #include "map.h"

View File

@ -37,7 +37,6 @@
#include "debugging/debugging.h" #include "debugging/debugging.h"
#include "ifilesystem.h" #include "ifilesystem.h"
//#include "imap.h"
#include <map> #include <map>
@ -59,15 +58,10 @@
#include "preferences.h" #include "preferences.h"
#include "watchbsp.h" #include "watchbsp.h"
#include "autosave.h" #include "autosave.h"
#include "convert.h"
QEGlobals_t g_qeglobals; QEGlobals_t g_qeglobals;
#if defined( WIN32 )
#define PATH_MAX 260
#endif
#if defined( POSIX ) #if defined( POSIX )
#include <sys/stat.h> // chmod #include <sys/stat.h> // chmod
#endif #endif
@ -193,27 +187,7 @@ void bsp_shutdown(){
build_clear_variables(); build_clear_variables();
} }
class ArrayCommandListener : public CommandListener class BatchCommandListener
{
GPtrArray* m_array;
public:
ArrayCommandListener(){
m_array = g_ptr_array_new();
}
~ArrayCommandListener(){
g_ptr_array_free( m_array, TRUE );
}
void execute( const char* command ){
g_ptr_array_add( m_array, g_strdup( command ) );
}
GPtrArray* array() const {
return m_array;
}
};
class BatchCommandListener : public CommandListener
{ {
TextOutputStream& m_file; TextOutputStream& m_file;
std::size_t m_commandCount; std::size_t m_commandCount;
@ -255,32 +229,25 @@ void RunBSP( size_t buildIdx ){
bsp_init(); bsp_init();
ArrayCommandListener listener; const std::vector<CopiedString> commands = build_construct_commands( buildIdx );
build_run( buildIdx, listener ); const bool monitor = std::any_of( commands.cbegin(), commands.cend(), []( const CopiedString& command ){
bool monitor = false; return strstr( command.c_str(), RADIANT_MONITOR_ADDRESS ) != 0;
for ( guint i = 0; i < listener.array()->len; ++i ) } );
if( strstr( (char*)g_ptr_array_index( listener.array(), i ), RADIANT_MONITOR_ADDRESS ) )
monitor = true;
if ( g_WatchBSP_Enabled && monitor ) { if ( g_WatchBSP_Enabled && monitor ) {
// grab the file name for engine running // grab the file name for engine running
const char* fullname = Map_Name( g_map ); const char* fullname = Map_Name( g_map );
const auto bspname = StringOutputStream( 64 )( PathFilename( fullname ) ); const auto bspname = StringOutputStream( 64 )( PathFilename( fullname ) );
BuildMonitor_Run( listener.array(), bspname.c_str() ); BuildMonitor_Run( commands, bspname.c_str() );
} }
else else
{ {
char junkpath[PATH_MAX]; const auto junkpath = StringOutputStream( 256 )( SettingsPath_get(), "junk.txt" );
strcpy( junkpath, SettingsPath_get() );
strcat( junkpath, "junk.txt" );
char batpath[PATH_MAX];
#if defined( POSIX ) #if defined( POSIX )
strcpy( batpath, SettingsPath_get() ); const auto batpath = StringOutputStream( 256 )( SettingsPath_get(), "qe3bsp.sh" );
strcat( batpath, "qe3bsp.sh" );
#elif defined( WIN32 ) #elif defined( WIN32 )
strcpy( batpath, SettingsPath_get() ); const auto batpath = StringOutputStream( 256 )( SettingsPath_get(), "qe3bsp.bat" );
strcat( batpath, "qe3bsp.bat" );
#else #else
#error "unsupported platform" #error "unsupported platform"
#endif #endif
@ -292,7 +259,8 @@ void RunBSP( size_t buildIdx ){
batchFile << "#!/bin/sh \n\n"; batchFile << "#!/bin/sh \n\n";
#endif #endif
BatchCommandListener listener( batchFile, g_WatchBSP0_DumpLog? junkpath : 0 ); BatchCommandListener listener( batchFile, g_WatchBSP0_DumpLog? junkpath : 0 );
build_run( buildIdx, listener ); for( const auto& command : commands )
listener.execute( command.c_str() );
written = true; written = true;
} }
} }

View File

@ -69,7 +69,6 @@
#include "commandlib.h" #include "commandlib.h"
#include "texmanip.h" #include "texmanip.h"
#include "textures.h" #include "textures.h"
#include "convert.h"
#include "gtkutil/menu.h" #include "gtkutil/menu.h"
#include "gtkutil/nonmodal.h" #include "gtkutil/nonmodal.h"

View File

@ -39,7 +39,6 @@
#include <QTimer> #include <QTimer>
#include "commandlib.h" #include "commandlib.h"
#include "convert.h"
#include "string/string.h" #include "string/string.h"
#include "stream/stringstream.h" #include "stream/stringstream.h"
@ -97,13 +96,13 @@ private:
socket_t *m_pListenSocket; socket_t *m_pListenSocket;
socket_t *m_pInSocket; socket_t *m_pInSocket;
netmessage_t msg; netmessage_t msg;
GPtrArray *m_pCmd; std::vector<CopiedString> m_commands;
// used to timeout EBeginStep // used to timeout EBeginStep
Timer m_timeout_timer; Timer m_timeout_timer;
std::size_t m_iCurrentStep; std::size_t m_iCurrentStep;
QTimer m_monitoring_timer; QTimer m_monitoring_timer;
// name of the map so we can run the engine // name of the map so we can run the engine
char *m_sBSPName; CopiedString m_sBSPName;
// buffer we use in push mode to receive data directly from the network // buffer we use in push mode to receive data directly from the network
xmlParserInputBufferPtr m_xmlInputBuffer; xmlParserInputBufferPtr m_xmlInputBuffer;
xmlParserCtxtPtr m_xmlParserCtxt; xmlParserCtxtPtr m_xmlParserCtxt;
@ -118,12 +117,10 @@ private:
public: public:
CWatchBSP(){ CWatchBSP(){
m_pCmd = 0;
m_bBSPPlugin = false; m_bBSPPlugin = false;
m_pListenSocket = NULL; m_pListenSocket = NULL;
m_pInSocket = NULL; m_pInSocket = NULL;
m_eState = EIdle; m_eState = EIdle;
m_sBSPName = NULL;
m_xmlInputBuffer = NULL; m_xmlInputBuffer = NULL;
m_bNeedCtxtInit = true; m_bNeedCtxtInit = true;
m_monitoring_timer.callOnTimeout( [this](){ RoutineProcessing(); } ); m_monitoring_timer.callOnTimeout( [this](){ RoutineProcessing(); } );
@ -142,17 +139,9 @@ public:
// called regularly to keep listening // called regularly to keep listening
void RoutineProcessing(); void RoutineProcessing();
// start a monitoring loop with the following steps // start a monitoring loop with the following steps
void DoMonitoringLoop( GPtrArray *pCmd, const char *sBSPName ); void DoMonitoringLoop( const std::vector<CopiedString>& commands, const char *sBSPName );
void EndMonitoringLoop(){ void EndMonitoringLoop(){
Reset(); Reset();
if ( m_sBSPName ) {
string_release( m_sBSPName, string_length( m_sBSPName ) );
m_sBSPName = 0;
}
if ( m_pCmd ) {
g_ptr_array_free( m_pCmd, TRUE );
m_pCmd = 0;
}
} }
// close everything - may be called from the outside to abort the process // close everything - may be called from the outside to abort the process
void Reset(); void Reset();
@ -316,7 +305,7 @@ CWatchBSP *GetWatchBSP(){
return g_pWatchBSP; return g_pWatchBSP;
} }
void BuildMonitor_Run( GPtrArray* commands, const char* mapName ){ void BuildMonitor_Run( const std::vector<CopiedString>& commands, const char* mapName ){
GetWatchBSP()->DoMonitoringLoop( commands, mapName ); GetWatchBSP()->DoMonitoringLoop( commands, mapName );
} }
@ -617,12 +606,12 @@ void CWatchBSP::DoEBeginStep(){
if ( !m_bBSPPlugin ) { if ( !m_bBSPPlugin ) {
globalOutputStream() << "=== running build command ===\n" globalOutputStream() << "=== running build command ===\n"
<< static_cast<const char*>( g_ptr_array_index( m_pCmd, m_iCurrentStep ) ) << "\n"; << m_commands[m_iCurrentStep] << "\n";
if ( !Q_Exec( NULL, (char *)g_ptr_array_index( m_pCmd, m_iCurrentStep ), NULL, true, false ) ) { if ( !Q_Exec( NULL, const_cast<char*>( m_commands[m_iCurrentStep].c_str() ), NULL, true, false ) ) {
StringOutputStream msg( 256 ); StringOutputStream msg( 256 );
msg << "Failed to execute the following command: "; msg << "Failed to execute the following command: ";
msg << reinterpret_cast<const char*>( g_ptr_array_index( m_pCmd, m_iCurrentStep ) ); msg << m_commands[m_iCurrentStep];
msg << "\nCheck that the file exists and that you don't run out of system resources.\n"; msg << "\nCheck that the file exists and that you don't run out of system resources.\n";
globalOutputStream() << msg.c_str(); globalOutputStream() << msg.c_str();
qt_MessageBox( MainFrame_getWindow(), msg.c_str(), "Build monitoring", EMessageBoxType::Error ); qt_MessageBox( MainFrame_getWindow(), msg.c_str(), "Build monitoring", EMessageBoxType::Error );
@ -644,8 +633,8 @@ void CWatchBSP::RoutineProcessing(){
// timeout: if we don't get an incoming connection fast enough, go back to idle // timeout: if we don't get an incoming connection fast enough, go back to idle
if ( m_timeout_timer.elapsed_sec() > g_WatchBSP_Timeout ) { if ( m_timeout_timer.elapsed_sec() > g_WatchBSP_Timeout ) {
qt_MessageBox( MainFrame_getWindow(), "The connection timed out, assuming the build process failed\n" qt_MessageBox( MainFrame_getWindow(), "The connection timed out, assuming the build process failed\n"
"Make sure you are using a networked version of Q3Map?\n" "Make sure you are using a networked version of Q3Map?\n"
"Otherwise you need to disable BSP Monitoring in prefs.", "BSP process monitoring" ); "Otherwise you need to disable BSP Monitoring in prefs.", "BSP process monitoring" );
EndMonitoringLoop(); EndMonitoringLoop();
#if 0 #if 0
if ( m_bBSPPlugin ) { if ( m_bBSPPlugin ) {
@ -731,7 +720,7 @@ void CWatchBSP::RoutineProcessing(){
#endif #endif
// move to next step or finish // move to next step or finish
m_iCurrentStep++; m_iCurrentStep++;
if ( m_iCurrentStep < m_pCmd->len ) { if ( m_iCurrentStep < m_commands.size() ) {
DoEBeginStep(); DoEBeginStep();
} }
else else
@ -764,7 +753,7 @@ void CWatchBSP::RoutineProcessing(){
globalOutputStream() << cmd.c_str() << " " << cmdline.c_str() << "\n"; globalOutputStream() << cmd.c_str() << " " << cmdline.c_str() << "\n";
// execute now // execute now
if ( !Q_Exec( cmd.c_str(), (char *)cmdline.c_str(), EnginePath_get(), false, false ) ) { if ( !Q_Exec( cmd.c_str(), cmdline.c_str(), EnginePath_get(), false, false ) ) {
StringOutputStream msg; StringOutputStream msg;
msg << "Failed to execute the following command: " << cmd.c_str() << cmdline.c_str(); msg << "Failed to execute the following command: " << cmd.c_str() << cmdline.c_str();
globalOutputStream() << msg.c_str(); globalOutputStream() << msg.c_str();
@ -782,17 +771,8 @@ void CWatchBSP::RoutineProcessing(){
} }
} }
GPtrArray* str_ptr_array_clone( GPtrArray* array ){ void CWatchBSP::DoMonitoringLoop( const std::vector<CopiedString>& commands, const char *sBSPName ){
GPtrArray* cloned = g_ptr_array_sized_new( array->len ); m_sBSPName = sBSPName;
for ( guint i = 0; i < array->len; ++i )
{
g_ptr_array_add( cloned, g_strdup( (char*)g_ptr_array_index( array, i ) ) );
}
return cloned;
}
void CWatchBSP::DoMonitoringLoop( GPtrArray *pCmd, const char *sBSPName ){
m_sBSPName = string_clone( sBSPName );
if ( m_eState != EIdle ) { if ( m_eState != EIdle ) {
globalWarningStream() << "WatchBSP got a monitoring request while not idling...\n"; globalWarningStream() << "WatchBSP got a monitoring request while not idling...\n";
// prompt the user, should we cancel the current process and go ahead? // prompt the user, should we cancel the current process and go ahead?
@ -802,7 +782,7 @@ void CWatchBSP::DoMonitoringLoop( GPtrArray *pCmd, const char *sBSPName ){
Reset(); Reset();
// } // }
} }
m_pCmd = str_ptr_array_clone( pCmd ); m_commands = commands;
m_iCurrentStep = 0; m_iCurrentStep = 0;
DoEBeginStep(); DoEBeginStep();
} }

View File

@ -30,11 +30,13 @@
#pragma once #pragma once
#include <vector>
#include "string/string.h"
void BuildMonitor_Construct(); void BuildMonitor_Construct();
void BuildMonitor_Destroy(); void BuildMonitor_Destroy();
typedef struct _GPtrArray GPtrArray; void BuildMonitor_Run( const std::vector<CopiedString>& commands, const char* mapName );
void BuildMonitor_Run( GPtrArray* commands, const char* mapName );
extern bool g_WatchBSP_Enabled; extern bool g_WatchBSP_Enabled;
extern bool g_WatchBSP_LeakStop; extern bool g_WatchBSP_LeakStop;