192 lines
7.0 KiB
C#
192 lines
7.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Assets.Scripts
|
|
{
|
|
public class FileHandler
|
|
{
|
|
static StreamWriter sw;
|
|
static string settingsPath = "./settings.txt";
|
|
public static void saveGame(string data, string path)
|
|
{
|
|
sw = new StreamWriter(path);
|
|
sw.Write(data);
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
|
|
public static void loadGame(Player player, WorldGenerator worldGenerator, Inventory inventory, QuestLog questLog)
|
|
{
|
|
if (hasSaveFile())
|
|
{
|
|
string[] lines = File.ReadAllLines("./save.json");
|
|
string jsonString = "";
|
|
foreach (string line in lines)
|
|
{
|
|
jsonString = jsonString + line.Replace("\r\n", "");
|
|
}
|
|
JObject json = JsonConvert.DeserializeObject<JObject>(jsonString);
|
|
player.loadPlayer(json["player"]);
|
|
worldGenerator.loadWorld(json["world"]);
|
|
inventory.loadInventory(json["inventory"]);
|
|
questLog.loadQuests(json["questlog"]);
|
|
}
|
|
}
|
|
|
|
public static void saveOptions(string saveText){
|
|
sw = new StreamWriter(settingsPath);
|
|
sw.Write(saveText);
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
|
|
public static void loadOptions(bool isIngame){
|
|
if (!File.Exists(settingsPath))
|
|
{
|
|
sw = File.CreateText(settingsPath);
|
|
sw.WriteLine("Music:0.5");
|
|
sw.WriteLine("Effects:0.5");
|
|
sw.WriteLine("Resolution:0");
|
|
sw.WriteLine("Mode:0");
|
|
sw.WriteLine("SensitivityMouse:1");
|
|
sw.WriteLine("SensitivityController:1");
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
string[] lines = File.ReadAllLines(settingsPath);
|
|
foreach(string line in lines){
|
|
switch(line.Split(":")[0]){
|
|
case "Music":
|
|
GameObject.Find("Main Camera").GetComponent<AudioSource>().volume = float.Parse(line.Split(':')[1]);
|
|
break;
|
|
case "Effects":
|
|
GameObject.Find("Player").GetComponent<AudioSource>().volume = float.Parse(line.Split(':')[1]);
|
|
break;
|
|
case "Resolution":
|
|
switch(line.Split(":")[1].ToLower()){
|
|
case "0":
|
|
Screen.SetResolution(800, 600, Screen.fullScreenMode);
|
|
break;
|
|
case "1":
|
|
Screen.SetResolution(1280, 800, Screen.fullScreenMode);
|
|
break;
|
|
case "2":
|
|
Screen.SetResolution(1920, 1080, Screen.fullScreenMode);
|
|
break;
|
|
}
|
|
break;
|
|
case "Mode":
|
|
switch(line.Split(":")[1].ToLower()){
|
|
case "0":
|
|
Screen.fullScreenMode = FullScreenMode.Windowed;
|
|
break;
|
|
case "1":
|
|
Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
|
|
break;
|
|
case "2":
|
|
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
|
|
break;
|
|
}
|
|
break;
|
|
case "SensitivityMouse":
|
|
if(isIngame){
|
|
GameObject.Find("Main Camera").GetComponent<PlayerCamera>().mouseSpeed = float.Parse(line.Split(':')[1]);
|
|
}
|
|
break;
|
|
case "SensitivityController":
|
|
if(isIngame){
|
|
GameObject.Find("Main Camera").GetComponent<PlayerCamera>().controllerSpeed = float.Parse(line.Split(':')[1]);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void loadOptionDisplay(){
|
|
string[] lines = File.ReadAllLines(settingsPath);
|
|
foreach(string line in lines){
|
|
switch(line.Split(":")[0]){
|
|
case "Music":
|
|
GameObject.Find("slideMusic").GetComponent<Slider>().value = float.Parse(line.Split(':')[1]);
|
|
break;
|
|
case "Effects":
|
|
GameObject.Find("slideEffects").GetComponent<Slider>().value = float.Parse(line.Split(':')[1]);
|
|
break;
|
|
case "Resolution":
|
|
GameObject.Find("dropResolution").GetComponent<Dropdown>().value = int.Parse(line.Split(':')[1]);
|
|
break;
|
|
case "Mode":
|
|
GameObject.Find("dropMode").GetComponent<Dropdown>().value = int.Parse(line.Split(':')[1]);
|
|
break;
|
|
case "SensitivityMouse":
|
|
GameObject.Find("slideSensitivityMouse").GetComponent<Slider>().value = float.Parse(line.Split(':')[1]);
|
|
break;
|
|
case "SensitivityController":
|
|
GameObject.Find("slideSensitivityController").GetComponent<Slider>().value = float.Parse(line.Split(':')[1]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static string generateJSON(string key, object value)
|
|
{
|
|
return "\"" + key + "\": " + value;
|
|
}
|
|
|
|
public static bool hasSaveFile()
|
|
{
|
|
return File.Exists("./save.json");
|
|
}
|
|
|
|
public static void saveTile(string content, string path)
|
|
{
|
|
sw = new StreamWriter(path);
|
|
sw.Write(content);
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
|
|
public static void saveNoise(string content, string path)
|
|
{
|
|
sw = new StreamWriter(path, true);
|
|
sw.Write(content);
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
|
|
public static void generateDirectory()
|
|
{
|
|
if (Directory.Exists("./save/"))
|
|
{
|
|
foreach (string file in Directory.GetFiles("./save/"))
|
|
{
|
|
File.Delete(file);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Directory.CreateDirectory("./save/");
|
|
}
|
|
}
|
|
|
|
public static string loadTile(string path)
|
|
{
|
|
string result = "";
|
|
string[] lines = File.ReadAllLines(path);
|
|
foreach (string line in lines)
|
|
{
|
|
result = result + line.Replace("\r\n", "");
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|