From 97f85ae398077c41e94eb9682ca6558e5f6d899e Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 25 Mar 2018 15:01:46 +0200 Subject: [PATCH] Fixed long, double and float data types in windows systems --- CMakeLists.txt | 7 ++++--- src/File/ByteBuffer.cpp | 28 ++++++++++++++++++++++------ src/NBT/NBTHelper.cpp | 2 +- src/NBT/NBTHelper.h | 2 +- src/NBT/NBTTag.h | 8 ++++---- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dd78388..74af71b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.1.0) -project(nbteditor VERSION 1.2) -set (NBTEDITOR_VERSION 1.2) +project(nbteditor VERSION 1.3) +set (NBTEDITOR_VERSION 1.3) find_package(Qt5Widgets) @@ -22,9 +22,10 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h.in" "${CMAKE_CURRENT_B set(nbteditor_res "${CMAKE_CURRENT_SOURCE_DIR}/resources/resources.qrc") # For Windows release build: set(nbteditor_res "${CMAKE_CURRENT_SOURCE_DIR}/resources/resources.qrc" "${CMAKE_CURRENT_SOURCE_DIR}/resources/nbteditor.rc" "${CMAKE_CURRENT_SOURCE_DIR}/resources/qt_de.qm") -file(GLOB_RECURSE nbteditor_src "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/UI/*.ui") +file(GLOB_RECURSE nbteditor_src "${CMAKE_CURRENT_BINARY_DIR}/src/Version.h" "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/UI/*.ui") include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src") +include_directories("${CMAKE_CURRENT_BINARY_DIR}/src") include_directories("${CMAKE_CURRENT_SOURCE_DIR}/window") include_directories("${CMAKE_CURRENT_SOURCE_DIR}/resources") include_directories( ${CMAKE_BINARY_DIR} ) diff --git a/src/File/ByteBuffer.cpp b/src/File/ByteBuffer.cpp index 193b8c6..2a16ea3 100644 --- a/src/File/ByteBuffer.cpp +++ b/src/File/ByteBuffer.cpp @@ -56,18 +56,34 @@ namespace File { } jdouble ByteBuffer::ReadDouble() { - jlong doubleAsLong = ReadLong(); - jdouble number = 0; + Byte* bytes = ReadBytes(8); + uint64_t longNumber = + uint64_t(bytes[7]) + | (uint64_t(bytes[6]) << 8) + | (uint64_t(bytes[5]) << 16) + | (uint64_t(bytes[4]) << 24) + | (uint64_t(bytes[3]) << 32) + | (uint64_t(bytes[2]) << 40) + | (uint64_t(bytes[1]) << 48) + | (uint64_t(bytes[0]) << 56); + delete[] bytes; - memcpy(&number, &doubleAsLong, 8); + jdouble number = 0; + memcpy(&number, &longNumber, 8); return number; } jfloat ByteBuffer::ReadFloat() { - jint floatAsInt = ReadInt(); - jfloat number = 0; + Byte* bytes = ReadBytes(4); + uint32_t intNumber = + uint32_t(bytes[3]) + | (uint32_t(bytes[2]) << 8) + | (uint32_t(bytes[1]) << 16) + | (uint32_t(bytes[0]) << 24); + delete[] bytes; - memcpy(&number, &floatAsInt, 4); + jfloat number = 0; + memcpy(&number, &intNumber, 4); return number; } diff --git a/src/NBT/NBTHelper.cpp b/src/NBT/NBTHelper.cpp index d9599b8..d03dd49 100644 --- a/src/NBT/NBTHelper.cpp +++ b/src/NBT/NBTHelper.cpp @@ -77,7 +77,7 @@ namespace NBT { return GetTag(NbtIntArray)->GetData(entry); } - void NBTHelper::SetDouble(NBTEntry& entry, double value) { + void NBTHelper::SetDouble(NBTEntry& entry, jdouble value) { GetTag(NbtDouble)->SetData(entry, value); } diff --git a/src/NBT/NBTHelper.h b/src/NBT/NBTHelper.h index 14d4508..042c40f 100644 --- a/src/NBT/NBTHelper.h +++ b/src/NBT/NBTHelper.h @@ -24,7 +24,7 @@ namespace NBT { static NBTList& GetList(NBTEntry& entry); static NBTCompound* GetCompound(NBTEntry& entry); - static void SetDouble(NBTEntry& entry, double value); + static void SetDouble(NBTEntry& entry, jdouble value); static void SetString(NBTEntry& entry, QString value); private: diff --git a/src/NBT/NBTTag.h b/src/NBT/NBTTag.h index 54a9789..381bae3 100644 --- a/src/NBT/NBTTag.h +++ b/src/NBT/NBTTag.h @@ -197,7 +197,7 @@ namespace NBT { NBTTagLong() : NBTTagBasic("nbt-long.png", "Long") {} void* Read(ByteBuffer* buffer) const override { - long* data = new long; + jlong* data = new jlong; *data = buffer->ReadLong(); return data; } @@ -219,7 +219,7 @@ namespace NBT { return false; bool success = false; - jlong number = value.toString().toLong(&success, 10); + jlong number = (sizeof(long) == 8) ? value.toString().toLong(&success, 10) : value.toString().toLongLong(&success, 10); if (!success) return false; @@ -233,7 +233,7 @@ namespace NBT { NBTTagFloat() : NBTTagBasic("nbt-float.png", "Float") {} void* Read(ByteBuffer* buffer) const override { - float* data = new float; + jfloat* data = new jfloat; *data = buffer->ReadFloat(); return data; } @@ -291,7 +291,7 @@ namespace NBT { return false; bool success = false; - double number = value.toString().toDouble(&success); + jdouble number = (jdouble) value.toString().toDouble(&success); if (!success) return false;