-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
68 lines (63 loc) · 1.62 KB
/
Config.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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using TShockAPI;
namespace InfiniteInventories
{
class Config
{
public int MaxInventories = 4;
/// <summary>
/// Reads a configuration file from a given path
/// </summary>
/// <param name="path">string path</param>
/// <returns>ConfigFile object</returns>
public static Config Read(string path)
{
if (!File.Exists(path))
return new Config();
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return Read(fs);
}
}
/// <summary>
/// Reads the configuration file from a stream
/// </summary>
/// <param name="stream">stream</param>
/// <returns>ConfigFile object</returns>
public static Config Read(Stream stream)
{
using (var sr = new StreamReader(stream))
{
return JsonConvert.DeserializeObject<Config>(sr.ReadToEnd());
}
}
/// <summary>
/// Writes the configuration to a given path
/// </summary>
/// <param name="path">string path - Location to put the config file</param>
public void Write(string path)
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
Write(fs);
}
}
/// <summary>
/// Writes the configuration to a stream
/// </summary>
/// <param name="stream">stream</param>
public void Write(Stream stream)
{
var str = JsonConvert.SerializeObject(this, Formatting.Indented);
using (var sw = new StreamWriter(stream))
{
sw.Write(str);
}
}
}
}