chunk 1: core gameplay scripts scenes runtime assets
This commit is contained in:
92
Assets/Scripts/SaveSystem/Services/ApiService.cs
Normal file
92
Assets/Scripts/SaveSystem/Services/ApiService.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
public static class ApiService
|
||||
{
|
||||
private const string BASE_URL = "http://127.0.0.1:8001";
|
||||
|
||||
public static async Task<GameData> FetchUserGameData()
|
||||
{
|
||||
try
|
||||
{
|
||||
string url = $"{BASE_URL}/api/game/";
|
||||
|
||||
using (UnityWebRequest request = UnityWebRequest.Get(url))
|
||||
{
|
||||
string token = PlayerPrefs.GetString("AuthToken");
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
Debug.LogError("No auth token found");
|
||||
throw new UnauthorizedAccessException("No auth token found");
|
||||
}
|
||||
|
||||
request.SetRequestHeader("Authorization", $"Token {token}");
|
||||
request.SetRequestHeader("Accept", "application/json");
|
||||
|
||||
var operation = request.SendWebRequest();
|
||||
while (!operation.isDone)
|
||||
await Task.Yield();
|
||||
|
||||
Debug.Log($"Response Code: {request.responseCode}");
|
||||
Debug.Log($"Response: {request.downloadHandler.text}");
|
||||
|
||||
if (request.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
string jsonResponse = request.downloadHandler.text;
|
||||
Debug.Log($"Received data: {jsonResponse}");
|
||||
return JsonConvert.DeserializeObject<GameData>(jsonResponse);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"API Error: {request.responseCode} - {request.error}");
|
||||
throw new Exception($"Server error: {request.error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Failed to fetch game data: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task SaveUserGameData(GameData gameData)
|
||||
{
|
||||
string token = PlayerPrefs.GetString("AuthToken");
|
||||
string jsonData = JsonConvert.SerializeObject(gameData);
|
||||
|
||||
using (UnityWebRequest request = new UnityWebRequest($"{BASE_URL}/api/game/save", "POST"))
|
||||
{
|
||||
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
|
||||
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.SetRequestHeader("Authorization", $"Token {token}");
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
|
||||
try
|
||||
{
|
||||
var operation = request.SendWebRequest();
|
||||
while (!operation.isDone)
|
||||
await Task.Yield();
|
||||
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
if (request.responseCode == 401)
|
||||
{
|
||||
throw new UnauthorizedAccessException("Authentication failed");
|
||||
}
|
||||
throw new Exception($"Failed to save: {request.error}");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Failed to save game data: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/SaveSystem/Services/ApiService.cs.meta
Normal file
2
Assets/Scripts/SaveSystem/Services/ApiService.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba8e7ec37c2833f45bf7c48206d8e110
|
||||
80
Assets/Scripts/SaveSystem/Services/JsonDataService.cs
Normal file
80
Assets/Scripts/SaveSystem/Services/JsonDataService.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
public class JsonDataService : IDataService
|
||||
{
|
||||
public bool SaveData<T>(string relativePath, T data, bool encrypted)
|
||||
{
|
||||
string path = Application.persistentDataPath + relativePath;
|
||||
try
|
||||
{
|
||||
string jsonData = JsonConvert.SerializeObject(data, Formatting.Indented);
|
||||
if (encrypted)
|
||||
{
|
||||
jsonData = EncryptDecrypt(jsonData);
|
||||
}
|
||||
using (FileStream stream = new FileStream(path, FileMode.Create))
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(stream))
|
||||
{
|
||||
writer.Write(jsonData);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Failed to save data: {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public T LoadData<T>(string relativePath, bool encrypted)
|
||||
{
|
||||
string path = Application.persistentDataPath + relativePath;
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
throw new FileNotFoundException($"Cannot load file at {path}. File does not exist!");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string jsonData = "";
|
||||
using (FileStream stream = new FileStream(path, FileMode.Open))
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(stream))
|
||||
{
|
||||
jsonData = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
if (encrypted)
|
||||
{
|
||||
jsonData = EncryptDecrypt(jsonData);
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(jsonData);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Failed to load data: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private string EncryptDecrypt(string data)
|
||||
{
|
||||
// Simple XOR encryption (replace with more secure method for production)
|
||||
string key = "YOUR_ENCRYPTION_KEY";
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
result.Append((char)(data[i] ^ key[i % key.Length]));
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9317a7208a9f0ac40b2962d943b9bbc9
|
||||
Reference in New Issue
Block a user