-
Notifications
You must be signed in to change notification settings - Fork 4
/
Config.cs
142 lines (132 loc) · 4.54 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using EnterpriseDT.Net.Ftp;
namespace modsync
{
static class Config
{
public static FtpSettings ftpsettings = new FtpSettings();
public static Settings settings = new Settings();
// decodes FTP link in format ftp://user:pass@server:port/folder
public static void FtpLinkParse(string link)
{
try
{
// remove ftp://
link = link.Substring(6);
// split the string by separator
int idx;
List<String> str = new List<String>();
foreach (char sep in new char[] { ':', '@', ':', '/' })
{
idx = link.IndexOf(sep, 0);
str.Add(link.Substring(0, idx));
link = link.Substring(idx + 1);
}
str.Add(link);
// all ok, update the settings
ftpsettings.FtpUserName = str[0];
ftpsettings.FtpPassword = str[1];
ftpsettings.FtpServerAddress = str[2];
ftpsettings.FtpServerPort = str[3];
ftpsettings.FtpServerFolder = str[4];
}
catch
{
Console.WriteLine(Strings.Get("FtpBadCommandLine"));
Console.ReadKey();
Program.Exit(false);
return;
}
}
// reads modsyncftp.xml
public static void FtpLinkRead()
{
string FtpFile = Locations.LocalFolderName_Minecraft + "\\" + "modsyncftp.xml";
if (File.Exists(FtpFile))
{
try
{
ftpsettings = (FtpSettings)Read(typeof(FtpSettings), FtpFile);
}
catch (Exception ex)
{
Console.WriteLine(Strings.Get("ConfigError") + ex.Message);
Console.ReadKey();
}
}
}
// writes modsyncftp.xml
public static void FtpLinkInput()
{
// have user enter the ftp parameters
string str;
Console.WriteLine(Strings.Get("FtpInputLink"));
foreach (FieldInfo field in ftpsettings.GetType().GetFields())
{
do
{
Console.Write(field.Name + ": ");
str = Console.ReadLine();
}
while (str == "");
field.SetValue(ftpsettings, str);
}
// write file
string FtpFile = Locations.LocalFolderName_Minecraft + "\\" + "modsyncftp.xml";
try
{
Write(ftpsettings, FtpFile);
}
catch (Exception ex)
{
Console.WriteLine(Strings.Get("ConfigError") + ex.Message);
Console.ReadKey();
}
}
// downloads modsync.xml
public static void FtpUpdate(ref FTPConnection ftpcon)
{
string ConfigFile = "modsync.xml";
string LocalFile = Locations.LocalFolderName_Minecraft + "\\" + ConfigFile;
string RemoteFile = ftpsettings.FtpServerFolder + "/" + ConfigFile;
try
{
if (ftpcon.Exists(RemoteFile))
{
ftpcon.DownloadFile(LocalFile, RemoteFile);
settings = (Settings)Read(typeof(Settings), LocalFile);
}
else
{
Console.WriteLine(Strings.Get("ConfigMissing"));
}
}
catch (Exception ex)
{
Console.WriteLine(Strings.Get("ConfigError") + ex.Message);
Console.WriteLine(Strings.Get("PressKey"));
Console.ReadKey();
}
}
/// Write Settings to XML File
public static void Write(object settings, string path)
{
XmlSerializer x = new XmlSerializer(settings.GetType());
StreamWriter writer = new StreamWriter(path);
x.Serialize(writer, settings);
}
/// Read Settings from XML File
public static object Read(Type typ, string path)
{
XmlSerializer x = new XmlSerializer(typ);
StreamReader reader = new StreamReader(path);
return x.Deserialize(reader);
}
}
}