添加Visual Studio编译支持

This commit is contained in:
_Karasu_ 2022-10-20 13:15:42 +08:00
parent aea49d6b31
commit a08102fc30
12 changed files with 152 additions and 74 deletions

13
.gitignore vendored
View File

@ -2,6 +2,8 @@
build/ build/
lib/ lib/
cmake*/ cmake*/
out/
CMakeSettings.json
# Prerequisites # Prerequisites
*.d *.d
@ -37,3 +39,14 @@ cmake*/
*.app *.app
cmake-build-debug cmake-build-debug
.idea .idea
.vs
# Built Dependencies
dependencies/Http/out
dependencies/Http/lib
dependencies/Http/.vs./
dependencies/Http/CMakeSettings.json
dependencies/Person/out
dependencies/Person/lib
dependencies/Person/.vs./
dependencies/Person/CMakeSettings.json

View File

@ -25,6 +25,9 @@ set(SOURCE_HEADER_PATH
# srccmakecmake # srccmakecmake
file(GLOB PLUGIN_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) file(GLOB PLUGIN_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
if (MSVC)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
endif (MSVC)
# #
add_library( add_library(
@ -38,11 +41,15 @@ target_include_directories(MiraiCPPlugin PUBLIC ${SOURCE_HEADER_PATH})
# #
if (WIN32) if (WIN32)
target_link_libraries(MiraiCPPlugin -static if (MINGW)
${CMAKE_CURRENT_SOURCE_DIR}/lib/libcurl.dll.a target_link_libraries(MiraiCPPlugin -static
${CMAKE_CURRENT_SOURCE_DIR}/lib/libHttp.dll.a ${CMAKE_CURRENT_SOURCE_DIR}/lib/libHttp.dll.a
${CMAKE_CURRENT_SOURCE_DIR}/lib/libiconv.dll.a
${CMAKE_CURRENT_SOURCE_DIR}/lib/libPerson.dll.a) ${CMAKE_CURRENT_SOURCE_DIR}/lib/libPerson.dll.a)
else (MSVC)
target_link_libraries(MiraiCPPlugin -static
${CMAKE_CURRENT_SOURCE_DIR}/lib/Http.lib
${CMAKE_CURRENT_SOURCE_DIR}/lib/Person.lib)
endif()
endif (WIN32) endif (WIN32)
if (UNIX) if (UNIX)
target_link_libraries(MiraiCPPlugin -static-libstdc++ -static-libgcc) target_link_libraries(MiraiCPPlugin -static-libstdc++ -static-libgcc)

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24) cmake_minimum_required(VERSION 3.23)
project(Http) project(Http)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)

View File

@ -18,10 +18,18 @@
#ifndef HTTP_LIBRARY_H #ifndef HTTP_LIBRARY_H
#define HTTP_LIBRARY_H #define HTTP_LIBRARY_H
#ifdef _MSC_VER
#ifdef EXPORT_HTTP
#define HTTP_LIB __declspec(dllexport)
#else
#define HTTP_LIB __declspec(dllimport)
#endif
#endif
#include <string> #include <string>
namespace Http { namespace Http {
class Request { class HTTP_LIB Request {
public: public:
Request(); Request();
~Request(); ~Request();
@ -42,11 +50,13 @@ namespace Http {
static size_t writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata); static size_t writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata);
}; };
class FileUtilities { class HTTP_LIB FileUtilities {
public: public:
static std::string readFile(const std::string& path); static std::string readFile(const std::string& path);
static void writeFile(const std::string& path, const std::string& content); static void writeFile(const std::string& path, const std::string& content);
}; };
} }
#endif //HTTP_LIBRARY_H #endif //HTTP_LIBRARY_H

View File

@ -15,7 +15,9 @@
with Lucas' Bot. If not, see <https://www.gnu.org/licenses/>. with Lucas' Bot. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "library.h" #define EXPORT_HTTP
#include "http.h"
#include "curl/curl.h" #include "curl/curl.h"
#include <random> #include <random>
@ -23,7 +25,7 @@
#include <sstream> #include <sstream>
namespace Http { namespace Http {
Request::Request() { HTTP_LIB Request::Request() {
this->url = ""; this->url = "";
this->method = "GET"; this->method = "GET";
this->body = ""; this->body = "";
@ -32,33 +34,33 @@ namespace Http {
this->verbose = false; this->verbose = false;
} }
Request::~Request() = default; HTTP_LIB Request::~Request() = default;
void Request::setUrl(const std::string& theUrl) { void HTTP_LIB Request::setUrl(const std::string& theUrl) {
this->url = theUrl; this->url = theUrl;
} }
void Request::setMethod(const std::string& theMethod) { void HTTP_LIB Request::setMethod(const std::string& theMethod) {
this->method = theMethod; this->method = theMethod;
} }
void Request::setBody(const std::string& theBody) { void HTTP_LIB Request::setBody(const std::string& theBody) {
this->body = theBody; this->body = theBody;
} }
void Request::setHeader(const std::string& theHeader) { void HTTP_LIB Request::setHeader(const std::string& theHeader) {
this->header = theHeader; this->header = theHeader;
} }
void Request::setVerbose(bool theVerbose) { void HTTP_LIB Request::setVerbose(bool theVerbose) {
this->verbose = theVerbose; this->verbose = theVerbose;
} }
std::string Request::getResponse() { std::string HTTP_LIB Request::getResponse() {
return this->response; return this->response;
} }
void Request::send() { void HTTP_LIB Request::send() {
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
curl = curl_easy_init(); curl = curl_easy_init();
@ -78,14 +80,14 @@ namespace Http {
} }
} }
size_t Request::writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata) { size_t HTTP_LIB Request::writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata) {
auto *response = static_cast<std::string *>(userdata); auto *response = static_cast<std::string *>(userdata);
response->append(ptr, size * nmemb); response->append(ptr, size * nmemb);
return size * nmemb; return size * nmemb;
} }
std::string FileUtilities::readFile(const std::string &path) { std::string HTTP_LIB FileUtilities::readFile(const std::string &path) {
std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate); std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate);
std::stringstream buffer; std::stringstream buffer;
// read binary from path to the string // read binary from path to the string
@ -93,7 +95,7 @@ namespace Http {
return buffer.str(); return buffer.str();
} }
void FileUtilities::writeFile(const std::string &path, const std::string &content) { void HTTP_LIB FileUtilities::writeFile(const std::string &path, const std::string &content) {
std::ofstream file(path, std::ios::out | std::ios::binary); std::ofstream file(path, std::ios::out | std::ios::binary);
// write content to path in binary format // write content to path in binary format
file.write(content.c_str(), content.size()); file.write(content.c_str(), content.size());

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24) cmake_minimum_required(VERSION 3.23)
project(Person) project(Person)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)

