Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Gregjazz on Mon 08/08/2005 07:38:09

Title: Need help with file manipulation
Post by: Gregjazz on Mon 08/08/2005 07:38:09
Basically I need a way to rename a file from AGS. I have the AGS File Plugin, and that has a "FileRename" function, but in its parameters, two "chars" are needed, not strings. So I can't figure out how to get that working.

I'm sure it wouldn't be too difficult or take too long to make a plugin that could rename files... if I can't get this working, would someone be willing to make me that plugin really fast? I would do it myself if I had C++, but I don't. :(
Title: Re: Need help with file manipulation
Post by: Gilbert on Mon 08/08/2005 08:03:12
Hmmm I just downloaded the plugin and read its document:
QuoteFileRename (string oldname, string newname);
Maybe you're using some other version of the plugin?

ALso, maybe it's some issue with the new String handling system in V2.71 beta.
Title: Re: Need help with file manipulation
Post by: Gregjazz on Mon 08/08/2005 08:05:07
Quote from: Gilbot V7000a on Mon 08/08/2005 08:03:12
Hmmm I just downloaded the plugin and read its document:
QuoteFileRename (string oldname, string newname);
Maybe you're using some other version of the plugin?

ALso, maybe it's some issue with the new String handling system in V2.71 beta.

I think you're probably right that it's an older version -- seeing that the one I have doesn't have any documentation. Where can you download it? (I'll look for it, too)
Title: Re: Need help with file manipulation
Post by: Gilbert on Mon 08/08/2005 08:06:24
Got it from Redrum's page (http://www.freewebs.com/skimbleshanks/).
Title: Re: Need help with file manipulation
Post by: Rui 'Trovatore' Pires on Mon 08/08/2005 08:08:16
Heh. Today is the day of revelations. I not only found that my page is indeed a useful place, I also found I'm still refered to as "redruM". Joy.
Title: Re: Need help with file manipulation
Post by: Gregjazz on Mon 08/08/2005 08:10:51
Ah yes indeed it is a newer version!
Title: Re: Need help with file manipulation
Post by: Gilbert on Mon 08/08/2005 08:12:11
Quote from: Rui "Brisby" Pires on Mon 08/08/2005 08:08:16
Heh. Today is the day of revelations. I not only found that my page is indeed a useful place, I also found I'm still refered to as "redruM". Joy.

