Download and Save Binary Files

I’m currently stuck with the patcher for my game. This game isn’t big and will be mostly updated for friends. I was wondering if I could get some help. I am getting the following error:

System.ArgumentException: Name has invalid chars
at System.IO.FileStream…ctor

I have the diff file on google that it reads and parses through. Then it will download the file from my raw files(also on google drive) and save it to the users local directory. For testing purposes I have made this directory C:\games. I am downloading and saving binary files so they don’t have a file extension like .exe or .txt.
Here is my Code:

This checks if there is an update needed:

function checkForUpdates(){	//for the patcher. Check the version first. 
	buildVersion = "1.0.0";
	updating = true;
	showUpdateButton = false;
	updateMsg = "

Checking For Updates…"; //GUI update for user
yield WaitForSeconds(1.0f); //make it visible
var url = “https://googledrive.com/host/0B9EJGlco2Hy6fkU4QVVzMlQ0QnBqTmVjazZTR2dTSkJJal83eEhlYVJsU1puSjA2b3h3VU0/version.txt”; //txt file with version # in it
updateMsg = "

Establishing Connection…";//GUI update for user
var patchwww:WWW = WWW(url); //create new web connection to the build number site
yield patchwww; // wait for download to finish

    if (patchwww.text == buildVersion){    //check version
       	updateMsg = "

Currently update to date.";
yield WaitForSeconds(1.0f);
updating = false;
showGUI = true;
}
else {
patch = patchwww.text;
updateMsg = "Update Available.

Current Version: “+buildVersion+”
Patch Version: “+patchwww.text+”

Would you like to download updates?";
showUpdateButton = true;
}
}

The following two functions go about apply and downloading the update(The actual applying is not implemented yet):

function applyUpdate(fileListing:String){
	updateMsg = "

Identifying Platform";
var currPatch = “https://googledrive.com/host/0B9EJGlco2Hy6fkU4QVVzMlQ0QnBqTmVjazZTR2dTSkJJal83eEhlYVJsU1puSjA2b3h3VU0/patch_v"+fileListing+".txt”;
if(Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor){
updateMsg = "

Identifying updates for the Mac platform

Establishing Connection…";
//savePath += “/…/…/”; //use this to find the executable
}
else if(Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor){
updateMsg = "

Identifying updates for the Windows platform

Establishing Connection…";
rawDataFolder =“https://googledrive.com/host/0B9EJGlco2Hy6fmUxQzN3ZVAtYkczRzA0S29ubjhfVGVBUFpHTm9HRmxOeldraDhhOXBCa1E/”;
rawExc = “https://googledrive.com/host/0B9EJGlco2Hy6fkFNeVZELXZqRTc5M01sT1lOWjNfUC1nRmlpcEJfaTczQW1QMHBoMnV0TjA/The Dark Tower.exe”;
//savePath += “/…/”; //use this to find the executable
}

	updateMsg = "

Retrieving List of Files To Be Patched…“;
var patchlist:WWW = WWW(currPatch);
yield patchlist;
var fileList = patchlist.text;
var fileListArray:String = fileList.Split(”
" [0]);
var fileSaveSuccess = true;
for (var i = 0; i < fileListArray.Length; i++)
{
updateMsg = "

File “+i+” of “+fileListArray.Length+”

Downloading "+fileListArray*;*
_ yield downloadFile(fileListArray*);_
_
if(errorOccured == true){_
_
break;_
_
}_
_
}_
_
if(errorOccured == false){_
_
updateMsg = "

File List Retrieved and Downloaded.“;_
_
}_
_
}_
This is the function im having problems with:
function downloadFile(file:String){
_
var download:WWW = WWW(rawDataFolder+”"+file); //download file from platforms raw folder*_
yield download; // wait for download to finish
// var saveLoc = Application.persistentDataPath; //Location where the files will go
var saveLoc = “C:/games”;
try{
* Debug.Log(saveLoc+“/”+file);*
* File.WriteAllBytes (saveLoc+“/”+file, download.bytes); //<----PROBLEM HERE.*
}
catch(error){
* var errorMsg = “”;*
* var charCount = 0;*
* errorMsg = “”;*
* for(var i = 0; i < error.ToString().Length; i++){*
* charCount += 1;*
* if(charCount%15 == 0){*
* errorMsg += error.ToString().Substring(i, i+15);*
* errorMsg += "
";*

* }*
* if(charCount >89){*
* errorMsg +=“…”;*
* break;*
* }*
* }*

* updateMsg ="Make sure to run this game as Administrator - Error:
"+errorMsg;*

* errorOccured = true;*
* Debug.Log(error);*
}
}
Any help or direction on this one would be helpful.
EDIT: I just found this link
_*http://docs.unity3d.com/Manual/binarydata.html*_
where it talks about saving binary data. However, I don’t really understand a whole lot of what is happening in that example script.

Anyone? I could really use some help here.

what's the file name?

1 Answer

1

Make sure your path and file name doesn’t have any special characters like space or & or | etc… Some OS doesn’t supports it.
I guess that’s why you having the error.

I notice here that you are adding a "1" onto the end of the filename? Is that important or is that just an example here? Also do you need to Load the file prior to saving it? Also there weren't any special characters in the name unless google drive adds them. The output was "C:\games\level0".

That "1" was only for testing because I put it in the same folder.I think yes. but I will check if Memorystream or something else works.(I'm just going to eat something quickly) EDIT: Ok I don't know why but now it works like this (it never did before for me): IEnumerator downloadFile(string file){ string saveLoc = "C:/games/"; string url = "http://localhost/" + file + "1"; WWW www = new WWW(url); yield return www; while (!www.isDone) {} File.WriteAllBytes(saveLoc + file + "2", www.bytes); }

So I am writing this in Javascript. Can you actually say IEnumator vs just function? I thought IEnumator was for C#. Any I am going to try out the code you provided me a little later to see how it works out and I will let you know. Thanks for all the help so far.

Sorry I forget to translate it to java : function downloadFile(file : String){ var saveLoc = "C:/games/"; var url = "http://localhost/" + file + "1"; var www = WWW(url); yield www; while (!www.isDone) {} File.WriteAllBytes(saveLoc + file + "2", www.bytes); }

I copied your code for download file and tried it but modified http://localhost to rawDataFolder and it still have me the invalid characters thing :( ArgumentException: Name has invalid chars