Creating a Directory

Posted By: Dooley

Creating a Directory - 02/19/19 19:11

My question has two parts: First, I am able to create a directory in my game's folder using a kind of hack:

Quote:
str_cpy(save_dir,level_string);

compatibility = 8;

galaxy_write = file_open_write("Save_Test.txt");

file_str_write(galaxy_write,"SAVE TEST");//creates a file in order to actually create the folder
file_str_write(galaxy_write,",");

file_close(galaxy_write);

compatibility = 10;//resumes normal compatibility


However, this seems to create other problems in my game.

Is there a way to do it without changing the compatibility?

Second, I can't seem to create a directory in the "ProgramData" folder. I can write a file into an existing folder, but I can't create a new one, with or without the compatibility hack.

I'm thinking I would need to use the windows.h API thing, but I am unsure how to do that.
Posted By: Dooley

Re: Creating a Directory - 02/19/19 19:22

Ah ... got it!

Code:
CreateDirectory("C:\ProgramData\FolderName",0.000);



(it's not showing double backslash for some reason )

Fairly self explanatory, it requires windows.h to be included of course. You would think something like this would be in the manual.

Is there a separate manual somewhere that describes the Windows API?
Posted By: txesmi

Re: Creating a Directory - 02/19/19 19:43

https://docs.microsoft.com/en-us/windows/desktop/api/index

Check SHGetFolderPath too. Sorry, I read it is mostly deprecated. Check SHGetKnownFolderPath instead.

Salud!
Posted By: Dooley

Re: Creating a Directory - 02/19/19 22:06

What is deprecated? The one I used or the one you are suggesting?
Posted By: txesmi

Re: Creating a Directory - 02/19/19 22:49

The one I sugested first (and striked out)
Posted By: Emre

Re: Creating a Directory - 02/21/19 22:04

Txesmi is right. But SHGetKnownFolderPath is seems a bit tricky to integrate.

I used SHGetFolderPath years ago. it no needs dll. but i'm unable to use SHGetKnownFolderPath without dll.

So, if you will follow this path, here is example for you:

plugin:
Code:
#include <wchar.h>
#include <KnownFolders.h>
#include <shlobj.h>
#include <comdef.h> 

wchar_t* path = new wchar_t[128];
DLLFUNC void  SHGetKnownFolderPathProgramData(char* str)
{
	
	SHGetKnownFolderPath(FOLDERID_ProgramData, 0, NULL, &path);
	_bstr_t b(path);
	const char* c = b;

	strcpy((char*)str, (char*)c);

}



lite-c

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <windows.h>



char* savedir_str="";
void SHGetKnownFolderPathProgramData(str);
void on_1_event()
{	
	SHGetKnownFolderPathProgramData(savedir_str);
	printf(_chr(savedir_str));
}

void main()
{
	fps_max = 60;
	level_load(NULL);

}



i'm not sure my codes are totally correct. i'm just showing the way.

And here is the sample with dll. There are two function in it. FOLDERID_LocalAppData and FOLDERID_ProgramData.

Good luck!

edit: btw, it's not for create directory, it's for get the correct directory on every pc.
Posted By: Dooley

Re: Creating a Directory - 02/22/19 05:46

Thanks!

The one I used seems to work fine. I was able to enhance it even more by using the "_chr()" function, so I can create the name of the folder in-game...

Code:
CreateDirectory(_chr(str_directory_name),0.000);



Previously, I was creating the folder right in the game folder, which is fine, until people start installing it on newer Windows machines. Then it seems to cause trouble.
Posted By: txesmi

Re: Creating a Directory - 02/22/19 09:15

The point is that you need to ask to windows for the ProgramData folder path because it can change on different windows versions, especially if you want to give support for XP and Vista. It also happens that a user can install windows on any drive on his system, so it can easily not be C:. Hardcode a path out of your game installation folder is not a good practice and, as you experienced, ProgramFiles folder is locked for writting on newest systems.
Posted By: Dooley

Re: Creating a Directory - 03/09/19 05:18

Just thinking about the best way to deal with it. I think having a text file with the file directory would be a good way to do it. Then the user could edit it if their system is set up differently, or they want a custom location.

It would be nice to be able to handle that in-game, of course...
Posted By: WretchedSid

Re: Creating a Directory - 03/09/19 18:57

Just ask the OS where the paths are! SHGetKnownFolderPath is your friend, like txsemi has already said before!

Also, the last parameter to CreateDirectory is a pointer. Don't pass a float in there, just pass NULL.
Posted By: Emre

Re: Creating a Directory - 03/09/19 21:34

Yes. And it's not big deal. i already gave the sample. Here is new one:

Dll:

Code:
#include <wchar.h>
#include <KnownFolders.h>
#include <shlobj.h>
#include <comdef.h> 

wchar_t* path = new wchar_t[256];
DLLFUNC void  SHGetKnownFolderliteC(int id, char* str)
{
	
	if (id == 1)
	{
		SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &path);
	}
	else if (id == 2)
	{
		SHGetKnownFolderPath(FOLDERID_PublicDesktop, 0, NULL, &path);
	}
	else if (id == 3)
	{
		SHGetKnownFolderPath(FOLDERID_ProgramData, 0, NULL, &path);
	}
	else if (id == 4)
	{
		SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &path);
	}
	else 
	{
		SHGetKnownFolderPath(FOLDERID_Fonts, 0, NULL, &path);
	}
	

	if (path != NULL)
	{
		_bstr_t b(path);	strcpy_s((char*)str, 256, (char*)b);
	}
	


}



Lite-c:

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <windows.h>

//needed windows.h
char* savedir_str="";
void SHGetKnownFolderliteC(int id,char* str);
#define FOLDER_LocalAppData 1
#define FOLDER_PublicDesktop 2
#define FOLDER_ProgramData 3
#define FOLDER_RoamingAppData 4 


void main()
{
	SHGetKnownFolderliteC(FOLDER_LocalAppData,savedir_str);
	printf(savedir_str);
	sys_exit("");
}



ask someone skilled if there is something wrong with the sample. if it's okay, then use it.

i don't know what else you want.
Posted By: Dooley

Re: Creating a Directory - 06/07/19 01:35

Follow up question ...
I am seeing that if I run the game on one account, it will create the folder as instructed, now problems.

However, if I log onto the same computer under a different user name, and run the game, it will not have access to the same folder, and it will get an "invalid pointer" error when trying to start a new game (and thereby write files into that folder).

Is there a way to create the folder to make it public, rather than private to the account that made it?
Posted By: Dooley

Re: Creating a Directory - 06/07/19 01:38

UPDATE:
Yes, it seems to work perfectly! You guys are great, you answered my questions before they even occurred to me. Thanks for being patient laugh

@Emre,
I just saw your sample, and I think it might solve my new problem.

By placing the file into "FOLDER_LocalAppData" instead of "FOLDER_ProgramData" I'm thinking that would create a new file location for each user, and there would be no problems for users with different accounts.

Thanks again!

Posted By: Emre

Re: Creating a Directory - 06/08/19 19:15

You are welcome. Yet you may still face with a problem. Some people use unicode characters in their username. I suggest you to take a look at this post.

Though that post about SHGetFolderPath, maybe you may encounter the same problem with SHGetKnownFolderPath. I am not sure. Just an advice.
Posted By: Dooley

Re: Creating a Directory - 06/08/19 22:21

Okay, I will have to put in some kind of backup plan for that situation I guess.
© 2024 lite-C Forums