First pass for collab editing

This commit is contained in:
Marcus Huderle 2020-10-04 13:20:21 -05:00
parent 502ebd449f
commit 17ebd029ea
7 changed files with 280 additions and 40 deletions

View file

@ -236,23 +236,23 @@
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<widget class="QTabBar" name="mainTabBar" native="true"/>
</item>
<item>
<spacer name="horizontalSpacer_20">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
<item>
<widget class="QTabBar" name="mainTabBar" native="true"/>
</item>
<item>
<spacer name="horizontalSpacer_20">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QStackedWidget" name="mainStackedWidget">
@ -365,8 +365,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>543</width>
<height>600</height>
<width>508</width>
<height>665</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_8">
@ -893,8 +893,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>443</width>
<height>74</height>
<width>442</width>
<height>77</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_7">
@ -1081,10 +1081,10 @@
</property>
<property name="geometry">
<rect>
<x>8</x>
<x>0</x>
<y>0</y>
<width>431</width>
<height>341</height>
<width>425</width>
<height>419</height>
</rect>
</property>
<property name="sizePolicy">
@ -1499,8 +1499,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>430</width>
<height>521</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<property name="sizePolicy">
@ -1544,8 +1544,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>430</width>
<height>521</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<property name="sizePolicy">
@ -1589,8 +1589,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>430</width>
<height>521</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<property name="sizePolicy">
@ -1634,8 +1634,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>430</width>
<height>521</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<property name="sizePolicy">
@ -1679,8 +1679,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>430</width>
<height>521</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<property name="sizePolicy">
@ -1730,8 +1730,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>430</width>
<height>521</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<property name="sizePolicy">
@ -2431,8 +2431,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>118</width>
<height>118</height>
<width>101</width>
<height>101</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_14">
@ -2693,7 +2693,7 @@
<x>0</x>
<y>0</y>
<width>1287</width>
<height>22</height>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -2742,6 +2742,10 @@
<addaction name="actionNew_Tileset"/>
<addaction name="actionTileset_Editor"/>
<addaction name="actionRegion_Map_Editor"/>
<addaction name="separator"/>
<addaction name="actionConnect_to_Collab"/>
<addaction name="actionCreate_Collab_Session"/>
<addaction name="actionJoin_Collab_Session"/>
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
@ -3022,6 +3026,21 @@
<string>Export Map Stitch Image...</string>
</property>
</action>
<action name="actionConnect_to_Collab">
<property name="text">
<string>Connect to Collab</string>
</property>
</action>
<action name="actionCreate_Collab_Session">
<property name="text">
<string>Create Collab Session</string>
</property>
</action>
<action name="actionJoin_Collab_Session">
<property name="text">
<string>Join Collab Session</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
@ -3034,6 +3053,7 @@
<class>AdjustingStackedWidget</class>
<extends>QStackedWidget</extends>
<header>adjustingstackedwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>GraphicsView</class>

31
include/collabsession.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef COLLABSESSION_H
#define COLLABSESSION_H
#include "mainwindow.h"
#include <QString>
#define COMMAND_BLOCKS_CHANGED 0x1
#define SERVER_MESSAGE_CREATED_SESSION 0x1
#define SERVER_MESSAGE_JOINED_SESSION 0x2
#define SERVER_MESSAGE_BROADCAST_COMMAND 0x3
class CollabSession
{
public:
CollabSession(MainWindow *mainWindow);
static void init(MainWindow *mainWindow);
static bool connect(QString host, int port);
static void createSession(QString sessionName);
static void joinSession(QString sessionName);
static QByteArray prepareSocketMessage(QByteArray message, int messageType);
static void onBlockChanged(int x, int y, Block prevBlock, Block newBlock);
void processMessage();
MainWindow *mainWindow = nullptr;
private:
static void handleBlocksChangedCommand(QByteArray message);
QTcpSocket *socket = nullptr;
QByteArray messageBuffer;
};
#endif // COLLABSESSION_H

View file

@ -1,6 +1,7 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QTcpSocket>
#include <QString>
#include <QModelIndex>
#include <QMainWindow>
@ -232,6 +233,10 @@ private slots:
void on_actionRegion_Map_Editor_triggered();
void on_actionConnect_to_Collab_triggered();
void on_actionCreate_Collab_Session_triggered();
void on_actionJoin_Collab_Session_triggered();
private:
Ui::MainWindow *ui;
TilesetEditor *tilesetEditor = nullptr;

