Push Chunks Manipulator

Also commented out the shaderc build script, since it seems broken
on upstream.
jocadbz
Joca 2024-07-09 21:09:22 -03:00
parent 61cec73c49
commit 14f0fc1f3a
Signed by: jocadbz
GPG Key ID: B1836DCE2F50BDF7
2 changed files with 148 additions and 17 deletions

View File

@ -70,24 +70,24 @@ fi
popd >/dev/null 2>&1
echo ">>> done"
echo ">>> Dealing with shaderc shenanigans..."
pushd dependencies/tmp/shaderc
echo "Getting sources using the provided script..."
./update_shaderc_sources.py
SHADERC_BUILD="build-$(dd if=/dev/urandom bs=1 count=5 2>/dev/null | base32)"
echo "Creating and entering directory $SHADERC_BUILD."
mkdir "$SHADERC_BUILD"
cd "$SHADERC_BUILD"
echo "Running CMake..."
CXXFLAGS="-Wno-error" cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../src/
echo "Running Ninja..."
ninja
popd >/dev/null 2>&1
#echo ">>> Dealing with shaderc shenanigans..."
#pushd dependencies/tmp/shaderc
#echo "Getting sources using the provided script..."
#./update_shaderc_sources.py
#SHADERC_BUILD="build-$(dd if=/dev/urandom bs=1 count=5 2>/dev/null | base32)"
#echo "Creating and entering directory $SHADERC_BUILD."
#mkdir "$SHADERC_BUILD"
#cd "$SHADERC_BUILD"
#echo "Running CMake..."
#CXXFLAGS="-Wno-error" cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../src/
#echo "Running Ninja..."
#ninja
#popd >/dev/null 2>&1
#if needed copy more relevant files to dependencies/shaderc
echo "Copying binary to dependencies/shaderc/bin..."
mkdir -vp dependencies/shaderc/bin
cp -v "dependencies/tmp/shaderc/$SHADERC_BUILD/glslc/glslc" dependencies/shaderc/bin
echo ">>> done"
#echo "Copying binary to dependencies/shaderc/bin..."
#mkdir -vp dependencies/shaderc/bin
#cp -v "dependencies/tmp/shaderc/$SHADERC_BUILD/glslc/glslc" dependencies/shaderc/bin
#echo ">>> done"
echo ">>> Building zlib..."
pushd dependencies/zlib-1.3.1/

View File

@ -0,0 +1,131 @@
/*
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;
}