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 "stream/stringstream.h"
#include "convert.h"
#include "os/path.h"
#include "qerplugin.h"

View File

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

View File

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

View File

@ -468,7 +468,7 @@ void project_verify( Project& project, Tools& tools ){
#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 )
{
StringBuffer output;
@ -476,6 +476,8 @@ void build_run( size_t buildIdx, CommandListener& listener ){
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() )
{
g_lastExecutedBuild = buildIt;
@ -484,13 +486,15 @@ void build_run( size_t buildIdx, CommandListener& listener ){
StringBuffer output;
command.evaluate( output );
if ( !output.empty() )
listener.execute( output.c_str() );
commands.emplace_back( output.c_str() );
}
}
else
{
globalErrorStream() << "build #" << buildIdx << " not defined";
}
return commands;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -39,7 +39,6 @@
#include <QTimer>
#include "commandlib.h"
#include "convert.h"
#include "string/string.h"
#include "stream/stringstream.h"
@ -97,13 +96,13 @@ private:
socket_t *m_pListenSocket;
socket_t *m_pInSocket;
netmessage_t msg;
GPtrArray *m_pCmd;
std::vector<CopiedString> m_commands;
// used to timeout EBeginStep
Timer m_timeout_timer;
std::size_t m_iCurrentStep;
QTimer m_monitoring_timer;
// 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
xmlParserInputBufferPtr m_xmlInputBuffer;
xmlParserCtxtPtr m_xmlParserCtxt;
@ -118,12 +117,10 @@ private:
public:
CWatchBSP(){
m_pCmd = 0;
m_bBSPPlugin = false;
m_pListenSocket = NULL;
m_pInSocket = NULL;
m_eState = EIdle;
m_sBSPName = NULL;
m_xmlInputBuffer = NULL;
m_bNeedCtxtInit = true;
m_monitoring_timer.callOnTimeout( [this](){ RoutineProcessing(); } );
@ -142,17 +139,9 @@ public:
// called regularly to keep listening
void RoutineProcessing();
// 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(){
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
void Reset();
@ -316,7 +305,7 @@ CWatchBSP *GetWatchBSP(){
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 );
}
@ -617,12 +606,12 @@ void CWatchBSP::DoEBeginStep(){
if ( !m_bBSPPlugin ) {
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 );
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";
globalOutputStream() << msg.c_str();
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
if ( m_timeout_timer.elapsed_sec() > g_WatchBSP_Timeout ) {
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"
"Otherwise you need to disable BSP Monitoring in prefs.", "BSP process monitoring" );
"Make sure you are using a networked version of Q3Map?\n"
"Otherwise you need to disable BSP Monitoring in prefs.", "BSP process monitoring" );
EndMonitoringLoop();
#if 0
if ( m_bBSPPlugin ) {
@ -731,7 +720,7 @@ void CWatchBSP::RoutineProcessing(){
#endif
// move to next step or finish
m_iCurrentStep++;
if ( m_iCurrentStep < m_pCmd->len ) {
if ( m_iCurrentStep < m_commands.size() ) {
DoEBeginStep();
}
else
@ -764,7 +753,7 @@ void CWatchBSP::RoutineProcessing(){
globalOutputStream() << cmd.c_str() << " " << cmdline.c_str() << "\n";
// 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;
msg << "Failed to execute the following command: " << cmd.c_str() << cmdline.c_str();
globalOutputStream() << msg.c_str();
@ -782,17 +771,8 @@ void CWatchBSP::RoutineProcessing(){
}
}
GPtrArray* str_ptr_array_clone( GPtrArray* array ){
GPtrArray* cloned = g_ptr_array_sized_new( array->len );
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 );
void CWatchBSP::DoMonitoringLoop( const std::vector<CopiedString>& commands, const char *sBSPName ){
m_sBSPName = sBSPName;
if ( m_eState != EIdle ) {
globalWarningStream() << "WatchBSP got a monitoring request while not idling...\n";
// 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();
// }
}
m_pCmd = str_ptr_array_clone( pCmd );
m_commands = commands;
m_iCurrentStep = 0;
DoEBeginStep();
}

View File

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