Page 1 of 3

Creating your own executable from a PB program?

Posted: Thu Oct 19, 2006 8:17 pm
by Fluid Byte
I'm working on game creation suite similar to Games Factory but this ones specialized in 2D Shooters only. So you can do all the regular stuff like add / manage resources or edit levels with a built-in editor. When the user has completed all necessary tasks I want him to be able to export his game into a standalone executable.

The way I'm doing it at the moment is to put all project specific options and resource locations into a .INI file wich will "parsed" with a template .EXE I created for this purpose. The program will just make a copy of that .EXE along with the .INI file and all needed resources and puts it into a new folder.

This current method has many disadvantages (I'm too lazy to list 'em all) and that's why I want to create my own executable from the ground up. I would appreciate any help on this particular topic.

Posted: Thu Oct 19, 2006 8:25 pm
by Killswitch
The easiest way around that would be to appened that INI data to the very end of your .exe. Instead of reading from that file, you read from the .exe (using normal file manipulation commands) - although you have to be really clear on the layout of the data.

Posted: Fri Oct 20, 2006 6:30 pm
by Fluid Byte
Well, I want my executable to be unique. I mean for example if I encrypt my data I put the key into my executable so it is individual and only this copy will decode the specified resources. I can't do that with my template because I can't edit the .EXE after compiling.

Furthermore, the template .EXE is sort of my engine so I always giving it away for free when someone is creating a new game. Other people could just code their own games by using commandline parameters on the template without using my program or paying any fees. Nothing should be used or modfied from outside.

Posted: Fri Oct 20, 2006 7:47 pm
by ricardo
Fluid Byte wrote: Furthermore, the template .EXE is sort of my engine so I always give it away for free when creating a new game. Other people could just code their own games by using commandline parameters on the template without using my editor or paying any fees. Nothing should be used or modfied from outside.
Something very similar happends with interpreters (they put the engine/interpreter and the script in the same executable), AFAIK they just encryot the script to avoid this possibility of creating new 'executables' (interpreter with script attached) without using the IDE.

Posted: Sun Oct 22, 2006 4:04 pm
by Fluid Byte
Couldn't I use a freeware compiler or has my whole system to be written in C then?

Posted: Sun Oct 22, 2006 4:13 pm
by thefool
I need to understand: do you want a standalone exe file wich would hold all resources and everything and load from memory, or do you want the exe to bind the game info [the inf file or whatever] and then the resources side-by-side?

You can easily embed the inf file to the exe, encrypt it and read it at runtime (even decrypt it) and then parse it directly from memory. this would stop people from using the runtime engine without using your "compiler"

Posted: Sun Oct 22, 2006 5:35 pm
by Fluid Byte
I need to understand: do you want a standalone exe file wich would hold all resources and everything and load from memory, or do you want the exe to bind the game info [the inf file or whatever] and then the resources side-by-side?
None of both. :P

I want to be able to create executables wich can contain source code dependent parameters. Those that can only be inlcuded / modified directly in the source before compiling. Like a numerical value that holds a key to decrypt something.

My description sucks, I know but lemme try to explain.

I could put that key (or any kind of value / data) into the .INI file, append it (or not) and encrypt it. But now where are you storing the decryption key for the .INI file :?:

You could only hardcode that decryption key into the already compiled .EXE but this would mean you have the same key for every application.

Posted: Sun Oct 22, 2006 6:47 pm
by thefool
Oh no! You can easily have a program changing the key in seperate exe's. you just make an exe that reads it from a place in itself [the end, even in the middle] and then inject it.

Posted: Sun Oct 22, 2006 8:03 pm
by Fluid Byte
thefool wrote:Oh no! You can easily have a program changing the key in seperate exe's. you just make an exe that reads it from a place in itself [the end, even in the middle] and then inject it.
This wouldn't screw up the .EXE? Forgive my ignorance but I know nothing about these kinda' things.

So I just can write a template executable, define a key somewhere in the source, compile it and then read/write a new key directly in the .EXE if I know the file position?

Posted: Sun Oct 22, 2006 9:42 pm
by thefool
Exactly! Its quite simple. If you can't grasp it i will make an example tomorrow. Otherwise try to search the forum, i think PB once had a nice example somewhere.

Posted: Sun Oct 22, 2006 10:17 pm
by Droopy
I don't know if it could help?
I coded an EXE with Self Modified resource.
Try this : http://1000gp.ovh.net/~droopyli/file/SlideShow.zip

Posted: Mon Oct 23, 2006 1:01 am
by ricardo
Don't know if this what you are looking for, this a a very simple example:

