You are here

NSIS help (redirect Appdata)

22 posts / 0 new
Last post
Simeon
Simeon's picture
Offline
Last seen: 9 years 4 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
NSIS help (redirect Appdata)

I am currently working on WAtomic. It saves its settings to C:\Dokumente und Einstellungen\username\Anwendungsdaten\WAtomic which I think can be correctly replaced by %APPDATA%/WAtomic cause although I dont know anything about programming, I found one sourcefile that says:

unit sh_fun;
{
------------------------------------------------------------------
WATOMIC - Windows(c) KAtomic clone

Copyright (C) 2005  Federico Blaseotto (ironfede)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
------------------------------------------------------------------
}

interface
  //This function prepares the execution environment
  //of WAtomic storing ini filenames and getting the
  //correct Application Directory path in order
  //to allow WAtomic running in multiuser mode (e.g w2k-ntfs)
  function InitEnv:boolean;

var
  HS_INI_FILENAME:string;
  MAIN_INI_FILENAME:string;

implementation

uses shellAPI,shlObj,ActiveX,Windows,Forms,SysUtils;

//------------------------------------------------------------------------------
//It's a caller job cleaning the allocator
procedure FreePidl(pidl: PItemIDList);
var
  allocator: IMalloc;
  
begin
  if Succeeded(SHGetMalloc(allocator)) then
  begin
    allocator.Free(pidl);
{$IFDEF VER90}
    allocator.Release;
{$ENDIF}
  end;
end;

//------------------------------------------------------------------------------
//Get the application data directory to write high scores
function GetAppDataPath(var sPath: string): Boolean;
var
  pidl: PItemIDList;
  buf: array[0..MAX_PATH] of char;

begin
  result := false;

  if Succeeded(ShGetSpecialFolderLocation(
    Application.Handle,
    CSIDL_APPDATA,
    pidl
    )) then
  begin
    if ShGetPathfromIDList(pidl, buf) then
    begin
      result := true;
      sPath := string(buf);
    end;
    FreePidl(pidl);
  end;
end;


//------------------------------------------------------------------------------
function InitEnv:boolean;
var
  AppPath:string;
begin
  try
    if not GetAppDataPath(AppPath) then
    begin
      Result:=FALSE;
      Exit;
    end;

    if not DirectoryExists(AppPath+'\WAtomic') then
      Mkdir(AppPath+'\WAtomic');

    MAIN_INI_FILENAME :=AppPath + '\WAtomic\WAtomic.ini';
    HS_INI_FILENAME :=AppPath + '\WAtomic\hs.ini';

    Result:=TRUE;
  except
    Result:=FALSE;
  end;
end;

//--------------------------------
// ironfede@users.sourceforge.net
//--------------------------------

end.

ehich I interpreted as %APPPDATA%/WAtomic.

But adding
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("APPDATA", "$SETTINGSDIRECTORY").r0'
to the launcher seems to have no effect.

I tried adding
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("USERPROFILE", "$SETTINGSDIRECTORY").r0'

but that resulted in the settings being saved into \WAtomicPortable\Data\settings\Anwendungsdaten\WAtomic

Can anyone help me?

denash
Offline
Last seen: 14 years 5 months ago
Joined: 2007-04-08 12:14
http://portableapps.com/node/

https://portableapps.com/node/12739

I think that MIGHT help....

If you don't understand it, just give me the application name, and the setting it creates, and I' try to make it for you Wink (and to explain it step by step ;))

Nathan9222
Nathan9222's picture
Offline
Last seen: 2 years 5 months ago
Developer
Joined: 2007-12-06 22:35
Just do this....

Put this before it launches

Rename "$SETTINGSDIRECTORY\APPNAME" "$APPDATA\APPNAME"

Then put this after it exits

Rename "$APPDATA\APPNAME" "$SETTINGSDIRECTORY\APPNAME"

You might want to do a

IfFileExists "$APPDATA\APPNAME\*.*" Backup

Do this so if the user already has that folder, then you can tell it to rename it to like appname_Backup then at the end change it back to the original name. If you need more help on the backing up part, check out Ares Portable I released a while back.
Remember to change the above APPNAME to WAtomic

An eye for an eye makes the whole world blind.
Mahatma Gandhi,
Indian political and spiritual leader (1869 - 1948)

Simeon
Simeon's picture
Offline
Last seen: 9 years 4 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
Thanks

I want to do it by redirecting the Appdata variable but it doesnt seem to respond to it (although the source file suggests it afaict).
The App in question is WAtomic.

