🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

asIBinaryStream read()

Started by
2 comments, last by Rain Dog 18 years, 3 months ago
Hi, I've just started using AngelScript & so far I'm impressed with how painless the process has been. For the moment I'm using the tutorial sample as a reference. However I'd like to be able to protect scripts in my game from being easily modified, to prevent end-users from being able to cheat by simply changing what they do. I read the docs regarding SaveByteCode() and asIBinaryStream, but am unsure as to how to use these functions to save the bytecode out to disk. I understand that I need to call Read() and provide a pointer to a location in memory (and then use this to write the contents out to disk), but can't work out how to retrieve the correct size of the binary stream. Is what I'm asking possible, and if so, how can it be done?
"I must not fear. Fear is the mindkiller. Fear is the little death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past me I will turn to see fear's path. Where the fear has gone there will be nothing. Only I will remain." ~Frank Herbert, DuneMy slice of the web
Advertisement
Actually, it's quite easy. AngelScript does all the hard work for you. :)

One example implementation would be:

class CBytecodeStream : public asIBinaryStream{public:	CBytecodeStream() {wpointer = 0;rpointer = 0;}	void Write(const void *ptr, asUINT size) {if( size == 0 ) return; buffer.resize(buffer.size() + size); memcpy(&buffer[wpointer], ptr, size); wpointer += size;}	void Read(void *ptr, asUINT size) {memcpy(ptr, &buffer[rpointer], size); rpointer += size;}	int rpointer;	int wpointer;	std::vector<asBYTE> buffer;};--------------------------------------// Save the compiled byte codeCBytecodeStream stream;engine->SaveByteCode("module name", &stream);// Load the compiled byte code into the same moduleengine->LoadByteCode("module name", &stream);


This example is taken from the test file /tests/test_feature/source/test_saveload.cpp. It saves the bytecode to a memory buffer, but it shouldn't be difficult for you to convert it to save to disk.

When loading the saved bytecode you have to make sure the script engine was configured in the exact same way as it was when the original bytecode was saved. But this shouldn't be a problem, unless you're using the dynamic configuration groups.

But you should know that using precompiled bytecode instead of scripts will only make it sligthly more difficult to modify them. It's basically just an obfuscation of the script code.

What you really need to protect from cheating is to use some method for validating that the scripts are the original ones. Calculating the checksum of the script and validating it against some safe source is what I recommend.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks very much! Even less pain than I thought. I hadn't actually looked in the test folder until now.. doh

I understand it's not very secure. I plan on merging them into my dodgy custom level data format - the idea being the scripts are lost to the human eye amidst an assortment of other confusing characters that make no sense when opened in Notepad. It's purely to stop the casual cheater rather than the one who knows how to use a hex editor :)

An editor is included in the game so they could just cheat that way I guess (though I assume the script compilation is one-way in terms of meaningful variable names?), but it's only made available after the game has been completed [lol]
"I must not fear. Fear is the mindkiller. Fear is the little death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past me I will turn to see fear's path. Where the fear has gone there will be nothing. Only I will remain." ~Frank Herbert, DuneMy slice of the web
A good option would be to encrypt the byte code and then decrypt it before you load it. Even just a quick easily cracked encryption algorithm would substantially increase the amount of time/knowledge a person would need to modify the byte code.

This topic is closed to new replies.

Advertisement