-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializeHelper.cs
148 lines (128 loc) · 4.74 KB
/
SerializeHelper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Limestone.Utility;
using Limestone.Tiles;
using Limestone.Entities;
using Limestone.Entities.Enemies;
using Limestone.Items;
using Limestone.Generation;
using Limestone.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters;
namespace Limestone
{
public static class SerializeHelper
{
public class TileConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(Tile).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject item = JObject.Load(reader);
if (item["tileType"].Value<int>() == 0)
{
return item.ToObject<TileFloor>();
}
else if (item["tileType"].Value<int>() == 1)
{
return item.ToObject<TileWall>();
}
else
{
Console.WriteLine("Did not have a correct tile type!\nType: " + item["tileType"].Value<int>() + "\n Falling back to TileEmpty!");
return item.ToObject<TileEmpty>();
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
}
public static void Save(object obj, string saveName)
{
JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Include;
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
serializer.Formatting = Formatting.Indented;
using (StreamWriter sw = new StreamWriter(saveName))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, obj);
}
}
public static Options LoadOptions()
{
try
{
using (StreamReader sr = new StreamReader(@"options.json"))
{
string content = sr.ReadToEnd();
Options obj = JsonConvert.DeserializeObject<Options>(content);
return obj;
}
}
catch
{
return new Options();
}
}
public static object Load(string loadName, JsonConverter converter)
{
using (StreamReader sr = new StreamReader(@"" + loadName + ".json"))
{
string content = sr.ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject<dynamic>(content, converter);
return obj;
}
}
public static PlayerSave LoadSave(string savename)
{
PlayerSave save = null;
try
{
using (StreamReader sr = new StreamReader(savename))
{
string content = sr.ReadToEnd();
save = JsonConvert.DeserializeObject<PlayerSave>(content);
Console.WriteLine("Loading save " + savename + "... \nLocation is: " + save.map);
}
}
catch
{
Console.WriteLine("There was no player save that could be loaded! Creating new save...");
save = new PlayerSave();
if (!Directory.Exists("Saves"))
Directory.CreateDirectory("Saves");
Save(save, savename);
}
return save;
}
public static SerWorld LoadWorld(string fileName)
{
using (StreamReader sr = new StreamReader(fileName + ".json"))
{
string content = sr.ReadToEnd();
SerWorld obj = JsonConvert.DeserializeObject<SerWorld>(content, new TileConverter());
return obj;
}
/*BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
formatter.Binder = new Serialization.CustomizedBinder();
using (Stream sw = new FileStream(fileName + ".lbmf", FileMode.Open, FileAccess.Read, FileShare.Read))
{
return (SerWorld)formatter.Deserialize(sw);
}*/
}
}
}