Skip to content

Commit de0aa57

Browse files
committed
Added Universal Charset Detector. Removed update question dialog.
1 parent b38f864 commit de0aa57

File tree

8 files changed

+80
-17
lines changed

8 files changed

+80
-17
lines changed

.vs/SAMPLauncherNET/v15/.suo

-4 KB
Binary file not shown.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ Note: You don't need to commit `TranslatorInterface.cs`.
3232
You can change anything within the source code to contribute.
3333

3434
## Projects associated with this project
35+
- https://github.com/BigETI/INIEngine
3536
- https://github.com/BigETI/UpdaterNET
3637
- https://github.com/BigETI/WinFormsTranslator
38+
- https://github.com/errepi/ude
39+
- https://github.com/icsharpcode/SharpZipLib
40+
- https://github.com/IgnaceMaes/MaterialSkin
3741
- https://github.com/Hual/samp-discord-plugin
3842
- https://github.com/Southclaws/samp-servers-api
3943
- https://github.com/Southclaws/sampctl

SAMPLauncherNET/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
3333
// übernehmen, indem Sie "*" eingeben:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.6.0")]
36-
[assembly: AssemblyFileVersion("1.0.6.0")]
35+
[assembly: AssemblyVersion("1.0.6.1")]
36+
[assembly: AssemblyFileVersion("1.0.6.1")]

SAMPLauncherNET/SAMPLauncherNET.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
<Reference Include="System.Net.Http" />
8080
<Reference Include="System.Windows.Forms" />
8181
<Reference Include="System.Xml" />
82+
<Reference Include="Ude">
83+
<HintPath>libs\Ude.dll</HintPath>
84+
</Reference>
8285
<Reference Include="UpdaterNET">
8386
<HintPath>..\libs\UpdaterNET.dll</HintPath>
8487
</Reference>
@@ -393,13 +396,14 @@
393396
</ItemGroup>
394397
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
395398
<!-- Code to merge the assemblies into one -->
396-
<UsingTask TaskName="ILMerge.MSBuild.Tasks.ILMerge" AssemblyFile="$(SolutionDir)\packages\ILMerge.MSBuild.Tasks.1.0.0.3\tools\ILMerge.MSBuild.Tasks.dll" />
399+
<!-- <UsingTask TaskName="ILMerge.MSBuild.Tasks.ILMerge" AssemblyFile="$(SolutionDir)\packages\ILMerge.MSBuild.Tasks.1.0.0.3\tools\ILMerge.MSBuild.Tasks.dll" />
397400
<Target Name="AfterBuild">
398401
<ItemGroup>
399402
<MergeAsm Include="$(OutputPath)$(TargetFileName)" />
400403
<MergeAsm Include="$(OutputPath)ICSharpCode.SharpZipLib.dll" />
401404
<MergeAsm Include="$(OutputPath)INIEngine.dll" />
402405
<MergeAsm Include="$(OutputPath)MaterialSkin.dll" />
406+
<MergeAsm Include="$(OutputPath)Ude.dll" />
403407
<MergeAsm Include="$(OutputPath)UpdaterNET.dll" />
404408
<MergeAsm Include="$(OutputPath)WinFormsTranslator.dll" />
405409
</ItemGroup>
@@ -408,5 +412,5 @@
408412
</PropertyGroup>
409413
<Message Text="ILMerge @(MergeAsm) -&gt; $(MergedAssembly)" Importance="high" />
410414
<ILMerge InputAssemblies="@(MergeAsm)" OutputFile="$(MergedAssembly)" TargetKind="SameAsPrimaryAssembly" />
411-
</Target>
415+
</Target> -->
412416
</Project>

SAMPLauncherNET/Source/SAMPLauncherNET/Core/Program.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,9 @@ private static void OnDownloadFileCompleted(object sender, AsyncCompletedEventAr
152152
[STAThread]
153153
static void Main()
154154
{
155+
#if !DEBUG
155156
GitHubUpdateTask update = new GitHubUpdateTask("BigETI", "SAMPLauncherNET");
156-
bool start_update = false;
157157
if (update.IsUpdateAvailable)
158-
{
159-
start_update = (MessageBox.Show("A new update for SA:MP Launcher .NET is available.\r\nVersion: " + update.Version + "\r\n\r\nDo you want to install it now?", "Update available", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes);
160-
}
161-
if (start_update)
162158
{
163159
if (Utils.ExportResource("SAMPLauncherNET.PreBuilds.SAMPLauncherNETUpdater.exe", Path.Combine(Environment.CurrentDirectory, "SAMPLauncherNETUpdater.exe")))
164160
{
@@ -174,6 +170,7 @@ static void Main()
174170
}
175171
else
176172
{
173+
#endif
177174
try
178175
{
179176
Translator.TranslatorInterface = new TranslatorInterface();
@@ -196,6 +193,7 @@ static void Main()
196193
{
197194
MessageBox.Show("A fatal error has occured:\r\n\r\n" + e.Message, "Fatal error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
198195
}
196+
#if !DEBUG
199197
}
200198
if (installerPath != null)
201199
{
@@ -204,6 +202,7 @@ static void Main()
204202
Thread.Sleep(200);
205203
}
206204
}
205+
#endif
207206
Application.Exit();
208207
}
209208
}

SAMPLauncherNET/Source/SAMPLauncherNET/Core/Server.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -922,9 +922,9 @@ private void Receive()
922922
hasPassword = (reader.ReadByte() != 0);
923923
playerCount = reader.ReadUInt16();
924924
maxPlayers = reader.ReadUInt16();
925-
hostname = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
926-
gamemode = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
927-
language = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
925+
hostname = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadInt32()));
926+
gamemode = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadInt32()));
927+
language = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadInt32()));
928928
requestsRequired[ERequestType.Information] = false;
929929
break;
930930

