Firstborn/Assets/Scripts/MyGallery.cs

164 lines
5.9 KiB
C#
Raw Normal View History

using System.Collections; //
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
using System.Text;
using System.IO;
using UnityEngine.UI;
using RPGCreationKit;
using UnityEngine.Networking;
using System.Net; // For the Discord
using System.Drawing;
namespace RPGCreationKit
{
public class MyGallery : MonoBehaviour
{
[Header("Auto fills")]
[SerializeField] private OnlineAccountHandler OAH;
[SerializeField] private GameObject MyGalleryGO;
[SerializeField] private string[] Folders;
[SerializeField] private TextMeshProUGUI[] AvatarNames;
[SerializeField] public GameObject[] Screenshots;
[Header("Avatar")]
[SerializeField] private Transform FoldersContainer;
[SerializeField] List<GameObject> CreatedFolderPrefab;
[SerializeField] public AudioSource source;
[Header("Screenshots")]
[SerializeField] private GameObject FolderPrefab;
[SerializeField] private Transform ImagesContainer;
[SerializeField] public List<GameObject> CreatedImagePrefab;
[Header("Expanded Screenshot")]
[SerializeField] public RawImage BigImage;
[SerializeField] public GameObject BigImageContainer;
[SerializeField] public GameObject BigImageCloseButton;
[SerializeField] private TextMeshProUGUI ImageTitle;
[SerializeField] private TextMeshProUGUI ImageDescription;
[SerializeField] private TextMeshProUGUI FPlace;
[SerializeField] private TextMeshProUGUI FTime;
[SerializeField] private TextMeshProUGUI FExtension;
[SerializeField] public string FileDir;
public void RefreshAvatarList() {
Folders = System.IO.Directory.GetDirectories(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Screenshots","*", System.IO.SearchOption.AllDirectories);
for (int i = 0; i < Folders.Length; i++) {
var NewFolder = Instantiate(FolderPrefab, FoldersContainer);
CreatedFolderPrefab.Add(NewFolder);
string TempAvatarName = Folders[i].Split(new string[] { @"Firstborn\Screenshots\" }, StringSplitOptions.None)[1].Split (@"\")[0].Trim ();
AvatarFolderName NewFolderGO = NewFolder.GetComponent<AvatarFolderName>();
NewFolderGO.FolderPath = Folders[i];
NewFolderGO.ImagesContainer = ImagesContainer;
NewFolderGO.MyGalleryGO = MyGalleryGO;
ButtonSounds BS = NewFolderGO.GetComponent<ButtonSounds>();
BS.source = source;
NewFolderGO.AvatarName.text = TempAvatarName;
}
}
public void RefreshScreenshotList() {
Folders = System.IO.Directory.GetDirectories(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Screenshots","*", System.IO.SearchOption.AllDirectories);
for (int i = 0; i < Folders.Length; i++) {
var NewFolder = Instantiate(FolderPrefab, FoldersContainer);
CreatedFolderPrefab.Add(NewFolder);
string TempAvatarName = Folders[i].Split(new string[] { @"Firstborn\Screenshots\" }, StringSplitOptions.None)[1].Split (@"\")[0].Trim ();
Debug.Log(TempAvatarName);
AvatarFolderName NewFolderGO = NewFolder.GetComponent<AvatarFolderName>();
NewFolderGO.AvatarName.text = TempAvatarName;
}
}
public void ClearAllFolders() {
for (int i = 0; i < CreatedFolderPrefab.Count; i++) {
CreatedFolderPrefab[i].SetActive(false);
}
CreatedFolderPrefab.Clear();
}
public void ExpandImage(RawImage A) {
BigImage.texture = A.texture;
BigImage.SetNativeSize();
BigImage.SizeToParent();
BigImageCloseButton.SetActive(true);
BigImageContainer.SetActive(true);
}
public void PostInformation(FileInfo A) {
string TempName = A.Name.Split("_")[1].Split (".")[0].Trim ();
FPlace.text = TempName;
var CreatedAt = A.CreationTime;
FTime.text = CreatedAt.ToString();
FExtension.text = A.Extension;
}
public void UploadImage() {
StartCoroutine(PostTopicPostsA());
}
IEnumerator PostTopicPostsA() {
// StreamReader sr = new StreamReader(FileDir);
// string A = sr.ReadToEnd();
// sr.Close();
//byte[] imageArray = System.IO.File.ReadAllBytes(FileDir);
//string A = Convert.ToBase64String(File.ReadAllBytes(FileDir)).TrimEnd('=').Replace('+', '-').Replace('/', '_');//Convert.ToBase64String(imageArray);//;
//string A = @"data:image/png;base64," + Convert.ToBase64String(imageArray).TrimEnd('=').Replace('+', '-').Replace('/', '_');
// A = Base64Encode(A);
//string B = EncodeTo64(FileDir);
//A = System.Text.RegularExpressions.Regex.Escape(A);
string A = Base64Encode(FileDir);
Debug.Log("Image string: "+A);
Debug.Log("Imape Path: "+FileDir);
Dictionary<string, string> content = new Dictionary<string, string>();
content.Add("category", "27");
content.Add("image", A.TrimEnd('=').Replace('+', '-').Replace('/', '_'));//ImageString);
content.Add("caption", ImageTitle.text); // Filename?
content.Add("filename", ImageTitle.text); // Real title.
content.Add("description", ImageDescription.text); // Description.
using (UnityWebRequest www = UnityWebRequest.Post("https://schaken-mods.com/api/gallery/images", content)) {
www.SetRequestHeader("Authorization", "Bearer " + OAH.BearerID);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError) {
Debug.Log("Error While Sending TEST: " + www.error);
} else {
string result = www.downloadHandler.text;
string FullTXT = System.Text.RegularExpressions.Regex.Unescape(result);
Debug.Log("TEST: " + FullTXT);
}
}
}
public static string Base64Encode(string FileDirectory) {
StreamReader sr = new StreamReader(FileDirectory); // this makes the file into a string.
string A = sr.ReadToEnd();
sr.Close();
// byte[] imageArray = System.IO.File.ReadAllBytes(FileDirectory);
// string A = System.Convert.ToBase64String(imageArray);
return "data:image/png;base64,"+EncodeTo64(A);
}
public static string EncodeTo64(string toEncode) {
byte[] toEncodeAsBytes = Encoding.ASCII.GetBytes(toEncode);
return Convert.ToBase64String(toEncodeAsBytes);
}
}
}