Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
1.9.0_preview_2,使更多函数对接LL,修复nbt错误
Browse files Browse the repository at this point in the history
  • Loading branch information
twoone3l committed Jan 2, 2022
1 parent 30fc7f9 commit 74d24fd
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 223 deletions.
4 changes: 2 additions & 2 deletions mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static Json AccessUrlForJson(const wchar_t* url) {
} while (size);
InternetCloseHandle(handle2);
InternetCloseHandle(hSession);
return StringToJson(data);
return ToJson(data);
}
//访问url
static void AccessUrlForFile(const wchar_t* url, string_view filename) {
Expand Down Expand Up @@ -251,7 +251,7 @@ THook(int, "main",
int argc, char* argv[], char* envp[]) {
#if 0
while (true) {
CompoundTag* t = ToCompoundTag(StringToJson(R"(
CompoundTag* t = ToCompoundTag(ToJson(R"(
{
"Block10": {
"name8": "minecraft:crafting_table",
Expand Down
14 changes: 7 additions & 7 deletions mod/CPython.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once
#define PY_SSIZE_T_CLEAN
#include "../include/Python.h"
#include <vector>
Expand All @@ -10,7 +10,7 @@
#define Py_PARSE_WITH_KERWORDS(format,...) if (!PyArg_ParseTupleAndKeywords(args, kwds, format ":" __FUNCTION__, const_cast<char**>(kwlist), __VA_ARGS__))return nullptr

#define Py_RETURN_ERROR(str) return PyErr_SetString(PyExc_Exception, str), nullptr
#define Py_PRINT_REFCOUNT(obj) cout << "引用计数:" << obj->ob_refcnt << endl
#define Py_PRINT_REFCOUNT(obj) cout << "引用计数:" << obj->ob_refcnt << endl
//#define Py_BEGIN_CALL\
// int _has_gil = PyGILState_Check();\
// PyGILState_STATE _gil_state = PyGILState_LOCKED;\
Expand All @@ -22,7 +22,7 @@
// Py_END_ALLOW_THREADS;\
// if (!_has_gil)PyGILState_Release(_gil_state)

//字符串转Unicode
//字符串转Unicode
inline PyObject* ToPyStr(std::string_view str) {
return PyUnicode_InternFromString(str.data());
//return PyUnicode_FromStringAndSize(str.data(), str.length());
Expand All @@ -36,31 +36,31 @@ inline std::vector<std::string> ToStrArray(PyObject* list) {
}
return arr;
}
//Vec3转list
//Vec3转list
inline PyObject* ToList(Vec3 vec) {
PyObject* list = PyList_New(3);
PyList_SetItem(list, 0, PyFloat_FromDouble(vec.x));
PyList_SetItem(list, 1, PyFloat_FromDouble(vec.y));
PyList_SetItem(list, 2, PyFloat_FromDouble(vec.z));
return list;
}
//Vec3转list
//Vec3转list
inline PyObject* ToList(Vec3* vec) {
PyObject* list = PyList_New(3);
PyList_SetItem(list, 0, PyFloat_FromDouble(vec->x));
PyList_SetItem(list, 1, PyFloat_FromDouble(vec->y));
PyList_SetItem(list, 2, PyFloat_FromDouble(vec->z));
return list;
}
//方块坐标转list
//方块坐标转list
inline PyObject* ToList(BlockPos* bp) {
PyObject* list = PyList_New(3);
PyList_SetItem(list, 0, PyLong_FromLong(bp->x));
PyList_SetItem(list, 1, PyLong_FromLong(bp->y));
PyList_SetItem(list, 2, PyLong_FromLong(bp->z));
return list;
}
//打印错误信息
//打印错误信息
inline void PrintPythonError() {
if (PyErr_Occurred()) {
PyErr_Print();
Expand Down
8 changes: 4 additions & 4 deletions mod/DataIO.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "DataIO.h"
#include "DataIO.h"
#include "Tool.h"
#include "NBT.h"

void serialize<CompoundTag>::write(CompoundTag* item, BinaryStream* stream) {
void serialize<CompoundTag>::write(const std::unique_ptr<CompoundTag>& item, BinaryStream* stream) {
return SymCall("?write@?$serialize@VCompoundTag@@@@SAXAEBVCompoundTag@@AEAVBinaryStream@@@Z",
item, stream);
item.get(), stream);
}

std::unique_ptr<CompoundTag> serialize<CompoundTag>::read(BinaryStream* stream) {
std::unique_ptr<CompoundTag> serialize<CompoundTag>::read(ReadOnlyBinaryStream* stream) {
auto tag = CompoundTag::create();
SymCall("?read@?$serialize@VCompoundTag@@@@SA?AVCompoundTag@@AEAVReadOnlyBinaryStream@@@Z",
tag.get(), stream);
Expand Down
4 changes: 2 additions & 2 deletions mod/DataIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BinaryStream : public ReadOnlyBinaryStream {
template <typename T>
class serialize {
public:
static void write(T* val, BinaryStream* stream);
static std::unique_ptr<T> read(BinaryStream* stream);
static void write(const std::unique_ptr<T>& val, BinaryStream* stream);
static std::unique_ptr<T> read(ReadOnlyBinaryStream* stream);
};

110 changes: 60 additions & 50 deletions mod/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ struct PyEntity {
Py_GET_ACTOR;
unique_ptr<CompoundTag> t = CompoundTag::create();
a->save(*t);
return ToPyStr(ToJson(move(t)));
return ToPyStr(ToJson(*t).dump(4));
}
//获取生命值
static PyObject* getHealth(PyObject* self, void*) {
Expand Down Expand Up @@ -204,81 +204,71 @@ struct PyEntity {
//获取玩家所有物品
static PyObject* getAllItem(PyObject* self, PyObject*) {
Py_GET_PLAYER;
fifo_json value;

fifo_json& inventory = value["Inventory"];
fifo_json items_json = fifo_json::object();
fifo_json& inventory = items_json["Inventory"];
for (auto& i : p->getInventory().getSlots()) {
inventory.push_back(ToJson(i->save()));
inventory.push_back(ToJson(*i->save()));
}

fifo_json& endchest = value["EndChest"];
fifo_json& endchest = items_json["EndChest"];
for (auto& i : p->getEnderChestContainer()->getSlots()) {
endchest.push_back(ToJson(i->save()));
endchest.push_back(ToJson(*i->save()));
}

fifo_json& armor = value["Armor"];
fifo_json& armor = items_json["Armor"];
for (auto& i : p->getArmorContainer().getSlots()) {
armor.push_back(ToJson(i->save()));
armor.push_back(ToJson(*i->save()));
}

value["OffHand"] = ToJson(p->getOffhandSlot().save());
value["Hand"] = ToJson(p->getSelectedItem().save());

return ToPyStr(value.dump(4));
items_json["OffHand"] = ToJson(*p->getOffhandSlot().save());
items_json["Hand"] = ToJson(*p->getSelectedItem().save());
return ToPyStr(items_json.dump(4));
}
//设置玩家所有物品
static PyObject* setAllItem(PyObject* self, PyObject* args) {
const char* x = "";
Py_PARSE("s", &x);
const char* items_data = "";
Py_PARSE("s", &items_data);
Py_GET_PLAYER;
fifo_json value(StringToJson(x));

if (value.contains("Inventory")) {
fifo_json items_json(ToJson(items_data));
if (items_json.contains("Inventory")) {
auto& items = p->getInventory();
fifo_json& inventory = value["Inventory"];
fifo_json& inventory = items_json["Inventory"];
for (unsigned i = 0; i < inventory.size(); i++) {
*items.getSlot(i) = LoadItemFromJson(inventory[i]);
}
}

if (value.contains("EndChest")) {
if (items_json.contains("EndChest")) {
auto items = p->getEnderChestContainer();
fifo_json& endchest = value["EndChest"];
fifo_json& endchest = items_json["EndChest"];
for (unsigned i = 0; i < endchest.size(); i++) {
*items->getSlot(i) = LoadItemFromJson(endchest[i]);
}
}

if (value.contains("Armor")) {
if (items_json.contains("Armor")) {
auto& items = p->getArmorContainer();
fifo_json& armor = value["Armor"];
fifo_json& armor = items_json["Armor"];
for (unsigned i = 0; i < armor.size(); i++) {
*items.getSlot(i) = LoadItemFromJson(armor[i]);
}
}

if (value.contains("OffHand")) {
p->setOffhandSlot(LoadItemFromJson(value["OffHand"]));
if (items_json.contains("OffHand")) {
p->setOffhandSlot(LoadItemFromJson(items_json["OffHand"]));
}
p->sendInventory(true);

Py_RETURN_NONE;
}
//设置玩家手上物品
static PyObject* setHand(PyObject* self, PyObject* args) {
const char* x = "";
Py_PARSE("s", &x);
const char* item_data = "";
Py_PARSE("s", &item_data);
Py_GET_PLAYER;
const_cast<ItemStack&>(p->getSelectedItem()) = LoadItemFromString(x);
const_cast<ItemStack&>(p->getSelectedItem()) = LoadItemFromString(item_data);
p->sendInventory(true);
Py_RETURN_NONE;
}
//增加玩家背包物品
static PyObject* addItem(PyObject* self, PyObject* args) {
const char* x = "";
Py_PARSE("s", &x);
const char* item_data = "";
Py_PARSE("s", &item_data);
Py_GET_PLAYER;
auto item = LoadItemFromString(x);
auto item = LoadItemFromString(item_data);
p->giveItem(&item);
p->sendInventory(true);
Py_RETURN_NONE;
Expand All @@ -294,7 +284,8 @@ struct PyEntity {
}
//传送
static PyObject* teleport(PyObject* self, PyObject* args) {
Vec3 pos; int did;
Vec3 pos;
int did;
Py_PARSE("fffi", &pos.x, &pos.y, &pos.z, &did);
Py_GET_PLAYER;
p->teleport(pos, did);
Expand Down Expand Up @@ -330,19 +321,38 @@ struct PyEntity {
p->kick(msg);
Py_RETURN_NONE;
}
//计分板操作
//获取玩家分数
static PyObject* getScore(PyObject* self, PyObject* args) {
const char* objname = "";
Py_PARSE("s", &objname);
Py_GET_PLAYER;
return PyLong_FromLong(p->getScore(objname));
}
//todo add reduce set
static PyObject* modifyScore(PyObject* self, PyObject* args) {
//设置玩家分数
static PyObject* setScore(PyObject* self, PyObject* args) {
const char* objname = "";
int count; PlayerScoreSetFunction mode;
Py_PARSE("sii", &objname, &count, &mode);
int count;
Py_PARSE("si", &objname, &count);
Py_GET_PLAYER;
p->setScore(objname, count);
Py_RETURN_NONE;
}
//增加玩家分数
static PyObject* addScore(PyObject* self, PyObject* args) {
const char* objname = "";
int count;
Py_PARSE("si", &objname, &count);
Py_GET_PLAYER;
p->addScore(objname, count);
Py_RETURN_NONE;
}
//减少玩家分数
static PyObject* reduceScore(PyObject* self, PyObject* args) {
const char* objname = "";
int count;
Py_PARSE("si", &objname, &count);
Py_GET_PLAYER;
p->reduceScore(objname, count);
Py_RETURN_NONE;
}
//增加等级
Expand Down Expand Up @@ -425,7 +435,7 @@ struct PyEntity {
Py_PARSE("ss|i", &title, &side_data, &order);
Py_GET_PLAYER;
vector<pair<string, int>> data;
fifo_json value = StringToJson(side_data);
fifo_json value = ToJson(side_data);
if (value.is_object())
for (auto& [key, val] : value.items()) {
data.push_back({ key, val });
Expand Down Expand Up @@ -529,7 +539,9 @@ struct PyEntity {
{ "resendAllChunks", resendAllChunks, METH_NOARGS, nullptr },
{ "disconnect", disconnect, METH_VARARGS, nullptr },
{ "getScore", getScore, METH_VARARGS, nullptr },
{ "modifyScore", modifyScore, METH_VARARGS, nullptr },
{ "setScore", setScore, METH_VARARGS, nullptr },
{ "addScore", addScore, METH_VARARGS, nullptr },
{ "reduceScore", reduceScore, METH_VARARGS, nullptr },
{ "addLevel", addLevel, METH_VARARGS, nullptr },
{ "transferServer", transferServer, METH_VARARGS, nullptr },
{ "sendCustomForm", sendCustomForm, METH_VARARGS, nullptr },
Expand Down Expand Up @@ -597,11 +609,9 @@ PyTypeObject PyEntity_Type{
0, /* tp_version_tag */
nullptr, /* tp_finalize */
};

PyObject* ToEntity(Actor* ptr) {
PyEntity* obj = nullptr;
//Py_BEGIN_CALL;
obj = PyObject_New(PyEntity, &PyEntity_Type);
//Py_END_CALL;
PyEntity* obj = PyObject_New(PyEntity, &PyEntity_Type);
obj->actor = ptr;
return reinterpret_cast<PyObject*>(obj);
}
Loading

0 comments on commit 74d24fd

Please sign in to comment.