View File

@ -15,10 +15,12 @@
with Lucas' Bot. If not, see <https://www.gnu.org/licenses/>. with Lucas' Bot. If not, see <https://www.gnu.org/licenses/>.
*/ */
#define EXPORT_PERSON
#include "Person.h" #include "Person.h"
#include <random> #include <random>
bool Person::isSignOnToday() const { bool PERSON_LIB Person::isSignOnToday() const {
// Get today's 12:00 A.M. time point // Get today's 12:00 A.M. time point
std::time_t now = std::time(nullptr); std::time_t now = std::time(nullptr);
std::tm* nowTm = std::localtime(&now); std::tm* nowTm = std::localtime(&now);
@ -34,7 +36,7 @@ bool Person::isSignOnToday() const {
return lastSignOnTime >= today; return lastSignOnTime >= today;
} }
void Person::signOn() { void PERSON_LIB Person::signOn() {
if (isSignOnToday()) { if (isSignOnToday()) {
return; return;
} }
@ -56,7 +58,7 @@ void Person::signOn() {
copperCoin += static_cast<int>(dis(gen)); copperCoin += static_cast<int>(dis(gen));
} }
int Person::continuousSignOnDays() const { int PERSON_LIB Person::continuousSignOnDays() const {
// Check last sign-on date // Check last sign-on date
// If it's not yesterday, return 0 // If it's not yesterday, return 0
// If it is yesterday or today, return signOnDays // If it is yesterday or today, return signOnDays
@ -83,36 +85,34 @@ int Person::continuousSignOnDays() const {
} }
} }
bool Person::willBeAttackNextTime() const { bool PERSON_LIB Person::willBeAttackNextTime() const {
static std::default_random_engine e(std::time(nullptr)); static std::default_random_engine e(std::time(nullptr));
std::uniform_int_distribution<unsigned> u(0, 10000); std::uniform_int_distribution<unsigned> u(0, 10000);
// u(e) divide 100 is the probability // u(e) divide 100 is the probability
switch (speakCount) { if (speakCount >= 0 && speakCount <= 73)
// Case 0-73: the probability is 0.60% // Case 0-73: the probability is 0.60%
case 0 ... 73: return u(e) < 60;
return u(e) < 60; else if (speakCount >= 74 && speakCount <= 90)
// Case 74-90: // Case 74-90:
// The base probability is 6.60% // The base probability is 6.60%
// Increase by 6% for each 1 times // Increase by 6% for each 1 times
// Max probability is 100% // Max probability is 100%
case 74 ... 90: return u(e) < 660 + 60 * (speakCount - 74);
return u(e) < 660 + 60 * (speakCount - 74); else
// If speakCount > 90, the probability is 100% // If speakCount > 90, the probability is 100%
default: return true;
return true;
}
} }
void Person::resetAttackCount() { void PERSON_LIB Person::resetAttackCount() {
speakCount = 0; speakCount = 0;
} }
void Person::increaseSpeakCount() { void PERSON_LIB Person::increaseSpeakCount() {
speakCount++; speakCount++;
} }
// Serialize to JSON // Serialize to JSON
nlohmann::json Person::serializeToJSON() { nlohmann::json PERSON_LIB Person::serializeToJSON() {
nlohmann::json j; nlohmann::json j;
j["qqID"] = qqID; j["qqID"] = qqID;
j["lastSignOnTime"] = lastSignOnTime; j["lastSignOnTime"] = lastSignOnTime;
@ -123,20 +123,32 @@ nlohmann::json Person::serializeToJSON() {
} }
// Deserialize from JSON // Deserialize from JSON
Person::Person(const nlohmann::json &json) : PERSON_LIB Person::Person(const nlohmann::json &json) :
qqID(json["qqID"]), qqID(json["qqID"]),
lastSignOnTime(json["lastSignOnTime"]), lastSignOnTime(json["lastSignOnTime"]),
signOnDays(json["signOnDays"]), signOnDays(json["signOnDays"]),
speakCount(json["speakCount"]), speakCount(json["speakCount"]),
copperCoin(json["copperCoin"]) {} copperCoin(json["copperCoin"]) {}
int Person::getCopperCoin() const { PERSON_LIB Person::~Person() = default;
PERSON_LIB Person::Person(const Person&) = default;
PERSON_LIB Person& Person::operator=(const Person&) = default;
PERSON_LIB Person::Person(Person&&) = default;
PERSON_LIB Person& Person::operator=(Person&&) = default;
int PERSON_LIB Person::getCopperCoin() const {
return copperCoin; return copperCoin;
} }
MiraiCP::QQID Person::getQQID() const { MiraiCP::QQID PERSON_LIB Person::getQQID() const {
return qqID; return qqID;
} }
Person::Person(MiraiCP::QQID id) : PERSON_LIB Person::Person() = default;
PERSON_LIB Person::Person(MiraiCP::QQID id) :
qqID(id), lastSignOnTime(0), signOnDays(0), speakCount(0), copperCoin(0) {} qqID(id), lastSignOnTime(0), signOnDays(0), speakCount(0), copperCoin(0) {}

View File

@ -18,12 +18,21 @@
#ifndef PERSON_DEMO_PERSON_H #ifndef PERSON_DEMO_PERSON_H
#define PERSON_DEMO_PERSON_H #define PERSON_DEMO_PERSON_H
#ifdef _MSC_VER
#ifdef EXPORT_PERSON
#define PERSON_LIB __declspec(dllexport)
#else
#define PERSON_LIB __declspec(dllimport)
#endif
#endif
#include <ctime> #include <ctime>
#include "json.hpp" #include "json.hpp"
#ifndef MIRAICP_PRO_PLUGINCONFIG_H
namespace MiraiCP { namespace MiraiCP {
using QQID = unsigned long long; using QQID = unsigned long long;
} }
#endif
class Person { class Person {
private: private:
@ -39,29 +48,29 @@ private:
public: public:
// Generate ctors and dtors // Generate ctors and dtors
// assignment operators // assignment operators
Person() = default; PERSON_LIB Person();
explicit Person(MiraiCP::QQID id); explicit PERSON_LIB Person(MiraiCP::QQID id);
// Deserialize from JSON // Deserialize from JSON
explicit Person(const nlohmann::json&); explicit PERSON_LIB Person(const nlohmann::json&);
~Person() = default; PERSON_LIB ~Person();
Person(const Person&) = default; PERSON_LIB Person(const Person&);
Person& operator=(const Person&) = default; PERSON_LIB Person& operator=(const Person&);
Person(Person&&) = default; PERSON_LIB Person(Person&&);
Person& operator=(Person&&) = default; PERSON_LIB Person& operator=(Person&&);
[[nodiscard]] bool isSignOnToday() const; [[nodiscard]] bool PERSON_LIB isSignOnToday() const;
void signOn(); void PERSON_LIB signOn();
[[nodiscard]] int continuousSignOnDays() const; [[nodiscard]] int PERSON_LIB continuousSignOnDays() const;
[[nodiscard]] bool willBeAttackNextTime() const; [[nodiscard]] bool PERSON_LIB willBeAttackNextTime() const;
void resetAttackCount(); void PERSON_LIB resetAttackCount();
void increaseSpeakCount(); void PERSON_LIB increaseSpeakCount();
[[nodiscard]] int getCopperCoin() const; [[nodiscard]] int PERSON_LIB getCopperCoin() const;
nlohmann::json serializeToJSON(); nlohmann::json PERSON_LIB serializeToJSON();
[[nodiscard]] MiraiCP::QQID getQQID() const; [[nodiscard]] MiraiCP::QQID PERSON_LIB getQQID() const;
}; };

View File

@ -14,6 +14,9 @@
You should have received a copy of the GNU Affero General Public License along You should have received a copy of the GNU Affero General Public License along
with Lucas' Bot. If not, see <https://www.gnu.org/licenses/>. with Lucas' Bot. If not, see <https://www.gnu.org/licenses/>.
*/ */
#define EXPORT_PERSON
#include "PersonCollection.h" #include "PersonCollection.h"
#include <fstream> #include <fstream>
#include <filesystem> #include <filesystem>
@ -36,7 +39,7 @@ namespace PersonCollection {
// Refactoring: // Refactoring:
// loadPersonMap and savePersonMap are now using // loadPersonMap and savePersonMap are now using
// globally defined localFilePath // globally defined localFilePath
void loadPersonMap() { PERSON_LIB void loadPersonMap() {
// If localFilePath is not defined // If localFilePath is not defined
if (initialized || localFilePath.empty() || !std::filesystem::exists(localFilePath)) { if (initialized || localFilePath.empty() || !std::filesystem::exists(localFilePath)) {
return; return;
@ -56,7 +59,7 @@ namespace PersonCollection {
initialized = true; initialized = true;
} }
void savePersonMap() { PERSON_LIB void savePersonMap() {
// If localFilePath is not defined // If localFilePath is not defined
if (localFilePath.empty()) { if (localFilePath.empty()) {
// Throw an exception // Throw an exception
@ -77,19 +80,19 @@ namespace PersonCollection {
ofs << j.dump(4); ofs << j.dump(4);
} }
void addPerson(MiraiCP::QQID id) { PERSON_LIB void addPerson(MiraiCP::QQID id) {
// Lock the mutex // Lock the mutex
std::lock_guard<std::mutex> lock(personMapMutex); std::lock_guard<std::mutex> lock(personMapMutex);
personMap[id] = Person(id); personMap[id] = Person(id);
} }
Person& getPerson(MiraiCP::QQID id) { PERSON_LIB Person& getPerson(MiraiCP::QQID id) {
if (personMap.find(id) == personMap.end()) if (personMap.find(id) == personMap.end())
personMap.insert({id, Person(id)}); personMap.insert({id, Person(id)});
return personMap[id]; return personMap[id];
} }
void removePerson(MiraiCP::QQID id) { PERSON_LIB void removePerson(MiraiCP::QQID id) {
// Lock the mutex // Lock the mutex
std::lock_guard<std::mutex> lock(personMapMutex); std::lock_guard<std::mutex> lock(personMapMutex);
personMap.erase(id); personMap.erase(id);
@ -107,7 +110,7 @@ namespace PersonCollection {
} }
} }
void registerLocalFile(const std::string &path) { PERSON_LIB void registerLocalFile(const std::string &path) {
localFilePath = path; localFilePath = path;
} }
} }

View File

@ -18,18 +18,25 @@
#ifndef PERSON_DEMO_PERSONCOLLECTION_H #ifndef PERSON_DEMO_PERSONCOLLECTION_H
#define PERSON_DEMO_PERSONCOLLECTION_H #define PERSON_DEMO_PERSONCOLLECTION_H
#ifdef _MSC_VER
#ifdef EXPORT_PERSON
#define PERSON_LIB __declspec(dllexport)
#else
#define PERSON_LIB __declspec(dllimport)
#endif
#endif
#include <unordered_map> #include <unordered_map>
#include "Person.h" #include "Person.h"
#include <string> #include <string>
namespace PersonCollection { namespace PersonCollection {
void loadPersonMap(); PERSON_LIB void loadPersonMap();
void savePersonMap(); PERSON_LIB void savePersonMap();
void addPerson(MiraiCP::QQID id); PERSON_LIB void addPerson(MiraiCP::QQID id);
Person& getPerson(MiraiCP::QQID id); PERSON_LIB Person& getPerson(MiraiCP::QQID id);
void removePerson(MiraiCP::QQID id); PERSON_LIB void removePerson(MiraiCP::QQID id);
void registerLocalFile(const std::string&); PERSON_LIB void registerLocalFile(const std::string&);
} }

View File

@ -17,6 +17,22 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef MIRAICP_PRO_PLUGINCONFIG_H #ifndef MIRAICP_PRO_PLUGINCONFIG_H
#define MIRAICP_PRO_PLUGINCONFIG_H #define MIRAICP_PRO_PLUGINCONFIG_H
#if defined(_WIN32)
#include <windows.h>
#if defined(_MSC_VER)
#pragma warning(disable: 4474)
#pragma warning(disable: 4615)
#pragma warning(disable: 4819)
#pragma warning(disable: 6031)
#endif
#elif defined(__linux__) || defined(unix) || defined(__unix__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#include <dlfcn.h>
#include <unistd.h>
#elif defined(__APPLE__) || defined(__MACH__)
#include <mach-o/dyld.h>
#endif
#include <json.hpp> #include <json.hpp>
namespace MiraiCP { namespace MiraiCP {
const std::string MiraiCPVersion = "v2.12.0-RC2"; const std::string MiraiCPVersion = "v2.12.0-RC2";

View File

@ -21,7 +21,6 @@
#include <http.h> #include <http.h>
#include <regex> #include <regex>
#include <RandomLibrary.h> #include <RandomLibrary.h>
#include <windef.h>
/** /**
* @brief * @brief