Hack to remove main.cs with 3 lines of code
by Bryce "Cogburn" Weiner · 08/16/2004 (10:19 pm) · 26 comments
From time to time people have wondered how to get rid of the main.cs file that resides in the directory with the Torque EXE. This resource will outline one way to do this.
As always, read this resource completely and backup your files prior to attempting this resource.
Step 1: Prepare your existing main.cs file
Open main.cs in your favorite text editor
Search and replace all " (double quotation marks) with \"
All of your text must be as one line. Replace all line breaks with \n
Before:
After:
Save your changes as main.txt or some such and set it aside for a second.
Step 2: Engine changes
Open engine/main/game.cc
Approximately around line 357 comment out the following code:
Underneath, add this code:
Note you'll insert the contents of your main.txt file into the dStrcpy statement. Also, you'll need to change char *script = new char[1220]; to match the number of characters in your new string. You can get this from your text editor.
Recompile, delete your main.cs file and enjoy.
EDIT: Corrections.
As always, read this resource completely and backup your files prior to attempting this resource.
Step 1: Prepare your existing main.cs file
Open main.cs in your favorite text editor
Search and replace all " (double quotation marks) with \"
All of your text must be as one line. Replace all line breaks with \n
Before:
$defaultGame = "base";
$displayHelp = false;
function pushFront(%list, %token, %delim)
{
if (%list !$= "")
return %token @ %delim @ %list;
return %token;
}
function pushBack(%list, %token, %delim)
{
if (%list !$= "")
return %list @ %delim @ %token;
return %token;
}
function popFront(%list, %delim)
{
return nextToken(%list, unused, %delim);
}
$modcount = 1;
$userMods = $defaultGame;
function onStart()
{
}
function onExit()
{
}
function parseArgs()
{
}
package Help {
function onExit() {
}
};
function displayHelp() {
activatePackage(Help);
}
if (!$logModeSpecified) {
setLogMode(6);
}
setModPaths($userMods);
nextToken($userMods, currentMod, ";");
function loadDir(%dir)
{
setModPaths(pushback($userMods, %dir, ";"));
exec(%dir @ "/main.cs");
}
function loadMods(%modPath)
{
%modPath = nextToken(%modPath, token, ";");
if (%modPath !$= "")
loadMods(%modPath);
if(exec(%token @ "/main.cs") != true){
error("Error: Unable to find specified mod: " @ %token );
$modcount--;
}
}
loadMods($userMods);
parseArgs();
if ($displayHelp) {
enableWinConsole(true);
displayHelp();
quit();
}
else {
onStart();
echo("Engine initialized...");
}After:
$defaultGame = \"base\";\n$displayHelp = false;\nfunction pushFront(%list, %token, %delim)\n{\nif (%list !$= \"\")\nreturn %token @ %delim @ %list;\nreturn %token;\n}\nfunction pushBack(%list, %token, %delim)\n{\nif (%list !$= \"\")\nreturn %list @ %delim @ %token;\nreturn %token;\n}\nfunction popFront(%list, %delim)\n{\nreturn nextToken(%list, unused, %delim);\n}\n$modcount = 1;\n$userMods = $defaultGame;\nfunction onStart()\n{\n}\nfunction onExit()\n{\n}\nfunction parseArgs()\n{\n}\npackage Help {\nfunction onExit() {\n}\n};\nfunction displayHelp() {\nactivatePackage(Help);\n}\nif (!$logModeSpecified) {\nsetLogMode(6);\n}\nsetModPaths($userMods);\nnextToken($userMods, currentMod, \";\");\nfunction loadDir(%dir)\n{\nsetModPaths(pushback($userMods, %dir, \";\"));\nexec(%dir @ \"/main.cs\");\n}\nfunction loadMods(%modPath)\n{\n%modPath = nextToken(%modPath, token, \";\");\nif (%modPath !$= \"\")\nloadMods(%modPath);\nif(exec(%token @ \"/main.cs\") != true){\nerror(\"Error: Unable to find specified mod: \" @ %token );\n$modcount--;\n}\n}\nloadMods($userMods);\nparseArgs();\nif ($displayHelp) {\nenableWinConsole(true);\ndisplayHelp();\nquit();\n}\nelse {\nonStart();\necho(\"Engine initialized...\");\n}Save your changes as main.txt or some such and set it aside for a second.
Step 2: Engine changes
Open engine/main/game.cc
Approximately around line 357 comment out the following code:
FileStream str;
const char* mainCS = "main.cs";
if(!str.open(mainCS, FileStream::Read))
{
char msg[1024];
dSprintf(msg, sizeof(msg), "Failed to open \"%s\".", mainCS);
Platform::AlertOK("Error", msg);
return false;
}
U32 size = str.getStreamSize();
char *script = new char[size + 1];
str.read(size, script);
str.close();Underneath, add this code:
char *script = new char[1220];
dStrcpy(script,"$defaultGame = \"base\";\n$displayHelp = false;\nfunction pushFront(%list, %token, %delim)\n{\nif (%list !$= \"\")\nreturn %token @ %delim @ %list;\nreturn %token;\n}\nfunction pushBack(%list, %token, %delim)\n{\nif (%list !$= \"\")\nreturn %list @ %delim @ %token;\nreturn %token;\n}\nfunction popFront(%list, %delim)\n{\nreturn nextToken(%list, unused, %delim);\n}\n$modcount = 1;\n$userMods = $defaultGame;\nfunction onStart()\n{\n}\nfunction onExit()\n{\n}\nfunction parseArgs()\n{\n}\npackage Help {\nfunction onExit() {\n}\n};\nfunction displayHelp() {\nactivatePackage(Help);\n}\nif (!$logModeSpecified) {\nsetLogMode(6);\n}\nsetModPaths($userMods);\nnextToken($userMods, currentMod, \";\");\nfunction loadDir(%dir)\n{\nsetModPaths(pushback($userMods, %dir, \";\"));\nexec(%dir @ \"/main.cs\");\n}\nfunction loadMods(%modPath)\n{\n%modPath = nextToken(%modPath, token, \";\");\nif (%modPath !$= \"\")\nloadMods(%modPath);\nif(exec(%token @ \"/main.cs\") != true){\nerror(\"Error: Unable to find specified mod: \" @ %token );\n$modcount--;\n}\n}\nloadMods($userMods);\nparseArgs();\nif ($displayHelp) {\nenableWinConsole(true);\ndisplayHelp();\nquit();\n}\nelse {\nonStart();\necho(\"Engine initialized...\");\n}");
U32 size = dStrlen(script);Note you'll insert the contents of your main.txt file into the dStrcpy statement. Also, you'll need to change char *script = new char[1220]; to match the number of characters in your new string. You can get this from your text editor.
Recompile, delete your main.cs file and enjoy.
EDIT: Corrections.
#2
08/19/2004 (9:38 am)
Ace, I think you missed his point. He's hard coding the main.cs into a char array. The parts that look like they aren't formatted correctly have had the line breaks removed so that it can be processed directly in C instead of reading it from the file.
#3
One word of warning for those who modify their stock scripts like I do. New Char has a limit of 2048 bytes. Go over this amount and it will not compile.
However, by just deleting all the spaces, comments and new lines (there is no point in adding \n, just delete them!) I was able to get all of mine to fit at exactly 2048. It compiled and rans perfect!
Thanks Cogburn! This is an invaluable resource!
10/06/2004 (2:20 pm)
This isn't a hack, its a kludge! A damn clever kludge! ...I guess that "clever kludge" is a bit redundant but still, this is wonderful! It will help keep the script kiddies out of my code when I publish my game. One word of warning for those who modify their stock scripts like I do. New Char has a limit of 2048 bytes. Go over this amount and it will not compile.
However, by just deleting all the spaces, comments and new lines (there is no point in adding \n, just delete them!) I was able to get all of mine to fit at exactly 2048. It compiled and rans perfect!
Thanks Cogburn! This is an invaluable resource!
#4
11/23/2004 (10:25 am)
Doesn't work here.
#5
02/11/2005 (2:24 pm)
Hmm perhaps someone with a similiar "hack" can post the proper formatting for this?
#6
That IS the proper formatting. As Joe stated above, he is hard coding the script into the character array.
03/04/2005 (7:33 am)
@JaceThat IS the proper formatting. As Joe stated above, he is hard coding the script into the character array.
#7
04/09/2005 (10:08 pm)
....why do people not get this?
#8
05/24/2005 (8:25 pm)
Works like a charm.
#9
Or was it a typo on the instructions?
-Yuzairee
10/08/2005 (11:45 pm)
With version 1.3 that I have, it should be done at runEntryScript function in .\Torque\SDK\engine\game\main.cc Or was it a typo on the instructions?
-Yuzairee
#10
the file is engine\game\main.cc (its no longer in engine\main\game.cc) there isnt even a main directory anymore.
The function to modify is now
bool runEntryScript (int argc, const char **argv)
rest of the changes are pretty much the same.
01/10/2006 (6:39 pm)
Updating for 1.4 (also seems to apply to 1.3)the file is engine\game\main.cc (its no longer in engine\main\game.cc) there isnt even a main directory anymore.
The function to modify is now
bool runEntryScript (int argc, const char **argv)
rest of the changes are pretty much the same.
#11
04/13/2006 (5:47 pm)
#13
If you f.ex. removed the creator/editor code from your distribution, I could just get the TGE demo, copy this dirs over and voila I'm able to modify/copy your code/resources. Not everyone is happy about this. So removing main.cs and using a encrypted package are the first steps to protect your resources.
04/26/2006 (2:48 am)
Main gives you the ability to load any custom script before/after loading the game. By removing main you prevent users from loading custom script. If you f.ex. removed the creator/editor code from your distribution, I could just get the TGE demo, copy this dirs over and voila I'm able to modify/copy your code/resources. Not everyone is happy about this. So removing main.cs and using a encrypted package are the first steps to protect your resources.
#15
UNTESTED:
You should follow the second part of step two, and add your code between lines 253 and 254.
- Tom.
Edit: Just saw the above posts about this. Might as well keep this post anyway. Thanks, - Tom.
05/25/2006 (12:45 pm)
If anyone is wondering, the code for main.cs is now under \engine\game\main.cc. UNTESTED:
You should follow the second part of step two, and add your code between lines 253 and 254.
- Tom.
Edit: Just saw the above posts about this. Might as well keep this post anyway. Thanks, - Tom.
#16
Edit: the caveat is that it doesn't work with zipfiles. :(
06/23/2006 (7:50 am)
This isn't actually necessary with 1.4. You can simply rename the file and/or path and the engine should find it there now. You do need to recompile since that's how you change the filename:FileStream str; // The working filestream. const char* defaultScriptName = "main.cs"; bool useDefaultScript = true;Change the defaultScriptName, and there you go. Very glad they fixed this issue!
Edit: the caveat is that it doesn't work with zipfiles. :(
#17
At the time when Torque needs to load main.cs, there is no resManager or compiler loaded yet. This is why it is clean, and that's also why you experience that it does not work with zip files.
Edit: And to anyone reading:
This resource *is* still useful in 1.4, I think Jason just fails (no offence at all here! but he's not even a SDK owner) to understand what it does.
06/30/2006 (10:37 am)
@Jason:At the time when Torque needs to load main.cs, there is no resManager or compiler loaded yet. This is why it is clean, and that's also why you experience that it does not work with zip files.
Edit: And to anyone reading:
This resource *is* still useful in 1.4, I think Jason just fails (no offence at all here! but he's not even a SDK owner) to understand what it does.
#18
06/10/2007 (12:28 am)
is it nessary to keep the char size equal to file size. We may keep it to a larger one to play safe?
#19
const char* defaultScriptName = "main.cs";
and that didn't work either, and it had the error "Failed to open launcher.cs"
is there anything other than what needs to be changed for 1.4 need to be changed for 1.5?
12/06/2007 (6:21 pm)
it doesn't work for me, and it doesn't say anything about it, in the .log file; so i tried doing the thing with:const char* defaultScriptName = "main.cs";
and that didn't work either, and it had the error "Failed to open launcher.cs"
is there anything other than what needs to be changed for 1.4 need to be changed for 1.5?
#20
12/29/2007 (7:32 am)
Torque Owner Ace