Skip to content

Commit

Permalink
Added Universal Charset Detector. Removed update question dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
BigETI committed Apr 25, 2018
1 parent b38f864 commit de0aa57
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 17 deletions.
Binary file modified .vs/SAMPLauncherNET/v15/.suo
Binary file not shown.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ Note: You don't need to commit `TranslatorInterface.cs`.
You can change anything within the source code to contribute.

## Projects associated with this project
- https://github.com/BigETI/INIEngine
- https://github.com/BigETI/UpdaterNET
- https://github.com/BigETI/WinFormsTranslator
- https://github.com/errepi/ude
- https://github.com/icsharpcode/SharpZipLib
- https://github.com/IgnaceMaes/MaterialSkin
- https://github.com/Hual/samp-discord-plugin
- https://github.com/Southclaws/samp-servers-api
- https://github.com/Southclaws/sampctl
Expand Down
4 changes: 2 additions & 2 deletions SAMPLauncherNET/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.6.0")]
[assembly: AssemblyFileVersion("1.0.6.0")]
[assembly: AssemblyVersion("1.0.6.1")]
[assembly: AssemblyFileVersion("1.0.6.1")]
8 changes: 6 additions & 2 deletions SAMPLauncherNET/SAMPLauncherNET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Ude">
<HintPath>libs\Ude.dll</HintPath>
</Reference>
<Reference Include="UpdaterNET">
<HintPath>..\libs\UpdaterNET.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -393,13 +396,14 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Code to merge the assemblies into one -->
<UsingTask TaskName="ILMerge.MSBuild.Tasks.ILMerge" AssemblyFile="$(SolutionDir)\packages\ILMerge.MSBuild.Tasks.1.0.0.3\tools\ILMerge.MSBuild.Tasks.dll" />
<!-- <UsingTask TaskName="ILMerge.MSBuild.Tasks.ILMerge" AssemblyFile="$(SolutionDir)\packages\ILMerge.MSBuild.Tasks.1.0.0.3\tools\ILMerge.MSBuild.Tasks.dll" />
<Target Name="AfterBuild">
<ItemGroup>
<MergeAsm Include="$(OutputPath)$(TargetFileName)" />
<MergeAsm Include="$(OutputPath)ICSharpCode.SharpZipLib.dll" />
<MergeAsm Include="$(OutputPath)INIEngine.dll" />
<MergeAsm Include="$(OutputPath)MaterialSkin.dll" />
<MergeAsm Include="$(OutputPath)Ude.dll" />
<MergeAsm Include="$(OutputPath)UpdaterNET.dll" />
<MergeAsm Include="$(OutputPath)WinFormsTranslator.dll" />
</ItemGroup>
Expand All @@ -408,5 +412,5 @@
</PropertyGroup>
<Message Text="ILMerge @(MergeAsm) -&gt; $(MergedAssembly)" Importance="high" />
<ILMerge InputAssemblies="@(MergeAsm)" OutputFile="$(MergedAssembly)" TargetKind="SameAsPrimaryAssembly" />
</Target>
</Target> -->
</Project>
9 changes: 4 additions & 5 deletions SAMPLauncherNET/Source/SAMPLauncherNET/Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,9 @@ private static void OnDownloadFileCompleted(object sender, AsyncCompletedEventAr
[STAThread]
static void Main()
{
#if !DEBUG
GitHubUpdateTask update = new GitHubUpdateTask("BigETI", "SAMPLauncherNET");
bool start_update = false;
if (update.IsUpdateAvailable)
{
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);
}
if (start_update)
{
if (Utils.ExportResource("SAMPLauncherNET.PreBuilds.SAMPLauncherNETUpdater.exe", Path.Combine(Environment.CurrentDirectory, "SAMPLauncherNETUpdater.exe")))
{
Expand All @@ -174,6 +170,7 @@ static void Main()
}
else
{
#endif
try
{
Translator.TranslatorInterface = new TranslatorInterface();
Expand All @@ -196,6 +193,7 @@ static void Main()
{
MessageBox.Show("A fatal error has occured:\r\n\r\n" + e.Message, "Fatal error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#if !DEBUG
}
if (installerPath != null)
{
Expand All @@ -204,6 +202,7 @@ static void Main()
Thread.Sleep(200);
}
}
#endif
Application.Exit();
}
}
Expand Down
14 changes: 7 additions & 7 deletions SAMPLauncherNET/Source/SAMPLauncherNET/Core/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,9 @@ private void Receive()
hasPassword = (reader.ReadByte() != 0);
playerCount = reader.ReadUInt16();
maxPlayers = reader.ReadUInt16();
hostname = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
gamemode = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
language = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
hostname = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadInt32()));
gamemode = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadInt32()));
language = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadInt32()));
requestsRequired[ERequestType.Information] = false;
break;

