Singularity/Assets/Scripts/DownloadFileChecker.cs
2024-05-06 11:45:45 -07:00

189 lines
8.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class DownloadFileChecker : MonoBehaviour
{
[SerializeField] private Coder coder;
[SerializeField] private OnlineAccountHandler OAH;
[SerializeField] private Controller Con;
[SerializeField] private GameObject AdminFileChecker;
[SerializeField] private TextMeshProUGUI deleteMe;
[Header("\tUser Info")]
[SerializeField] private RawImage ProfilePicture;
[SerializeField] private RawImage BackgroundImage;
[SerializeField] private GameObject UserInfoPrefab;
[SerializeField] private Transform UserInfoContainer;
[SerializeField] private List<GameObject> UserInfoList;
[Header("\tUser Options")]
[SerializeField] private GameObject UserOptionPrefab;
[SerializeField] private Transform UserOptionContainer;
[SerializeField] private List<GameObject> UserOptionList;
[Header("\tMod Info")]
[SerializeField] private TextMeshProUGUI Description;
[SerializeField] private RawImage ModOwnerPicture;
[SerializeField] private RawImage ModImage;
[SerializeField] private Transform ModInfoContainer;
[SerializeField] private List<GameObject> ModInfoList;
private readonly string SMAPIKey = "46c5a181399ac4e96213c0b078d22140";
private string UserURL;
private string ModURL;
private SchakenMods.Download CurrentMod;
private SchakenMods.User Downloader;
// Start is called before the first frame update
void Start()
{
Con = gameObject.GetComponent<Controller>();
}
// Update is called once per frame
void Update()
{
}
[ContextMenu("Test")]
private void TestUser() {
GetFileInfo(@"C:\Users\Schaken\Downloads\DAZ_SMMBPreset.7z");
}
public void GetFileInfo(string FilePath) {
if (OAH.isAdmin) {
string Code = coder.RetrieveMessageFromBinaryFile(FilePath);
if (Code.ToLower().Contains("separator not found")) {
Con.Alert("Admin", "User information is not present");
return;
}
string UserID = Code.Split("?")[0];
string ModID = Code.Split("?")[1];
StartCoroutine(GetUserInfo(UserID));
StartCoroutine(GetModInfo(ModID));
AdminFileChecker.SetActive(true);
}
}
IEnumerator GetUserInfo(string UserID) {
var url = $"https://schaken-mods.com/api/core/members/{UserID}";
using (UnityWebRequest uwr = UnityWebRequest.Get(url)) {
uwr.SetRequestHeader("Authorization", "Bearer " + OAH.BearerID);
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError) {
Debug.LogError("Error While Sending for User info: " + uwr.error);
Con.Alert("Schaken-Mods", "Error While Sending for User info: " + uwr.error);
} else {
string result = uwr.downloadHandler.text;
string FullTXT = result.Replace(@"\", string.Empty);
string wrappedResult = "{ \"author\": " + result + " }";
SchakenMods Author = JsonConvert.DeserializeObject<SchakenMods>(wrappedResult);
Downloader = Author.Author;
if (FullTXT.Contains("photoUrl") == true) {
StartCoroutine(OAH.LoadCurrentImage(Author.Author.PhotoUrl, ProfilePicture));
StartCoroutine(OAH.LoadCurrentImage(Author.Author.CoverPhotoUrl, BackgroundImage));
UserURL = Author.Author.ProfileUrl;
Dictionary<string, string> UserInfo = new() {
{ "ID", Author.Author.Id.ToString() },
{ "Name", Author.Author.Name },
{ "Joined", OAH.FormatDate(Author.Author.Joined.ToString()) },
{ "Reputation", Author.Author.ReputationPoints.ToString() },
{ "Posts", Author.Author.Posts.ToString() },
{ "Last Activity", OAH.FormatDate(Author.Author.LastActivity.ToString()) },
{ "Profile Views", Author.Author.ProfileViews.ToString() },
{ "Birthday", (Author.Author.Birthday == null) ? "Undefined" : OAH.FormatDate(Author.Author.Birthday.ToString()) },
{ "Achievements", Author.Author.AchievementsPoints.ToString() },
{ "Primary Group", Author.Author.PrimaryGroup.Name },
// { "Last Visit", OAH.FormatDate(Author.Author.LastVisit.ToString()) },
// { "Last Post", OAH.FormatDate(Author.Author.LastPost.ToString()) }
};
for (int i = 0; i < UserInfoList.Count; i++) {
DestroyImmediate(UserInfoList[i]);
}
UserInfoList.Clear();
foreach (KeyValuePair<string, string> info in UserInfo) {
GameObject NewInfo = Instantiate(UserInfoPrefab, UserInfoContainer);
NewInfo.GetComponent<ModInfoPrefab>().FillMe(info.Key, info.Value);
UserInfoList.Add(NewInfo);
}
}
}
}
for (int i = 0; i < UserOptionList.Count; i++) {
DestroyImmediate(UserOptionList[i]);
}
UserOptionList.Clear();
AddButton("Go To Admin Profile", $"https://schaken-mods.com/admin/?app=core&module=members&controller=members&do=view&id={Downloader.Id}");
AddButton("Go To Public Profile", Downloader.ProfileUrl);
AddButton("Write Message", $"https://schaken-mods.com/messenger/compose/?to={Downloader.Id}");
AddButton("Add Warning", $"https://schaken-mods.com/profile/{Downloader.Id}-{Downloader.Name}/warnings/add/");
}
private GameObject AddButton(string Text, string Link) {
GameObject NewButton = Instantiate(UserOptionPrefab, UserOptionContainer);
NewButton.GetComponent<ButtonPrefab>().FillMe(Text, Link);
UserOptionList.Add(NewButton);
return NewButton;
}
public void VisitProfile() {
Application.OpenURL(UserURL);
}
IEnumerator GetModInfo(string ID) {
using (UnityWebRequest uwr = UnityWebRequest.Get($"https://schaken-mods.com/api/downloads/files/{ID}?key={SMAPIKey}")) {
// uwr.SetRequestHeader("Authorization", "Bearer " + OAH.BearerID);
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError) {
Debug.LogError("Error While Sending TEST: " + uwr.error);
Con.Alert("Schaken-Mods", "Error While Sending for User info: " + uwr.error);
} else {
SchakenMods.Download mod = JsonConvert.DeserializeObject<SchakenMods.Download>(uwr.downloadHandler.text);
CurrentMod = mod;
if (mod.Title.Length > 1) {
StartCoroutine(OAH.LoadCurrentImage(mod.ModAuthor.PhotoUrl, ModOwnerPicture));
StartCoroutine(OAH.LoadCurrentImage(mod.PrimaryScreenshot.Url, ModImage));
ModURL = mod.Url;
Dictionary<string, string> ModInfo = new() {
{ "ID", mod.Id.ToString() },
{ "Title", mod.Title },
{ "category", mod.ModCategory.Name },
{ "Posted Date", OAH.FormatDate(mod.Date.ToString()) },
{ "Last Updated", OAH.FormatDate(mod.Updated.ToString()) },
{ "Last Downloaded", OAH.FormatDate(mod.Downloaded.ToString()) },
{ "Version", mod.Version.ToString() },
{ "Downloads", mod.Downloads.ToString() },
{ "Comments", mod.Comments.ToString() },
{ "Reviews", mod.Reviews.ToString() },
{ "Views", mod.Views.ToString() },
{ "Hidden", mod.Hidden.ToString() },
{ "Rating", mod.Rating.ToString() }
};
Description.text = mod.Description;
for (int i = 0; i < ModInfoList.Count; i++) {
DestroyImmediate(ModInfoList[i]);
}
ModInfoList.Clear();
foreach (KeyValuePair<string, string> info in ModInfo) {
GameObject NewInfo = Instantiate(UserInfoPrefab, ModInfoContainer);
NewInfo.GetComponent<ModInfoPrefab>().FillMe(info.Key, info.Value);
ModInfoList.Add(NewInfo);
}
}
}
}
}
}