@@ -938,8 +938,8 @@ private void Receive()
938938
{
939939
for (int i = 0; i < rc; i++)
940940
{
941-
k = Encoding.Default.GetString(reader.ReadBytes(reader.ReadByte()));
942-
rules.Add(k, Encoding.Default.GetString(reader.ReadBytes(reader.ReadByte())));
941+
k = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadByte()));
942+
rules.Add(k, Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadByte())));
943943
}
944944
}
945945
catch (Exception e)
@@ -961,7 +961,7 @@ private void Receive()
961961
for (int i = 0; i < pc; i++)
962962
{
963963
// Name and score
964-
k = Encoding.Default.GetString(reader.ReadBytes(reader.ReadByte()));
964+
k = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadByte()));
965965
clients.Add(k, reader.ReadInt32());
966966
}
967967
}
@@ -987,7 +987,7 @@ private void Receive()
987987
for (ushort i = 0; i != playerCount; i++)
988988
{
989989
id = reader.ReadByte();
990-
pn = Encoding.Default.GetString(reader.ReadBytes(reader.ReadByte()));
990+
pn = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadByte()));
991991
s = reader.ReadInt32();
992992
p = reader.ReadUInt32();
993993
players.Add(id, new Player(id, pn, s, p));

SAMPLauncherNET/Source/SAMPLauncherNET/Core/ServerListConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public Dictionary<string, Server> ServerListIO
236236
{
237237
string ip = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
238238
ushort port = (ushort)(reader.ReadUInt32());
239-
string cn = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
239+
string cn = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadInt32()));
240240
string sp = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
241241
string rp = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
242242
ip = ip + ":" + port;

SAMPLauncherNET/Source/SAMPLauncherNET/Core/Utils.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using System.Diagnostics;
77
using System.Windows.Forms;
88
using System.Reflection;
9+
using Ude;
10+
using System.Text;
11+
using System.Threading;
912

1013
/// <summary>
1114
/// SA:MP launcher .NET namespace
@@ -22,6 +25,11 @@ public static class Utils
2225
/// </summary>
2326
public static readonly Size GalleryImageSize = new Size(256, 256);
2427

28+
/// <summary>
29+
/// Charset detector
30+
/// </summary>
31+
private static readonly CharsetDetector charsetDetector = new CharsetDetector();
32+
2533
/// <summary>
2634
/// Are arrays equal
2735
/// </summary>
@@ -119,6 +127,54 @@ public static string NAString(string str)
119127
return ((str == null) ? "N/A" : ((str.Length > 0) ? str : "N/A"));
120128
}
121129

130+
/// <summary>
131+
/// Guessed string encoding
132+
/// </summary>
133+
/// <param name="bytes">Bytes to encode</param>
134+
/// <returns>Encoded string</returns>
135+
public static string GuessedStringEncoding(byte[] bytes)
136+
{
137+
string ret = null;
138+
if (bytes != null)
139+
{
140+
charsetDetector.Reset();
141+
charsetDetector.Feed(bytes, 0, bytes.Length);
142+
charsetDetector.DataEnd();
143+
try
144+
{
145+
string charset_name = charsetDetector.Charset;
146+
if (charset_name != null)
147+
{
148+
Encoding encoding = Encoding.GetEncoding(charset_name);
149+
if (encoding != null)
150+
{
151+
ret = encoding.GetString(bytes);
152+
}
153+
}
154+
}
155+
catch (Exception e)
156+
{
157+
Console.Error.WriteLine(e.Message);
158+
}
159+
if (ret == null)
160+
{
161+
try
162+
{
163+
ret = Encoding.Default.GetString(bytes);
164+
}
165+
catch (Exception e)
166+
{
167+
Console.Error.WriteLine(e.Message);
168+
}
169+
}
170+
}
171+
if (ret == null)
172+
{
173+
ret = "";
174+
}
175+
return ret;
176+
}
177+
122178
/// <summary>
123179
/// Get file resources from directory
124180
/// </summary>

0 commit comments

Comments
 (0)