Skip to content

Commit

Permalink
📃 Finished lua-ing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lygaen committed Jan 15, 2024
1 parent 1f672c1 commit 180118c
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 14 deletions.
36 changes: 34 additions & 2 deletions src/entities/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef MINESERVER_ENTITY_H
#define MINESERVER_ENTITY_H

#include <plugins/luaheaders.h>
#include <types/vector.hpp>
#include <types/angle.hpp>
#include <types/uuid.h>
Expand Down Expand Up @@ -57,7 +58,24 @@ class IEntity
*/
UUID uuid;

// TODO implement everything in Lua
/**
* @brief Loads the IEntity class to a Lua one
*
* @param state the state to load to
* @param namespaceName the namespace to load to
*/
static void loadLua(lua_State *state, const char *namespaceName)
{
luabridge::getGlobalNamespace(state)
.beginNamespace(namespaceName)
.beginClass<IEntity>("IEntity")
.addProperty("position", &IEntity::position)
.addProperty("yaw", &IEntity::yaw)
.addProperty("pitch", &IEntity::pitch)
.addProperty("uuid", &IEntity::uuid)
.endClass()
.endNamespace();
}
};

/**
Expand All @@ -75,7 +93,21 @@ class ILiving : public IEntity
*
*/
virtual ~ILiving() = default;
// TODO implement everything in Lua

/**
* @brief Loads the ILiving class to a Lua one
*
* @param state the state to load to
* @param namespaceName the namespace to load to
*/
static void loadLua(lua_State *state, const char *namespaceName)
{
luabridge::getGlobalNamespace(state)
.beginNamespace(namespaceName)
.beginClass<ILiving>("ILiving")
.endClass()
.endNamespace();
}
};

#endif // MINESERVER_ENTITY_H
33 changes: 33 additions & 0 deletions src/entities/luaregentities.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file luaregentities.hpp
* @author Lygaen
* @brief Utility for loading types lua classes
* @version 0.1
* @date 2024-01-15
*
* @copyright Copyright (c) 2024
*
*/

#ifndef MINESERVER_LUAREGENTITIES_H
#define MINESERVER_LUAREGENTITIES_H

#include <plugins/luaheaders.h>
#include <entities/entity.h>
#include <entities/player.h>

/**
* @brief Loads entities classes to lua
*
* @param state the lua state to load to
*/
void loadEntitiesLua(lua_State *state)
{
const char *namespaceName = "entities";

IEntity::loadLua(state, namespaceName);
ILiving::loadLua(state, namespaceName);
Player::loadLua(state, namespaceName);
}

#endif // MINESERVER_LUAREGENTITIES_H
17 changes: 16 additions & 1 deletion src/entities/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,22 @@ class Player : public ILiving
* Also called the username or tag
*/
std::string name;
// TODO implement everything in Lua

/**
* @brief Loads the Player class to a Lua one
*
* @param state the state to load to
* @param namespaceName the namespace to load to
*/
static void loadLua(lua_State *state, const char *namespaceName)
{
luabridge::getGlobalNamespace(state)
.beginNamespace(namespaceName)
.beginClass<Player>("Player")
.addProperty("name", &Player::name)
.endClass()
.endNamespace();
}
};

#endif // MINESERVER_PLAYER_H
12 changes: 8 additions & 4 deletions src/net/packets/login/encryptionexchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
*
* Used in protocol encryption scheme,
* for the server to ask to the client.
*
* It is not exposed to lua for security
* reasons. A plugin should not handle
* encryption and stuff.
*/
class EncryptionRequest : public IPacket
{
Expand Down Expand Up @@ -55,15 +59,17 @@ class EncryptionRequest : public IPacket
@deprecated should not be used, useless
*/
void read(IMCStream *stream) override;

// TODO implement everything in Lua
};

/**
* @brief Encryption Response Packet
*
* Used to verify the verify token as well
* as encrypting the channel with the shared secret.
*
* It is not exposed to lua for security
* reasons. A plugin should not handle
* encryption and stuff.
*/
class EncryptionResponse : public IPacket
{
Expand Down Expand Up @@ -114,8 +120,6 @@ class EncryptionResponse : public IPacket
* @param stream the stream to read from
*/
void read(IMCStream *stream) override;

// TODO implement everything in Lua
};

#endif // MINESERVER_ENCRYPTIONEXCHANGE_H
5 changes: 3 additions & 2 deletions src/net/packets/login/setcompression.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
/**
* @brief Set Compression Packet
*
* Is not exposed to lua as it could
* break if the threshold value is
* modified.
*/
class SetCompression : public IPacket
{
Expand Down Expand Up @@ -48,8 +51,6 @@ class SetCompression : public IPacket
* @deprecated should not be used, useless
*/
void read(IMCStream *stream) override;

// TODO implement everything in Lua
};

#endif // MINESERVER_SETCOMPRESSION_H
3 changes: 3 additions & 0 deletions src/net/packets/luaregpackets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <net/packets/status/serverlist.h>
#include <net/packets/status/pingpong.h>
#include <net/packets/login/loginstartend.h>
#include <net/packets/play/luaregplaypackets.hpp>
#include <net/packets/handshake.h>

