playMaker

Author Topic: SQLiteKIt and playMaker  (Read 1833 times)

Vern_S

  • Playmaker Newbie
  • *
  • Posts: 32
    • View Profile
SQLiteKIt and playMaker
« on: April 20, 2014, 06:57:36 AM »
Hello all,

I've had quite a bit of success using SQLiteKit in my project collecting Data and storing it in a SQLite database. The roadblock I am trying to overcome is transferring that database to a web server. I will also need to pull a database from a web server and into the StreamingAssets folder of my project.

The author of SQLiteKit has been very helpful and has written a custom action for me that he plans to add to his next release but I am having issues figuring out the PHP code to receive the file.

Here is the custom action he wrote to upload a database from the persistent folder.

Code: [Select]
using UnityEngine;
using System;
using System.IO;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("SQLiteKit")]
[Tooltip("Upload SQLite database.")]
public class UploadFromPersistent : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.FsmString)]
[Tooltip("Url")]
public FsmString url;

[RequiredField]
[UIHint(UIHint.FsmString)]
[Tooltip("Copied database file full path")]
public FsmString persistentFilename;

[Tooltip("On succeeded file copy.")]
public FsmEvent onSuccess;

[Tooltip("On file copy fail.")]
public FsmEvent onFail;

WWW www;
string filename;

public override void Reset()
{
onSuccess = null;
onFail = null;
}

public override void OnUpdate ()
{
base.OnUpdate ();

if(www != null)
{
if(www.isDone)
{
if(www.error != null)
{
Fsm.Event(onFail);
}
else
{
Fsm.Event(onSuccess);
}
www = null;
}
}


}

public override void OnEnter()
{

// persistant database path.
filename = Application.persistentDataPath + "/" + persistentFilename;


// check if database already exists.
if(File.Exists(filename))
{
//www = new WWW(url.Value,File.ReadAllBytes(filename));

WWW upload = new WWW(url.Value,File.ReadAllBytes(filename));

yield return upload;
if (upload.error == null)
Debug.Log("upload done :" + upload.text);
else
Debug.Log("Error during upload: " + upload.error);


}
else
{
Fsm.Event(onFail);
Finish();
}
}
}
}

I am also getting compiler errors using the code above.

I am able to transfer a file to a local host using code from this Unity Answers page.

I have tried to figure out how to edit the playMaker Action code above but so far no luck. The PHP script appears to be what I need. I just need a little help connecting the two. Here is the PHP script;

Code: [Select]
<?php
   
if(isset($_FILES['theFile']))
   {
      print(
"Success! ");
      print(
"tmpName: " $_FILES['theFile']['tmp_name'] . " ");
      print(
"size: " $_FILES['theFile']['size'] . " ");
      print(
"mime: " $_FILES['theFile']['type'] . " ");
      print(
"name: " $_FILES['theFile']['name'] . " ");
 
      
move_uploaded_file($_FILES['theFile']['tmp_name'], "uploads/" $_FILES['theFile']['name']);
   } else
   {
      print(
"Failed!");
   }
?>


Any help would be appreciated.

Vern
« Last Edit: April 20, 2014, 07:03:58 AM by Vern_S »

Vern_S

  • Playmaker Newbie
  • *
  • Posts: 32
    • View Profile
Re: SQLiteKIt and playMaker
« Reply #1 on: April 20, 2014, 08:04:26 AM »
Here is the C# code that is working with the PHP script in prior post.

Code: [Select]
using UnityEngine;
using System.Collections;

public class SendFile : MonoBehaviour {


private string m_LocalFileName = "d:/test.png";
private string m_URL = "http://localhost/test/upload_file.php";

IEnumerator UploadFileCo(string localFileName, string uploadURL)
{


WWW localFile = new WWW("file:///" + localFileName);
yield return localFile;
if (localFile.error == null)
Debug.Log("Loaded file successfully");
else
{
Debug.Log("Open file error: "+localFile.error);
yield break; // stop the coroutine here
}

WWWForm postForm = new WWWForm();
// version 1
//postForm.AddBinaryData("theFile",localFile.bytes);

// version 2
postForm.AddBinaryData("theFile",localFile.bytes,localFileName,"text/plain");

WWW upload = new WWW(uploadURL,postForm);       
yield return upload;
if (upload.error == null)
Debug.Log("upload done :" + upload.text);
else
Debug.Log("Error during upload: " + upload.error);
}

void UploadFile(string localFileName, string uploadURL)
{
StartCoroutine(UploadFileCo(localFileName, uploadURL));
}

void OnGUI()
{
GUILayout.BeginArea(new Rect(0,0,Screen.width,Screen.height));
m_LocalFileName = GUILayout.TextField(m_LocalFileName);
m_URL           = GUILayout.TextField(m_URL);
if (GUILayout.Button("Upload"))
{
UploadFile(m_LocalFileName,m_URL);
}
GUILayout.EndArea();
}
}

