The question is answered, right answer was accepted
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 = "\n\n\n\n\nChecking 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 = "\n\n\n\n\nEstablishing 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 = "\n\n\nCurrently update to date.";
yield WaitForSeconds(1.0f);
updating = false;
showGUI = true;
}
else {
patch = patchwww.text;
updateMsg = "Update Available.\n\n Current Version: "+buildVersion+"\n Patch Version: "+patchwww.text+"\n\n 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 = "\n\n\nIdentifying Platform";
var currPatch = "https://googledrive.com/host/0B9EJGlco2Hy6fkU4QVVzMlQ0QnBqTmVjazZTR2dTSkJJal83eEhlYVJsU1puSjA2b3h3VU0/patch_v"+fileListing+".txt";
if(Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor){
updateMsg = "\n\n\n\n\nIdentifying updates for the Mac platform\n\nEstablishing Connection...";
//savePath += "/../../"; //use this to find the executable
}
else if(Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor){
updateMsg = "\n\n\n\n\nIdentifying updates for the Windows platform\n\nEstablishing 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 = "\n\n\n\nRetrieving List of Files To Be Patched...";
var patchlist:WWW = WWW(currPatch);
yield patchlist;
var fileList = patchlist.text;
var fileListArray:String[] = fileList.Split("\n" [0]);
var fileSaveSuccess = true;
for (var i = 0; i < fileListArray.Length; i++)
{
updateMsg = "\n\n\nFile "+i+" of "+fileListArray.Length+"\n\nDownloading "+fileListArray[i];
yield downloadFile(fileListArray[i]);
if(errorOccured == true){
break;
}
}
if(errorOccured == false){
updateMsg = "\n\n\n\nFile 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 += "\n";
}
if(charCount >89){
errorMsg +="...";
break;
}
}
updateMsg ="Make sure to run this game as Administrator - Error:\n"+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.
Answer by Positive7 · Sep 05, 2015 at 06:40 PM
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 $$anonymous$$emorystream 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
Follow this Question
Related Questions
reading and writing files in built project,Files 0 Answers
Download files, not stupid HTML code! 0 Answers
Unity - Syncing/Downloading/overwriting files to a local folder accessed by the game when offline? 0 Answers
How can I download/upload file from/to USB Flash Drive? 0 Answers
Find file in unkown location (Steam) 0 Answers