/**
Expand All @@ -28,6 +29,8 @@ void loadPacketsLua(lua_State *state)
LoginStart::loadLua(state, namespaceName);
LoginSuccess::loadLua(state, namespaceName);

loadPlayPacketsLua(state);

ServerListPacket::loadLua(state, namespaceName);
PingPongPacket::loadLua(state, namespaceName);

Expand Down
22 changes: 22 additions & 0 deletions src/net/packets/play/disconnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ void DisconnectLogin::read(IMCStream *stream)
throw std::runtime_error("DisconnectLogin read should not be called !");
}

void DisconnectLogin::loadLua(lua_State *state, const char *baseNamespaceName)
{
luabridge::getGlobalNamespace(state)
.beginNamespace(baseNamespaceName)
.beginClass<DisconnectLogin>("DisconnectLogin")
.addConstructor<void(const ChatMessage &)>()
.addProperty("reason", &DisconnectLogin::reason)
.endClass()
.endNamespace();
}

void DisconnectPlay::write(IMCStream *stream)
{
stream->writeChat(reason);
Expand All @@ -34,3 +45,14 @@ void DisconnectPlay::read(IMCStream *stream)
// Does nothing
throw std::runtime_error("DisconnectPlay read should not be called !");
}

void DisconnectPlay::loadLua(lua_State *state, const char *baseNamespaceName)
{
luabridge::getGlobalNamespace(state)
.beginNamespace(baseNamespaceName)
.beginClass<DisconnectPlay>("DisconnectPlay")
.addConstructor<void(const ChatMessage &)>()
.addProperty("reason", &DisconnectPlay::reason)
.endClass()
.endNamespace();
}
16 changes: 14 additions & 2 deletions src/net/packets/play/disconnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ class DisconnectPlay : public IPacket
*/
void read(IMCStream *stream) override;

// TODO implement everything in Lua
/**
* @brief Loads packet as lua class
*
* @param state lua state to load to
* @param baseNamespaceName the base namespace name to load to
*/
static void loadLua(lua_State *state, const char *baseNamespaceName);
};

/**
Expand Down Expand Up @@ -85,7 +91,13 @@ class DisconnectLogin : public IPacket
*/
void read(IMCStream *stream) override;

// TODO implement everything in Lua
/**
* @brief Loads packet as lua class
*
* @param state lua state to load to
* @param baseNamespaceName the base namespace name to load to
*/
static void loadLua(lua_State *state, const char *baseNamespaceName);
};

#endif // MINESERVER_DISCONNECT_H
31 changes: 31 additions & 0 deletions src/net/packets/play/luaregplaypackets.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @file luaregplaypackets.hpp
* @author Lygaen
* @brief Utility for loading types lua classes
* @version 0.1
* @date 2024-01-15
*
* @copyright Copyright (c) 2024
*
*/

#ifndef MINESERVER_LUAREGPLAYPACKETS_H
#define MINESERVER_LUAREGPLAYPACKETS_H

#include <plugins/luaheaders.h>
#include <net/packets/play/disconnect.h>

/**
* @brief Loads entities classes to lua
*
* @param state the lua state to load to
*/
void loadPlayPacketsLua(lua_State *state)
{
const char *namespaceName = "packets";

DisconnectLogin::loadLua(state, namespaceName);
DisconnectPlay::loadLua(state, namespaceName);
}

#endif // MINESERVER_LUAREGPLAYPACKETS_H
2 changes: 2 additions & 0 deletions src/plugins/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <net/luaregnet.hpp>
#include <utils/luaregutils.hpp>
#include <plugins/events/luaregevents.hpp>
#include <entities/luaregentities.hpp>

Plugin::Plugin(std::string path) : path(std::move(path)), state(nullptr)
{
Expand Down Expand Up @@ -64,6 +65,7 @@ void Plugin::defineLibs()
loadEventsLua(state);
loadUtilsLua(state);
loadNetLua(state);
loadEntitiesLua(state);
}

PluginsManager::PluginsManager() : plugins()
Expand Down
19 changes: 18 additions & 1 deletion src/types/angle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define MINESERVER_ANGLE_H

#include <cstddef>
#include <plugins/luaheaders.h>

/**
* @brief Angle class holder
Expand Down Expand Up @@ -70,7 +71,23 @@ class Angle
return value;
}

// TODO implement everything in Lua
/**
* @brief Loads Angle to lua state
*
* @param state lua state
* @param namespaceName base namespace name
*/
static void loadLua(lua_State *state, const char *namespaceName)
{
luabridge::getGlobalNamespace(state)
.beginNamespace(namespaceName)
.beginClass<Angle>("Angle")
.addConstructor<void(float), void(std::byte)>()
.addFunction("getDegrees", &Angle::getDegrees)
.addFunction("getByte", &Angle::getByte)
.endClass()
.endNamespace();
}
};

#endif // MINESERVER_ANGLE_H
4 changes: 4 additions & 0 deletions src/types/luaregtypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <plugins/luaheaders.h>
#include <types/clientstate.h>
#include <types/chatmessage.h>
#include <types/angle.hpp>
#include <types/vector.hpp>
#include <types/uuid.h>

/**
Expand All @@ -29,6 +31,8 @@ void loadTypesLua(lua_State *state)
UUID::loadLua(state, namespaceName);
loadClientStateLua(state, namespaceName);
ChatMessage::loadLua(state, namespaceName);
Angle::loadLua(state, namespaceName);
loadVectorLua(state, namespaceName);
}

#endif // MINESERVER_LUAREGTYPES_H
Loading

0 comments on commit 180118c

Please sign in to comment.