Expand All @@ -938,8 +938,8 @@ private void Receive()
{
for (int i = 0; i < rc; i++)
{
k = Encoding.Default.GetString(reader.ReadBytes(reader.ReadByte()));
rules.Add(k, Encoding.Default.GetString(reader.ReadBytes(reader.ReadByte())));
k = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadByte()));
rules.Add(k, Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadByte())));
}
}
catch (Exception e)
Expand All @@ -961,7 +961,7 @@ private void Receive()
for (int i = 0; i < pc; i++)
{
// Name and score
k = Encoding.Default.GetString(reader.ReadBytes(reader.ReadByte()));
k = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadByte()));
clients.Add(k, reader.ReadInt32());
}
}
Expand All @@ -987,7 +987,7 @@ private void Receive()
for (ushort i = 0; i != playerCount; i++)
{
id = reader.ReadByte();
pn = Encoding.Default.GetString(reader.ReadBytes(reader.ReadByte()));
pn = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadByte()));
s = reader.ReadInt32();
p = reader.ReadUInt32();
players.Add(id, new Player(id, pn, s, p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public Dictionary<string, Server> ServerListIO
{
string ip = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
ushort port = (ushort)(reader.ReadUInt32());
string cn = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
string cn = Utils.GuessedStringEncoding(reader.ReadBytes(reader.ReadInt32()));
string sp = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
string rp = Encoding.Default.GetString(reader.ReadBytes(reader.ReadInt32()));
ip = ip + ":" + port;
Expand Down
56 changes: 56 additions & 0 deletions SAMPLauncherNET/Source/SAMPLauncherNET/Core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Diagnostics;
using System.Windows.Forms;
using System.Reflection;
using Ude;
using System.Text;
using System.Threading;

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

/// <summary>
/// Charset detector
/// </summary>
private static readonly CharsetDetector charsetDetector = new CharsetDetector();

/// <summary>
/// Are arrays equal
/// </summary>
Expand Down Expand Up @@ -119,6 +127,54 @@ public static string NAString(string str)
return ((str == null) ? "N/A" : ((str.Length > 0) ? str : "N/A"));
}

/// <summary>
/// Guessed string encoding
/// </summary>
/// <param name="bytes">Bytes to encode</param>
/// <returns>Encoded string</returns>
public static string GuessedStringEncoding(byte[] bytes)
{
string ret = null;
if (bytes != null)
{
charsetDetector.Reset();
charsetDetector.Feed(bytes, 0, bytes.Length);
charsetDetector.DataEnd();
try
{
string charset_name = charsetDetector.Charset;
if (charset_name != null)
{
Encoding encoding = Encoding.GetEncoding(charset_name);
if (encoding != null)
{
ret = encoding.GetString(bytes);
}
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
if (ret == null)
{
try
{
ret = Encoding.Default.GetString(bytes);
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
}
}
if (ret == null)
{
ret = "";
}
return ret;
}

/// <summary>
/// Get file resources from directory
/// </summary>
Expand Down

0 comments on commit de0aa57

Please sign in to comment.