Hehe, because i can never spell your new nick.
Title: Re: Need help with file manipulation
Post by: Rui 'Trovatore' Pires on Mon 08/08/2005 08:13:12
Brisby? ;)
Title: Re: Need help with file manipulation
Post by: Gregjazz on Mon 08/08/2005 08:16:44
And... I have that same 'char' problem. Looks like the newer version of AGS is incompatible. :(

And this is AGS 2.7, so it doesn't have those new string things...
Title: Re: Need help with file manipulation
Post by: Gilbert on Mon 08/08/2005 09:29:53
I tried it, it doesn't work with V2.62 either.
Since I'm a natural plugin hater, I won't be that adventurous to invest on it further.

A less elegant method is to copy the file to another new file  byte by byte, you can reuse the old file by overwriting it, that doesn't even need a plugin.
Title: Re: Need help with file manipulation
Post by: Gregjazz on Mon 08/08/2005 09:36:04
Quote from: Gilbot V7000a on Mon 08/08/2005 09:29:53
I tried it, it doesn't work with V2.62 either.
Since I'm a natural plugin hater, I won't be that adventurous to invest on it further.

A less elegant method is to copy the file to another new file  byte by byte, you can reuse the old file by overwriting it, that doesn't even need a plugin.

I thought about that, but was reluctant to try. I guess that's my only option since I hope to finish this little project in the next few days and needed something immediate.

I'll give it a shot. Hopefully CJ will implement a FileRename function (file.rename or whatever) and maybe a FileDelete, etc.
Title: Re: Need help with file manipulation
Post by: Gilbert on Mon 08/08/2005 11:24:31
Quote from: Geoffkhan on Mon 08/08/2005 09:36:04
I thought about that, but was reluctant to try. I guess that's my only option since I hope to finish this little project in the next few days and needed something immediate.

Something like below (untested), but it's obviously not efficient, since it's not smart to do a file copy when you just need a file rename anyway:

function FileCopy(string inname, string outname) {
Ã,  File* infile=File.Open(inname, eFileRead);
Ã,  if (infile==null) return 1; //error opening file for reading.
Ã,  File* outfile=File.Open(outname, eFileWrite);
Ã,  if (infile==null) {
Ã,  Ã,  infile.Close();
Ã,  Ã,  return 1; //error opening file for writing.
Ã,  }
Ã,  while (!infile.EOF) outfile.WriteRawChar(infile.ReadRawChar());
Ã,  infile.Close();
Ã,  outfile.Close();
}


Of course, if your file is a bit "large" the function will crash, due to the while loop iteration limit. There're workarounds, but in my opinion it's probably not worth the effort for such a simple task.
Title: Re: Need help with file manipulation
Post by: Gregjazz on Mon 08/08/2005 19:27:46
Quote from: Gilbot V7000a on Mon 08/08/2005 11:24:31
Quote from: Geoffkhan on Mon 08/08/2005 09:36:04
I thought about that, but was reluctant to try. I guess that's my only option since I hope to finish this little project in the next few days and needed something immediate.

Something like below (untested), but it's obviously not efficient, since it's not smart to do a file copy when you just need a file rename anyway:

function FileCopy(string inname, string outname) {
  File* infile=File.Open(inname, eFileRead);
  if (infile==null) return 1; //error opening file for reading.
  File* outfile=File.Open(outname, eFileWrite);
  if (infile==null) {
    infile.Close();
    return 1; //error opening file for writing.
  }
  while (!infile.EOF) outfile.WriteRawChar(infile.ReadRawChar());
  infile.Close();
  outfile.Close();
}


Of course, if your file is a bit "large" the function will crash, due to the while loop iteration limit. There're workarounds, but in my opinion it's probably not worth the effort for such a simple task.

Yeah, the files I'm working with will tend to be over a meg in size. I don't want it to take ages to copy.

Hmmm... it'd be great if I could run a .bat file from AGS... I think I can run an .exe from AGS, so I'll think about that.

I just really need the ability to rename a file... if anyone comes up with a plugin doing that I'd be more than happy to Paypal some money...
Title: Re: Need help with file manipulation
Post by: Gregjazz on Mon 08/08/2005 22:00:21
Okay, I got it working via use of a .bat file. Cool!
Title: Re: Need help with file manipulation
Post by: Pumaman on Mon 08/08/2005 22:09:04
Would anyone else find a FileRename/FileDelete function useful? I think we've discussed it before and decided against it because of the potential to mess around with the player's system.

But functions restricted to the game folder only might do the trick, if they'd be useful?
Title: Re: Need help with file manipulation
Post by: Gregjazz on Mon 08/08/2005 22:24:43
Really I would only need those functions to work in the game folder.
Title: Re: Need help with file manipulation
Post by: RickJ on Tue 09/08/2005 21:19:25
I would find them very useful as well.     
Title: Re: Need help with file manipulation
Post by: Dorcan on Tue 09/08/2005 23:48:11
Yes, definately, for the game folder AND its subfolders. In fact, it would be quite useful to have all the file functions working in subfolders as well (I'm thinking about the eFileWrite mode I would have used in this case, but had to create a plugin which allowed me to create/move a file into the "/profile" folder)
Title: Re: Need help with file manipulation
Post by: Gregjazz on Wed 10/08/2005 00:15:49
Quote from: Dorcan on Tue 09/08/2005 23:48:11
Yes, definately, for the game folder AND its subfolders. In fact, it would be quite useful to have all the file functions working in subfolders as well (I'm thinking about the eFileWrite mode I would have used in this case, but had to create a plugin which allowed me to create/move a file into the "/profile" folder)

Yeah, support for files in subfolders would be great too.
Title: Re: Need help with file manipulation
Post by: GarageGothic on Wed 10/08/2005 15:57:46
Yes, I've been wishing for these functions for a while now (I thought the file plugin might do the trick, but I'd rather do without plugins). Remember CJ, if you wanted to mess up the player's files, you could just RawWrite to them to corrupt them anyway, so as long as it's restricted to the game directory and subdirectories, there should be no problem.
Title: Re: Need help with file manipulation
Post by: Pumaman on Wed 10/08/2005 18:51:33
Restricting to subdirectories is actually more tricky because there are various sneaky tricks that people can use to get around path checking. I'm sure you've heard of security holes in software like IIS where you can specify the path as "subdir\..\..\..\windows\system32\driver.sys" and so forth.

The present method of simply disallowing / and \ characters in the path is the safest way of doing things.

Anyway, since there's support for FileRename and FileDelete I'll certainly consider them for a future version.
Title: Re: Need help with file manipulation
Post by: strazer on Wed 10/08/2005 20:42:57
Tracker'd:
http://www.adventuregamestudio.co.uk/tracker.php?action=detail&id=381
http://www.adventuregamestudio.co.uk/tracker.php?action=detail&id=525
Title: Re: Need help with file manipulation
Post by: GarageGothic on Thu 11/08/2005 02:48:03
Quote from: Pumaman on Wed 10/08/2005 18:51:33
Restricting to subdirectories is actually more tricky because there are various sneaky tricks that people can use to get around path checking. I'm sure you've heard of security holes in software like IIS where you can specify the path as "subdir\..\..\..\windows\system32\driver.sys" and so forth.The present method of simply disallowing / and \ characters in the path is the safest way of doing things.

Why not just disallow multiple periods? I see little need for more full stops in a file name than the one preceeding the file extension.
Title: Re: Need help with file manipulation
Post by: Gilbert on Thu 11/08/2005 12:56:16
Still, that doesn't help if you do something like:
FileDelete("\aut0exec.bat");
Title: Re: Need help with file manipulation
Post by: GarageGothic on Thu 11/08/2005 14:19:57
Would that first slash take you to the root directory? I didn't know that. But wouldn't banning the first character from being a backslash guard against that? Or do you mean that autoexec.bat is in the path so it's accessible from anywhere?
Title: Re: Need help with file manipulation
Post by: Snarky on Thu 11/08/2005 15:04:45
Couldn't you take the path and the filename as two separate parameters? That way you could check to see which directory the path resolved to, and then only perform the action if it is the game directory or a subdirectory. (Of course, you'd have to make sure there's no path fragment in the filename, by simply disallowing \ and / in that string.)
Title: Re: Need help with file manipulation
Post by: Gilbert on Fri 12/08/2005 01:58:08
I think one reasonable implication is:

FileDelete(String name, String path="");

where / \ : and . are not allowed in both parameters (should generate a warning and crash out).

However, I doubt if it's really that important to go through all these problems.