Interpreter: (compile it as engine.exe)

Code: Select all

;Interpreter

FileName$ = Space(1025)
GetModuleFileName_(GetModuleHandle_(0), @FileName$, 1025)

If ReadFile(0,FileName$);read on itself
   FileSeek(0,Lof(0)-4)
   LenghtOfScript = ReadLong(0);Here read the size of attachment
   FileSeek(0,Lof(0)-4-LenghtOfScript)
   Script$ = Space(LenghtOfScript)
   ReadData(0,@Script$, LenghtOfScript);Now we have the script in string
   ;Do whatever to the script, per example deencrypt it and the interpret it
   MessageRequester("Script",Script$)
   CloseFile(0)
EndIf

Compiler: (Just use it in same folder than engine)

Code: Select all

;Compiler

If CreateFile(0,"script.txt")
   WriteStringN(0,"whatever");this is the script, in this case is not encrypted but can be easily
   CloseFile(0)
EndIf

If ReadFile(0,"engine.exe")
   *engine =AllocateMemory(Lof(0))
   ReadData(0,*engine,Lof(0))
   If ReadFile(1,"script.txt")
      Script$ = Space(Lof(1))
      ReadData(1,@Script$,Lof(1))
      If CreateFile(2,"new_executable.exe")
         WriteData(2,*engine,Lof(0)) ;write engine
         WriteData(2,@Script$,Lof(1)) ; write script
         WriteLong(2,Lof(1)) ; write lenght of script
         CloseFile(2)
      EndIf
      CloseFile(1)
      CloseFile(0)
   EndIf
EndIf

Posted: Mon Oct 23, 2006 2:17 am
by ricardo
If you want to see some more complete example, i show you some Basic interpreter i code in PB (for some Visual Basic like Basic language).

Download engine.exe from http://www.yenerich.com/engine.zip

Use same code i put before as compiler, and the script is:

Code: Select all

createwindow(10,10,700,500,'MyBasic WebBrowser')

MyLabel = newLabel(1)
MyLabel.Top(5)
MyLabel.Left(10)
MyLabel.Width(200)
MyLabel.Text('Type the URL you want to visit:')
MyLabel.Show()

MyEdit = newEdit(2)
MyEdit.Top(25)
MyEdit.Left(10)
MyEdit.Width(300)
MyEdit.Text('http://www.google.com')
MyEdit.Show()

MyButton = newButton(3)
MyButton.Top(25)
MyButton.Left(310)
MyButton.Caption('Go')
MyButton.Show()

WB = newWebBrowser(4)
WB.Top(60)
WB.Left(10)
WB.Width(680)
WB.Height(350)
WB.Show()

MyProgressBar = newProgressBar(5)
MyProgressBar.Top(420)
MyProgressBar.Left(100)
MyProgressBar.Width(250)
MyProgressBar.Show()

function Button3()
WB.Text(MyEdit.GetText())
for i = 1,100
do
sleep(10)
MyProgressBar.SetState(i)
end
msgbox('hello there','just having fun!')
end

Compile it and run it, it create a window, a webgadget, etc.

What it does it to attach the script to the engine. When run, engine read itself, find the script and run it.

Yes, i know its better to encrypt is some way the script, otherwise people could make their own scripts, attach it to the engine and voila.

This is juts a working example on what you may looking for...

Hope it helps.

Posted: Mon Oct 23, 2006 5:23 pm
by Fluid Byte
@Droopy: Not quite what I'm looking for but a nice and useful piece of code for Windows resource modifying.

@ricardo: I'm doing it quite similar but your code is cleaner and more elegant. But it still doesn't solve the problem of properly storing / editing the decryption key (or any other hardcoded value) in my template .EXE.

@thefool: I wouldn't have asked these questions at all if just wanted to store addtional data at the end of my executable. It's an easy thing to do but if you wanted to crack the game and unpack the data you would first look there because it's an obvious place. I just tried to store a value somewhere in the middle of the .EXE by using a call to WriteLong(). Of course it was screwed afterwards because have no clue what I'm actually doing. :roll:

As for that code from PB, could you gime the link? I tried to search for it but only found useless stuff.

Posted: Mon Oct 23, 2006 5:37 pm
by ricardo
As far as i remember you can write in some places in the header of the executable without destroying it.

I don't remember (and don't know where do i put that code... maybe i put it here in the forums) but afaik i was reading longs with fastfile... i copy the first 80 values and then i change just values 82º to 85º and keep the rest of the executable without modification.

*I just do that as blind, without knowing exactly what i was doing... but i works for me!