Jump to content
  • Advertisement
Sign in to follow this  
mind in a box

Python to C++, file chaos

This topic is 2952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

I want to write a little extension to an existing Python script, so that I can write my own files for it. The Python script is open source, so I was able to find the part where the script writes to such a file.

However, I can't do python and I have problems with doing the same in C++.

Here is the part of the script:
[source="Python"]
with open(self.filename + postfix,"wb") as f:
x1,y1,z1 = self.first_pos
x2,y2,z2 = self.second_pos
if x1 > x2:
x1, x2 = x2, x1
if y1 > y2:
y1, y2 = y2, y1
if z1 > z2:
z1, z2 = z2, z1
b = self.blocks
## +1 to the coordinate since the volume copied is inclusive of point 2
f.write( struct.pack('!3i',x2 - x1+1, y2 - y1+1, z2 - z1+1) )
while x1 <= x2:
y = y1
while y <= y2:
z = z1
while z <= z2:
offset = self.bot.calculateOffset(x1,y,z)
f.write( struct.pack('!B',int(self.bot.block_array[offset])))
z +=1
y +=1
x1 += 1



And this is what I was trying:

f=fopen(ChunkFile,"wb");

if(f)
{
//Inside bot spits out BigEndian files, we are little endian here.
//Perform a swap
int SizeX=LongSwap(Header2.biWidth);
int SizeY=LongSwap(Header2.biHeight);
int SizeZ=LongSwap(1);

//Write region size
fwrite( &SizeX, sizeof(int), 1, f );
fwrite( &SizeY, sizeof(int), 1, f );
fwrite( &SizeZ, sizeof(int), 1, f );

//Write blocks to the file
for(int i=0;i<Matches.size();i++)
{
unsigned char M_F=Matches;
fwrite(&M_F,sizeof(unsigned char),1,f);
}

printf("<INFO>Done, closing file...\n",argv);
//Close file
fclose(f);



Well.. The script seems to crash when I want to load that file.
Would someone who understands python please take a look over my code?

Share this post


Link to post
Share on other sites
Advertisement
Does the Python script (the file loader function I assume) crash when it tries to load a file generated by your C++ program? It's not really clear from your post.

Either way, the Python script takes a 3D bounding box. It first writes the dimensions to the file as 3 big-endian integers. Then it writes (width * height * depth) number of bytes (unsigned characters) to the file. It seems you're doing something similar though, so the problem may lie elsewhere. Did you compare files generated by your program with files generated by the Python script?

Share this post


Link to post
Share on other sites
The Python script was compiled into an .exe. When I load the file it's window closes, so I assume it crashed somewhere.

I indeed compared files made by the script with my files. My file has much more new-lines as the "original" file. The original file seems to have none of them.

I'll try if I can load it backwards...

Share this post


Link to post
Share on other sites
Quote:
Original post by mind in a box
Hi!

I want to write a little extension to an existing Python script, so that I can write my own files for it.


What exactly are you trying to do?

The Python script both reads and writes files, is that it?

You want to write a file from C++ that the Python script will read?

When you say "extension", I assume you mean that you are changing the file contents. Will you change the Python script also to understand the changes? Otherwise how do you expect it to work?

Share this post


Link to post
Share on other sites
Quote:

What exactly are you trying to do?

The Python script both reads and writes files, is that it?

You want to write a file from C++ that the Python script will read?

When you say "extension", I assume you mean that you are changing the file contents. Will you change the Python script also to understand the changes? Otherwise how do you expect it to work?


The Python script is called "Inside Bot" and can be used with Minecraft's creativity mode. It can draw things like lines or cuboid, but it also is able to extract a portion of the map to disk. This portion gets saved as such a .chunk file with the code I've shown above.
Players are able to paste such a file in the world again. It's copy/paste.

I want to hook that function to be able to put my own content into a .chunk file, so that I can paste it directly in Minecraft. The thing I am after is to directly convert bitmaps to Minecrafts blocks.

I do this just for fun, pixeling those images in Minecraft is fun, too, but hacking is also funny [smile]

For this I don't need to change the python file. I just want to give it my file to paste.

Share this post


Link to post
Share on other sites
So you are making the file, in the Python script's format, that represents some custom data from somewhere else. OK.

Did you check that Matches.size() == SizeX * SizeY?

Newlines shouldn't be an issue since you are writing binary data to a file opened in binary mode. Use a hex editor to compare the files, not a text editor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!