Skip to content

Commit

Permalink
emeplo onde o codigo c++ usa funções da lib em rust e a lib rust usa …
Browse files Browse the repository at this point in the history
…funções do c++
  • Loading branch information
beats-dh committed Sep 11, 2023
1 parent 63e19ed commit 1076770
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
Binary file added beats-rust/beats.lib
Binary file not shown.
28 changes: 28 additions & 0 deletions beats-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
use std::f64;

enum Player {}

extern "C" {
// Declara as funções que serão importadas do C++
fn getPlayerByID(player_id: u32) -> *mut Player;
fn setInMarket(player: *mut Player, in_market: bool);

fn set_in_market(player: *mut std::ffi::c_void, value: bool);
}

#[no_mangle]
pub extern "C" fn your_rust_function(player_ptr: *mut std::ffi::c_void, value: bool) {
unsafe {
set_in_market(player_ptr, value);
}
}

// A função que substituirá playerLeaveMarket em C++
#[no_mangle]
pub extern "C" fn player_leave_market(player_id: u32) {
let player = unsafe { getPlayerByID(player_id) };
if !player.is_null() {
unsafe {
setInMarket(player, false);
}
}
}

#[no_mangle]
pub extern "C" fn get_base_attack(level: u32) -> i32 {
let square = f64::sqrt(2.0 * (level as f64) - 1.0 + 2025.0);
Expand Down
21 changes: 15 additions & 6 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8088,13 +8088,22 @@ void Game::playerNpcGreet(uint32_t playerId, uint32_t npcId) {
}
}

void Game::playerLeaveMarket(uint32_t playerId) {
Player* player = getPlayerByID(playerId);
if (!player) {
return;
}
// essa função pode ser chamada tanto pelo codigo ou a lib rust, como pode ser chamada diretamente no codigo c++
extern "C" {
Player* getPlayerByID(uint32_t playerId) {
g_logger().info("beats rust chamou getPlayerByID no c++");
return g_game().getPlayerByID(playerId, false);
}
}

player->setInMarket(false);
// essa função pode ser chamada tanto pelo codigo ou a lib rust, como pode ser chamada diretamente no codigo c++
extern "C" {
void setInMarket(Player* player, bool inMarket) {
if (player) {
g_logger().info("beats rust chamou setInMarket no c++");
player->setInMarket(inMarket);
}
}
}

void Game::playerBrowseMarket(uint32_t playerId, uint16_t itemId, uint8_t tier) {
Expand Down
1 change: 0 additions & 1 deletion src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ class Game {
void playerLeaveParty(uint32_t playerId);
void playerEnableSharedPartyExperience(uint32_t playerId, bool sharedExpActive);
void playerToggleMount(uint32_t playerId, bool mount);
void playerLeaveMarket(uint32_t playerId);
void playerBrowseMarket(uint32_t playerId, uint16_t itemId, uint8_t tier);
void playerBrowseMarketOwnOffers(uint32_t playerId);
void playerBrowseMarketOwnHistory(uint32_t playerId);
Expand Down
20 changes: 18 additions & 2 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
extern "C" {
int32_t get_base_attack(uint32_t level);
int32_t get_max_weapon_damage(int32_t attack_skill, int32_t attack_value, float attack_factor, int32_t attack_value_base, bool is_melee);
void player_leave_market(uint32_t player_id);
void your_rust_function(void* player, bool value);
}

/*
Expand Down Expand Up @@ -2987,7 +2989,9 @@ void ProtocolGame::parseQuestLine(NetworkMessage &msg) {
}

void ProtocolGame::parseMarketLeave() {
addGameTask(&Game::playerLeaveMarket, player->getID());
auto task = [=]() { player_leave_market(player->getID()); };

Check warning on line 2992 in src/server/network/protocol/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-release

implicit capture of ‘this’ via ‘[=]’ is deprecated in C++20 [-Wdeprecated]

Check warning on line 2992 in src/server/network/protocol/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-release

implicit capture of ‘this’ via ‘[=]’ is deprecated in C++20 [-Wdeprecated]

Check warning on line 2992 in src/server/network/protocol/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-20.04-linux-debug

implicit capture of ‘this’ via ‘[=]’ is deprecated in C++20 [-Wdeprecated]

Check warning on line 2992 in src/server/network/protocol/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

implicit capture of ‘this’ via ‘[=]’ is deprecated in C++20 [-Wdeprecated]
g_logger().info("beats c++ chamou player_leave_market no rust");
g_dispatcher().addTask(task, "ProtocolGame::parseMarketLeave");
}

void ProtocolGame::parseMarketBrowse(NetworkMessage &msg) {
Expand Down Expand Up @@ -4439,6 +4443,16 @@ void ProtocolGame::sendSaleItemList(const std::vector<ShopBlock> &shopVector, co
writeToOutputBuffer(msg);
}

// essa função pode ser chamada tanto pelo codigo ou a lib rust, como pode ser chamada diretamente no codigo c++
extern "C" {
void set_in_market(Player* player, bool value) {

Check warning on line 4448 in src/server/network/protocol/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/server/network/protocol/protocolgame.cpp#L4448

The function 'set_in_market' is never used.
Raw output
src/server/network/protocol/protocolgame.cpp:4448:The function 'set_in_market' is never used.
g_logger().info("beats rust chamou set_in_market no c++");
if (player) {
player->setInMarket(value);
}
}
}

void ProtocolGame::sendMarketEnter(uint32_t depotId) {
NetworkMessage msg;
msg.addByte(0xF6);
Expand All @@ -4456,7 +4470,9 @@ void ProtocolGame::sendMarketEnter(uint32_t depotId) {
return;
}

player->setInMarket(true);
// player->setInMarket(true);
// aqui eu chamo a função na lib rust
your_rust_function(static_cast<void*>(player), true);

// Only use here locker items, itemVector is for use of Game::createMarketOffer
auto [itemVector, lockerItems] = player->requestLockerItems(depotLocker, true);
Expand Down

0 comments on commit 1076770

Please sign in to comment.