forked from AvilanceLtd/StarryboundServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
154 lines (126 loc) · 5.36 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
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
149
150
151
152
153
154
/*
* Starrybound Server
* Copyright 2013, Avilance Ltd
* Created by Zidonuke ([email protected]) and Crashdoom ([email protected])
*
* This file is a part of Starrybound Server.
* Starrybound Server is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Starrybound Server is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Starrybound Server. If not, see http://www.gnu.org/licenses/.
*/
using com.avilance.Starrybound.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.ComponentModel;
namespace com.avilance.Starrybound
{
class Config {
internal static string RulesPath { get { return Path.Combine(StarryboundServer.SavePath, "rules.txt"); } }
internal static string MotdPath { get { return Path.Combine(StarryboundServer.SavePath, "motd.txt"); } }
internal static string ConfigPath { get { return Path.Combine(StarryboundServer.SavePath, "config.json"); } }
public static void CreateIfNot(string file, string data = "")
{
if (!File.Exists(file))
{
File.WriteAllText(file, data);
}
}
public static string ReadConfigFile(string file)
{
return File.ReadAllText(file);
}
public static string GetMotd()
{
string returnString = StarryboundServer.motdData;
returnString = returnString.Replace("%players%", StarryboundServer.clientCount.ToString());
returnString = returnString.Replace("%versionNum%", StarryboundServer.VersionNum.ToString());
return returnString;
}
public static string GetRules()
{
return StarryboundServer.rulesData;
}
public static void SetupConfig()
{
if (!Directory.Exists(StarryboundServer.SavePath))
{
Directory.CreateDirectory(StarryboundServer.SavePath);
}
CreateIfNot(RulesPath, "1) Respect all players 2) No griefing/hacking 3) Have fun!");
CreateIfNot(MotdPath, "This server is running Starrybound Server v%versionNum%. Type /help for a list of commands. There are currently %players% player(s) online.");
StarryboundServer.motdData = ReadConfigFile(MotdPath);
StarryboundServer.rulesData = ReadConfigFile(RulesPath);
if (File.Exists(ConfigPath))
{
StarryboundServer.config = ConfigFile.Read(ConfigPath);
}
if (StarryboundServer.IsMono && StarryboundServer.config == null)
StarryboundServer.config = new ConfigFile();
StarryboundServer.config.Write(ConfigPath);
}
}
class ConfigFile
{
[Description("")]
public string serverAccount = "";
public string serverPass = "";
public short serverPort = 21024;
public string proxyIP = "0.0.0.0";
public short proxyPort = 21025;
public string proxyPass = "";
public int passwordRounds = 5000;
public int maxClients = 25;
public string logFile = "proxy.log";
public LogType logLevel = LogType.Info;
public bool allowSpaces = false;
public bool allowSymbols = false;
public string[] sectors = new string[] { "alpha", "beta", "gamma", "delta", "sectorx" };
public bool useAssetDigest = false;
public string assetDigest = "8168975B43CBB5A002D3CCBD41FAFD226D3F58ECC3A6F835C26980531EF6AA6C";
public static ConfigFile Read(string path) {
if (!File.Exists(path))
return new ConfigFile();
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
ConfigFile file = Read(fs);
StarryboundServer.logInfo("Starrybound config loaded successfully.");
return file;
}
}
public static ConfigFile Read(Stream stream)
{
try
{
using (var sr = new StreamReader(stream))
{
return JsonConvert.DeserializeObject<ConfigFile>(sr.ReadToEnd());
}
}
catch (Exception)
{
StarryboundServer.logException("Starrybound config is unreadable - Re-creating config with default values");
return new ConfigFile();
}
}
public void Write(string path)
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
Write(fs);
}
}
public void Write(Stream stream)
{
var str = JsonConvert.SerializeObject(this, Formatting.Indented);
using (var sw = new StreamWriter(stream))
{
sw.Write(str);
}
}
}
}