Remove game/preview functions split in library mode, just let normal procedure run instead:
authorunc0rr
Tue, 04 Mar 2014 00:40:23 +0400
changeset 10175 c92668840ea8
parent 10174 710ffaae7d59
child 10176 ea022e9483c2
Remove game/preview functions split in library mode, just let normal procedure run instead: - Remove tons of HWLIBRARY ifdefs - Override ParamCount and ParamStr to let GetParams work - Small fixes here and there
QTfrontend/net/tcpBase.cpp
QTfrontend/net/tcpBase.h
QTfrontend/ui/widget/mapContainer.cpp
hedgewars/ArgParsers.pas
hedgewars/hwLibrary.pas
hedgewars/hwengine.pas
hedgewars/uVariables.pas
--- a/QTfrontend/net/tcpBase.cpp	Mon Mar 03 22:36:30 2014 +0400
+++ b/QTfrontend/net/tcpBase.cpp	Tue Mar 04 00:40:23 2014 +0400
@@ -17,48 +17,54 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include "tcpBase.h"
-
 #include <QList>
 #include <QImage>
 #include <QThread>
+#include <QApplication>
 
+#include "tcpBase.h"
 #include "hwconsts.h"
 #include "MessageDialog.h"
 
 #ifdef HWLIBRARY
-extern "C" void Game(char**arguments);
-extern "C" void GenLandPreview(int port);
+extern "C" {
+    void RunEngine(int argc, char ** argv);
+
+    int operatingsystem_parameter_argc;
+    char ** operatingsystem_parameter_argv;
+}
 
 
 EngineInstance::EngineInstance(QObject *parent)
     : QObject(parent)
 {
-    port = 0;
+
 }
 
 EngineInstance::~EngineInstance()
 {
 }
 
+void EngineInstance::setArguments(const QStringList & arguments)
+{
+    m_arguments.clear();
+    m_arguments << qApp->arguments().at(0).toUtf8();
+
+    m_argv.resize(arguments.size() + 1);
+    m_argv[0] = m_arguments.last().data();
+
+    int i = 1;
+    foreach(const QString & s, arguments)
+    {
+        m_arguments << s.toUtf8();
+        m_argv[i] = m_arguments.last().data();
+        ++i;
+    }
+}
+
 void EngineInstance::start()
 {
-#if 0
-    char *args[11];
-    args[0] = "65000";  //ipcPort
-    args[1] = "1024";   //cScreenWidth
-    args[2] = "768";    //cScreenHeight
-    args[3] = "0";      //cReducedQuality
-    args[4] = "en.txt"; //cLocaleFName
-    args[5] = "koda";   //UserNick
-    args[6] = "1";      //SetSound
-    args[7] = "1";      //SetMusic
-    args[8] = "0";      //cAltDamage
-    args[9]= datadir->absolutePath().toAscii().data(); //cPathPrefix
-    args[10]= NULL;     //recordFileName
-    Game(args);
-#endif
-    GenLandPreview(port);
+    RunEngine(m_argv.size(), m_argv.data());
 }
 
 #endif
@@ -129,9 +135,9 @@
     IPCSocket = 0;
 
 #ifdef HWLIBRARY
-    QThread *thread = new QThread;
-    EngineInstance *instance = new EngineInstance;
-    instance->port = IPCServer->serverPort();
+    QThread *thread = new QThread();
+    EngineInstance *instance = new EngineInstance();
+    instance->setArguments(getArguments());
 
     instance->moveToThread(thread);
 
@@ -144,7 +150,7 @@
     QProcess * process;
     process = new QProcess();
     connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
-    QStringList arguments=getArguments();
+    QStringList arguments = getArguments();
 
 #ifdef QT_DEBUG
     // redirect everything written on stdout/stderr
--- a/QTfrontend/net/tcpBase.h	Mon Mar 03 22:36:30 2014 +0400
+++ b/QTfrontend/net/tcpBase.h	Tue Mar 04 00:40:23 2014 +0400
@@ -28,6 +28,8 @@
 #include <QDir>
 #include <QProcess>
 #include <QPointer>
+#include <QVector>
+#include <QList>
 
 #include <QImage>
 
@@ -93,12 +95,15 @@
     EngineInstance(QObject *parent = 0);
     ~EngineInstance();
 
