Refactored and moved some PCK Center related classes

This commit is contained in:
miku-666
2022-12-19 17:31:42 +01:00
parent 9b29a02e09
commit 554370752d
6 changed files with 145 additions and 199 deletions

View File

@@ -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<string[]>
($"{Program.BaseAPIUrl}/center/packs/{(isVita ? "VitaCategiories.json" : "Categiories.json")}", client);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return Array.Empty<string>();
}
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<PCKCenterJSON>(cat);
return Data;
}
[Obsolete]
public string[] GetPackDescription(string category, bool isVita)
{
return Array.Empty<string>();
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<T>(string address, WebClient client = null)
{
WebClient wc = client ?? new WebClient();
string jsonData = wc.DownloadString(address);
return JsonConvert.DeserializeObject<T>(jsonData);
}
PCKCenterJSON DownloadCategoryJson(string categorie, bool isVita, WebClient client = null)
{
return DownloadAndDeserializeJson<PCKCenterJSON>
($"{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<string[]>(categoryJSON);
PCKCenterJSON result = DownloadCategoryJson(categories[2], false, client);
}
catch
{
}
}
}
}

View File

@@ -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
{

View File

@@ -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;

View File

@@ -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<string[]>(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<PCKCenterJSON>(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<string[]>(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<PCKCenterJSON>(DataJSON);
return Data;
}
}
}

View File

@@ -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

View File

@@ -136,6 +136,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\API\PCKCenter\model\PCKCenterJSON.cs" />
<Compile Include="Classes\API\PCKCenter\PCKCollections.cs" />
<Compile Include="Classes\API\PCKCenter\PCKCollectionsLocal.cs" />
<Compile Include="Classes\API\PCKCenter\SaveLocalJSON.cs" />
<Compile Include="Classes\FileTypes\ARCFile.cs" />
<Compile Include="Classes\FileTypes\BehaviourFile.cs" />
@@ -169,7 +171,6 @@
<Compile Include="Classes\IO\GRF\GRFFileWriter.cs" />
<Compile Include="Classes\IO\LOC\LOCFileReader.cs" />
<Compile Include="Classes\IO\LOC\LOCFileWriter.cs" />
<Compile Include="Classes\IO\PCKCollectionsLocal.cs" />
<Compile Include="Classes\IO\PCK\PCKFileReader.cs" />
<Compile Include="Classes\IO\PCK\PCKFileWriter.cs" />
<Compile Include="Classes\IO\StreamDataReader.cs" />
@@ -201,7 +202,6 @@
<Compile Include="Classes\Models\TexelComparer.cs" />
<Compile Include="Classes\Models\TexturePlane.cs" />
<Compile Include="Classes\Networking\Network.cs" />
<Compile Include="Classes\Networking\PCKCollections.cs" />
<Compile Include="Classes\Misc\RichPresenceClient.cs" />
<Compile Include="Classes\Networking\Update.cs" />
<Compile Include="Classes\Utils\GRF\CRC32.cs" />