Of course I could do it by copying files but redirecting the variable is cleaner and faster.

If you could figure out how to do it would be great.

"What about Love?" - "Overrated. Biochemically no different than eating large quantities of chocolate." - Al Pacino in The Devils Advocate

OliverK
OliverK's picture
Offline
Last seen: 2 years 8 months ago
Developer
Joined: 2007-03-27 15:21
Soduku redirects. I think

Soduku redirects. I think its doing what it should-isn't it?

Too many lonely hearts in the real world
Too many bridges you can burn
Too many tables you can't turn
Don't wanna live my life in the real world

denash
Offline
Last seen: 14 years 5 months ago
Joined: 2007-04-08 12:14
I think Soduku creates the

I think Soduku creates the settings, then copies the setting to the "data" folder on your USB and then deletes the settings, it created on the system.

You have to be very good with NSIS if you want the files to be redirected straight to your USB instead of being saved on the computer first...

Nathan9222
Nathan9222's picture
Offline
Last seen: 2 years 5 months ago
Developer
Joined: 2007-12-06 22:35
I wish....

I wish I was that good, but I still consider myself a begginner in Nsis, I just mostly know the basics. I plan on learning more about it this summer. BTW does anyone know how to do that, where you can make it just save to the usb instead of moving files. I think im gonna start to look into scripts that might do this

An eye for an eye makes the whole world blind.
Mahatma Gandhi,
Indian political and spiritual leader (1869 - 1948)

wraithdu
Offline
Last seen: 10 years 8 months ago
Developer
Joined: 2007-06-27 20:22
The problem is that this

The problem is that this program actually gets the APPDATA path the correct way by calling the SHGetSpecialFolderLocation function (also correct to use SHGetFolderPath). The redirection that many PApps do, works by modifying the PATH environment variables. I don't believe this has any effect on the above function, which is why your redirection fails. (I'm going to test this to be sure.)

You're probably out of luck with the PATH modify method, so you'll have to manually copy files.

Simeon
Simeon's picture
Offline
Last seen: 9 years 4 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
Thanks

for your clear answer. Then I'll have to take the "copy files" way.

"What about Love?" - "Overrated. Biochemically no different than eating large quantities of chocolate." - Al Pacino in The Devils Advocate

wraithdu
Offline
Last seen: 10 years 8 months ago
Developer
Joined: 2007-06-27 20:22
I tested and confirmed. The

I tested and confirmed. The currently correct function for this is SHGetFolderPath (SHGetSpecialFolderLocation is deprecated, but the results are the same).

Another option, if you really wanna go there, is to recompile the source to use environment variables to get the path. Then you could do your redirection.

Simeon
Simeon's picture
Offline
Last seen: 9 years 4 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
nope

I dont know anything about compiling/programming and dont want to do something wrong.

"What about Love?" - "Overrated. Biochemically no different than eating large quantities of chocolate." - Al Pacino in The Devils Advocate

wraithdu
Offline
Last seen: 10 years 8 months ago
Developer
Joined: 2007-06-27 20:22
I did a little more testing,

I did a little more testing, with some mixed results. After re-reading your first post, you might wanna just stick with redirecting USERPROFILE. It seems that SHGetSpecialFolderLocation builds the return for CSIDL_APPDATA from the USERPROFILE environment variable. So while you can't change the 'Application Data\WAtomic' portion of the path, you can at least change the portion before that (the USERPROFILE portion).