-    int port;
+    void setArguments(const QStringList & arguments);
+
 public slots:
     void start(void);
 signals:
     void finished(void);
 private:
+    QList<QByteArray> m_arguments;
+    QVector<char *> m_argv;
 };
 #endif
 
--- a/QTfrontend/ui/widget/mapContainer.cpp	Mon Mar 03 22:36:30 2014 +0400
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Tue Mar 04 00:40:23 2014 +0400
@@ -615,7 +615,7 @@
 
     if (pMap)
     {
-        disconnect(pMap, 0, this, SLOT(setImage(const QImage)));
+        disconnect(pMap, 0, this, SLOT(setImage(const QPixmap)));
         disconnect(pMap, 0, this, SLOT(setHHLimit(int)));
         pMap = 0;
     }
--- a/hedgewars/ArgParsers.pas	Mon Mar 03 22:36:30 2014 +0400
+++ b/hedgewars/ArgParsers.pas	Tue Mar 04 00:40:23 2014 +0400
@@ -21,19 +21,36 @@
 unit ArgParsers;
 interface
 
-{$IFNDEF HWLIBRARY}
 procedure GetParams;
-{$ELSE}
-procedure parseCommandLine(argc: LongInt; argv: PPChar);
-
-var operatingsystem_parameter_argc: integer = 0; export;
+{$IFDEF HWLIBRARY}
+var operatingsystem_parameter_argc: LongInt = 0; export;
     operatingsystem_parameter_argv: pointer = nil; export;
     operatingsystem_parameter_envp: pointer = nil; export;
+
+function ParamCount: LongInt;
+function ParamStr(i: LongInt): shortstring;
 {$ENDIF}
 
 implementation
 uses uVariables, uTypes, uUtils, uSound, uConsts;
-var isInternal: Boolean {$IFDEF HWLIBRARY} = true{$ENDIF};
+var isInternal: Boolean;
+
+{$IFDEF HWLIBRARY}
+
+type PCharArray = array[0..255] of PChar;
+     PPCharArray = ^PCharArray;
+
+function ParamCount: LongInt;
+begin
+    ParamCount:= operatingsystem_parameter_argc - 1
+end;
+
+function ParamStr(i: LongInt): shortstring;
+begin
+    ParamStr:= StrPas(PPCharArray(operatingsystem_parameter_argv)^[i])
+end;
+
+{$ENDIF}
 
 procedure GciEasterEgg;
 begin
@@ -310,20 +327,16 @@
     WriteLn(stdout, '');
 end;
 
-procedure parseCommandLine{$IFDEF HWLIBRARY}(argc: LongInt; argv: PPChar){$ENDIF};
+procedure parseCommandLine;
 var paramIndex: LongInt;
     paramTotal: LongInt;
     index, nextIndex: LongInt;
     wrongParameter: boolean;
 //var tmpInt: LongInt;
 begin
-    {$IFDEF HWLIBRARY}
-    operatingsystem_parameter_argc:= argc;
-    operatingsystem_parameter_argv:= argv;
-    {$ENDIF}
 
-    paramIndex:= {$IFDEF HWLIBRARY}0{$ELSE}1{$ENDIF};
-    paramTotal:= {$IFDEF HWLIBRARY}argc-1{$ELSE}ParamCount{$ENDIF}; //-1 because pascal enumeration is inclusive
+    paramIndex:= 1;
+    paramTotal:= ParamCount; //-1 because pascal enumeration is inclusive
     (*
     WriteLn(stdout, 'total parameters: ' + inttostr(paramTotal));
     tmpInt:= 0;
@@ -340,18 +353,13 @@
         index:= paramIndex;
         if index = paramTotal then nextIndex:= index
         else nextIndex:= index+1;
-        {$IFDEF HWLIBRARY}
-        wrongParameter:= parseParameter( argv[index], argv[nextIndex], paramIndex);
-        {$ELSE}
         wrongParameter:= parseParameter( ParamStr(index), ParamStr(nextIndex), paramIndex);
-        {$ENDIF}
         inc(paramIndex);
         end;
     if wrongParameter = true then
         GameType:= gmtSyntax;
 end;
 
-{$IFNDEF HWLIBRARY}
 procedure GetParams;
 begin
     isInternal:= (ParamStr(1) = '--internal');
@@ -383,7 +391,6 @@
     WriteLn(stdout,'UserPathPrefix: ' + UserPathPrefix);
     *)
 end;
