plug + basic parsing

This commit is contained in:
Green Sky 2023-11-25 20:43:00 +01:00
parent 8f02e6b5ee
commit 0219d24647
No known key found for this signature in database
8 changed files with 181 additions and 36 deletions

View File

@ -68,6 +68,7 @@ endif()
add_subdirectory(./src) add_subdirectory(./src)
if (SOLANACEAE_TOXIC_GAMES_BUILD_PLUGINS) if (SOLANACEAE_TOXIC_GAMES_BUILD_PLUGINS)
message("II SOLANACEAE_TOXIC_GAMES_BUILD_PLUGINS " ${SOLANACEAE_TOXIC_GAMES_BUILD_PLUGINS})
add_subdirectory(./plugins) add_subdirectory(./plugins)
endif() endif()

View File

@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
include(FetchContent) include(FetchContent)
if (NOT TARGET imgui) if (NOT TARGET imgui)
message("II using FetchContent imgui")
FetchContent_Declare(imgui FetchContent_Declare(imgui
GIT_REPOSITORY https://github.com/ocornut/imgui.git GIT_REPOSITORY https://github.com/ocornut/imgui.git
GIT_TAG d4ddc46e7 GIT_TAG d4ddc46e7
@ -35,6 +36,7 @@ endif()
# TODO: move entt dep into solanaceae_contact # TODO: move entt dep into solanaceae_contact
if (NOT TARGET EnTT::EnTT) if (NOT TARGET EnTT::EnTT)
message("II using FetchContent EnTT")
FetchContent_Declare(EnTT FetchContent_Declare(EnTT
GIT_REPOSITORY https://github.com/skypjack/entt.git GIT_REPOSITORY https://github.com/skypjack/entt.git
GIT_TAG v3.12.2 GIT_TAG v3.12.2
@ -44,6 +46,7 @@ if (NOT TARGET EnTT::EnTT)
endif() endif()
if (NOT TARGET solanaceae_contact) if (NOT TARGET solanaceae_contact)
message("II using FetchContent solanaceae_contact")
FetchContent_Declare(solanaceae_contact FetchContent_Declare(solanaceae_contact
GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_contact.git GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_contact.git
GIT_TAG master GIT_TAG master
@ -53,6 +56,7 @@ if (NOT TARGET solanaceae_contact)
endif() endif()
if (NOT TARGET solanaceae_plugin) if (NOT TARGET solanaceae_plugin)
message("II using FetchContent solanaceae_plugin")
FetchContent_Declare(solanaceae_plugin FetchContent_Declare(solanaceae_plugin
GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_plugin.git GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_plugin.git
GIT_TAG master GIT_TAG master
@ -62,6 +66,7 @@ if (NOT TARGET solanaceae_plugin)
endif() endif()
if (NOT TARGET solanaceae_toxcore) if (NOT TARGET solanaceae_toxcore)
message("II using FetchContent solanaceae_toxcore")
# set option to interface only? or make default # set option to interface only? or make default
FetchContent_Declare(solanaceae_toxcore FetchContent_Declare(solanaceae_toxcore
GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_toxcore.git GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_toxcore.git
@ -72,6 +77,7 @@ if (NOT TARGET solanaceae_toxcore)
endif() endif()
if (NOT TARGET solanaceae_tox_contacts AND NOT TARGET solanaceae_tox_messages) if (NOT TARGET solanaceae_tox_contacts AND NOT TARGET solanaceae_tox_messages)
message("II using FetchContent solanaceae_tox")
FetchContent_Declare(solanaceae_tox FetchContent_Declare(solanaceae_tox
GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_tox.git GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_tox.git
GIT_TAG master GIT_TAG master

View File

@ -1,2 +1,11 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR) cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
add_library(plugin_toxic_games SHARED
./plugin_toxic_games.cpp
)
target_link_libraries(plugin_toxic_games PUBLIC
solanaceae_plugin
solanaceae_toxic_games
)

View File

@ -0,0 +1,90 @@
#include <solanaceae/plugin/solana_plugin_v1.h>
//#include <solanaceae/util/config_model.hpp>
#include <solanaceae/toxic_games/toxic_games.hpp>
#include <memory>
#include <iostream>
#define RESOLVE_INSTANCE(x) static_cast<x*>(solana_api->resolveInstance(#x))
#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast<x*>(v))
static std::unique_ptr<ToxicGames> g_tg = nullptr;
extern "C" {
SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) {
return "ToxicGames";
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) {
return SOLANA_PLUGIN_VERSION;
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) {
std::cout << "PLUGIN TG START()\n";
if (solana_api == nullptr) {
return 1;
}
//RegistryMessageModel* rmm = nullptr;
//ConfigModelI* conf = nullptr;
Contact3Registry* cr;
ToxI* t;
ToxEventProviderI* tep;
ToxContactModel2* tcm;
{ // make sure required types are loaded
//rmm = RESOLVE_INSTANCE(RegistryMessageModel);
//conf = RESOLVE_INSTANCE(ConfigModelI);
cr = RESOLVE_INSTANCE(Contact3Registry);
t = RESOLVE_INSTANCE(ToxI);
tep = RESOLVE_INSTANCE(ToxEventProviderI);
tcm = RESOLVE_INSTANCE(ToxContactModel2);
if (cr == nullptr) {
std::cerr << "PLUGIN TG missing Contact3Registry\n";
return 2;
}
if (t == nullptr) {
std::cerr << "PLUGIN TG missing ToxI\n";
return 2;
}
if (tep == nullptr) {
std::cerr << "PLUGIN TG missing ToxEventProviderI\n";
return 2;
}
if (tcm == nullptr) {
std::cerr << "PLUGIN TG missing ToxContactModel2\n";
return 2;
}
}
// static store, could be anywhere tho
// construct with fetched dependencies
g_tg = std::make_unique<ToxicGames>(*cr, *t, *tep, *tcm);
// register types
PROVIDE_INSTANCE(ToxicGames, "ToxicGames", g_tg.get());
return 0;
}
SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) {
std::cout << "PLUGIN TG STOP()\n";
g_tg.reset();
}
SOLANA_PLUGIN_EXPORT void solana_plugin_tick(float delta) {
(void)delta;
//std::cout << "PLUGIN TG TICK()\n";
//g_tg->iterate();
}
} // extern C

View File

@ -29,11 +29,14 @@ endif()
######################################## ########################################
add_library(solanaceae_toxic_games add_library(solanaceae_toxic_games
./solanaceae_toxic_games.hpp ./solanaceae/toxic_games/toxic_games.hpp
./solanaceae_toxic_games.cpp ./solanaceae/toxic_games/toxic_games.cpp
) )
target_link_libraries(solanaceae_toxic_games PUBLIC target_link_libraries(solanaceae_toxic_games PUBLIC
solanaceae_tox_contacts solanaceae_tox_contacts
) )
target_include_directories(solanaceae_toxic_games PUBLIC .)
target_compile_features(solanaceae_toxic_games PUBLIC cxx_std_17)

View File

@ -0,0 +1,70 @@
#include "./toxic_games.hpp"
#include <cstdint>
#include <iostream>
ToxicGames::ToxicGames(
Contact3Registry& cr,
ToxI& t,
ToxEventProviderI& tep,
ToxContactModel2& tcm
) :
_cr(cr),
_t(t),
_tep(tep),
_tcm(tcm)
{
// register custom packet handlers
_tep.subscribe(this, Tox_Event::TOX_EVENT_FRIEND_LOSSLESS_PACKET);
_tep.subscribe(this, Tox_Event::TOX_EVENT_GROUP_CUSTOM_PACKET);
_tep.subscribe(this, Tox_Event::TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET);
}
bool ToxicGames::onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) {
//CUSTOM_PACKET_GAME_INVITE = 160,
//CUSTOM_PACKET_GAME_DATA = 161,
const uint32_t friend_number = tox_event_friend_lossless_packet_get_friend_number(e);
const uint32_t data_length = tox_event_friend_lossless_packet_get_data_length(e);
const uint8_t* data = tox_event_friend_lossless_packet_get_data(e);
if (data_length < 7) { // packet id + netver + gametype + 4 gameid
return false;
}
if (data[0] != 160 && data[0] != 161) {
// not a toxic games friend packet
return false;
}
const uint8_t game_networking_version = data[1];
const uint8_t game_type = data[2];
const uint32_t game_id =
(data[3] << 24) |
(data[4] << 16) |
(data[5] << 8) |
(data[6] << 0)
;
if (game_networking_version != 0x01) {
std::cerr << "TG warning: peer sent mismatching network version packet\n";
return false;
}
if (data[0] == 160) {
std::cout << "TG: game invite packet gt:" << (uint32_t)game_type << " id:" << game_id << "\n";
} else if (data[0] == 161) {
std::cout << "TG: game packet gt:" << (uint32_t)game_type << " id:" << game_id << "\n";
}
return true;
}
bool ToxicGames::onToxEvent(const Tox_Event_Group_Custom_Packet* e) {
return false;
}
bool ToxicGames::onToxEvent(const Tox_Event_Group_Custom_Private_Packet* e) {
return false;
}

View File

@ -1,34 +0,0 @@
#include "./solanaceae_toxic_games.hpp"
ToxicGames::ToxicGames(
Contact3Registry& cr,
ToxI& t,
ToxEventProviderI& tep,
ToxContactModel2& tcm
) :
_cr(cr),
_t(t),
_tep(tep),
_tcm(tcm)
{
// register custom packet handlers
_tep.subscribe(this, Tox_Event::TOX_EVENT_FRIEND_LOSSLESS_PACKET);
_tep.subscribe(this, Tox_Event::TOX_EVENT_GROUP_CUSTOM_PACKET);
_tep.subscribe(this, Tox_Event::TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET);
}
bool ToxicGames::onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) {
//CUSTOM_PACKET_GAME_INVITE = 160,
//CUSTOM_PACKET_GAME_DATA = 161,
return false;
}
bool ToxicGames::onToxEvent(const Tox_Event_Group_Custom_Packet* e) {
return false;
}
bool ToxicGames::onToxEvent(const Tox_Event_Group_Custom_Private_Packet* e) {
return false;
}