More refactoring: move PoC preview getting code into flib qmlfrontend
authorunc0rr
Sun, 28 Sep 2014 00:49:04 +0400
branchqmlfrontend
changeset 10428 7c25297720f1
parent 10426 727a154cf784
child 10430 899a30018ede
More refactoring: move PoC preview getting code into flib
hedgewars/CMakeLists.txt
hedgewars/hwLibrary.pas
hedgewars/uFLGameConfig.pas
hedgewars/uFLIPC.pas
hedgewars/uFLTypes.pas
qmlFrontend/flib.h
qmlFrontend/hwengine.cpp
qmlFrontend/hwengine.h
qmlFrontend/qml/qmlFrontend/GameConfig.qml
--- a/hedgewars/CMakeLists.txt	Sun Sep 28 00:18:01 2014 +0400
+++ b/hedgewars/CMakeLists.txt	Sun Sep 28 00:49:04 2014 +0400
@@ -99,9 +99,10 @@
     uGearsHandlersMess.pas
     uGearsUtils.pas
     uTeams.pas
-    
+
     uFLIPC.pas
     uFLTypes.pas
+    uFLGameConfig.pas
 
     #these interact with everything, so compile last
     uScript.pas
--- a/hedgewars/hwLibrary.pas	Sun Sep 28 00:18:01 2014 +0400
+++ b/hedgewars/hwLibrary.pas	Sun Sep 28 00:49:04 2014 +0400
@@ -146,8 +146,7 @@
     RunEngine,
     runQuickGame,
     getPreview,
-    registerPreviewCallback,
-    ipcToEngine,
+    registerGUIMessagesCallback,
     flibInit,
     flibFree,
     LoadLocaleWrapper,
--- a/hedgewars/uFLGameConfig.pas	Sun Sep 28 00:18:01 2014 +0400
+++ b/hedgewars/uFLGameConfig.pas	Sun Sep 28 00:49:04 2014 +0400
@@ -1,12 +1,18 @@
 unit uFLGameConfig;
-
 interface
+uses uFLTypes;
 
 procedure resetGameConfig; cdecl; export;
 procedure runQuickGame; cdecl; export;
 procedure getPreview; cdecl; export;
 
+procedure registerGUIMessagesCallback(p: pointer; f: TGUICallback); cdecl; export;
+
 implementation
+uses uFLIPC, hwengine;
+
+var guiCallbackPointer: pointer;
+    guiCallbackFunction: TGUICallback;
 
 const
     MAXCONFIGS = 5;
@@ -54,9 +60,9 @@
         for i:= 0 to Pred(MAXARGS) do
         begin
             if arguments[i][0] = #255 then 
-                arguments[i][255] = #0
+                arguments[i][255]:= #0
             else
-                arguments[i][byte(arguments[i][0]) + 1] = #0;
+                arguments[i][byte(arguments[i][0]) + 1]:= #0;
             argv[i]:= @arguments[i][1]
         end;
 
@@ -81,9 +87,26 @@
         arguments[1]:= '--internal';
         arguments[2]:= '--landpreview';
         argumentsNumber:= 3;
+
+        queueExecution;
+
+        ipcToEngine('eseed ' + seed);
+        ipcToEngine('e$mapgen 0');
+        ipcToEngine('!');
     end;
+end;
 
-    queueExecution
+procedure engineMessageCallback(p: pointer; msg: PChar; len: Longword);
+begin
+    if len = 128 * 256 then guiCallbackFunction(guiCallbackPointer, mtPreview, msg, len)
+end;
+
+procedure registerGUIMessagesCallback(p: pointer; f: TGUICallback); cdecl; export;
+begin
+    guiCallbackPointer:= p;
+    guiCallbackFunction:= f;
+
+    registerIPCCallback(nil, @engineMessageCallback)
 end;
 
 end.
