RAGECOOP-V/Server/Util.cs

38 lines
956 B
C#
Raw Normal View History

using System;
using System.IO;
2021-07-07 13:36:25 +02:00
using System.Xml.Serialization;
namespace CoopServer
{
class Util
{
public static T Read<T>(string file) where T : new()
{
XmlSerializer ser = new(typeof(T));
2021-07-12 01:47:01 +02:00
string path = AppContext.BaseDirectory + file;
2021-08-14 21:49:23 +02:00
T data;
2021-07-07 13:36:25 +02:00
if (File.Exists(path))
{
using (FileStream stream = File.OpenRead(path))
{
2021-08-14 21:49:23 +02:00
data = (T)ser.Deserialize(stream);
2021-07-07 13:36:25 +02:00
}
using (FileStream stream = new(path, File.Exists(path) ? FileMode.Truncate : FileMode.Create, FileAccess.ReadWrite))
{
2021-08-14 21:49:23 +02:00
ser.Serialize(stream, data);
2021-07-07 13:36:25 +02:00
}
}
else
{
using FileStream stream = File.OpenWrite(path);
2021-08-14 21:49:23 +02:00
ser.Serialize(stream, data = new T());
2021-07-07 13:36:25 +02:00
}
2021-08-14 21:49:23 +02:00
return data;
2021-07-07 13:36:25 +02:00
}
}
}