|
|
|
@ -1,131 +0,0 @@
|
|
|
|
|
/*
|
|
|
|
|
Copyright 2024, FOSS-VG Developers and Contributers
|
|
|
|
|
|
|
|
|
|
Author(s):
|
|
|
|
|
Jocadbz
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
|
|
|
under the terms of the GNU Affero General Public License as published
|
|
|
|
|
by the Free Software Foundation, version 3.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied
|
|
|
|
|
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
See the GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
version 3 along with this program.
|
|
|
|
|
If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Ok, before you read this, please understand.
|
|
|
|
|
* This is not a elaborate program. We are not
|
|
|
|
|
* using fancy parsers.
|
|
|
|
|
* I'm straight up rawdogging the .mca files
|
|
|
|
|
* here. Of course this should be fixed, but
|
|
|
|
|
* don't get mad.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Define constants related to .mca file format
|
|
|
|
|
const int SECTOR_SIZE = 4096;
|
|
|
|
|
const int CHUNK_OFFSET = 4; // Offset where chunk data starts within a sector
|
|
|
|
|
const int CHUNK_SIZE = 4096; // Size of a chunk data block
|
|
|
|
|
const int HEADER_SIZE = 4;
|
|
|
|
|
const int TIMESTAMP_SIZE = 4;
|
|
|
|
|
const int NUM_CHUNKS_PER_REGION = 1024; // 32x32 chunks per region file
|
|
|
|
|
|
|
|
|
|
// Function to extract chunks from a .mca file
|
|
|
|
|
// Function to extract chunks from a .mca file and print position, offset, and timestamp
|
|
|
|
|
void extractChunks(const string& mcaFilename) {
|
|
|
|
|
ifstream file(mcaFilename, ios::binary);
|
|
|
|
|
if (!file.is_open()) {
|
|
|
|
|
cerr << "Error opening file: " << mcaFilename << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate the number of sectors in the file
|
|
|
|
|
file.seekg(0, ios::end);
|
|
|
|
|
int fileSize = file.tellg();
|
|
|
|
|
int numSectors = fileSize / SECTOR_SIZE;
|
|
|
|
|
|
|
|
|
|
// Print header information
|
|
|
|
|
cout << "Position (x, z), Offset, Timestamp:" << endl;
|
|
|
|
|
|
|
|
|
|
// Iterate through each sector
|
|
|
|
|
for (int sector = 0; sector < numSectors; ++sector) {
|
|
|
|
|
// Read the sector header (4 bytes)
|
|
|
|
|
file.seekg(sector * SECTOR_SIZE);
|
|
|
|
|
char header[HEADER_SIZE];
|
|
|
|
|
file.read(header, HEADER_SIZE);
|
|
|
|
|
|
|
|
|
|
// Extract position (x, z) from the sector header
|
|
|
|
|
int x = sector % NUM_CHUNKS_PER_REGION;
|
|
|
|
|
int z = sector / NUM_CHUNKS_PER_REGION;
|
|
|
|
|
|
|
|
|
|
// Calculate offset and timestamp positions
|
|
|
|
|
int offset = sector * SECTOR_SIZE + HEADER_SIZE;
|
|
|
|
|
int timestampOffset = sector * SECTOR_SIZE + HEADER_SIZE + CHUNK_SIZE;
|
|
|
|
|
|
|
|
|
|
// Read the timestamp (4 bytes) from the sector
|
|
|
|
|
file.seekg(timestampOffset);
|
|
|
|
|
char timestampBytes[TIMESTAMP_SIZE];
|
|
|
|
|
file.read(timestampBytes, TIMESTAMP_SIZE);
|
|
|
|
|
int timestamp = *reinterpret_cast<int*>(timestampBytes);
|
|
|
|
|
|
|
|
|
|
// Print formatted information
|
|
|
|
|
cout << "(" << x << ", " << z << "), " << offset << ", " << timestamp << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Function to add or replace a chunk in a .mca file
|
|
|
|
|
void addReplaceChunk(const string& mcaFilename, int sectorIndex, const vector<uint8_t>& newChunkData) {
|
|
|
|
|
fstream file(mcaFilename, ios::in | ios::out | ios::binary);
|
|
|
|
|
if (!file.is_open()) {
|
|
|
|
|
cerr << "Error opening file: " << mcaFilename << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate the offset in the file where the chunk should be written
|
|
|
|
|
int fileOffset = sectorIndex * SECTOR_SIZE + CHUNK_OFFSET;
|
|
|
|
|
|
|
|
|
|
// Seek to the appropriate position in the file
|
|
|
|
|
file.seekp(fileOffset);
|
|
|
|
|
|
|
|
|
|
// Write the new chunk data
|
|
|
|
|
file.write(reinterpret_cast<const char*>(newChunkData.data()), newChunkData.size());
|
|
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
|
|
cout << "Chunk added/replaced successfully at sector " << sectorIndex << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
// TODO: Remember to implement CLI here.
|
|
|
|
|
string mcaFilename = "r.0.0.mca"; // Example .mca file path
|
|
|
|
|
|
|
|
|
|
// Example: Extract chunks from the .mca file
|
|
|
|
|
extractChunks(mcaFilename);
|
|
|
|
|
|
|
|
|
|
// Example: Add or replace a chunk in the .mca file
|
|
|
|
|
int sectorIndex = 0; // Example: Replace the chunk in the first sector
|
|
|
|
|
vector<uint8_t> newChunkData(CHUNK_SIZE, 0); // Example: New chunk data (all zeroes for demonstration)
|
|
|
|
|
|
|
|
|
|
addReplaceChunk(mcaFilename, sectorIndex, newChunkData);
|
|
|
|
|
|
|
|
|
|
extractChunks(mcaFilename);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|