View file

@ -4,7 +4,7 @@
#
#-------------------------------------------------
QT += core gui qml
QT += core gui qml network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
@ -15,6 +15,7 @@ ICON = resources/icons/porymap.icns
QMAKE_CXXFLAGS += -std=c++11 -Wall
SOURCES += src/core/block.cpp \
src/collabsession.cpp \
src/core/blockdata.cpp \
src/core/event.cpp \
src/core/heallocation.cpp \
@ -134,6 +135,7 @@ HEADERS += include/core/block.h \
include/ui/regionmapeditor.h \
include/ui/newmappopup.h \
include/ui/mapimageexporter.h \
include/collabsession.h \
include/config.h \
include/editor.h \
include/mainwindow.h \

161
src/collabsession.cpp Normal file
View file

@ -0,0 +1,161 @@
#include "collabsession.h"
#include "log.h"
CollabSession::CollabSession(MainWindow *mainWindow) {
this->mainWindow = mainWindow;
}
CollabSession *collabInstance = nullptr;
void CollabSession::init(MainWindow *mainWindow) {
if (collabInstance) {
delete collabInstance;
}
collabInstance = new CollabSession(mainWindow);
}
bool CollabSession::connect(QString host, int port) {
if (!collabInstance) return false;
collabInstance->socket = new QTcpSocket();
collabInstance->socket->connectToHost(host, port);
QObject::connect(collabInstance->socket, &QTcpSocket::connected, [=](){
logInfo("Socket connected to collab server...");
});
QObject::connect(collabInstance->socket, &QTcpSocket::disconnected, [=](){
logInfo("Socket disconnected from collab server...");
});
QObject::connect(collabInstance->socket, &QTcpSocket::readyRead, [=](){
collabInstance->processMessage();
});
if (!collabInstance->socket->waitForConnected(10000)) {
logError(collabInstance->socket->errorString());
delete collabInstance->socket;
collabInstance->socket = nullptr;
return false;
}
return true;
}
void CollabSession::processMessage() {
QByteArray newContent = collabInstance->socket->readAll();
this->messageBuffer.append(newContent);
// Process all the available messages in the read buffer.
while (true) {
if (this->messageBuffer.size() < 12) {
break;
}
uint32_t signature = this->messageBuffer.at(0) |
(this->messageBuffer.at(1) << 8) |
(this->messageBuffer.at(2) << 16) |
(this->messageBuffer.at(3) << 24);
if (signature != 0x98765432) {
// Invalid signature from server. Unclear how to recover--just disconnect.
logError("Invalid message signature received from collab server. Disconnecting...");
this->socket->disconnectFromHost();
delete this->socket;
this->socket = nullptr;
break;
}
uint32_t payloadSize = this->messageBuffer.at(4) |
(this->messageBuffer.at(5) << 8) |
(this->messageBuffer.at(6) << 16) |
(this->messageBuffer.at(7) << 24);
int messageSize = 12 + payloadSize;
if (this->messageBuffer.size() < messageSize) {
break;
}
uint32_t messageType = this->messageBuffer.at(8) |
(this->messageBuffer.at(9) << 8) |
(this->messageBuffer.at(10) << 16) |
(this->messageBuffer.at(11) << 24);
QByteArray message = this->messageBuffer.left(messageSize);
message.remove(0, 12);
this->messageBuffer = this->messageBuffer.remove(0, messageSize);
if (message.size() < 2) {
break;
}
switch (messageType) {
case SERVER_MESSAGE_BROADCAST_COMMAND:
int commandType = (message.at(1) << 8) | message.at(0);
switch (commandType) {
case COMMAND_BLOCKS_CHANGED:
this->handleBlocksChangedCommand(message);
break;
}
break;
}
}
}
void CollabSession::createSession(QString sessionName) {
QByteArray msg = prepareSocketMessage(sessionName.toLocal8Bit(), 0x1);
logInfo(QString("Joining session: %1").arg(collabInstance->socket->write(QByteArray(msg))));
}
void CollabSession::joinSession(QString sessionName) {
QByteArray msg = prepareSocketMessage(sessionName.toLocal8Bit(), 0x2);
logInfo(QString("Joining session: %1").arg(collabInstance->socket->write(QByteArray(msg))));
}
QByteArray CollabSession::prepareSocketMessage(QByteArray message, int messageType) {
QByteArray header = QByteArray();
// 4-byte little endian signature is 0x12345678.
header.append(0x78);
header.append(0x56);
header.append(0x34);
header.append(0x12);
// 4-byte little endian payload size.
header.append(message.size() & 0xff);
header.append((message.size() >> 8) & 0xff);
header.append((message.size() >> 16) & 0xff);
header.append((message.size() >> 24) & 0xff);
// 4-byte little endian message type.
header.append(messageType & 0xff);
header.append((messageType >> 8) & 0xff);
header.append((messageType >> 16) & 0xff);
header.append((messageType >> 24) & 0xff);
// Append payload.
return header + message;
}
void CollabSession::handleBlocksChangedCommand(QByteArray message) {
uint16_t numBlocks = (message.at(3) << 8) | message.at(2);
if (message.size() != 4 + numBlocks * 6)
return;
for (int i = 0; i < numBlocks; i++) {
int index = 4 + i * 6;
int x = (uint8_t(message.at(index + 1)) << 8) | uint8_t(message.at(index));
int y = (uint8_t(message.at(index + 3)) << 8) | uint8_t(message.at(index + 2));
uint16_t metatileId = (uint8_t(message.at(index + 5)) << 8) | uint8_t(message.at(index + 4));
collabInstance->mainWindow->setBlock(x, y, metatileId, 0, 0, true, false);
}
}
void CollabSession::onBlockChanged(int x, int y, Block prevBlock, Block newBlock) {
if (!collabInstance || !collabInstance->socket) return;
if (prevBlock.rawValue() == newBlock.rawValue()) return;
QByteArray message;
message.append(COMMAND_BLOCKS_CHANGED & 0xFF);
message.append((COMMAND_BLOCKS_CHANGED >> 8) & 0xFF);
message.append(0x1);
message.append('\0');
message.append(x & 0xFF);
message.append((x >> 8) & 0xFF);
message.append(y & 0xFF);
message.append((y >> 8) & 0xFF);
message.append(newBlock.tile & 0xFF);
message.append((newBlock.tile >> 8) & 0xFF);
QByteArray msg = CollabSession::prepareSocketMessage(message, 0x3);
collabInstance->socket->write(QByteArray(msg));
}

