From 7e1da4a1a68bfba703782f66953774cacc44aa3e Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Mon, 14 Jul 2025 16:00:14 -0700 Subject: [PATCH] Upload icon feature --- Assets/Scenes/IconMarketplaceMenu.unity | 2 + .../IconMarketplaceUploadIcon.cs | 81 +++++++++++++------ 2 files changed, 60 insertions(+), 23 deletions(-) diff --git a/Assets/Scenes/IconMarketplaceMenu.unity b/Assets/Scenes/IconMarketplaceMenu.unity index 26e91e5..5c2e1a8 100644 --- a/Assets/Scenes/IconMarketplaceMenu.unity +++ b/Assets/Scenes/IconMarketplaceMenu.unity @@ -4388,6 +4388,8 @@ MonoBehaviour: selectButton: {fileID: 1745561408} birdNameInput: {fileID: 382778012} birdPriceInput: {fileID: 210372877} + birdImage: {fileID: 2023785527} + birdData: --- !u!1 &1708620717 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/IconMarketplace/IconMarketplaceUploadIcon.cs b/Assets/Scripts/IconMarketplace/IconMarketplaceUploadIcon.cs index 122fc2a..3f2b316 100644 --- a/Assets/Scripts/IconMarketplace/IconMarketplaceUploadIcon.cs +++ b/Assets/Scripts/IconMarketplace/IconMarketplaceUploadIcon.cs @@ -12,49 +12,80 @@ public class IconMarketplaceUploadIcon : MonoBehaviour public IconMarketplaceManager marketplaceManager; public TMP_Text statusText; public Button backButton; + public Button uploadButton; + public Button selectButton; + public TMP_InputField birdNameInput; + public TMP_InputField birdPriceInput; + public Image birdImage; + private string birdData = null; void Awake() { backButton.onClick.AddListener(() => marketplaceManager.SwitchPanel(0)); - OpenFilePicker(); + uploadButton.onClick.AddListener(() => + { + if (birdNameInput.text.Trim() == string.Empty) + { + AccountHandler.UpdateStatusText(statusText, "Bird name can't be empty", Color.red); + } + else if (birdPriceInput.text.Trim() == string.Empty) + { + AccountHandler.UpdateStatusText(statusText, "Bird price can't be empty", Color.red); + } + else if (birdData == null) + { + AccountHandler.UpdateStatusText(statusText, "You must upload an image", Color.red); + } + else + { + UploadIcon(); + } + }); + selectButton.onClick.AddListener(() => + { + string path = OpenFilePicker(); + if (path == null) return; + byte[] fileContent; + try + { + fileContent = File.ReadAllBytes(path); + if (fileContent.Length > 1024 * 1024) + { + AccountHandler.UpdateStatusText(statusText, "File size exceeds 1 MB limit", Color.red); + return; + } + birdData = Convert.ToBase64String(fileContent); + ImageUtil.RenderFromBase64(birdData, birdImage); + } + catch (Exception e) + { + AccountHandler.UpdateStatusText(statusText, "Failed to read file: " + e.Message, Color.red); + return; + } + }); } - void OpenFilePicker() + string OpenFilePicker() { -#if UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_EDITOR var filters = new[] { new ExtensionFilter("Image Files", "png"), }; var paths = StandaloneFileBrowser.OpenFilePanel("Select an icon file", "", filters, false); if (paths.Length > 0 && !string.IsNullOrEmpty(paths[0])) { - UploadFile(paths[0]); + return paths[0]; } -#endif + return null; } - async void UploadFile(string path) + async void UploadIcon() { - byte[] fileContent; - try - { - fileContent = File.ReadAllBytes(path); - if (fileContent.Length > 1024 * 1024) - { - AccountHandler.UpdateStatusText(statusText, "File size exceeds 1 MB limit", Color.red); - return; - } - } - catch (Exception e) - { - AccountHandler.UpdateStatusText(statusText, "Failed to read file: " + e.Message, Color.red); - return; - } - EncryptedWWWForm dataForm = new(); dataForm.AddField("token", BazookaManager.Instance.GetAccountSession()); dataForm.AddField("username", BazookaManager.Instance.GetAccountName()); - dataForm.AddField("filecontent", Convert.ToBase64String(fileContent)); + dataForm.AddField("name", birdNameInput.text); + dataForm.AddField("price", birdPriceInput.text); + dataForm.AddField("filecontent", birdData); using UnityWebRequest request = UnityWebRequest.Post(SensitiveInfo.SERVER_DATABASE_PREFIX + "uploadMarketplaceIcon.php", dataForm.GetWWWForm()); request.SetRequestHeader("Requester", "BerryDashClient"); request.SetRequestHeader("ClientVersion", Application.version); @@ -91,6 +122,10 @@ public class IconMarketplaceUploadIcon : MonoBehaviour var jsonResponse = JObject.Parse(response); if ((bool)jsonResponse["success"]) { + birdImage.sprite = Resources.Load("Other/X"); + birdData = null; + birdNameInput.text = ""; + birdPriceInput.text = ""; AccountHandler.UpdateStatusText(statusText, "Icon uploaded successfully!", Color.green); } else