-
Notifications
You must be signed in to change notification settings - Fork 2
/
QServer.cs
219 lines (195 loc) · 8.25 KB
/
QServer.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace IGIEditor
{
[Serializable]
class QServer
{
internal static string serverBaseURL = @"ftp://igiresearchdevelopers.orgfree.com/";
internal static string missionDir = "QMissions";
internal static string graphsDir = "QGraphs";
internal static string updateDir = "QUpdater";
internal static string resourceDir = "QResources";
internal struct QServerData
{
string persmission;
string ownerGroup;
string fileSize;
string fileName;
public QServerData(string persmission, string ownerGroup, string fileSize, string fileName)
{
this.persmission = persmission;
this.ownerGroup = ownerGroup;
this.fileSize = fileSize;
this.fileName = fileName;
}
public string FileName { get => fileName; set => fileName = value; }
public string FileSize { get => fileSize; set => fileSize = value; }
public string OwnerGroup { get => ownerGroup; set => ownerGroup = value; }
public string Persmission { get => persmission; set => persmission = value; }
}
[Serializable]
internal struct QMissionsData
{
string missionName;
float missionSize;
string missionAuthor;
int missionLevel;
public QMissionsData(string missionName, float missionSize, string missionAuthor, int missionLevel)
{
this.missionName = missionName;
this.missionSize = missionSize;
this.missionAuthor = missionAuthor;
this.missionLevel = missionLevel;
}
public string MissionName { get => missionName; set => missionName = value; }
public float MissionSize { get => missionSize; set => missionSize = value; }
public string MissionAuthor { get => missionAuthor; set => missionAuthor = value; }
public int MissionLevel { get => missionLevel; set => missionLevel = value; }
}
private static QFtpClient GetQFtpClient()
{
string qHash1, qHash2, qHashTmp;
qHash1 = qHashTmp = serverBaseURL.Slice(serverBaseURL.IndexOf("://") + 3, serverBaseURL.LastIndexOf(@"/"));
qHash2 = qHashTmp = qHash1.Substring(0, 3) + "#p@ro@z##" + "#.@@h###m@";
qHash2 = qHash2.Replace("@", String.Empty).Replace("#", String.Empty);
var qftpClient = new QFtpClient(serverBaseURL, qHash1, qHash2);
return qftpClient;
}
internal static bool Upload(string remoteFile, string localFile)
{
bool status = false;
try
{
var qftpClient = GetQFtpClient();
qftpClient.upload(remoteFile, localFile);
status = true;
}
catch (Exception ex)
{
QUtils.ShowLogException("Server" + MethodBase.GetCurrentMethod().Name, ex);
status = false;
}
return status;
}
internal static bool Download(string remoteFile, string localFile, string destPath)
{
bool status;
try
{
var qftpClient = GetQFtpClient();
qftpClient.download(remoteFile, localFile);
QUtils.ShellExec("move /Y " + localFile + " " + destPath);
status = true;
}
catch (Exception ex)
{
if (ex.Message.Contains("File unavailable")) QUtils.ShowLogError(MethodBase.GetCurrentMethod().Name, "File '" + localFile + "' was not found on server.");
else
QUtils.ShowLogException("Server" + MethodBase.GetCurrentMethod().Name, ex);
status = false;
}
return status;
}
internal static bool Delete(string remoteFile)
{
bool status = false;
try
{
var qftpClient = GetQFtpClient();
qftpClient.delete(remoteFile);
status = true;
}
catch (Exception ex)
{
QUtils.ShowLogException("Server" + MethodBase.GetCurrentMethod().Name, ex);
status = false;
}
return status;
}
internal static List<QServerData> GetDirList(string dirName, bool useCache = false, List<string> extensions = null)
{
if (useCache)
{
if (QUtils.qServerDataList.Count > 0)
return QUtils.qServerDataList;
}
var qftpClient = GetQFtpClient();
var dirs = qftpClient.directoryListDetailed(dirName);
var qServerData = new List<QServerData>();
foreach (var dir in dirs)
{
var qServer = new QServerData();
var dirData = dir.Split(' ');
int dirDataSize = dirData.Length;
try
{
qServer.FileName = extensions.Any(dirData[dirDataSize - 1].Contains) ? dirData[dirDataSize - 1] : dirData[dirDataSize - 1] + dirData[dirDataSize];
if (dirName == missionDir && !qServer.FileName.Contains(QUtils.FileExtensions.Mission)) continue;
qServer.Persmission = dirData[0];
qServer.OwnerGroup = dirData[5];
for (int i = 17; i <= 19; i++)
{
var dataSize = dirData[i];
if (dataSize.Length > 3 && dataSize.All(char.IsDigit))
{
qServer.FileSize = dirData[i];
break;
}
}
qServerData.Add(qServer);
}
catch (Exception) { }
}
QUtils.qServerDataList = qServerData;
return qServerData;
}
internal static List<QMissionsData> GetMissionsData(bool useCache = false)
{
var missionsDataList = new List<QMissionsData>();
try
{
if (useCache)
{
if (QUtils.qServerMissionDataList.Count > 0)
{
QUtils.AddLog(MethodBase.GetCurrentMethod().Name, " using cache method.");
return QUtils.qServerMissionDataList;
}
}
QUtils.AddLog(MethodBase.GetCurrentMethod().Name, "using normal method.");
var dirList = GetDirList(missionDir, useCache, new List<string>() { QUtils.FileExtensions.Mission });
foreach (var dir in dirList)
{
try
{
string dirFileName = dir.FileName;
string missionName = dirFileName.Slice(0, dirFileName.IndexOf("@"));
float missionSize = 0.0f;
if (!String.IsNullOrEmpty(dir.FileSize))
missionSize = (float)Convert.ToInt32(dir.FileSize) / 1000;
string missionAuthor = dirFileName.Slice(dirFileName.IndexOf("@") + 1, dirFileName.IndexOf("#"));
string missionLevelStr = dirFileName.Slice(dirFileName.IndexOf("#") + 1, dirFileName.IndexOf("."));
int missionLevel = Convert.ToInt32(missionLevelStr);
var missionData = new QMissionsData(missionName, missionSize, missionAuthor, missionLevel);
missionsDataList.Add(missionData);
}
catch (Exception ex)
{
QUtils.LogException(MethodBase.GetCurrentMethod().Name, ex);
}
}
}
catch (Exception ex)
{
QUtils.LogException(MethodBase.GetCurrentMethod().Name, ex);
}
QUtils.qServerMissionDataList = missionsDataList;
return missionsDataList;
}
}
}