File System directory issues

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

File System directory issues

Post by Swarmer »

I am working on my game from different computers occasionally. How can I change my working directory to something relative to my project folder, not the whole computer?

For example, in Computer A my working directory is:
"C:\ComputerAFiles\MyProject\"
In Computer B, my working directory is:
"D:\BFiles\Stuff\MyProject\"

How can I detect and set my directory to the relative path of "\MyProject\"? Do I have to manually parse the getWorkingDirectory string to find out the last folder?

Remember that the starting directory isn't always the same: if I open one of my scenes (custom file format), open the .exe directly, or run it from the VS2008 debugger, I get a different starting working directory.

Also, how exactly does getFileDir work? Do I just input the name of any file and it should find it and tell me its full path? That doesn't seem realistic. When I tried it, it just outputted garbage.
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

well by default irrlicht searches in the same folder the .exe was launched from... if you for example place all your models textures and sounds in:

bla/bla/myProject/media

and your .exe is in:

bla/bla/myProject/bin or
bla/bla/myProject/release or
bla/bla/myProject/debug

to get to your files you would use a RELATIVE path to access them like so:

scene::IAnimatedMesh* myMesh = smgr->getMesh("../media/myMesh.x");

where the two dots and the slash (../) is what your looking for... hopefully...
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

Actually, the only time the working directory is equal to the folder of the .exe file is when I open the .exe file directly (through Windows Explorer).

I'm making a map editor, and when I try to directly open a map file (through Windows Explorer) with my program, the working directory becomes the directory of the map file, not my program's .exe file's directory. Likewise, when I run the VS2008 debugger, the working directory becomes my .vcproj file's directory, not my .exe file's directory.

Is there a way to consistently find the same relative directory?
Nox
Posts: 304
Joined: Wed Jan 14, 2009 6:23 pm

Post by Nox »

VS starts the .exe from your $(SolutionDir) which is set to the .vproj dir and saves the exe in the $(SolutionDir)$(ConfigurationName). You can change this in the projects settings, if you want to.
If you open a file using the OpenFileDialog, the working dir is changed by Irrlicht (a bit dirty), so maybe you have to set it to the old dir.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

the problem he is having is this:
he has assosiated a map/mesh or what ever extension with his program...
let's say the program is "C:\programs\myProg\theProg.exe" and the map/mesh is "D:\map\data\theMap.3ds"...
now, if he double klicks the map/mesh then the program will start, but the working directory is the one from the map/mesh and not the one from the program !!! ;)

EDIT: the solution could be to use the arguments passed to function main:

Code: Select all

int main(int argc, char** argv){

    // argv[0] holds the name of the exe incl. path
    printf("%s\n",argv[0]);
    
    // argv[1] holds the name of the assosiated file incl. path
    if(argc > 1) printf("%s\n",argv[0]);

    system("pause");
    return 0;
}
Edit 2:

Code: Select all

    // and getFileDir() can be used to get the directory
    stringc theDir = FileSystem->getFileDir(stringc(argv[0]));
Last edited by Acki on Sun Jan 18, 2009 8:08 pm, edited 2 times in total.
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

Acki wrote:the problem he is having is this:
he has assosiated a map/mesh or what ever extension with his program...
let's say the program is "C:\programs\myProg\theProg.exe" and the map/mesh is "D:\map\data\theMap.3ds"...
now, if he double klicks the map/mesh then the program will start, but the working directory is the one from the map/mesh and not the one from the program !!! ;)
Exactly!
The only way I can get it to work consistently is if I just set the working directory to "C:\programs\etc", but using absolute paths like that is bad, since it'll fail if I change folder names or if I switch computers (which I do).

I am using a winforms gui, and if I find the directory with the .NET shell using this code:

Code: Select all

#undef GetCurrentDirectory
System::IO::Directory::GetCurrentDirectory();
I get the same directory that Irrlicht finds. I guess I could just do it manually and have a "Set Directory" option, but is that the only way?
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

Acki wrote:EDIT: the solution could be to use the arguments passed to function main:

Code: Select all

int main(int argc, char** argv){

    // argv[0] holds the name of the exe incl. path
    printf("%s\n",argv[0]);

    // argv[1] holds the name of the assosiated file incl. path
    if(argc > 1) printf("%s\n",argv[0]);

    system("pause");
    return 0;
}
That's a good idea, I'll try that out.
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

Hm it doesn't work. The args array is empty unless I open a file. If I open a file, the only argument is the path of the file that I am opening, not the .EXE.

I am using CLR, if that makes a difference:

Code: Select all

int main(cli::array<System::String ^> ^args)
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

realy ??? :shock:
hmm, do you realy need the function this way,
or can you not just simply change it to the other format ??? ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

I have to, that's the only valid style of entry point for a .NET program.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

unfortunately I never used .net, so I don't know about this... :cry:
but I can't imagine that this is the only valid style to use... :shock:
but I don't know, though it could be... ;)
sorry, I can't help you further then... :cry:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

That's okay, I might just make a "Set working directory" option that the user has to manually set.

Unless anyone has a better solution.
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

do it windows style...
get nsis (nullsoft scriptable install system) and write a small installer which sets a registry value with the path to the .exe, and retrieve that path with .net code from inside your application.
Probably the most complicated way but probably also the most common.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

zillion42 wrote:get nsis (nullsoft scriptable install system) and write a small installer which sets a registry value with the path to the .exe, and retrieve that path with .net code from inside your application.
yes, not a bad idea, if all others fail !!! :lol:
I think this is possible with all (good) setup tools (e.g. InnoSetup)...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Swarmer
Posts: 100
Joined: Mon Apr 16, 2007 7:23 am

Post by Swarmer »

Actually I just found out a pretty weird but simple way to do it:

Code: Select all

	char path[MAX_PATH + 1];
    GetModuleFileNameA( NULL, path, MAX_PATH + 1 );
"path" gives the full path of the exe file.
Thanks for the suggestions.
Post Reply