You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.5 KiB

5 months ago
using UnityEngine;
4 months ago
using Amazon.S3.Model;
using UltraCombos.Upload;
4 months ago
using OscJack;
using System.Collections;
4 months ago
using TMPro;
4 months ago
using System;
3 months ago
using UnityEngine.UI;
4 months ago
[System.Serializable]
public class S3Tag
{
public string key;
public string value;
public static implicit operator Tag(S3Tag t) => new Tag { Key = t.key, Value = t.value };
}
5 months ago
public class SaveImage : MonoBehaviour
{
4 months ago
4 months ago
4 months ago
public Uploader uploader;
4 months ago
public RenderTexture renderTexture_postcard;
public RenderTexture renderTexture_share;
4 months ago
public string OutputFolder = "output";
4 months ago
public string FirebaseUrl = "https://firestore.googleapis.com/v1/projects/uc-24070-thegreattipsy/databases/(default)/documents/";
4 months ago
public TextMeshProUGUI TextNumber;
public TextMeshProUGUI TextSummary;
4 months ago
3 months ago
public GameObject BackgroundText;
public GameObject BackgroundDefault;
public GameObject ImageDiscard;
public GameObject ImageGenerate;
public GameObject ImageDiscardPostcard;
public GameObject ImageGeneratePostcard;
5 months ago
// Start is called before the first frame update
void Start()
{
4 months ago
uploader = GetComponent<Uploader>();
5 months ago
}
// Update is called once per frame
void Update()
{
4 months ago
5 months ago
}
4 months ago
5 months ago
4 months ago
public void Save(string input_text)
5 months ago
{
4 months ago
var parmas = input_text.Split('#');
if (parmas.Length < 2)
{
Debug.LogError("Input text must contain at least two parts separated by '#'.");
return;
}
4 months ago
string uploadDest=parmas[0].Trim();
string id = parmas[1].Trim();
3 months ago
string summary = parmas[2].Trim();
3 months ago
string filename = parmas.Length > 3 ? parmas[3].Trim() : System.DateTime.Now.ToString("yyyyMMdd_hhmmss");
3 months ago
bool discard = parmas.Length > 4 && (parmas[4].Trim().ToLower() == "discard");
bool default_image= parmas.Length > 5 && (parmas[5].Trim().ToLower() == "default");
if (discard)
{
if (ImageDiscard != null) ImageDiscard.SetActive(true);
}
else
{
if (ImageDiscard != null) ImageDiscard.SetActive(false);
}
Debug.Log("choice: " + discard +" default"+ default_image+" summary:"+summary);
if (summary.Length > 0)
{
BackgroundDefault.SetActive(false);
BackgroundText.SetActive(true);
}
else
{
BackgroundDefault.SetActive(true);
BackgroundText.SetActive(false);
}
if (default_image)
{
ImageGenerate.SetActive(false);
if(!discard) ImageDiscard.SetActive(false);
ImageGeneratePostcard.SetActive(false);
if(!discard) ImageDiscardPostcard.SetActive(false);
}
else
{
ImageGenerate.SetActive(true);
ImageGeneratePostcard.SetActive(true);
}
4 months ago
3 months ago
TextNumber.SetText(id);
TextSummary.SetText(summary);
4 months ago
3 months ago
TextNumber.ForceMeshUpdate();
TextSummary.ForceMeshUpdate();
4 months ago
4 months ago
4 months ago
4 months ago
if (string.IsNullOrEmpty(filename))
4 months ago
{
Debug.LogError("Filename cannot be null or empty.");
return;
}
4 months ago
Debug.Log("Saving image to: " + uploadDest + " filename=" + filename+ " id=" + id);
4 months ago
4 months ago
string timestamp = System.DateTime.Now.ToString("yyyyMMdd");
4 months ago
4 months ago
if (!System.IO.Directory.Exists(OutputFolder+"/" + uploadDest))
4 months ago
{
4 months ago
System.IO.Directory.CreateDirectory(OutputFolder+"/" + uploadDest);
4 months ago
}
4 months ago
4 months ago
SaveRenderTextureToPNG(renderTexture_postcard, System.IO.Path.Combine(OutputFolder+"/" + uploadDest, filename + "_print.png"));
4 months ago
Debug.Log("Image saved to " + filename);
4 months ago
4 months ago
SaveRenderTextureToPNG(renderTexture_share, System.IO.Path.Combine(OutputFolder+"/" + uploadDest, filename + ".png"));
4 months ago
Debug.Log("Image saved to " + filename);
5 months ago
// Optionally, you can also log the full path
4 months ago
string print_fullPath = System.IO.Path.Combine(OutputFolder+"/" + uploadDest + "/", filename + "_print.png");
4 months ago
Debug.Log("print_path: " + print_fullPath);
4 months ago
upload(print_fullPath, id, "print");
4 months ago
4 months ago
string share_fullPath = System.IO.Path.Combine(OutputFolder+"/" + uploadDest + "/", filename + ".png");
4 months ago
Debug.Log("share_path: " + share_fullPath);
4 months ago
upload(share_fullPath, id, "share");
5 months ago
}
void SaveRenderTextureToPNG(RenderTexture rt, string filePath)
{
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = rt;
Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes(filePath, bytes);
4 months ago
Debug.Log("Saved RenderTexture to PNG at " + filePath + " with size: " + bytes.Length + " bytes");
4 months ago
5 months ago
RenderTexture.active = currentRT;
Destroy(tex);
}
4 months ago
4 months ago
void upload(string filename, string id, string type)
4 months ago
{
4 months ago
if (uploader != null)
{
uploader.Upload(filename, (response) =>
{
if (response.success)
{
4 months ago
Debug.Log("Upload successful: " + response.message);
4 months ago
4 months ago
if(type=="print") writeToFirebase("prints", System.DateTime.Now.ToString("yyyyMMdd_hhmmss"), response.message, type);
writeToFirebase("records","user/"+id+"/file/"+type, response.message, type);
4 months ago
}
else
{
Debug.LogError("Upload failed: " + response);
}
});
}
else
{
Debug.LogError("Uploader is not assigned.");
}
}
4 months ago
4 months ago
public void writeToFirebase(string collection, string id, string url, string type)
4 months ago
{
4 months ago
Debug.Log("Writing to Firebase: " + collection + "/" + id);
4 months ago
StartCoroutine(WriteToFirebaseCoroutine(id, url));
4 months ago
4 months ago
IEnumerator WriteToFirebaseCoroutine(string id, string url)
4 months ago
{
4 months ago
// string[] tags=filename.Split('/');
// string id=tags[tags.Length - 1].Split('.')[0];
4 months ago
Debug.Log("Extracted ID: " + id);
4 months ago
string firebaseUrl = FirebaseUrl + collection + "/" + id;
4 months ago
string jsonData = "{ \"fields\": { " +
4 months ago
"\""+type+"_url\": { \"stringValue\": \"" + url + "\" }, " +
4 months ago
"\"timestamp\": { \"timestampValue\": \"" + System.DateTime.UtcNow.ToString("o") + "\" } " +
"} }";
using (var www = new UnityEngine.Networking.UnityWebRequest(firebaseUrl, "PATCH"))
{
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
www.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(bodyRaw);
www.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
yield return www.SendWebRequest();
#if UNITY_2020_1_OR_NEWER
if (www.result != UnityEngine.Networking.UnityWebRequest.Result.Success)
#else
if (www.isNetworkError || www.isHttpError)
#endif
{
Debug.LogError("Error writing to Firebase: " + www.error);
Debug.LogError("Response: " + www.downloadHandler.text);
}
else
{
Debug.Log("Successfully wrote to Firebase: " + www.downloadHandler.text);
}
}
}
}
4 months ago
4 months ago
}