Skip to content

Commit

Permalink
NIAHttpBOT指令判断重构
Browse files Browse the repository at this point in the history
  • Loading branch information
NIANIANKNIA committed Jul 9, 2024
1 parent e4cd73a commit d17eae7
Show file tree
Hide file tree
Showing 16 changed files with 888 additions and 962 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG-PRE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# v1.5.0-pre-2 更新日志
# v1.5.0-pre-3 更新日志

[![BDS VERSION](https://img.shields.io/badge/BDS-1.20.61.01-green?style=for-the-badge&logo=appveyor)](https://www.minecraft.net/en-us/download/server/bedrock)

Expand All @@ -7,6 +7,9 @@
> **未完成开发版本提醒**:该版本**部分功能仍然在开发状态中**,可能出现包括但不限于服务器无法正常运行等问题,仅供开发者预览,请勿使用!
## 优化



## 新增

Expand Down
2 changes: 1 addition & 1 deletion NIAHttpBOT/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
"${CMAKE_SOURCE_DIR}/lib/libcrypto-3-x64.dll"
"$<TARGET_FILE_DIR:${PROJECT}>/libcrypto-3-x64.dll"
COMMENT "Copying dll to executable directory")
endif()
endif()
69 changes: 69 additions & 0 deletions NIAHttpBOT/src/CommandListener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "CommandListener.h"


void listenForCommands(const char* programName) {

using CommandHandler = std::function<void(const std::vector<std::string>&)>;

std::unordered_map<std::string, CommandHandler> commandMap;

commandMap["help"] = [](const std::vector<std::string>&) {
std::cout << "可用指令列表:" << std::endl;
std::cout << " reload - 重启程序" << std::endl;
std::cout << " stop - 关闭程序" << std::endl;
std::cout << " setcfg <cfgname> <cfgdata> - 设置配置项" << std::endl;
};

commandMap["reload"] = [programName](const std::vector<std::string>&) {
INFO("1s后重启程序..." );
std::this_thread::sleep_for(std::chrono::seconds(1));
#ifdef _WIN32
std::system(("start cmd /k " + std::string(programName)).c_str());
#else
if (fork() == 0) {
execl(programName, programName, (char*)NULL);
}
#endif
exit(0);
};

commandMap["stop"] = [](const std::vector<std::string>&) {
INFO("1s后将关闭程序...");
std::this_thread::sleep_for(std::chrono::seconds(1));
exit(0);
};

commandMap["setcfg"] = [](const std::vector<std::string>& args) {
if (args.size() < 3) {
WARN("setcfg 指令需要两个参数: <cfgname> <cfgdata>");
return;
}
std::string cfgname = args[1];
std::string cfgdata = args[2];
};

// 启动输入监听线程
std::thread inputThread([&commandMap]() {
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream iss(line);
std::vector<std::string> tokens;
std::string token;
while (iss >> token) {
tokens.push_back(token);
}
if (tokens.empty()) {
continue;
}
auto it = commandMap.find(tokens[0]);
if (it != commandMap.end()) {
it->second(tokens); // 调用对应的处理函数
} else {
std::cout << "未知指令,请检查后再次输入!" << std::endl;
}
}
});

// 等待输入线程完成
inputThread.join();
}
26 changes: 26 additions & 0 deletions NIAHttpBOT/src/CommandListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef COMMANDLISTENER_H
#define COMMANDLISTENER_H

#include <string>
#include <iostream>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <unordered_map>
#include <functional>
#include <sstream>
#include "Logger.hpp"

#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
#endif




// 声明命令监听函数
void listenForCommands(const char* programName);

#endif
58 changes: 13 additions & 45 deletions NIAHttpBOT/src/NIAHttpBOT.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
/*
You must accept Minecraft's End User Licence Agreement (EULA).
It means please do not use any content that violates the EULA for commercial purposes!
Accepting this licence means you also accept the Minecraft EULA(https://account.mojang.com/terms)
If you violate the EULA, the developer is not liable for any damages.
Copyright (C) 2024 Nia-Server
The developer is not responsible for you, and the developer is not obliged to write code for you, and is not liable for any consequences of your use.
Expand All @@ -16,7 +10,7 @@ If you do not accept these terms, please delete this project immediately.
authors: NIANIANKNIA && jiansyuan
email: server@mcnia.com
email: dev@mcnia.com
Project address: https://github.com/Nia-Server/NiaServer-Core/
Expand Down Expand Up @@ -52,6 +46,8 @@ If you have any problems with this project, please contact the authors.
#include "QQBot.h"
#include "File_API.h"
#include "Game_API.h"
#include "CommandListener.h"


//定义版本号
#define VERSION "v1.5.0-pre-3"
Expand Down Expand Up @@ -235,18 +231,21 @@ signed int main(signed int argc, char** argv) {
else XINFO("语言配置已加载成功");
}

INFO(X("监听服务器已在 ") + IPAddress + ":" + std::to_string(ServerPort) + X(" 上成功启动!"));
//如果配置文件中启用使用qq机器人,则输出qq机器人的监听端口
if (UseQQBot) INFO(X("客户端已在 ") + IPAddress + ":" + std::to_string(ClientPort) + Locate + X(" 上成功启动!"));
INFO(X("sapi事件监听服务器已在 ") + IPAddress + ":" + std::to_string(ServerPort) + X(" 上成功启动!"));
if (UseQQBot) {
INFO(X("qq-bot事件监听服务器已在 ") + IPAddress + ":" + std::to_string(ServerPort) + Locate + X(" 上成功启动!"));
INFO(X("qq-bot客户端已在 ") + IPAddress + ":" + std::to_string(ClientPort) + X(" 上成功启动!"));
}
XINFO("项目地址:https://github.com/Nia-Server/NiaServer-Core/tree/main/NIAHttpBOT");
XINFO("项目作者:@NIANIANKNIA @jiansyuan");
XINFO("在使用中遇到问题请前往项目下的 issue 反馈,如果觉得本项目不错不妨点个 star!");
if (UseCmd) XWARN("检测到执行DOS命令功能已启用,请注意服务器安全!");


#ifdef WIN32
std::thread ssl_thread(sslThread);
ssl_thread.detach();
// std::thread ssl_thread(sslThread);
// ssl_thread.detach();
sslThread();
#endif

//初始化服务器
Expand Down Expand Up @@ -323,40 +322,9 @@ signed int main(signed int argc, char** argv) {
init_file_API(svr);

//监听终端命令输入
std::thread inputThread([&]() {
std::string command;
bool hasCommand = false;
while (std::cin >> command) {
//重载指令
if (command == "reload") {
hasCommand = true;
INFO("1s后重启NiaHttp-BOT...");
std::this_thread::sleep_for(std::chrono::seconds(1));
#ifdef _WIN32
std::system(("start cmd /k " + std::string(argv[0])).c_str());
#else
if (fork() == 0) {
execl(argv[0], argv[0], (char*)NULL);
}
#endif
exit(0);
}
//停止指令
if (command == "stop") {
hasCommand = true;
INFO("1s后将关闭NiaHttp-BOT...");
std::this_thread::sleep_for(std::chrono::seconds(1));
exit(0);
}
if (!hasCommand) {
WARN("未知指令,请检查后再次输入!");
}
}
});
listenForCommands(argv[0]);

svr.listen(IPAddress, ServerPort);

inputThread.join();

return 0;
}
Loading

0 comments on commit d17eae7

Please sign in to comment.