-{$ENDIF}
 
 end.
 
--- a/hedgewars/hwLibrary.pas	Mon Mar 03 22:36:30 2014 +0400
+++ b/hedgewars/hwLibrary.pas	Tue Mar 04 00:40:23 2014 +0400
@@ -119,8 +119,7 @@
     Game;
 {$ELSE}
 exports
-    Game,
-    GenLandPreview,
+    RunEngine,
     LoadLocaleWrapper,
     HW_versionInfo,
     HW_versionString,
--- a/hedgewars/hwengine.pas	Mon Mar 03 22:36:30 2014 +0400
+++ b/hedgewars/hwengine.pas	Tue Mar 04 00:40:23 2014 +0400
@@ -39,11 +39,11 @@
      ;
 
 {$IFDEF HWLIBRARY}
+procedure RunEngine(argc: LongInt; argv: PPChar); cdecl; export;
+
 procedure preInitEverything();
 procedure initEverything(complete:boolean);
 procedure freeEverything(complete:boolean);
-procedure Game(argc: LongInt; argv: PPChar); cdecl; export;
-procedure GenLandPreview(port: Longint); cdecl; export;
 
 implementation
 {$ELSE}
@@ -324,15 +324,11 @@
 {$ENDIF}
 
 ///////////////////////////////////////////////////////////////////////////////
-procedure Game{$IFDEF HWLIBRARY}(argc: LongInt; argv: PPChar); cdecl; export{$ENDIF};
+procedure Game;
 //var p: TPathType;
 var s: shortstring;
     i: LongInt;
 begin
-{$IFDEF HWLIBRARY}
-    preInitEverything();
-    parseCommandLine(argc, argv);
-{$ENDIF}
     initEverything(true);
     WriteLnToConsole('Hedgewars engine ' + cVersionString + '-r' + cRevisionString +
                      ' (' + cHashString + ') with protocol #' + inttostr(cNetProtoVersion));
@@ -530,15 +526,11 @@
 end;
 
 ///////////////////////////////////////////////////////////////////////////////
-procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
+procedure GenLandPreview;
 var Preview: TPreviewAlpha;
 begin
     initEverything(false);
-{$IFDEF HWLIBRARY}
-    WriteLnToConsole('Preview connecting on port ' + inttostr(port));
-    ipcPort:= port;
-    InitStepsFlags:= cifRandomize;
-{$ENDIF}
+
     InitIPC;
     IPCWaitPongEvent;
     TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
@@ -552,18 +544,24 @@
     freeEverything(false);
 end;
 
-{$IFNDEF HWLIBRARY}
+{$IFDEF HWLIBRARY}
+procedure RunEngine(argc: LongInt; argv: PPChar); cdecl; export;
+begin
+    operatingsystem_parameter_argc:= argc;
+    operatingsystem_parameter_argv:= argv;
+{$ELSE}
+begin
+{$ENDIF}
 
 ///////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////// m a i n ///////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
-begin
 {$IFDEF PAS2C}
     // workaround for pascal's ParamStr and ParamCount
     init(argc, argv);
 {$ENDIF}
     preInitEverything();
-    cTagsMask:= htTeamName or htName or htHealth; // this one doesn't fit nicely w/ reset of other variables. suggestions welcome
+
     GetParams();
 
     if GameType = gmtLandPreview then
@@ -572,11 +570,14 @@
         Game();
 
     // return 1 when engine is not called correctly
-    {$IFDEF PAS2C}
+    {$IFDEF PAS2C OR HWLIBRARY}
     exit(LongInt(GameType = gmtSyntax));
     {$ELSE}
     halt(LongInt(GameType = gmtSyntax));
     {$ENDIF}
 
+{$IFDEF HWLIBRARY}
+end;
 {$ENDIF}
+
 end.
--- a/hedgewars/uVariables.pas	Mon Mar 03 22:36:30 2014 +0400
+++ b/hedgewars/uVariables.pas	Tue Mar 04 00:40:23 2014 +0400
@@ -2438,6 +2438,8 @@
     cVideoQuality      := 0;
     cAudioCodec        := '';
 {$ENDIF}
+
+    cTagsMask:= htTeamName or htName or htHealth;
 end;
 
 procedure initModule;