Skip to content

Commit

Permalink
Fixed long, double and float data types in windows systems
Browse files Browse the repository at this point in the history
  • Loading branch information
Howaner committed Mar 25, 2018
1 parent c8b076a commit 97f85ae
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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} )
Expand Down
28 changes: 22 additions & 6 deletions src/File/ByteBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NBT/NBTHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace NBT {
return GetTag<NBTTagIntArray>(NbtIntArray)->GetData(entry);
}

void NBTHelper::SetDouble(NBTEntry& entry, double value) {
void NBTHelper::SetDouble(NBTEntry& entry, jdouble value) {
GetTag<NBTTagDouble>(NbtDouble)->SetData(entry, value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NBT/NBTHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/NBT/NBTTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 97f85ae

Please sign in to comment.