/
Self.cs
178 lines (144 loc) · 5.67 KB
/
Self.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
using GameLauncher.App.Classes;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace GameLauncherReborn {
class Self {
public static string mainserver = "https://launchpad.soapboxrace.world";
public static string[] serverlisturl = new string[] {
"https://launchpad.soapboxrace.world/servers",
"http://api.nightriderz.world/servers.json",
"http://worldunited.gg/serverlist.json",
"https://nfswonline.pl/lista_serwerow.json"
};
//public static string serverlisturl = mainserver + "/servers";
public static string internetcheckurl = mainserver + "/generate_204.php";
public static string statsurl = mainserver + "/stats";
public static string CDNUrlList = "http://145.239.5.103/cdn/list.json"; //hosted on WOPL, coz why not.
private static IniFile SettingFile = new IniFile("Settings.ini");
public static int ProxyPort = new Random().Next(6260, 6269);
public static Boolean sendRequest = true;
public static void runAsAdmin() {
string[] args = Environment.GetCommandLineArgs();
ProcessStartInfo processStartInfo = new ProcessStartInfo() {
Verb = "runas",
FileName = Application.ExecutablePath
};
if ((int)args.Length > 0) {
processStartInfo.Arguments = args[0];
}
try {
Process.Start(processStartInfo);
} catch (Exception exception1) {
MessageBox.Show("Failed to self-run as admin: " + exception1.Message);
}
}
public static void Restart(string param = "") {
Application.Restart();
}
public static string CountryName(string twoLetterCountryCode) {
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in cultures) {
RegionInfo region = new RegionInfo(culture.LCID);
if (region.TwoLetterISORegionName.ToUpper() == twoLetterCountryCode.ToUpper()) {
return region.EnglishName;
}
}
return String.Empty;
}
public static long getTimestamp(bool valid = false) {
long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks;
if(valid == true) {
ticks /= 10000000;
} else {
ticks /= 10000;
}
return ticks;
}
public static bool hasWriteAccessToFolder(string path) {
try {
File.Create(path + "temp.txt").Close();
File.Delete(path + "temp.txt");
} catch (UnauthorizedAccessException) {
return false;
}
return true;
}
public static string getDiscordRPCImageIDFromServerName(string ServerName, string response) {
string returnvalue = "nfsw";
try {
String[] substrings = response.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (var substring in substrings) {
if (!String.IsNullOrEmpty(substring)) {
String[] substrings2 = substring.Split(new string[] { ";" }, StringSplitOptions.None);
if(substrings2[0] == ServerName) {
returnvalue = substrings2[2];
}
}
}
} catch {
returnvalue = "nfsw";
}
return returnvalue;
}
public static bool CheckForInternetConnection() {
try {
using (var client = new WebClientWithTimeout()) {
using (client.OpenRead(internetcheckurl)) {
return true;
}
}
} catch {
return false;
}
}
public static void centerScreen(Form form) {
form.StartPosition = FormStartPosition.Manual;
form.Top = (Screen.PrimaryScreen.Bounds.Height - form.Height) / 2;
form.Left = (Screen.PrimaryScreen.Bounds.Width - form.Width) / 2;
}
public static bool validateEmail(string email) {
String theEmailPattern = @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*"
+ "@"
+ @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$";
return Regex.IsMatch(email, theEmailPattern);
}
public static bool isTempFolder(string directory) {
return directory.Contains("Temp");
}
public static string CleanFromUnknownChars(string s) {
StringBuilder sb = new StringBuilder(s.Length);
foreach (char c in s) {
if(
(int)c >= 48 && (int)c <= 57 ||
(int)c == 60 ||
(int)c == 62 ||
(int)c >= 65 && (int)c <= 90 ||
(int)c >= 97 && (int)c <= 122 ||
(int)c == 47 ||
(int)c == 45 ||
(int)c == 46
) {
sb.Append(c);
}
}
return sb.ToString();
}
public static bool CheckArchitectureFile(string fileName) {
const int PE_POINTER_OFFSET = 60;
const int MACHINE_OFFSET = 4;
byte[] data = new byte[4096];
using (Stream s = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
s.Read(data, 0, 4096);
}
int PE_HEADER_ADDR = BitConverter.ToInt32(data, PE_POINTER_OFFSET);
int machineUint = BitConverter.ToUInt16(data, PE_HEADER_ADDR + MACHINE_OFFSET);
return machineUint == 0x014c;
}
}
}