--- a/hedgewars/uFLIPC.pas	Sun Sep 28 00:18:01 2014 +0400
+++ b/hedgewars/uFLIPC.pas	Sun Sep 28 00:49:04 2014 +0400
@@ -9,7 +9,7 @@
 procedure initIPC;
 procedure freeIPC;
 
-procedure ipcToEngine(p: PChar; len: byte); cdecl; export;
+procedure ipcToEngine(s: shortstring);
 //function  ipcReadFromEngine: shortstring;
 //function  ipcCheckFromEngine: boolean;
 
@@ -18,7 +18,7 @@
 function ipcReadFromFrontend: shortstring;
 function ipcCheckFromFrontend: boolean;
 
-procedure registerPreviewCallback(p: pointer; f: TIPCCallback); cdecl; export;
+procedure registerIPCCallback(p: pointer; f: TIPCCallback);
 
 implementation
 
@@ -69,12 +69,10 @@
     SDL_UnlockMutex(mut)
 end;
 
-procedure ipcToEngine(p: PChar; len: byte); cdecl; export;
+procedure ipcToEngine(s: shortstring);
 var msg: TIPCMessage;
 begin
-    writeln(stderr, len);
-    Move(p^, msg.str[1], len);
-    msg.str[0]:= char(len);
+    msg.str:= s;
     msg.buf:= nil;
     ipcSend(msg, msgEngine, mutEngine, condEngine)
 end;
@@ -133,7 +131,7 @@
     until false
 end;
 
-procedure registerPreviewCallback(p: pointer; f: TIPCCallback); cdecl; export;
+procedure registerIPCCallback(p: pointer; f: TIPCCallback);
 begin
     callbackPointer:= p;
     callbackFunction:= f;
--- a/hedgewars/uFLTypes.pas	Sun Sep 28 00:18:01 2014 +0400
+++ b/hedgewars/uFLTypes.pas	Sun Sep 28 00:49:04 2014 +0400
@@ -1,12 +1,16 @@
 unit uFLTypes;
 interface
 
-type TIPCMessage = record
+type
+    TMessageType = (mtPreview);
+
+    TIPCMessage = record
                    str: shortstring;
                    len: Longword;
                    buf: Pointer
                end;
     TIPCCallback = procedure (p: pointer; msg: PChar; len: Longword);
+    TGUICallback = procedure (p: pointer; msgType: TMessageType; msg: PChar; len: Longword);
 
 implementation
 
--- a/qmlFrontend/flib.h	Sun Sep 28 00:18:01 2014 +0400
+++ b/qmlFrontend/flib.h	Sun Sep 28 00:49:04 2014 +0400
@@ -7,6 +7,10 @@
 extern "C" {
 #endif
 
+enum MessageType {
+    MSG_PREVIEW
+};
+
 typedef union string255_
     {
         struct {
@@ -19,9 +23,9 @@
     } string255;
 
 typedef void RunEngine_t(int argc, const char ** argv);
-typedef void registerPreviewCallback_t(void * context, void (*)(void * context, const char * msg, uint32_t len));
-typedef void ipcToEngine_t(const char * msg, uint8_t len);
+typedef void registerGUIMessagesCallback_t(void * context, void (*)(void * context, MessageType mt, const char * msg, uint32_t len));
 typedef void flibInit_t(const char * localPrefix, const char * userPrefix);
+typedef void getPreview_t();
 typedef void flibFree_t();
 
 #ifdef __cplusplus
--- a/qmlFrontend/hwengine.cpp	Sun Sep 28 00:18:01 2014 +0400
+++ b/qmlFrontend/hwengine.cpp	Sun Sep 28 00:49:04 2014 +0400
@@ -8,9 +8,9 @@
 #include "previewimageprovider.h"
 
 extern "C" {
-    RunEngine_t *RunEngine;
-    registerPreviewCallback_t *registerPreviewCallback;
-    ipcToEngine_t *ipcToEngine;
+    RunEngine_t *flibRunEngine;
+    registerGUIMessagesCallback_t *flibRegisterGUIMessagesCallback;
+    getPreview_t *flibGetPreview;
     flibInit_t *flibInit;
     flibFree_t *flibFree;
 }
@@ -24,14 +24,14 @@
     if(!hwlib.load())
         qWarning() << "Engine library not found" << hwlib.errorString();
 
-    RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
-    registerPreviewCallback = (registerPreviewCallback_t*) hwlib.resolve("registerIPCCallback");
-    ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine");
+    flibRunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
+    flibRegisterGUIMessagesCallback = (registerGUIMessagesCallback_t*) hwlib.resolve("registerGUIMessagesCallback");
+    flibGetPreview = (getPreview_t*) hwlib.resolve("getPreview");
     flibInit = (flibInit_t*) hwlib.resolve("flibInit");
     flibFree = (flibFree_t*) hwlib.resolve("flibFree");
 
     flibInit(".", "~/.hedgewars");
-    registerPreviewCallback(this, &enginePreviewCallback);
+    flibRegisterGUIMessagesCallback(this, &guiMessagesCallback);
 }
 
 HWEngine::~HWEngine()
@@ -39,23 +39,10 @@
     flibFree();
 }
 
