Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: Darky on August 13, 2010, 12:06:51 AM

Title: How exactly does Copy File work?
Post by: Darky on August 13, 2010, 12:06:51 AM
Hi again,

I decided to put some unlockable extras into my game and now I'm not sure how I can access and copy those specific segments to a folder of my choice. I read that the safest bet is to use the savegame folder because it's supposed to be always writable. That's fine with me. So here is the code I tried as a test:

Code: WME Script
  1. var Bonus = new File("extras/test.png");
  2. Bonus.Close();
  3.  

And that did not work. Then I tried to specifiy a filename as well with
Code: WME Script
  1. Bonus.Copy(Game.SaveDirectory + "\Extra01_test.png");
  2.  

But it did not work either. I also tried to see what it returns by putting it into Game.Msg(); and it always returned false.

Where do I do wrong?

Thanks in advance
Title: Re: How exactly does Copy File work?
Post by: metamorphium on August 13, 2010, 07:59:12 AM
Hi.

first of all I am not sure if this copy-way of doing extra is a good approach, I'd hide unlockables to package but it's just my choice.

As this code worked for me:
Code: WME Script
  1. var file = new File("testfile.bin");
  2. file.Copy(Game.SaveDirectory + "/testfile.bin");
  3.  

check the following:

1. in your copy command revert \ to /
2. Double check the saved game path actually exists on your drive (it's created only after the first savegame has been created)
3. Triple check the file is not already in the target path. If so, you have to specify optional parameter overwrite.
Title: Re: How exactly does Copy File work?
Post by: Darky on August 13, 2010, 02:02:35 PM
Hi meta, thanks for the reply.

I'm not sure I understood you right about the hide unlockables to package. Did you mean you'd create a package exclusively to them?

Anyway. I don't have a .bin to test it with but I tried it with various other files. It didn't work.
I went through the check list you gave me as well, I can confirm I checked all of that and it's in proper order.
Title: Re: How exactly does Copy File work?
Post by: metamorphium on August 13, 2010, 04:03:02 PM
Darky,

I tried with many files and it always works. where exactly is your extras folder located? Try copying the file to your root (eg where wpr file is located) and start from there.
Title: Re: How exactly does Copy File work?
Post by: Darky on August 13, 2010, 05:37:51 PM
Hi meta,

I tried your suggestion with interesting results:
Copying file from root (where .wpr is) to any other location: Works
Copying file from inside a regular folder (no package) to somewhere else: Works
Copying file from inside package to somewhere else (root, savegame folder, etc): Does not work!

The "extras" folder is located in /data which is pretty much the original WME package. It seems only to copy when the file is in a non-package.

Why doesn't it work from inside other packages?
Title: Re: How exactly does Copy File work?
Post by: odnorf on August 13, 2010, 06:10:43 PM
That's by design. Dot.Copy(); doesn't extract files from within packages.
Title: Re: How exactly does Copy File work?
Post by: metamorphium on August 13, 2010, 06:20:42 PM
Exactly. Only reading is supported from files stored in your game packages (DCP files). (as documentation states)
Title: Re: How exactly does Copy File work?
Post by: Darky on August 13, 2010, 06:31:38 PM
Although I read that line in the doc's too, I didn't make the connection (it didn't say "only", just "also"). That's what you get for making assumptions...  ::slug

What's the best way to put the unlockables in without the Users directly seeing them?
Title: Re: How exactly does Copy File work?
Post by: Jyujinkai on August 13, 2010, 06:40:20 PM
this entire method seams flawed. What exactly is the nature of the unlockables... there are a number of ways depending on what you are looking for to do this in a much better way.
Title: Re: How exactly does Copy File work?
Post by: odnorf on August 13, 2010, 06:43:42 PM
What's the best way to put the unlockables in without the Users directly seeing them?

Just put the data in the data files and use a variable to check whether a condition is met to use them. Forget what I said. I misunderstood. You probably mean something like Larry6 wallpapers added in gamedir when doing certain actions.
Title: Re: How exactly does Copy File work?
Post by: Darky on August 13, 2010, 06:57:29 PM
Yes, like Larry ;) Mostly various Wallpapers for your Desktop. Otherwise I'd have probably made a in-game gallery of bonus content like some other games do :)
Title: Re: How exactly does Copy File work?
Post by: metamorphium on August 14, 2010, 01:12:29 AM
How about doing something like this (UNTESTED!):

Code: WME Script
  1.  
  2. var file1 = new File("your file in dcp");
  3. file1.OpenAsBinary(1);
  4.  
  5. var file2 = new File("your targetfile");
  6. file2.OpenAsBinary(2);
  7.  
  8. var b = 0;
  9. while ((b = file1.ReadByte())!=null) //it's 2AM and I'm not sure this construction works in WME, so I'm just giving you an idea here :)
  10. {
  11.    file2.WriteByte(b);
  12. }
  13.  
  14. file1.Close();
  15. file2.Close();
  16.  
Title: Re: How exactly does Copy File work?
Post by: Darky on August 15, 2010, 05:16:44 AM
Hi meta, thanks for the pointer. It looks good in your post but the condition for the loop doesn't seem to work with WME (it crashes). I first tried it as is as except I added a Sleep to the end of the loop.

After that I also tried some other variants of the loop I could think of but it didn't really work. At one point I got it as far as copying bytes to the target file but it wouldn't stop and repeat itself even though I specified that it should stop when file1.ReadByte() returns null. I'd post the code but I can't access it right now, I probably do that later.

Anyway just wanted to let you know about the result. Thanks everyone so far for the great help with this :)
Title: Re: How exactly does Copy File work?
Post by: Mnemonic on August 15, 2010, 09:00:40 AM
The file object has a Length property, so you can simply do a loop from 0 to Length. But I think it will be slow.
Title: Re: How exactly does Copy File work?
Post by: Darky on August 15, 2010, 03:01:31 PM
Mnemonic, it works but really is too slow for productive use. The duration to finish can take a few seconds if its just a few kb up to over 10 minutes depending on the file size. It's a good method for small textfiles or so but not for anything bigger like Wallpapers.

I used meta's method with this loop:
Code: WME Script
  1. Game.LOG("Starting File Copy...");
  2. var b = 0;
  3. while(b < file1.Length && b != null)
  4. {
  5.     b = file1.ReadByte();
  6.     file2.WriteByte(b);
  7.     Sleep(1); // i had it on 10 before, but I also tested it on 1 for a faster speed
  8. }
  9. Game.LOG("Finished File Copy!");
  10.  
Title: Re: How exactly does Copy File work?
Post by: Darky on August 15, 2010, 03:26:26 PM
Follow-up: I just reverted back to the original solution now with what I learned thanks to you guys. I'll store the bonus in a folder instead of a package and name the files different, just like Larry 7 did. Of course any player could tag along and just rename the files on his own (people did it with Larry) but it just seems the easiest and fastest way at the moment.

Thank you all for the help without it I probably still wouldn't have known that I can't extract files from the package for copy  ;D

If you come up with any other ideas let me know, I'd be glad to test it. Otherwise I'll stick with this solution.