155 lines
6.2 KiB
C#
155 lines
6.2 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.ComponentModel.Design.Serialization;
|
|||
|
|
using TMPro;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
using UnityEngine.Networking;
|
|||
|
|
public class CreatorMenu : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public string[] files;
|
|||
|
|
public GameObject Canv;
|
|||
|
|
public List<Button> starts;
|
|||
|
|
public List<Button> upload;
|
|||
|
|
public List<Button> edit;
|
|||
|
|
public GameObject LevelOpener;
|
|||
|
|
public GameObject Broken;
|
|||
|
|
public List<GameObject> Openers;
|
|||
|
|
public List<GameObject> broken;
|
|||
|
|
public RectTransform LevelList;
|
|||
|
|
public List<Button.ButtonClickedEvent> Open;
|
|||
|
|
public List<Button.ButtonClickedEvent> uplo;
|
|||
|
|
public List<Button.ButtonClickedEvent> Edit;
|
|||
|
|
public TransferScript TS;
|
|||
|
|
public List<List<GameObject>> Groups;
|
|||
|
|
string[] filem;
|
|||
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
filem = (Directory.GetFiles(Application.persistentDataPath));
|
|||
|
|
List<string> filess = new List<string> (filem.Where(s => s.Contains(".lpj")));
|
|||
|
|
|
|||
|
|
files = filess
|
|||
|
|
.Where(s => !s.Contains(".lpjl"))
|
|||
|
|
.ToArray();
|
|||
|
|
for (int i = 0; i < files.Length; i++)
|
|||
|
|
{
|
|||
|
|
Debug.Log(filess[i]);
|
|||
|
|
try{
|
|||
|
|
int num = i;
|
|||
|
|
//files[i] = files[i].Remove(0, (Application.persistentDataPath).Length + 1);
|
|||
|
|
files[i] = files[i].Remove(files[i].Length - 4);
|
|||
|
|
Openers.Add(Instantiate(LevelOpener, LevelOpener.transform.parent.transform.parent));
|
|||
|
|
Openers[i].GetComponent<RectTransform>().position -= new Vector3(0, i * LevelOpener.GetComponent<RectTransform>().rect.height* Canv.transform.localScale.y*1.1f, 0);
|
|||
|
|
Openers[i].GetComponentInChildren<TMP_Text>().text = GetName(i);
|
|||
|
|
starts.Add(Openers[i].GetComponentInChildren<Button>());
|
|||
|
|
print(starts.IndexOf(starts[i]));
|
|||
|
|
Open.Add(new Button.ButtonClickedEvent { });
|
|||
|
|
Open[i].AddListener(() => PlayOpen(num));
|
|||
|
|
print(i);
|
|||
|
|
starts[i].onClick.AddListener(() => PlayOpen(num));
|
|||
|
|
upload.Add(Openers[i].transform.GetChild(1).GetComponent<Button>());
|
|||
|
|
uplo.Add(new Button.ButtonClickedEvent { });
|
|||
|
|
uplo[i].AddListener(() => Upl(num));
|
|||
|
|
upload[i].onClick.AddListener(() => Upl(num));
|
|||
|
|
if(PlayerPrefs.HasKey("AccountID") && PlayerPrefs.GetString("AccountID") != "0") upload[i].interactable = true;
|
|||
|
|
edit.Add(Openers[i].transform.GetChild(3).GetComponent<Button>());
|
|||
|
|
Edit.Add(new Button.ButtonClickedEvent { });
|
|||
|
|
Edit[i].AddListener(() => PlayOpen(num));
|
|||
|
|
edit[i].onClick.AddListener(() => PlayOpen(num));
|
|||
|
|
}
|
|||
|
|
catch(Exception ex)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
broken.Add(Instantiate(Broken, LevelOpener.transform.parent.transform.parent));
|
|||
|
|
broken[broken.Count-1].GetComponent<RectTransform>().position -= new Vector3(0, i * LevelOpener.GetComponent<RectTransform>().rect.height* Canv.transform.localScale.y*1.1f, 0);
|
|||
|
|
broken[broken.Count -1].transform.GetChild(1).transform.GetChild(0).GetComponentInChildren<TMP_Text>().text = Application.persistentDataPath+"/"+files[i]+".lpj";
|
|||
|
|
Debug.Log(ex.StackTrace);
|
|||
|
|
Debug.Log(ex.ToString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
LevelList.sizeDelta = new Vector2(0, files.Length * LevelOpener.GetComponent<RectTransform>().sizeDelta.y*1.1f);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
void PlayOpen(int n)
|
|||
|
|
{
|
|||
|
|
DontDestroyOnLoad(TS);
|
|||
|
|
print(n);
|
|||
|
|
TS.string1 = files[n];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string GetName(int num)
|
|||
|
|
{
|
|||
|
|
return (new string (LevelImport.ConvertFromByteArray(File.ReadAllBytes(files[num]+".lpj"))[0]));
|
|||
|
|
//print (files[num]+".lpj");
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
// Update is called once per frame
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string GetFileHash(byte[] fileData) {
|
|||
|
|
using var sha256 = System.Security.Cryptography.SHA256.Create();
|
|||
|
|
var hashBytes = sha256.ComputeHash(fileData);
|
|||
|
|
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
|
|||
|
|
}
|
|||
|
|
public void Upl(int Name)
|
|||
|
|
{
|
|||
|
|
Debug.Log("Try1");
|
|||
|
|
SendLevelToServer($"{files[Name]}.lpj", GetName(Name), new string(GetFileHash(File.ReadAllBytes($"{files[Name]}.lpj"))));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string uploadUrl = "http://mc1.live-on.pro:64096/api/levels/upload";
|
|||
|
|
|
|||
|
|
public void SendLevelToServer(string filePath, string levelName, string fileHash)
|
|||
|
|
{
|
|||
|
|
StartCoroutine(UploadRoutine(filePath, levelName, fileHash));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IEnumerator UploadRoutine(string filePath, string levelName, string fileHash)
|
|||
|
|
{
|
|||
|
|
if (!File.Exists(filePath))
|
|||
|
|
{
|
|||
|
|
Debug.LogError("[Unity] Файл не найден по пути: " + filePath);
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
byte[] fileData = File.ReadAllBytes(filePath);
|
|||
|
|
|
|||
|
|
// Создаем форму, которая имитирует отправку данных через браузер
|
|||
|
|
WWWForm form = new WWWForm();
|
|||
|
|
// Поле "file" должно совпадать с аргументом IFormFile в C# контроллере
|
|||
|
|
form.AddBinaryData("file", fileData, Path.GetFileName(filePath), "application/octet-stream");
|
|||
|
|
form.AddField("name", levelName);
|
|||
|
|
form.AddField("hash", fileHash);
|
|||
|
|
form.AddField("AuthorName", PlayerPrefs.GetString("PlayerName"));
|
|||
|
|
|
|||
|
|
using (UnityWebRequest www = UnityWebRequest.Post(uploadUrl, form))
|
|||
|
|
{
|
|||
|
|
Debug.Log("[Unity] Отправка запроса на " + uploadUrl);
|
|||
|
|
yield return www.SendWebRequest();
|
|||
|
|
|
|||
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|||
|
|
{
|
|||
|
|
// Если файл не пришел, здесь будет написана причина (например, 404 или 400)
|
|||
|
|
Debug.LogError($"[Unity] Ошибка: {www.error}");
|
|||
|
|
Debug.LogError($"[Unity] Ответ сервера: {www.downloadHandler.text}");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// Если всё ок, сервер пришлет JSON с UUID
|
|||
|
|
Debug.Log($"[Unity] Успех! Ответ сервера: {www.downloadHandler.text}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|