From 554370752dce8d5f10d6ccf09e1228bc436656f7 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Mon, 19 Dec 2022 17:31:42 +0100 Subject: [PATCH] Refactored and moved some PCK Center related classes --- .../Classes/API/PCKCenter/PCKCollections.cs | 140 +++++++++++++ .../PCKCenter}/PCKCollectionsLocal.cs | 6 +- .../Classes/API/PCKCenter/SaveLocalJSON.cs | 2 +- .../Classes/Networking/PCKCollections.cs | 190 ------------------ PCK-Studio/Forms/Utilities/PckCenterBeta.cs | 2 +- PCK-Studio/PckStudio.csproj | 4 +- 6 files changed, 145 insertions(+), 199 deletions(-) create mode 100644 PCK-Studio/Classes/API/PCKCenter/PCKCollections.cs rename PCK-Studio/Classes/{IO => API/PCKCenter}/PCKCollectionsLocal.cs (94%) delete mode 100644 PCK-Studio/Classes/Networking/PCKCollections.cs diff --git a/PCK-Studio/Classes/API/PCKCenter/PCKCollections.cs b/PCK-Studio/Classes/API/PCKCenter/PCKCollections.cs new file mode 100644 index 00000000..317d5f0a --- /dev/null +++ b/PCK-Studio/Classes/API/PCKCenter/PCKCollections.cs @@ -0,0 +1,140 @@ +using System; +using System.IO; +using System.Net; +using System.Drawing; +using Newtonsoft.Json; +using PckStudio.API.PCKCenter.model; + +namespace PckStudio.API.PCKCenter +{ + public class PCKCollections + { + WebClient client = new WebClient(); + public string CurrentPackDl = ""; + string cache = Program.AppDataCache + "/packs"; + public PCKCenterJSON CenterPacks; + public LocalActions LocalAction = new LocalActions(); + + public string[] GetCategories() => GetCategories(false); + + public string[] GetCategories(bool isVita) + { + try + { + return DownloadAndDeserializeJson + ($"{Program.BaseAPIUrl}/center/packs/{(isVita ? "VitaCategiories.json" : "Categiories.json")}", client); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + return Array.Empty(); + } + + public PCKCenterJSON GetPackDescs(string category, bool isVita) + { + string cat; + try + { + cat = client.DownloadString($"{Program.BaseAPIUrl}/center/packs/{(isVita ? "vita" : "normal")}/{category}.json"); + } + catch + { + cat = client.DownloadString($"{Program.BackUpAPIUrl}/center/packs/{(isVita ? "vita" : "normal")}/{category}.json"); + } + PCKCenterJSON Data = JsonConvert.DeserializeObject(cat); + return Data; + } + + [Obsolete] + public string[] GetPackDescription(string category, bool isVita) + { + return Array.Empty(); + string cat; + try + { + cat = client.DownloadString($"{Program.BaseAPIUrl}/studio/PCK/api/pcks{(isVita ? "/Vita" : "")}/{category}.desc"); + } + catch + { + cat = client.DownloadString($"{Program.BackUpAPIUrl}/studio/PCK/api/pcks{(isVita ? "/Vita" : "")}/{category}.desc"); + } + return cat.Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + } + + public Image GetPackImage(int packId, bool isVita) + { + byte[] data; + try + { + data = client.DownloadData($"{Program.BaseAPIUrl}/center/packs/{(isVita ? "vita" : "normal")}/images/{packId}.png"); + } + catch + { + data = client.DownloadData($"{Program.BackUpAPIUrl}/center/packs/{(isVita ? "vita" : "normal")}/images/{packId}.png"); + } + Image image; + using (MemoryStream fs = new MemoryStream(data)) + { + image = Image.FromStream(fs); + } + return image; + } + + public bool TryDownloadPack(string category, int packId, bool isVita) + { + try + { + Image packImage = GetPackImage(packId, isVita); + packImage.Save($"{cache}/{(isVita ? "vita" : "normal")}/images/" + packId + ".png"); + packImage.Dispose(); + + Directory.CreateDirectory(cache + "/normal/"); + Directory.CreateDirectory(cache + "/normal/images"); + Directory.CreateDirectory(cache + "/normal/pcks"); + Directory.CreateDirectory(cache + "/vita/"); + Directory.CreateDirectory(cache + "/vita/images"); + Directory.CreateDirectory(cache + "/vita/pcks"); + PCKCenterJSON Local = LocalAction.GetLocalJSON(category, isVita); + client.DownloadFile($"{Program.BaseAPIUrl}/center/packs/{(isVita ? "vita" : "normal")}/pcks/{packId}.pck", $"{cache}/vita/pcks/{packId}.pck"); + + Local = LocalAction.AddPack(Local, CenterPacks.Data[packId.ToString()], packId); + LocalAction.SaveLocalJSON(Local, category, isVita); + LocalAction.SaveLocalCategories(isVita); + return true; + } + catch + { + return false; + } + } + + T DownloadAndDeserializeJson(string address, WebClient client = null) + { + WebClient wc = client ?? new WebClient(); + string jsonData = wc.DownloadString(address); + return JsonConvert.DeserializeObject(jsonData); + } + + PCKCenterJSON DownloadCategoryJson(string categorie, bool isVita, WebClient client = null) + { + return DownloadAndDeserializeJson + ($"{Program.BaseAPIUrl}/center/packs/{(isVita ? "vita" :"normal")}/{categorie}.json", client); + } + + public void TryTestPackInfo(bool isVita) + { + try + { + WebClient client = new WebClient(); + string categoryJSON = client.DownloadString(Program.BaseAPIUrl + "/center/packs/Categiories.json"); + string[] categories = JsonConvert.DeserializeObject(categoryJSON); + PCKCenterJSON result = DownloadCategoryJson(categories[2], false, client); + } + catch + { + + } + } + } +} diff --git a/PCK-Studio/Classes/IO/PCKCollectionsLocal.cs b/PCK-Studio/Classes/API/PCKCenter/PCKCollectionsLocal.cs similarity index 94% rename from PCK-Studio/Classes/IO/PCKCollectionsLocal.cs rename to PCK-Studio/Classes/API/PCKCenter/PCKCollectionsLocal.cs index 34d3a209..739e1b30 100644 --- a/PCK-Studio/Classes/IO/PCKCollectionsLocal.cs +++ b/PCK-Studio/Classes/API/PCKCenter/PCKCollectionsLocal.cs @@ -1,15 +1,11 @@ using System; using System.IO; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Drawing; using Newtonsoft.Json; using PckStudio.API.PCKCenter.model; -using PckStudio.API.PCKCenter; -namespace PckStudio.Classes.IO +namespace PckStudio.API.PCKCenter { public class PCKCollectionsLocal { diff --git a/PCK-Studio/Classes/API/PCKCenter/SaveLocalJSON.cs b/PCK-Studio/Classes/API/PCKCenter/SaveLocalJSON.cs index d2189ac6..92c0d77a 100644 --- a/PCK-Studio/Classes/API/PCKCenter/SaveLocalJSON.cs +++ b/PCK-Studio/Classes/API/PCKCenter/SaveLocalJSON.cs @@ -42,7 +42,7 @@ namespace PckStudio.API.PCKCenter return JSONData; } } - public PCKCenterJSON AddPack(PCKCenterJSON JSONData,EntryInfo EInfo, int PackID) + public PCKCenterJSON AddPack(PCKCenterJSON JSONData, EntryInfo EInfo, int PackID) { JSONData.Data.Add(PackID.ToString(), EInfo); return JSONData; diff --git a/PCK-Studio/Classes/Networking/PCKCollections.cs b/PCK-Studio/Classes/Networking/PCKCollections.cs deleted file mode 100644 index 7e06b21e..00000000 --- a/PCK-Studio/Classes/Networking/PCKCollections.cs +++ /dev/null @@ -1,190 +0,0 @@ -using System; -using System.IO; -using System.Net; -using System.Drawing; -using Newtonsoft.Json; -using PckStudio.API.PCKCenter.model; -using PckStudio.API.PCKCenter; - -namespace PckStudio.Classes.Networking -{ - public class PCKCollections - { - WebClient client = new WebClient(); - public string CurrentPackDl = ""; - string cache = Program.AppDataCache + "/packs/"; - public PCKCenterJSON CenterPacks; - public LocalActions LocalAction = new LocalActions(); - public string[] GetCategories() - { - string cat = ""; - try - { - cat = client.DownloadString(Program.BaseAPIUrl + "/center/packs/Categiories.json"); - } - catch - { - cat = client.DownloadString(Program.BaseAPIUrl + "/center/packs/VitaCategiories.json"); - } - return JsonConvert.DeserializeObject(cat); - } - - public PCKCenterJSON GetPackDescs(string Category, bool IsVita) - { - string cat = ""; - try - { - switch (IsVita) - { - case (true): - cat = client.DownloadString(Program.BaseAPIUrl + "/center/packs/vita/" + Category + ".json"); - break; - case (false): - cat = client.DownloadString(Program.BaseAPIUrl + "/center/packs/normal/" + Category + ".json"); - break; - } - } - catch - { - switch (IsVita) - { - case (true): - cat = client.DownloadString(Program.BackUpAPIUrl + "/center/packs/vita/" + Category + ".json"); - break; - case (false): - cat = client.DownloadString(Program.BackUpAPIUrl + "/center/packs/normal/" + Category + ".json"); - break; - } - } - - PCKCenterJSON Data = JsonConvert.DeserializeObject(cat); - return Data; - } - public string[] GetPackData(string Category, bool IsVita) - { - string cat = ""; - try - { - switch (IsVita) - { - case (true): - cat = client.DownloadString(Network.MainURL + "/studio/PCK/api/pcks/Vita/" + Category + ".desc"); - break; - case (false): - cat = client.DownloadString(Network.MainURL + "/studio/PCK/api/pcks/" + Category + ".desc"); - break; - } - } - catch - { - switch (IsVita) - { - case (true): - cat = client.DownloadString(Network.BackUpURL + "/studio/PCK/api/pcks/Vita/" + Category + ".desc"); - break; - case (false): - cat = client.DownloadString(Network.BackUpURL + "/studio/PCK/api/pcks/" + Category + ".desc"); - break; - } - } - return cat.Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); - } - - public Image GetPackImage(int packID, bool IsVita) - { - byte[] cat = new byte[] { }; - try - { - switch (IsVita) - { - case (true): - cat = client.DownloadData(Program.BaseAPIUrl + "/center/packs/vita/images/" + packID + ".png"); - break; - case (false): - cat = client.DownloadData(Program.BaseAPIUrl + "/center/packs/normal/images/" + packID + ".png"); - break; - } - } - catch - { - switch (IsVita) - { - case (true): - cat = client.DownloadData(Program.BackUpAPIUrl + "/center/packs/vita/images/" + packID + ".png"); - break; - case (false): - cat = client.DownloadData(Program.BackUpAPIUrl + "/center/packs/normal/images/" + packID + ".png"); - break; - } - } - Stream fs = new MemoryStream(cat); - Image image; - image = Image.FromStream(fs); - fs.Flush(); - fs.Dispose(); - return image; - } - - public bool TryDownloadPack(int packID, bool IsVita, string Category) - { - try - { - - Image image = GetPackImage(packID, IsVita); - string DescPath = cache; - Directory.CreateDirectory(cache + "normal/"); - Directory.CreateDirectory(cache + "normal/images"); - Directory.CreateDirectory(cache + "normal/pcks"); - Directory.CreateDirectory(cache + "vita/"); - Directory.CreateDirectory(cache + "vita/images"); - Directory.CreateDirectory(cache + "vita/pcks"); - PCKCenterJSON Local = LocalAction.GetLocalJSON(Category, IsVita); - switch (IsVita) - { - case (false): - image.Save(cache + "normal/images/" + packID + ".png"); - client.DownloadFile(Program.BaseAPIUrl + "/center/packs/normal/pcks/" + packID + ".pck", cache + "normal/pcks/" + packID + ".pck"); - break; - case (true): - image.Save(cache + "vita/images/" + packID + ".png"); - client.DownloadFile(Program.BaseAPIUrl + "/center/packs/vita/pcks/" + packID + ".pck", cache + "vita/pcks/" + packID + ".pck"); - break; - } - Local = LocalAction.AddPack(Local, CenterPacks.Data[packID.ToString()], packID); - LocalAction.SaveLocalJSON(Local, Category, IsVita); - LocalAction.SaveLocalCategories(IsVita); - /**/ - image.Dispose(); - return true; - } - catch - { - return false; - } - } - - public void TryTestPackInfo(bool IsVita) - { - try - { - WebClient wc = new WebClient(); - string CategoryJSON = wc.DownloadString(Program.BaseAPIUrl + "/center/packs/Categiories.json"); - string[] Categories = JsonConvert.DeserializeObject(CategoryJSON); - PCKCenterJSON Result = pk1(Categories[2]); - Console.Write(""); // this is a breakpoint - - } - catch - { - Console.Write(""); // this is a breakpoint - } - } - PCKCenterJSON pk1(string categorie) - { - WebClient wc = new WebClient(); - string DataJSON = wc.DownloadString(Program.BaseAPIUrl + "/center/packs/normal/" + categorie + ".json"); - PCKCenterJSON Data = JsonConvert.DeserializeObject(DataJSON); - return Data; - } - } -} diff --git a/PCK-Studio/Forms/Utilities/PckCenterBeta.cs b/PCK-Studio/Forms/Utilities/PckCenterBeta.cs index 852461db..d96eab9b 100644 --- a/PCK-Studio/Forms/Utilities/PckCenterBeta.cs +++ b/PCK-Studio/Forms/Utilities/PckCenterBeta.cs @@ -149,7 +149,7 @@ namespace PckStudio.Forms.Utilities { try { - Collections.TryDownloadPack(int.Parse(OnlineTreeView.SelectedNode.Tag.ToString()), VitaCheckBox.Checked, CategoryComboBox.Text); + Collections.TryDownloadPack(CategoryComboBox.Text, int.Parse(OnlineTreeView.SelectedNode.Tag.ToString()), VitaCheckBox.Checked); MessageBox.Show("Download complete");/**/ } catch diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index f780dca6..541d1abe 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -136,6 +136,8 @@ + + @@ -169,7 +171,6 @@ - @@ -201,7 +202,6 @@ -