the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 92 lines 2.5 kB view raw
1#include "stdafx.h" 2#include "ByteBuffer.h" 3#include "File.h" 4#include "ZoneFile.h" 5 6 7const int ZoneFile::slotsLength = ZonedChunkStorage::CHUNKS_PER_ZONE * ZonedChunkStorage::CHUNKS_PER_ZONE; 8 9ZoneFile::ZoneFile(__int64 key, File file, File entityFile) : slots(slotsLength) 10{ 11 lastUse = 0; 12 13 this->key = key; 14 this->file = file; 15 16 // 4J - try/catch removed 17// try { 18 this->entityFile = new NbtSlotFile(entityFile); 19// } catch (Exception e) { 20// System.out.println("Broken entity file: " + entityFile + " (" + e.toString() + "), replacing.."); 21// entityFile.delete(); 22// entityFile.createNewFile(); 23// this.entityFile = new NbtSlotFile(entityFile); 24// } 25 26 channel = CreateFile(wstringtofilename(file.getPath()), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 27 // 4J - try/catch removed 28// try { 29 readHeader(); 30// } catch (Exception e) { 31// e.printStackTrace(); 32// throw new IOException("Broken zone file: " + file + ": " + e); 33// } 34} 35 36ZoneFile::~ZoneFile() 37{ 38 delete [] slots.data; 39} 40 41void ZoneFile::readHeader() 42{ 43 ZoneIo *zoneIo = new ZoneIo(channel, 0); 44 ByteBuffer *bb = zoneIo->read(FILE_HEADER_SIZE); 45 bb->flip(); 46 if (bb->remaining() < 5) return; 47 int magic = bb->getInt(); 48// if (magic != MAGIC_NUMBER) throw new IOException("Bad magic number: " + magic); // 4J - TODO 49 short version = bb->getShort(); 50// if (version != 0) throw new IOException("Bad version number: " + version); // 4J - TODO 51 52 slotCount = bb->getShort(); 53 bb->getShortArray(slots); 54 bb->position(bb->position() + slotsLength * 2); 55} 56 57void ZoneFile::writeHeader() 58{ 59 ZoneIo *zoneIo = new ZoneIo(channel, 0); 60 61 ByteBuffer *bb = ByteBuffer::allocate(FILE_HEADER_SIZE); 62 bb->order(ZonedChunkStorage::BYTEORDER); 63 bb->putInt(MAGIC_NUMBER); 64 bb->putShort((short) 0); 65 bb->putShort((short) slotCount); 66 bb->putShortArray(slots); 67 bb->position(bb->position() + slots.length * 2); 68 bb->flip(); 69 zoneIo->write(bb, FILE_HEADER_SIZE); 70} 71 72void ZoneFile::close() 73{ 74 CloseHandle(channel); 75 entityFile->close(); 76} 77 78ZoneIo *ZoneFile::getZoneIo(int slot) 79{ 80 if (slots[slot] == 0) 81 { 82 slots[slot] = ++slotCount; 83 writeHeader(); 84 } 85 int byteOffs = (slots[slot] - 1) * ZonedChunkStorage::CHUNK_SIZE_BYTES + FILE_HEADER_SIZE; 86 return new ZoneIo(channel, byteOffs); 87} 88 89bool ZoneFile::containsSlot(int slot) 90{ 91 return slots[slot] > 0; 92}