213 lines
6.9 KiB
C#
213 lines
6.9 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.IO;
|
|
using Newtonsoft.Json.Linq;
|
|
using Crosstales.FB;
|
|
using TMPro;
|
|
|
|
public class Controller : MonoBehaviour
|
|
{
|
|
[SerializeField] private OnlineAccountHandler OAH;
|
|
[SerializeField] private AlertBox alertBox;
|
|
[SerializeField] private DownloadMod DM;
|
|
[SerializeField] private Coder coder;
|
|
[SerializeField] private SMImportList SMIL;
|
|
[SerializeField] private NexusModController NMC;
|
|
[SerializeField] private ImportImages II;
|
|
[SerializeField] public string CurrentJSON;
|
|
[SerializeField] private GameObject AdminButton;
|
|
[SerializeField] private TextMeshProUGUI DownloadFolderText;
|
|
private string DownloadsFolder;
|
|
private string JSONPath;
|
|
|
|
void Start() {
|
|
JSONPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+@"\Schaken-Mods®\Singularity\Mods\";
|
|
if (!Directory.Exists(JSONPath)) {
|
|
Directory.CreateDirectory(JSONPath);
|
|
}
|
|
}
|
|
|
|
public void DeepLink(string A) {
|
|
if (A.Contains("schakenmods://ModHandler")) {
|
|
if (A.Contains("/Login/?code=")) {
|
|
string B = A.Split(S("?code="))[1].Split("&")[0]; //key
|
|
string C = A.Split(S("&state="))[1]; //Safety
|
|
if (C == OAH.SecurityKey)
|
|
OAH.Login(B);
|
|
}
|
|
if (A.Contains("?Loc=SM&")) {
|
|
string ID = A.Split(S("&download="))[1];
|
|
StartCoroutine(LoadBigMod(ID));
|
|
}
|
|
if (A.Contains("ModHandler/Import=")) {
|
|
SMIL.ImportList(A);
|
|
}
|
|
if (A.Contains("ModHandler/NexusLogin/")) {
|
|
string ID = A.Split(S("ModHandler/NexusLogin/"))[1];
|
|
NMC.GetUser(ID);
|
|
}
|
|
if (A.Contains("/Nexus/MyMods/")) {
|
|
string ID = A.Split(S("/Nexus/MyMods/"))[1];
|
|
NMC.GetMyMods(ID);
|
|
}
|
|
if (A.Contains("/ModHandler/NexusMod/htt")) {
|
|
string Link = A.Split(S("schakenmods://ModHandler/NexusMod/"))[1];
|
|
NMC.DownloadMod(Link);
|
|
}
|
|
if (A.Contains("ModHandler/ImportImages")) {
|
|
II.FillMe(A);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetDownloadsFolder(string DF) {
|
|
DownloadFolderText.text = DF;
|
|
DownloadsFolder = DF;
|
|
}
|
|
|
|
private string S(string A) {
|
|
return A;
|
|
}
|
|
|
|
IEnumerator LoadBigMod(string ID) {
|
|
using (UnityWebRequest uwr = UnityWebRequest.Get($"https://schaken-mods.com/api/downloads/files/{ID}")) {
|
|
uwr.SetRequestHeader("Authorization", "Bearer " + OAH.BearerID);
|
|
yield return uwr.SendWebRequest();
|
|
if (uwr.result == UnityWebRequest.Result.ConnectionError) {
|
|
Debug.LogError("Error While Sending TEST: " + uwr.error);
|
|
Alert("Schaken-Mods", "Error While Sending: " + uwr.error);
|
|
} else {
|
|
CurrentJSON = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(uwr.downloadHandler.text), Formatting.Indented);
|
|
SchakenMods.Download mod = JsonConvert.DeserializeObject<SchakenMods.Download>(uwr.downloadHandler.text);
|
|
SchakenMods NewSM = new() {
|
|
Mod = mod
|
|
};
|
|
|
|
if (!DM.gameObject.activeSelf) {
|
|
DM.gameObject.SetActive(true);
|
|
}
|
|
DM.FillMe(NewSM);
|
|
}
|
|
}
|
|
}
|
|
|
|
private string FormatDate(string dateString) {
|
|
return dateString.Split(" ")[0];
|
|
}
|
|
|
|
public void DownloadFile(string file, string fileName, ModListPrefab A) {
|
|
StartCoroutine(DownloadFileA(file, fileName, A));
|
|
}
|
|
|
|
public IEnumerator DownloadFileA(string file, string fileName, ModListPrefab A, bool TF = true) {
|
|
string path = DownloadsFolder;
|
|
if (path == null || path.Length < 4)
|
|
path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+@"\Downloads";
|
|
string fullPath = Path.Combine(path, fileName);
|
|
|
|
var dh = new DownloadHandlerFile(fullPath) {
|
|
removeFileOnAbort = true
|
|
};
|
|
// Debug.Log(file);
|
|
var uwr = new UnityWebRequest(file, UnityWebRequest.kHttpVerbGET) {
|
|
downloadHandler = dh
|
|
};
|
|
|
|
var operation = uwr.SendWebRequest();
|
|
// Debug.Log("Web request sent");
|
|
while (!operation.isDone) {
|
|
A.UpdateProgress(uwr.downloadProgress);
|
|
// Debug.Log($"Downloaded {uwr.downloadProgress}");
|
|
yield return new WaitForSeconds(0.5f);
|
|
}
|
|
if (uwr.result == UnityWebRequest.Result.Success) {
|
|
// Debug.Log("Download complete");
|
|
A.UpdateProgress(1f);
|
|
} else {
|
|
Debug.LogError($"Download failed: {uwr.error}");
|
|
}
|
|
yield return new WaitForSeconds(0.25f);
|
|
StartCoroutine(GetNewModJSON(A));
|
|
coder.HideMessageInBinaryFile(fullPath, OAH.UserID, A.ID);//, "PathToOutputFile");
|
|
uwr.Dispose();
|
|
|
|
// if (TF) {// Lets make a new function just to refresh the JSON.
|
|
// }
|
|
}
|
|
|
|
public void SelectNewDownloadsFolder() {
|
|
string a = DownloadsFolder;
|
|
if (a == "" || a == null) {
|
|
a = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+@"\Downloads";
|
|
}
|
|
|
|
string file = FileBrowser.Instance.OpenSingleFolder("Pick a Download folder", a);
|
|
if (a != file) {
|
|
DownloadFolderText.text = file;
|
|
Settings.AppSettings setting = new() {
|
|
DownloadsFolder = file
|
|
};
|
|
string prettyJson = JsonConvert.SerializeObject(setting, Formatting.Indented);
|
|
OAH.Setting.DownloadsFolder = file;
|
|
OAH.SaveSettings(prettyJson, 2);
|
|
}
|
|
}
|
|
|
|
public string SanitizeFileName(string fileName) {
|
|
string invalidChars = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
|
|
foreach (char c in invalidChars) {
|
|
fileName = fileName.Replace(c.ToString(), string.Empty);
|
|
}
|
|
return fileName;
|
|
}
|
|
|
|
public void ActivateAdmin() {
|
|
AdminButton.SetActive(true);
|
|
}
|
|
|
|
IEnumerator GetNewModJSON(ModListPrefab A) {
|
|
string id = A.ID;
|
|
if (A.Mod.Mod.Url.ToLower().Contains("schaken-mods.com")) {
|
|
using (UnityWebRequest uwr = UnityWebRequest.Get($"https://schaken-mods.com/api/downloads/files/{id}")) {
|
|
uwr.SetRequestHeader("Authorization", "Bearer " + DM.OAH.BearerID);
|
|
yield return uwr.SendWebRequest();
|
|
if (uwr.result == UnityWebRequest.Result.ConnectionError) {
|
|
Debug.LogError("Error While Sending TEST: " + uwr.error);
|
|
Alert("Schaken-Mods", "Error While Sending: " + uwr.error);
|
|
} else {
|
|
string Path = $"{JSONPath}/{SanitizeFileName(id)}.sing";
|
|
if (File.Exists(Path)) {
|
|
File.Delete(Path);
|
|
}
|
|
SchakenMods.Download mod = JsonConvert.DeserializeObject<SchakenMods.Download>(uwr.downloadHandler.text);
|
|
|
|
DateTime now = DateTime.UtcNow; // Get the current UTC time
|
|
string Date = now.ToString("yyyy-MM-ddTHH:mm:ssZ");
|
|
|
|
string prettyJson = JsonConvert.SerializeObject(mod, Formatting.Indented);
|
|
JObject jsonObject = JObject.Parse(prettyJson);
|
|
jsonObject["downloaded"] = Date;
|
|
string TempCurrentJSON = jsonObject.ToString();
|
|
TempCurrentJSON = TempCurrentJSON.Split(S("\"downloaded\": \""))[0]+"\"downloaded\": \""+Date+"\""+TempCurrentJSON.Split(S("\"downloaded\": \""))[1].Split("\"")[1];
|
|
|
|
File.WriteAllText(Path, TempCurrentJSON);
|
|
mod = JsonConvert.DeserializeObject<SchakenMods.Download>(TempCurrentJSON);
|
|
A.Mod.Mod = mod;
|
|
A.Mod.Mod.Downloaded = mod.Downloaded;
|
|
A.Downloaded.text = $"Downloaded: {mod.Downloaded.ToLocalTime()}";
|
|
A.CheckForModUpdates();
|
|
//need to check if up to date after this
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Alert(string title, string message) {
|
|
alertBox.FillMe(title, message);
|
|
}
|
|
}
|