Custom compiler directives

BlitzMax Forums/BlitzMax Programming/Custom compiler directives

Oddball(Posted 2011) [#1]
How are people doing custom compiler directives these days? I'm at the point where I want to make different versions of my game from the same source. I'd like something along the lines of...

?Demo=True

?Demo
	'Demo specific code here
?Not Demo
	'Full game specific code here
?


...but obviously this isn't possible. What are the current popular solutions for this?


computercoder(Posted 2011) [#2]
Thats in the Conditional Compiling in Help.

If you look in the right pane, under Help -> Language -> Conditional compiling

This describes the various settings you can use.

I switch between Win32, MacOS, and Linux if I do anything with conditional compiling.

Hope this helps :)


Czar Flavius(Posted 2011) [#3]
Make a const which is either true or false and use if statements. There is no way to make custom ones.


Htbaa(Posted 2011) [#4]
Wouldn't it be possible to change the value of a Const with a hex editor?

Being able to add own conditionals would be a nice addition.


Oddball(Posted 2011) [#5]
@computercoder: I think you've misinterpreted my query. I already use compiler directives, but I need a way to define custom compiler directives.

@Czar Flavius: As Htbaa has pointed out constants can be altered by the end user so using them isn't an option, especially if I'm using them to create the demo version of the game. I need a solution that will not compile the excluded code at all.

Has no one made a pre-compiler that does this?


JazzieB(Posted 2011) [#6]
Make a const which is either true or false and use if statements. There is no way to make custom ones.

Only problem with that is BlitzMax compiles all the code regardless - potentially allowing someone to unlock the full game from the demo. Previous versions of Blitz would omit any code that would never be executed.

Wouldn't it be possible to change the value of a Const with a hex editor?

Not during compilation, as the reference to constants are replaced with the actual values. Although, I'm guessing you mean as in the context above.

Being able to add own conditionals would be a nice addition.

Yes, it would. Or even just a demo directive would be nice. Would save me from hacking my code to bits to produce a demo version.


Htbaa(Posted 2011) [#7]
I guess for this top happen BlitzMax should allow preprocessing. Didn't Brucey ones experimented with it?


Czar Flavius(Posted 2011) [#8]
Won't the assembly generator detected unused code? It seems strange it will still be compiled.


GW(Posted 2011) [#9]
Not during compilation, as the reference to constants are replaced with the actual values.


I don't believe this is true. Constants are still used by reference.


Czar Flavius(Posted 2011) [#10]
I thought integer constants are inserted into the code during compilation, and that assembly would detect a jump on a condition that never changed?


JazzieB(Posted 2011) [#11]
I don't believe this is true. Constants are still used by reference.

That doesn't make sense. If they're still referenced, what's the point in using them? I'm pretty sure that my assumption is true because it's a logical optimisation to do so.

I thought integer constants are inserted into the code during compilation, and that assembly would detect a jump on a condition that never changed?

I did a test some time ago by using two Print statements within an If..Then..Else clause using a constant and then checked the code in a hex editor after to see if both pieces of text were in the code. And they were, so it looks like everything gets compiled. It may have changed now though, as it was some time ago.


Czar Flavius(Posted 2011) [#12]
I'm pretty sure that my assumption is true because it's a logical optimisation to do so.
That's very dangerous!

And they were, so it looks like everything gets compiled.
Indeed, I only thought otherwise because it seemed a logical optimisation to do so ;)


GW(Posted 2011) [#13]
It was a few versions back, but I tested the option of using constants as compiler directives as both client and server projects pull from the same codebase for my project. Looking at the generated assembly, constants will still used by reference. It's an optimization that should have been done.


Zeke(Posted 2011) [#14]
What are the current popular solutions for this?

use include:
Include "full_game_code.bmx"
'Include "demo_game_code.bmx"

or rem/endrem:
'full_game_code
' < full game code here >

Rem
 < demo code here >
endrem



TomToad(Posted 2011) [#15]
Just tested it myself. The constant is replaced by the actual value any place it appears in the code. The reference is kept when you compile in debug mode, despite not actually being used in the code, probably for the debugger. In release mode, all references are replaced by the actual value and the constant completely disappears.

SuperStrict

Const Number:Int = 5

Local Result:Int
Local Adder:Int = 25

Result = Number + Adder

Print Result


Assembler code Debug (comments added by me)
' Local Adder:Int = 25
        mov	dword [ebp-8],25
'Some Debug Stuff
	push	_29
	call	dword [_bbOnDebugEnterStm]
	add	esp,4
'Result = Number + Adder
	mov	eax,dword [ebp-8]
	add	eax,5
	mov	dword [ebp-4],eax


Notice that "Adder" and "Result" are called by reference, yet "Number" was replaced by 5 in the code.

Assembler code Release
	mov	eax,25 'Adder
	add	eax,5 'Number
	push	eax

All references in release mode are optimized away from this simple example.

As for the OP, I tried using If True type thing, seems all code is still compiled regardless if in debug or release mode.


SLotman(Posted 2011) [#16]
Solution is simple: have a demo with a separated assets. Even if someone crack the demo EXE, they will have a nice crash on their hands when the program tries to load images and sounds there aren't there.

Otherwise: different checks in different functions in different points of the game. Crackers usually just test if the game "started" - they don't play it to the end! So, a nice validation check near the end, another halfway into the game could be another solution.


computercoder(Posted 2011) [#17]
I like that idea SLotman :) Sounds very effective and a nice way to keep the game protected; even if someone gets passed part of the security!


H&K(Posted 2011) [#18]
Ive not seen a demo in ages that wasnt simply the entire program minus some assets. (ie most levels)

Anyway to answer the question, use the community IDE (Or the uncompiled normal IE), and spend (I would guess 2 hours) adding your own ? directives.

(Im not sure, but does Blide managed builds do this already, or the community ide for that matter


Czar Flavius(Posted 2011) [#19]
Be very careful. What is to stop someone just downloading the full game content and putting it with their "demo" exe? If some non-demo required files are incbinned and only loaded from the incbin, that would be hard to break. But don't just load it from disk. (For the purposes of protection)