Advertisement

A bunch of questions

Started by October 05, 2008 05:37 AM
13 comments, last by Blednik 16 years, 1 month ago
Heyas. I'm currently working on a C/C++ DLL project and I've decided to use Angelscript as it's core scripting engine. I've played with AS before and now I'm finally planning to let it do some "dirty work". The project is in very early stages and I do have a bunch of questions regarding Angelscript integration, performance and stuff like that. I'm not sure I can write a full list of questions as I write this, but I'm going ask a single question at a time. Once I get an answer I'll post a new question as I go. Question 1: My first question is how to make Angelscript read/write the memory of its own process. Can it be done the same way as in C by simply using pointers or is it necessary to write a function in C that does this and then register it with angelscript?
As default AngelScript uses the standard malloc/free functions from the CRT library to do all memory management. You can easily change this by setting the global memory management functions with a call to SetGlobalMemoryFunctions() passing in two function pointers, one for allocations and one for deallocations.

The SetGlobalMemoryFunctions call should be made before the script engine is created with asCreateScriptEngine, so that even the script engine itself is allocated using the proper routines.

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

Advertisement
Hold on just a sec, let me make sure we're both talking about the same thing. I'd like a way to read/write the memory of the current process! The same thing that ReadProcessMemory and WriteProcessMemory are designed to do, however, these two API's are normally used to r/w memory on a *remote* process. Since this DLL will be a part of the process where I plan on r/w'ing memory, I don't think I have to OpenProcess() and then use those two API's to do the job.

In C/C++ you would simply declare a pointer like:
int* value = 0;

This is a null pointer and is not useful on its own, but you can assign the address of another variable to it. Like this:
int another_var;
value = &another_var;

Then if you plan to read/write stuff to that another variable, you would simply de-reference the pointer and do normal read/write.
(*value) = 300;

This is an example where you let the program assign the proper address of another_var to the value pointer, but you could also do it manually:
value = 0x00413590;
int result = (*value);

This would read whatever memory is being stored at the address 0x00413590 of the current process. Translation to assembly:
MOV ECX, 00413590
MOV EAX, DWORD PTR:[ECX]

So I was wondering whether AngelScript supports this kind of memory access or do I have to write two proxy functions in C/C++ - ReadMemory(int address, char * buffer, int length) and WriteMemory(int address, char * buffer, int length) and register those with the engine?

Sorry, if I wasn't being clear enough.

EDIT: Typo fix
Ah, now I understand your question.

You don't have to do anything special to have the DLL read or write memory in the application process. When the DLL is loaded by the application, it will share the same memory space as the application itself.

The DLL usually uses it's own memory heap though, a block of memory allocated in the application must not be deallocated by the DLL, and vice versa.

AngelScript doesn't however have any built-in functions for reading from or writing to random memory locations, as it's usually not something you would want to allow a script to do due to safety issues. If you need to do that, you would have to register a couple of functions to do it for you. Example:

int ReadFromMemory(int location){  return *(int*)location;}engine->RegisterGlobalFunction("int ReadFromMemory(int)", asFUNCTION(ReadFromMemory), asCALL_CDECL);


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

Yes, that's the answer I was looking for. I'll write my own memory r/w functions and have them registered then. Also, I forgot to congratulate you for the 300th SVN build of AngelScript!

Question 2:
You create an engine and configure it with a bunch of functions. Now it's time to add a couple of script sections to it. But suppose that something goes wrong after a number of sections have been added by AddScriptSection(...) e.g. a script seems corrupted. How would I remove all these script sections that have already been added so that I can begin anew? I've been poking through angelscript.h function prototypes and found this entry, could this be what I'm looking for?
virtual int ResetModule(const char *module) = 0;

Also, I assume the Discard(...) function is the opposite of Build(...), correct me, if I'm wrong. If yes, this answers another question I was about to ask.
Everytime Build() is called on a module it removes the previously added script sections. Discard() discard would be used when you don't want to use the module anymore, but also don't want to rebuild it. ResetModule() is used for reinitializing the global variables in the module to the state they had just after build.

You might want to check the manual for information on the interface methods. That way you don't have to try to guess your way by looking at the angelscript.h header file.

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

Advertisement
Nice, I didn't know that manual existed, I'll refer to it in the future.

So you're saying I should use Build() to remove the previously added script sections to discard them?

EDIT: Let me sum up the whole story to show you what I'm trying to do here.
I have a script loader, kind of like the one in examples that handles #include directives. The thing is that I don't trust the directives to always be genuine. A user might put some corrupted data into one of the scripts or perhaps a script doesn't exist. The engine has already included a number of existing scripts and needs to stop. Now the user might fix the error and try to load the same scripts again... or, he might want to load a totally different script. In any way, the engine should be able to dump the previously loaded scripts and begin anew. My question is what is the proper way to do so.

[Edited by - Blednik on October 5, 2008 2:44:05 PM]
In this case you should call Discard on the module. The next time you call AddScriptSection after that the engine will start on a new module (even if the name is the same).

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

And I suppose I can also use Discard() to free the bytecode after the script has been built, yes?
...
Maybe I should rephrase that question, assuming that Build() is the function that generates the bytecode...
When the user decides to load another script, does the previously built script need to be freed manually - e.g. Discard() - or can I just ignore it and do AddScriptSection() + Build() ontop of the existing bytecode, knowing it will be overwritten? The scripts use the same module.
You can just add new script sections and build the module again. The previously built bytecode will be discarded when the module is rebuilt with the new script.

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

This topic is closed to new replies.

Advertisement