-void HWEngine::run()
+void HWEngine::getPreview()
 {
-    m_argsList.clear();
-    m_argsList << "";
-    m_argsList << "--internal";
-    m_argsList << "--landpreview";
-
-    m_args.resize(m_argsList.size());
-    for(int i = m_argsList.size() - 1; i >=0; --i)
-        m_args[i] = m_argsList[i].constData();
-
-    m_seed = QUuid::createUuid().toString();
-
-    RunEngine(m_args.size(), m_args.data());
-    sendIPC("eseed " + m_seed.toLatin1());
-    sendIPC("e$mapgen 0");
-    sendIPC("!");
+    //m_seed = QUuid::createUuid().toString();
+    flibGetPreview();
 }
 
 static QObject *hwengine_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
@@ -72,15 +59,8 @@
     qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine", hwengine_singletontype_provider);
 }
 
-void HWEngine::sendIPC(const QByteArray & b)
-{
-    quint8 len = b.size() > 255 ? 255 : b.size();
-    qDebug() << "sendIPC: len = " << len;
 
-    ipcToEngine(b.constData(), len);
-}
-
-void HWEngine::enginePreviewCallback(void *context, const char * msg, quint32 len)
+void HWEngine::guiMessagesCallback(void *context, MessageType mt, const char * msg, uint32_t len)
 {
     HWEngine * obj = (HWEngine *)context;
     QByteArray b = QByteArray::fromRawData(msg, len);
--- a/qmlFrontend/hwengine.h	Sun Sep 28 00:18:01 2014 +0400
+++ b/qmlFrontend/hwengine.h	Sun Sep 28 00:49:04 2014 +0400
@@ -18,7 +18,7 @@
     ~HWEngine();
 
     static void exposeToQML();
-    Q_INVOKABLE void run();
+    Q_INVOKABLE void getPreview();
     Q_INVOKABLE QString currentSeed();
     
 signals:
@@ -27,13 +27,10 @@
 public slots:
 
 private:
-    QList<QByteArray> m_argsList;
-    QVector<const char *> m_args;
     QQmlEngine * m_engine;
     QString m_seed;
 
-    static void enginePreviewCallback(void *context, const char * msg, quint32 len);
-    void sendIPC(const QByteArray &b);
+    static void guiMessagesCallback(void * context, MessageType mt, const char * msg, uint32_t len);
 
 private slots:
     void engineMessageHandler(const QByteArray &msg);
--- a/qmlFrontend/qml/qmlFrontend/GameConfig.qml	Sun Sep 28 00:18:01 2014 +0400
+++ b/qmlFrontend/qml/qmlFrontend/GameConfig.qml	Sun Sep 28 00:49:04 2014 +0400
@@ -10,7 +10,7 @@
         height: 150
 
         onClicked: {
-            HWEngine.run()
+            HWEngine.getPreview()
         }
 
         Connections {