Upload and Download an ES3File in Memory

Examples using Easy Save's API code
Post Reply
User avatar
Joel
Moodkie Staff
Posts: 3061
Joined: Wed Nov 07, 2012 10:32 pm

Upload and Download an ES3File in Memory

Post by Joel »

This is a complete example of uploading and downloading an ES3File in memory, meaning that no data is stored on the local device during this process.

This example assumes that you have followed the ES3Cloud setup instructions HERE.

To use the example package below:
  1. Open the scene found in Assets/Easy Save 3/Examples/Upload and Download an ES3File in Memory
  2. Open the Upload & Download GameObject and enter the URL and API Key for the ES3Cloud.php file on your server
  3. Press the Upload and Download buttons.
  4. "Uploaded" will be outputted to console when it has finished uploading, and "Downloading" will be uploaded when it has finished downloading.

Code: Select all

using System.Collections;
using UnityEngine;

public class UploadDownloadES3FileInMemory : MonoBehaviour
{
	// Your ES3Cloud.php details
	public string url = "https://www.mysite.com/ES3Cloud.php";
	public string apiKey = "yourAPIKey";

	// The variables you want to upload/download.
	int myInt = 123;
	Vector3 myVector3 = new Vector3(1f, 2f, 3f);

	// Call these to upload or download.
	public void Upload()	{ StartCoroutine(UploadRoutine());	}
	public void Download()	{ StartCoroutine(DownloadRoutine()); }

	private IEnumerator UploadRoutine()
    {
		// Create a new ES3File, and ensure we specify false as the parameter
		// so that we don't sync it to local storage
        var file = new ES3File(false);

		// Save data to our ES3File in memory.
		file.Save<int>("myInt", myInt);
		file.Save<Vector3>("myVector3", myVector3);

		// Create an ES3Cloud and upload the file.
		var cloud = new ES3Cloud(url, apiKey);
		yield return cloud.UploadFile(file);
	
		// If an error occurs, log it to console.
		if(cloud.isError)
			Debug.LogError(cloud.error);
		else
			Debug.Log("Uploaded");
    }

    private IEnumerator DownloadRoutine()
    {
        // Create a new ES3File, and ensure we specify false as the parameter
		// so that we don't sync it to local storage
        var file = new ES3File(false);

		// Create an ES3Cloud and download the file.
		var cloud = new ES3Cloud(url, apiKey);
		yield return cloud.DownloadFile(file);

		if(cloud.isError)
			Debug.LogError(cloud.error);
		else
			Debug.Log("Downloaded");

		// Load the data from the ES3File and back into our variables.
		// We set the defaultValue parameter incase data doesn't exist.
		myInt = file.Load<int>("myInt", 0);
		myVector3 = file.Load<Vector3>("myVector3", Vector3.zero);
    }
}
Attachments
Upload and Download an ES3File in Memory.unitypackage
(12.91 KiB) Downloaded 111 times
Joel @ Moodkie Interactive
Twitter - Unity

krolldk
Posts: 27
Joined: Sun Jun 02, 2019 11:15 am

Re: Upload and Download an ES3File in Memory

Post by krolldk »

I'm sorry. The above code causes the same exception to be cast as I talked about yesterday :-(

Code: Select all

public void Test()
    {
        //StartCoroutine(doTest());
        StartCoroutine(UploadRoutine());
    }


    private IEnumerator UploadRoutine()
    {
        // Create a new ES3File, and ensure we specify false as the parameter
        // so that we don't sync it to local storage
        var file = new ES3File(false);

        // Save data to our ES3File in memory.
        file.Save<int>("myInt", 3);
        file.Save<string>("myVector3", "helloWorld");
        //file.Sync();  //IF THIS LINE IS COMMENTED OUT, EXCEPTION IS CAST
        // Create an ES3Cloud and upload the file.
        var cloud = new ES3Cloud(url, apiKey);
        yield return cloud.UploadFile(file);

        // If an error occurs, log it to console.
        if (cloud.isError)
            Debug.LogError(cloud.error);
        else
            Debug.Log("Uploaded");
    }
when run this throws an exception at the line yield return cloud.UploadFile(file)

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
ES3.LoadRawBytes (.ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:389)
ES3Cloud.UploadFile (System.Byte[] bytes, .ES3Settings settings, System.String user, System.String password) (at Assets/Plugins/Easy Save 3/Scripts/Web/ES3Cloud.cs:252)
ES3Cloud.UploadFile (.ES3File es3File) (at Assets/Plugins/Easy Save 3/Scripts/Web/ES3Cloud.cs:221)
CloudTest+<UploadRoutine>c__Iterator0.MoveNext () (at Assets/_Scripts/Cloud/CloudTest.cs:51)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
CloudTest:Test() (at Assets/_Scripts/Cloud/CloudTest.cs:35)
UnityEngine.EventSystems.EventSystem:Update()
NOTE !!!
If the line file.Sync() is added before the call to Upload, the upload works fine.
NOTE ALSO!!!
If the file already exists on disk, the Upload works fine, also WITHOUT the file.sync() line. Since your example is using the default file, I suspect odds are high that there IS already a file on your disk. I strongly suggest you try
var file = new ES3File("SomeFilename",false); instead, so you can verify no file exists.

If there is no file on disk, the exception will be cast.

I suspect you need to look at ES3.cs line 387:
using (var stream = ES3Stream.CreateStream(settings, ES3FileMode.Read))
that call to ES3Stream.CreateStream returns a null object under the circumstances described above

to be more precise, ES3Stream.cs line 13-18 look suspect to me:

Code: Select all

			if(settings.location == ES3.Location.Memory)
			{
				// There's no point in creating an empty MemoryStream if we're only reading from it.
				if(!isWriteStream)
					return null;
				stream = new MemoryStream(settings.bufferSize);
			}

Doesn't that return null statement exactly cause the problem when we try to read from memory ?

krolldk
Posts: 27
Joined: Sun Jun 02, 2019 11:15 am

Re: Upload and Download an ES3File in Memory

Post by krolldk »

BUT: ALSO:
Thank you for a good codeexample: That's exactly what I was looking for, and despite the bug I'm talking about, it's a good way for me to verify that it's not ME doing something wrong :-) Also: Ihadn't understood that ES3Files needed to be created with that bool set to false to not auto sync with files. That was helpful.

Post Reply