Reworked code, Code cleanup, No new Version
This commit is contained in:
122
Assets/Scripts/Handler/FileHandler.cs
Normal file
122
Assets/Scripts/Handler/FileHandler.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
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;
|
||||
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 saveAudio(string path, float music, float effects)
|
||||
{
|
||||
sw = new StreamWriter(path);
|
||||
sw.WriteLine("Music:" + music);
|
||||
sw.WriteLine("Effects:" + effects);
|
||||
sw.Flush();
|
||||
sw.Close();
|
||||
}
|
||||
|
||||
public static void loadAudio(string path, AudioSource cameraAudio, AudioSource playerAudio)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(path);
|
||||
cameraAudio.volume = float.Parse(lines[0].Split(':')[1]);
|
||||
playerAudio.volume = float.Parse(lines[1].Split(':')[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
sw = File.CreateText(path);
|
||||
sw.WriteLine("Music:0.5");
|
||||
sw.WriteLine("Effects:0.5");
|
||||
sw.Flush();
|
||||
sw.Close();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user