Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Yeppers
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Dec 17, 2023
1 parent 7f8a580 commit 1a33826
Show file tree
Hide file tree
Showing 166 changed files with 115,052 additions and 371 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ option(USE_LUA "Use lua for script support" ON)
add_subdirectory(vendored/fmt)
add_subdirectory(vendored/argparse)
add_subdirectory(vendored/DPP)
add_subdirectory(vendored/glfw)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down Expand Up @@ -54,7 +55,7 @@ set(HYDRA_QT_FILES

set(HYDRA_BOT_FILES
discord/bot.cxx
vendored/gifenc.c
vendored/glad.c
)

set(HYDRA_INCLUDE_DIRECTORIES
Expand Down Expand Up @@ -96,6 +97,7 @@ target_link_libraries(hydra PRIVATE
OpenSSL::SSL
fmt::fmt
dpp
glfw
)
target_include_directories(hydra PRIVATE ${HYDRA_INCLUDE_DIRECTORIES})
target_compile_definitions(hydra PRIVATE HYDRA_VERSION="${PROJECT_VERSION}")
Expand Down
138 changes: 124 additions & 14 deletions discord/bot.cxx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "commands.hxx"
#include "corewrapper.hxx"
#include "glad.h"
#include "hydra/core.hxx"
#include "intents.h"
#include "message.h"
#include "unicode_emoji.h"
#include <compatibility.hxx>
#include <cstdint>
#include <dpp.h>
#include <filesystem>
#include <GLFW/glfw3.h>
#include <settings.hxx>
#include <string>

Expand All @@ -16,7 +20,98 @@ constexpr uint32_t operator""_hash(const char* str, size_t)

namespace fs = std::filesystem;

std::shared_ptr<hydra::EmulatorWrapper> emulator;
GLuint fbo;
void* get_proc_address;

void init_gl()
{
if (!glfwInit())
printf("glfwInit() failed\n");
// glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow* window = glfwCreateWindow(640, 480, "", nullptr, nullptr);
if (!window)
printf("glfwCreateWindow() failed\n");
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
printf("gladLoadGLLoader() failed\n");
get_proc_address = (void*)glfwGetProcAddress;

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 400, 480);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
}

int32_t read_input_callback(uint32_t player, hydra::ButtonType button)
{
return 0;
}

struct BotState
{
BotState()
{
init_gl();
std::string core_path = Settings::Get("core_path");
emulator = hydra::EmulatorFactory::Create(fs::path(core_path) / "libAlber.so");
width = emulator->shell->getNativeSize().width;
height = emulator->shell->getNativeSize().height;
buffer.resize(width * height * 4);

if (emulator->shell->hasInterface(hydra::InterfaceType::IOpenGlRendered))
{
auto gli = emulator->shell->asIOpenGlRendered();
gli->setGetProcAddress(get_proc_address);
gli->resetContext();
gli->setFbo(fbo);
emulator->shell->setOutputSize({width, height});
}

if (emulator->shell->hasInterface(hydra::InterfaceType::IInput))
{
hydra::IInput* shell_input = emulator->shell->asIInput();
shell_input->setPollInputCallback([]() {});
shell_input->setCheckButtonCallback(read_input_callback);
}

emulator->LoadGame("/home/offtkp/Roms/3DS/zelda.3ds");
}

const std::vector<uint8_t>& get_frame()
{
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data());

for (uint32_t y = 0; y < height / 2; y++)
{
for (uint32_t x = 0; x < width; x++)
{
std::swap(buffer[(y * width + x) * 4 + 0],
buffer[((height - 1 - y) * width + x) * 4 + 0]);
std::swap(buffer[(y * width + x) * 4 + 1],
buffer[((height - 1 - y) * width + x) * 4 + 1]);
std::swap(buffer[(y * width + x) * 4 + 2],
buffer[((height - 1 - y) * width + x) * 4 + 2]);
std::swap(buffer[(y * width + x) * 4 + 3],
buffer[((height - 1 - y) * width + x) * 4 + 3]);
}
}
return buffer;
}

uint32_t width, height;
std::shared_ptr<hydra::EmulatorWrapper> emulator;
std::vector<uint8_t> buffer;
};

std::unique_ptr<BotState> state;

int bot_main()
{
Expand Down Expand Up @@ -50,12 +145,10 @@ int bot_main()
return 1;
}

emulator = hydra::EmulatorFactory::Create(fs::path(core_path) / "libAlber.so");

dpp::cluster bot(token);
state = std::make_unique<BotState>();

dpp::cluster bot(token, dpp::i_default_intents | dpp::i_message_content);
bot.on_log(dpp::utility::cout_logger());

bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>())
{
Expand Down Expand Up @@ -87,19 +180,19 @@ int bot_main()
{
dpp::embed embed =
dpp::embed()
.set_title(emulator->GetInfo(hydra::InfoType::CoreName))
.set_url(emulator->GetInfo(hydra::InfoType::Website))
.set_description(emulator->GetInfo(hydra::InfoType::Description))
.set_author(emulator->GetInfo(hydra::InfoType::Author),
.set_title(state->emulator->GetInfo(hydra::InfoType::CoreName))
.set_url(state->emulator->GetInfo(hydra::InfoType::Website))
.set_description(state->emulator->GetInfo(hydra::InfoType::Description))
.set_author(state->emulator->GetInfo(hydra::InfoType::Author),
"https://github.com/hydra-emu/hydra", "attachment://icon.png")
.set_thumbnail("attachment://icon.png");
dpp::message msg = dpp::message(event.command.channel_id, embed);
if (emulator->GetIcon().size() > 0)
if (state->emulator->GetIcon().size() > 0)
{
msg.add_file(
"icon.png",
std::string(emulator->GetIcon().begin(), emulator->GetIcon().end()),
"image/png");
msg.add_file("icon.png",
std::string(state->emulator->GetIcon().begin(),
state->emulator->GetIcon().end()),
"image/png");
}
event.reply(msg);
break;
Expand Down Expand Up @@ -193,6 +286,23 @@ int bot_main()
}
});

bot.on_message_create([&bot](const dpp::message_create_t& event) {
/* See if the message contains the phrase we want to check for.
* If there's at least a single match, we reply and say it's not allowed.
*/
printf("Got message: %s\n", event.msg.content.c_str());
if (event.msg.content.find("I hate pandas") != std::string::npos)
{
std::string fake_ip = std::to_string(rand() % 255) + "." +
std::to_string(rand() % 255) + "." +
std::to_string(rand() % 255) + "." + std::to_string(rand() % 255);
event.reply("That is not allowed here. Please, mind your language! I will now dox your "
"ip for saying this. Your ip is:" +
fake_ip,
true);
}
});

bot.start(dpp::st_wait);
return 0;
}
3 changes: 2 additions & 1 deletion discord/commands.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ struct Commands
static std::vector<dpp::slashcommand> commands = {
dpp::slashcommand("ping", "Ping the bot", appid),
dpp::slashcommand("status", "Print the emulator status", appid),
dpp::slashcommand("screen", "Show the game screen", appid)};
dpp::slashcommand("screen", "Show the game screen", appid),
};
return commands;
}
};
Loading

0 comments on commit 1a33826

Please sign in to comment.