Binary I/O in Torque 2D
by Quentin Headen · 07/03/2011 (1:26 am) · 2 comments
Edit 7/5/2011 - Fixed the readBool method. Was reading a float instead of a boolean value.
Sometimes, you need to read or write data to a file in binary format instead of plain text. For example, let's say you are writing a player's score to a save file. If you write the score in plain text, anyone can go into the file system, find the save file, and change their score at will using any plain text editor. This is too easy. So using this resource, you can write boolean, integer, and float values to a file in binary format using the FileStreamObject, thus obfuscating the data.
Enough talking. Let's get to the resource. Go into your Torque 2D source directory and look for a file named fileStreamObject.cc. This file should be under the "core" directory.
After opening up that file, add this code to the very bottom:
That's all there is to it. Now recompile the engine, and you can now read and write booleans, integers, and floats in binary format.
For example, lets say you want to write the values 50, 24.4, and "true" to a file in binary. You could do it this way:
What if you want to read the data back? Here's how:
After running that code, the variables intValue, floatValue, and boolValue should contain 50, 24.4, and "true" respectively.
Going back to the example mentioned in the outset of the resource, one use of these functions is to write obfuscated binary data to a save file. While this will make the plain text of the file look cryptic, the data can still be read using a binary editor. So any tech savvy person can find the save file, open it in a binary editor, and change the data. Still, it makes them work a little bit, and if you want, you can fill the file with random data and hide the important data within.
As a side note, this code was written for and tested with Torque Game Builder 1.7.5. As for other versions of TGB, or other versions of Torque, I can't say for sure if this code will work or not. If you try it in another version and it works, please let everyone know by posting a comment.
I hope this resource has helped you. If so, enjoy it. :)
Sometimes, you need to read or write data to a file in binary format instead of plain text. For example, let's say you are writing a player's score to a save file. If you write the score in plain text, anyone can go into the file system, find the save file, and change their score at will using any plain text editor. This is too easy. So using this resource, you can write boolean, integer, and float values to a file in binary format using the FileStreamObject, thus obfuscating the data.
Enough talking. Let's get to the resource. Go into your Torque 2D source directory and look for a file named fileStreamObject.cc. This file should be under the "core" directory.
After opening up that file, add this code to the very bottom:
//Binary writing methods
ConsoleMethod( FileStreamObject, writeInt, void, 3, 3, "FileStreamObject.writeInt(value)")
{
//Get the int value
S32 value = dAtoi(argv[2]);
//Get the file stream
Stream* stream = object->getStream();
//Write the integer into the stream
stream->write(sizeof(S32), &value);
}
ConsoleMethod( FileStreamObject, writeFloat, void, 3, 3, "FileStreamObject.writeFloat(value)")
{
//Get the float value
F32 value = dAtof(argv[2]);
//Get the file stream
Stream* stream = object->getStream();
//Write the float to the stream
stream->write(sizeof(F32), &value);
}
ConsoleMethod( FileStreamObject, writeBool, void, 3, 3, "FileStreamObject.writeBool(value)")
{
//Get the boolean value
bool boolValue = dAtob(argv[2]);
//Convert it to 8 bit 1 or 0
S8 value = (boolValue ? 1 : 0);
//Get the stream
Stream* stream = object->getStream();
//Write the value to the stream
stream->write(sizeof(S8), &value);
}
ConsoleMethod( FileStreamObject, readInt, S32, 2, 2, "FileStreamObject.readInt()")
{
//Get the file stream
Stream* stream = object->getStream();
//Create an int variable to hold the data
S32 value;
//Read the data into the variable
stream->read(sizeof(S32), &value);
//Return the value
return value;
}
ConsoleMethod( FileStreamObject, readFloat, F32, 2, 2, "FileStreamObject.readFloat()")
{
//Get the file stream
Stream* stream = object->getStream();
//Create a float variable to hold the data
F32 value;
//Read the data into the variable
stream->read(sizeof(F32), &value);
//Return the value
return value;
}
ConsoleMethod( FileStreamObject, readBool, bool, 2, 2, "FileStreamObject.readBool()")
{
//Get the file stream
Stream* stream = object->getStream();
//Create an 8-bit number variable to hold the data
S8 value;
//Read the data into the variable
stream->read(sizeof(S8), &value);
//If the value is 1, return true. Return false if it is zero
return (value == 1 ? true : false);
}That's all there is to it. Now recompile the engine, and you can now read and write booleans, integers, and floats in binary format.
For example, lets say you want to write the values 50, 24.4, and "true" to a file in binary. You could do it this way:
//Of course %stream is the FileStreamObject in script. We'll assume it is already //open for writing to a file. %stream.writeInt(50); %stream.writeFloat(24.4); %stream.writeBool(true);
What if you want to read the data back? Here's how:
//Of course %stream is the FileStreamObject in script. We'll assume it is already //open for reading from a file. %intValue = %stream.readInt(); %floatValue = %stream.readFloat(); %boolValue = %stream.readBool();
After running that code, the variables intValue, floatValue, and boolValue should contain 50, 24.4, and "true" respectively.
Going back to the example mentioned in the outset of the resource, one use of these functions is to write obfuscated binary data to a save file. While this will make the plain text of the file look cryptic, the data can still be read using a binary editor. So any tech savvy person can find the save file, open it in a binary editor, and change the data. Still, it makes them work a little bit, and if you want, you can fill the file with random data and hide the important data within.
As a side note, this code was written for and tested with Torque Game Builder 1.7.5. As for other versions of TGB, or other versions of Torque, I can't say for sure if this code will work or not. If you try it in another version and it works, please let everyone know by posting a comment.
I hope this resource has helped you. If so, enjoy it. :)
About the author
Just your average programmer who tries to finish the projects he starts. :) I am currently focused on creating games with Torque engines. My website is http://phaseshiftsoftware.com
#2
Thanks for the comment. I just edited the code because the readBool method was reading a 32 bit float value when only an 8 bit value is being written with the writeBool method. So if you have used the code before this edit, use the new code.
07/05/2011 (10:06 am)
@BrianThanks for the comment. I just edited the code because the readBool method was reading a 32 bit float value when only an 8 bit value is being written with the writeBool method. So if you have used the code before this edit, use the new code.

Torque Owner Brian Wilson