RAGECOOP-V/Server/Util.cs

38 lines
976 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-07-07 13:36:25 +02:00
T settings;
if (File.Exists(path))
{
using (FileStream stream = File.OpenRead(path))
{
settings = (T)ser.Deserialize(stream);
}
using (FileStream stream = new(path, File.Exists(path) ? FileMode.Truncate : FileMode.Create, FileAccess.ReadWrite))
{
ser.Serialize(stream, settings);
}
}
else
{
using FileStream stream = File.OpenWrite(path);
ser.Serialize(stream, settings = new T());
}
return settings;
}
}
}