View file

@ -2,6 +2,7 @@
#include "map.h"
#include "imageproviders.h"
#include "scripting.h"
#include "collabsession.h"
#include "editcommands.h"
@ -383,6 +384,7 @@ void Map::setBlock(int x, int y, Block block, bool enableScriptCallback) {
layout->blockdata->blocks->replace(i, block);
if (enableScriptCallback) {
Scripting::cb_MetatileChanged(x, y, prevBlock, block);
CollabSession::onBlockChanged(x, y, prevBlock, block);
}
}
}

View file

@ -10,6 +10,7 @@
#include "currentselectedmetatilespixmapitem.h"
#include "customattributestable.h"
#include "scripting.h"
#include "collabsession.h"
#include "adjustingstackedwidget.h"
#include "draggablepixmapitem.h"
#include "editcommands.h"
@ -394,6 +395,7 @@ bool MainWindow::openProject(QString dir) {
this->setProjectSpecificUIVisibility();
Scripting::init(this);
CollabSession::init(this);
bool already_open = isProjectOpen() && (editor->project->root == dir);
if (!already_open) {
editor->closeProject();
@ -1430,6 +1432,23 @@ void MainWindow::on_actionMap_Shift_triggered()
on_toolButton_Shift_clicked();
}
void MainWindow::on_actionConnect_to_Collab_triggered()
{
if (!CollabSession::connect("35.223.92.12", 4000)) {
logInfo("Failed to connect to collab session");
}
}
void MainWindow::on_actionCreate_Collab_Session_triggered()
{
CollabSession::createSession("TestSession");
}
void MainWindow::on_actionJoin_Collab_Session_triggered()
{
CollabSession::joinSession("TestSession");
}
void MainWindow::onWheelZoom(int s) {
// Don't zoom the map when the user accidentally scrolls while performing a magic fill. (ctrl + middle button click)
if (!(QApplication::mouseButtons() & Qt::MiddleButton)) {