Firstborn/Assets/Scripts/ModManager.cs

119 lines
4.9 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class ModManager : MonoBehaviour
{
[SerializeField] private OnlineAccountHandler Online;
[SerializeField] private string Key;
[SerializeField] private GameObject ModPrefab;
[SerializeField] private GameObject CatPrefab;
[SerializeField] public List<GameObject> CreatedModPrefabs;
[SerializeField] public List<GameObject> CreatedCatPrefabs;
[SerializeField] private Transform ModContainer;
[SerializeField] private Transform CatContainer;
[SerializeField] private GameObject This;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GetCategory(int i) {
for (int j = 0; j < CreatedModPrefabs.Count; j++) {
CreatedModPrefabs[j].SetActive(false);
}
CreatedModPrefabs.Clear();
string url = "https://schaken-mods.com/api/downloads/files?key="+Key+"&categories="+i+"&sortDir=desc&hidden=0&page=1&sortDir=desc&hidden=0";
StartCoroutine(GetCategoryMods(url));
}
IEnumerator GetCategoryMods(string uri)
{
UnityWebRequest uwr = UnityWebRequest.Get(uri);
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError) {
Debug.Log("Error While Sending: " + uwr.error);
} else {
// string FullTXT = uwr.downloadHandler.text.Replace(@"\", string.Empty);
string FullTXT = System.Text.RegularExpressions.Regex.Unescape(uwr.downloadHandler.text);
string BreakingPoints = "hasPendingVersion";
string[] Content = FullTXT.Split(BreakingPoints);
for (int i = 0; i < (Content.Length - 1); i++) {
var NewMod = Instantiate(ModPrefab, ModContainer);
CreatedModPrefabs.Add(NewMod);
ModItem Mod = NewMod.GetComponent<ModItem>();
string NewScreenshot = "hi";
if (Content[i].Contains("\"primaryScreenshotThumb\": null")) {
NewScreenshot = "https://schaken-mods.com/uploads/monthly_2021_11/NoImage.png.7598ee76366f585b2bb10610b640e551.png";
} else {
NewScreenshot = Content[i].Split (new string[] { "primaryScreenshotThumb" }, StringSplitOptions.None)[1].Split (',')[1].Split (new string[] { "\"url\": \"" }, StringSplitOptions.None)[1].Split ("\"")[0].Trim ();
}
Mod.Title.text = Content[i].Split (new string[] { "\"title\": \"" }, StringSplitOptions.None)[1].Split ("\"")[0].Trim ();
Mod.Date.text = DateTime.Parse(Content[i].Split (new string[] { "\"date\": \"" }, StringSplitOptions.None)[1].Split ("\"")[0].Trim ()).ToString();
Mod.Author.text = Content[i].Split (new string[] { "\"name\": \"" }, StringSplitOptions.None)[2].Split ("\"")[0].Trim ();
Mod.ModID = Content[i].Split (new string[] { "\"id\": " }, StringSplitOptions.None)[1].Split (",")[0].Trim ();
string EndDescription = "\",";
string OrigDescription = Content[i].Split (new string[] { "\"description\": \"" }, StringSplitOptions.None)[1].Split (EndDescription)[0].Trim ();
Mod.Description.text = Regex.Replace(OrigDescription, "<.*?>", String.Empty);
Mod.LoadSS(NewScreenshot);
}
}
}
public void GetCategoryList(int i) {
for (int j = 0; j < CreatedCatPrefabs.Count; j++) {
CreatedCatPrefabs[j].SetActive(false);
}
CreatedCatPrefabs.Clear();
string url = "https://schaken-mods.com/api/downloads/categories?key="+Key+"&clubs=0&page=1&perPage=300";
StartCoroutine(GetCategoryMods(url, i));
}
IEnumerator GetCategoryMods(string uri, int j)
{
UnityWebRequest uwr = UnityWebRequest.Get(uri);
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError) {
Debug.Log("Error While Sending: " + uwr.error);
} else {
string FullTXT = System.Text.RegularExpressions.Regex.Unescape(uwr.downloadHandler.text);
Debug.Log("Forum REST API: "+FullTXT);
string BreakingPoints = "\"perm_3\"";
string[] Content = FullTXT.Split(BreakingPoints);
for (int i = 0; i < (Content.Length - 1); i++) {
if (Content[i].Contains("\"parentId\": "+j+",")) {
var NewCat = Instantiate(CatPrefab, CatContainer);
CreatedCatPrefabs.Add(NewCat);
CatItem Cat = NewCat.GetComponent<CatItem>();
Cat.CatName.text = Content[i].Split (new string[] { "\"name\": \"" }, StringSplitOptions.None)[1].Split ("\"")[0].Trim ();
Cat.CatID = Content[i].Split (new string[] { "\"id\": " }, StringSplitOptions.None)[1].Split (",")[0].Trim ();
ModManager TempThis = This.GetComponent<ModManager>();
Cat.ModMan = TempThis;
}
}
}
}
}