I tried this with the SHGetFolderPath function, and it fails (returns blank) if the target folder doesn't exist (ie you changed the USERPROFILE env var before calling the function), or you don't instruct the function to create the folder (flag CSIDL_FLAG_CREATE). It seems the SHGetSpecialFolderLocation function behaves the same way (but there's no flag to force the folder creation).

Sorry if that's a bit of a read, but several devs have run into this roadblock, so it bears understanding.

Simeon
Simeon's picture
Offline
Last seen: 9 years 4 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
?

I'm confused.
So if I create the folder first it does work with redirecting the Appdata variable?
Or only with userprofile?

I know that redirecting the userprofile thing works but (as mentioned in the first post) it creates \WAtomicPortable\Data\settings\Anwendungsdaten\WAtomic which is the german version of \WAtomicPortable\Data\settings\Application Data\WAtomic. So if I have the german version I guess it wont work on a foreign machine...?
Thats why I am reluctant to reidrecting userprofile and would rather copy the files instead.

"What about Love?" - "Overrated. Biochemically no different than eating large quantities of chocolate." - Al Pacino in The Devils Advocate

haustin
Offline
Last seen: 12 years 6 months ago
Joined: 2007-09-19 17:59
Re:

So if I create the folder first it does work with redirecting the Appdata variable?
Or only with userprofile?

He's saying it'll only work with redirecting USERPROFILE, because SHGetSpecialFolderLocation() ignores the APPDATA environment variable.

So if I have the german version I guess it wont work on a foreign machine...?

Don't know the answer to this one, but if your guess is correct (which is my guess), you could actually Rename the "Application Data" portion of the path as necessary prior to use. Basically,

${GetFileName} $AppData $R0

would leave $R0 with the current system's name for that directory. If you saved the last-used directory name in an INI file (like FFP and TBP do with last drive letter), you'd be able to rename the "Application Data" portion as needed to make the local system find it. It would require a bit more logic than copying, but it would be more fault-tolerant and likely much faster.

Thats why I am reluctant to reidrecting userprofile and would rather copy the files instead.

Yeah, that's easiest to do. But when someone perfects the redirect method it'll work better for everyone. Smile

-hea

digitxp
digitxp's picture
Offline
Last seen: 12 years 5 months ago
Joined: 2007-11-03 18:33
I don't like the FFP replace

it replaced C:\Documents and Settings\all\Desktop to E:\.....
I think that FFP should account for that, running on the PC.

Insert original signature here with Greasemonkey Script.

wraithdu
Offline
Last seen: 10 years 8 months ago
Developer
Joined: 2007-06-27 20:22
Good summary, and good point

Good summary, and good point about the language issue. I that case it would be safer to copy files before launch.

My point before about mentioning the non-existent folders was that if an app is coded without a backup method to SHGet***Path and a user redirects the USERPROFILE var to a non-existent directory, then that app *should* fail or do something very strange since it can't find the APPDATA location (blank path).

Nathan9222
Nathan9222's picture
Offline
Last seen: 2 years 5 months ago
Developer
Joined: 2007-12-06 22:35
Would it be possible to.....

Would it be possible to add a variable for the folder, like...

StrCpy $R0 "$APPDATA\WAtomic"
StrCpy $R1 "$SETTINGSDIRECTORY"
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("R0", "R1").r0'

Or is there a way to intercept what variable the program called. or adding the variable u want to the system, and then removing it when the app closes.

An eye for an eye makes the whole world blind.
Mahatma Gandhi,
Indian political and spiritual leader (1869 - 1948)

wraithdu
Offline
Last seen: 10 years 8 months ago
Developer
Joined: 2007-06-27 20:22
1. R0 in your above example

1. R0 in your above example is the name of the env variable, so it cannot be a path. It would have to be something like -

StrCpy $R0 "APPDATA"
StrCpy $R1 "$SETTINGSDIRECTORY"
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("$R0", "$R1").r0'

2. You can create whatever named env vars you want and set the data to whatever you want.

3. Unless you change the source of an app, you cannot control where it tries to retrieve its data from.

4. With the method you are using, the env vars are only temporary and only "seen" by your launcher and its children. So if you launcher starts app "b" and "b" starts app "c", then only the launcher, "b", and "c" can see your new vars.

Simeon
Simeon's picture
Offline
Last seen: 9 years 4 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
OK

Thanks everyone, but especially Wraithdu, for their comments.
Now I know a little more about environment variables and I know that will copy the files around as I don't want to change the source code.

"What about Love?" - "Overrated. Biochemically no different than eating large quantities of chocolate." - Al Pacino in The Devils Advocate

ironfede
Offline
Last seen: 15 years 2 days ago
Joined: 2008-06-16 10:21
WAtomic original development

Hi everybody !

Thank You for interesting in WAtomic.

I'm the original developer of the puzzle game so if anyone would need some advice on the original Delphi source code please don't hesitate to send me a message at

ironfede AT users DOT sourceforge DOT net

I have not a lot of spare time at the moment so i can't warrant ultra-rapid answers Wink sorry !

Best Regards,
Federico Blaseotto

P.S.
Sorry for my bad English, my native language is Italian.

Simeon
Simeon's picture
Offline
Last seen: 9 years 4 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
Hi

Welcome here Smile

I think its always cool to have the original Developpers here in the forums.

"What about Love?" - "Overrated. Biochemically no different than eating large quantities of chocolate." - Al Pacino in The Devils Advocate

digitxp
digitxp's picture
Offline
Last seen: 12 years 5 months ago
Joined: 2007-11-03 18:33
Like Lamer.

It makes it way easier to work on portability issues :D:D.

Insert original signature here with Greasemonkey Script.

Log in or register to post comments