79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.IO;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
public class SMImportList : MonoBehaviour {
|
|
[SerializeField] private OnlineAccountHandler OAH;
|
|
[SerializeField] private ModList ML;
|
|
[SerializeField] private string JSONPath;
|
|
// Start is called before the first frame update
|
|
private readonly string SMAPIKey = "46c5a181399ac4e96213c0b078d22140";
|
|
void Start() {
|
|
JSONPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+@"\Schaken-Mods®\Singularity\Mods\";
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void ImportList(string URL) {
|
|
string[] A = URL.Split(",");
|
|
Debug.Log($"Mod Count: {A.Length}");
|
|
for (int i = 1; i < A.Length; i++) {
|
|
if (A[i].Length > 1)
|
|
StartCoroutine(GetModInfo(A[i]));
|
|
}
|
|
}
|
|
|
|
IEnumerator GetModInfo(string Input) {
|
|
string ID = Input.Split("?")[0];
|
|
string Date = DateConvert(Input.Split("?")[1]);
|
|
// Debug.Log($"GetModInfo({Input})");
|
|
if (!File.Exists($"{JSONPath}/{SanitizeFileName(ID)}.json")) {
|
|
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);
|
|
} else {
|
|
Debug.Log(uwr.downloadHandler.text);
|
|
JObject jsonObject = JObject.Parse(uwr.downloadHandler.text);
|
|
jsonObject["downloaded"] = Date;
|
|
string CurrentJSON = jsonObject.ToString();
|
|
CurrentJSON = CurrentJSON.Split(S("\"downloaded\": \""))[0]+"\"downloaded\": \""+Date+"\""+CurrentJSON.Split(S("\"downloaded\": \""))[1].Split("\"")[1];
|
|
File.WriteAllText($"{JSONPath}/{SanitizeFileName(ID)}.json", CurrentJSON);
|
|
SchakenMods.Download mod = JsonConvert.DeserializeObject<SchakenMods.Download>(CurrentJSON);
|
|
SchakenMods Mod = new() { Mod = mod};
|
|
ML.AddMod(Mod);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private string S(string A) {
|
|
return A;
|
|
}
|
|
|
|
private string DateConvert(string T)
|
|
{
|
|
|
|
long unixTimestamp = long.Parse(T);
|
|
DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).UtcDateTime;
|
|
return dateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
|
|
}
|
|
}
|