This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
forked from flarfo/Muck-SaveUtility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveSystem.cs
68 lines (58 loc) · 2.12 KB
/
SaveSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using UnityEngine;
using System.IO;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
namespace SaveUtility
{
public static class SaveSystem
{
public static void Save(string saveName)
{
string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves", saveName);
WorldSave worldSave = new WorldSave(0);
if (worldSave.position != null)
{
using (var stream = new FileStream(savePath, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(WorldSave));
serializer.Serialize(stream, worldSave);
Path.ChangeExtension(savePath, "muck");
}
}
}
public static WorldSave Load(string saveName)
{
Debug.Log("Loading");
string loadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves", saveName);
if (File.Exists(loadPath))
{
using (var stream = new FileStream(loadPath, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(WorldSave));
WorldSave loadedWorld = (WorldSave)serializer.Deserialize(stream);
return loadedWorld;
}
}
else
{
Debug.LogError($"Save File does not Exist at {loadPath}");
return null;
}
}
public static string[] GetAllSaves()
{
string[] saveFileNames = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves"));
List<string> savePaths = new List<string>();
for (int i = 0; i < saveFileNames.Length; i++)
{
if (Path.GetExtension(saveFileNames[i]) == ".muck")
{
savePaths.Add(Path.GetFileName(saveFileNames[i]));
}
}
return savePaths.ToArray();
}
}
}