Getting the code above to look in the persistent folder to find the database file and work within playMaker may also be an option.

 :)

Vern_S

  • Playmaker Newbie
  • *
  • Posts: 32
    • View Profile
Re: SQLiteKIt and playMaker
« Reply #2 on: April 22, 2014, 10:35:12 AM »
Well I have this working as a normal script attached to an empty game object and using "Send Message" to initiate the process and assign the name of the file which it is getting from the persistent folder. Here is the code;

Code: [Select]
using UnityEngine;
using System.Collections;

public class SendFile : MonoBehaviour {

//public string m_LocalFileName = "Testdb.db";
public string m_URL = "http://localhost/test/upload_file.php";

string filename;

/*void OnGUI()
{
GUILayout.BeginArea(new Rect(0,0,Screen.width,Screen.height));
m_LocalFileName = GUILayout.TextField(m_LocalFileName);
m_URL           = GUILayout.TextField(m_URL);
if (GUILayout.Button("Upload"))
{
filename = Application.persistentDataPath + "/" + m_LocalFileName;
Debug.Log(filename);

UploadFile(filename,m_URL);
}
GUILayout.EndArea();
}*/

void UploadStart(string name)
{
filename = Application.persistentDataPath + "/" + name;
Debug.Log(filename);

UploadFile(filename,m_URL);
}


void UploadFile(string localFileName, string uploadURL)
{
StartCoroutine(UploadFileCo(localFileName, uploadURL));
}

IEnumerator UploadFileCo(string localFileName, string uploadURL)
{

WWW localFile = new WWW("file:///" + localFileName);
yield return localFile;
if (localFile.error == null)
Debug.Log("Loaded file successfully");
else
{
Debug.Log("Open file error: "+localFile.error);
yield break; // stop the coroutine here
}

WWWForm postForm = new WWWForm();
// version 1
//postForm.AddBinaryData("theFile",localFile.bytes);

// version 2
postForm.AddBinaryData("theFile",localFile.bytes,localFileName,"text/plain");

WWW upload = new WWW(uploadURL,postForm);       
yield return upload;
if (upload.error == null)
Debug.Log("upload done :" + upload.text);
else
Debug.Log("Error during upload: " + upload.error);
}
}

I left the commented out code from the existing script I uploaded earlier for reference. The PHP code is relatively unchanged.

So this will post a database stored in the persistent folder by SQLiteKit to a web server. I am half way there. Now I need to pull a database from the web server and put it into the persistent folder so I can use it with SQLiteKit. Here is where I am at thus far which isn't working as of yet.

Code: [Select]
using UnityEngine;
using System.Collections;

public class ReceiveFile : MonoBehaviour {

public string url = "http://localhost/test/download_file.php?";

void DownloadStart(string name)
{

WWW www = new WWW(url + name);

StartCoroutine(WaitForRequest(www));
}

IEnumerator WaitForRequest(WWW www)
{
yield return www;

// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
} else {
Debug.Log("WWW Error: "+ www.error);
}   
}
}

and I am not sure where to start on the PHP end.

So now I have two things to accomplish. First, GET a file from the web server and store it in either the persistent folder or the streaming asset folder. I would prefer the former. Second, is to get these scripts as Actions to be used in playMaker. I think this would be very beneficial to the community.

Any assistance would be greatly appreciated.

~V~

Vern_S

  • Playmaker Newbie
  • *
  • Posts: 32
    • View Profile
Re: SQLiteKIt and playMaker
« Reply #3 on: April 22, 2014, 07:35:01 PM »
Ok, I have downloading figured out. This code is initiated using "Send Message" like the upload script.  This required no PHP coding.

Code: [Select]
using UnityEngine;
using System.Collections;
using System.IO;

public class ReceiveFile : MonoBehaviour {

string url = "http://localhost/test/";

string dbName = "";

void startDownload(string filename)
{
dbName = filename;
Debug.Log(dbName);

WWW www = new WWW(url + filename);
//WWW www = new WWW(url);

StartCoroutine(DownloadFile(www));
}

IEnumerator DownloadFile(WWW www)
{
yield return www;

// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
} else {
Debug.Log("WWW Error: "+ www.error);
}
if (www.isDone)
{
File.WriteAllBytes(Application.persistentDataPath + "/" + dbName, www.bytes);
}
}
}

Again, it would be nice to see this as custom Actions. :)
« Last Edit: April 22, 2014, 07:37:15 PM by Vern_S »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14400
  • Official Playmaker Support
    • View Profile
Re: SQLiteKIt and playMaker
« Reply #4 on: November 22, 2018, 12:52:26 AM »
Hi,

 you can now do that using the action WebDownloadRequest on the Ecosystem. you couple it with the action GetPersistentDataFolder to get the app folder for external content and you are good to go.


Bye,

 Jean