Initial commit
This commit is contained in:
36
Server/Util.cs
Normal file
36
Server/Util.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace CoopServer
|
||||
{
|
||||
class Util
|
||||
{
|
||||
public static T Read<T>(string file) where T : new()
|
||||
{
|
||||
XmlSerializer ser = new(typeof(T));
|
||||
|
||||
string path = Directory.GetCurrentDirectory() + "\\" + file;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user