Creating executable files in GM - A how to tutorial
-- Intro on creating EXEs
So how do we create exe's in GM? Some time ago, I discovered that I could add text to the end of the GM exes without harming/corrupting them. So this opened the world of creating exe with GM but without extra files. This is done by having an exe that read itself (ie in the start of game) and check for the extra text, which could be simply GML code.
The problem with finding the text is how to know it was found. So I built up a "header" system, were some text shows that the extra text is starting.
-- Tutorial intro
Next, what we will do is create two actual GM projects.
The first project is the exe that reads itself for the extra code. (named 'p1').
The second project will be used to create the exe's, (we will name this and refer to as 'p2').
-- Project P1 (exe template)
Let's start with 'p1'. First add a new script named "exe_read()", the code is found here.
Then add a new object and put it in one room. Put the following code in an "Execute code" action, in the Create event:
var t,n;
n=string(round(random(99999)))
t=exe_read("ThisIsTheCodeHeader",n)
if (t=n) {
show_message("Error: No extra code found.")
game_end()
} else {
execute_string(t)
}This code will read the exe's extra data using the header 'ThisIsTheCodeHeader' and if no code is found then the text in variable n is returned. Then if the returned code is equal to n (no code found), show error and end game, else run the code.Next we will put up some tricky things, we will search for special variables found in the extra code. These variables were never created by our program.
Create an 'Execute code' action in 'Step' event and add the following code:
if variable_global_exists("sc_step") then execute_string(global.sc_step)Then add the following code to an 'Execute code' action in a 'game end' event:if variable_global_exists("sc_end") then execute_string(global.sc_end)Then add the following code to an 'Execute code' action in a 'draw' event:if variable_global_exists("sc_draw") then execute_string(global.sc_draw)And lastly add the following code in the 'Execute code' action in the 'Create' event, JUST AFTER the code we already did there.if variable_global_exists("sc_create") then execute_string(global.sc_create)And thats done the sample named 'p1' exe is done. Save all and build it into a program. (make sure it is named p1.exe)-- Project P2 (exe writer)
Next p2 is very much simpler, it will 'create' the exe files
Create a new object and a new room. Put the object in the room. Add the following code in the "Execute Code" action of SPACE RELEASED event:
var a,b,c,d,e,f;
show_message("This will create exes with your own code. Press OK to continue.")
a=get_string("Enter game start code:","show_message('Hello Sir!!') s=0")
b=get_string("Enter game step code:","s+=1")
c=get_string("Enter game draw code:","draw_text(0,0,string(s))")
d=get_string("Enter game end code:","show_message('Bye Sir!!')")
e=get_save_filename("Executables|*.exe","Test.exe")
if file_exists(e) then if show_question("WARNING: File already exists. Rewrite?")=false then exit
if file_exists(e) then file_delete(e)
file_copy('p1.exe',e)
f=file_text_open_append(e)
file_text_writeln(f)
file_text_write_string(f,"ThisIsTheCodeHeader")
file_text_writeln(f)
file_text_write_string(f,'global.sc_create="'+a+'" global.sc_step="'+b+'" global.sc_draw="'+c+'" global.sc_end="'+d+'"')
file_text_close(f)
if show_question("File created. Do you want to execute it?") then execute_program(e,"",false)Save the program and run it. Make sure that p1.exe is in the same directory as this program's gm6 file. When it has started press space.This will show a series of messages were you enter the new program's code. Then at last you will be asked were to save the file. Afterwords you have the option to test the file by running it, you should click 'yes'.
Note that there is already code in the code messages. To test everything, you should continue with changing the code. As you might have guessed, the program starts and shows a "Hello sir" message. Then it increases a variable "s", which was declared in the first code message. The contents of this variable are drawn in the new program's window (you should see an increasing number). When you close the program, a "Bye sir" message is shown.
-- Encryption, security and compression
Note that the code is writen in the resulting exe as is, uncompressed and unencrypted. Another security risk is that the code read is directly used in execute_string() which may pose a risk to resource unpacking (GMResUnpack TID 129). To avoid execute_string() attack, use security scripts found here: Security Scripts
-- Legality
The software/scripts does not break any GM license (6.x and below). I am not sure this is the same for gm7 as the licence is not clear about this. According to some friends it is legal though, as the licence covers the runner part of exes.
-- Download Sample
After several requests for making some sample program and putting the tutorial code into a GM program, (well two of them), I decided to do it.
Covac Website (Main mirror)
Host-A.net (Secondary Mirror)
To use it, run p2.exe and press SPACE.
(Tutorial as written in Romania's message May 26 2007, 07:26 PM)
Good Luck with creating exe's!
There is no limit to what GM can do! (if there is, I didn't find it yet)
Regards,
Christian Sciberras ~ creator
edit: fixed typo and link to script (how the heck did I manage to loose the link?)
edit: renamed script variables.
edit: definately fixed it.
edit: added sample GM programs and their gm6 source code.
edit: fixed download link.
Edited by uuf6429, 07 August 2009 - 12:00 PM.











