Skip to content
This repository has been archived by the owner on Jan 2, 2022. It is now read-only.

Code cleanup #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Translator/EasyHook32.dll
Binary file not shown.
Binary file modified Translator/EasyHook32Svc.exe
Binary file not shown.
Binary file modified Translator/EasyHook64.dll
Binary file not shown.
Binary file modified Translator/EasyHook64Svc.exe
Binary file not shown.
Binary file modified Translator/EasyLoad32.dll
Binary file not shown.
Binary file modified Translator/EasyLoad64.dll
Binary file not shown.
95 changes: 54 additions & 41 deletions Translator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,61 @@
using System;
#region

using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Net;
using EasyHook;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using IniParser;
using System.Text;
using IniParser.Model;
using System.Threading;
using EasyHook;
using IniParser;
using Newtonsoft.Json.Linq;

#endregion

namespace Translator
{
class Program
internal class Program
{
static string TargetLang = "";
private static string _targetLang = "";

static void Main(string[] args)
private static void Main()
{
try
{
var parser = new FileIniDataParser();
IniData data = parser.ReadFile("Config.ini");
TargetLang = data["Translation"]["lang"];
var data = parser.ReadFile("Config.ini");
_targetLang = data["Translation"]["lang"];
}
catch
{
Console.WriteLine("Can't find Config.ini, creating one.");
File.AppendAllText("Config.ini", "[Translation]" + Environment.NewLine + "lang=en");
Console.WriteLine("A new configuration file has been created.");
Console.WriteLine("Please edit Config.ini if you wish to change the language (currently English).");
TargetLang = "en";
_targetLang = "en";
}

Console.WriteLine("Welcome to DotA 2 Translator (v2)");
Console.WriteLine("Translating to: " + TargetLang + " (edit Config.ini to change this).");
Console.WriteLine("Translating to: " + _targetLang + " (edit Config.ini to change this).");

int targetPID = 0;
Process[] processes = Process.GetProcesses();
for (int i = 0; i < processes.Length; i++)
var targetPid = 0;
var processes = Process.GetProcesses();
foreach (var t in processes)
{
try
{
if (processes[i].MainWindowTitle == "Dota 2" && processes[i].HasExited == false)
if (t.MainWindowTitle == "Dota 2" && t.HasExited == false)
{
targetPID = processes[i].Id;
targetPid = t.Id;
}
}
catch { }
catch
{
}
}
if (targetPID == 0)
if (targetPid == 0)
{
Console.WriteLine("You see, you need to run the game for the translator to do something!");
Console.WriteLine("Please start the translator after you start DotA 2");
Expand All @@ -68,11 +73,13 @@ static void Main(string[] args)

try
{
NativeAPI.RhInjectLibrary(targetPID, 0, NativeAPI.EASYHOOK_INJECT_DEFAULT, "", ".\\Injectee.dll", IntPtr.Zero, 0);
NativeAPI.RhInjectLibrary(targetPid, 0, NativeAPI.EASYHOOK_INJECT_DEFAULT, "", ".\\Injectee.dll",
IntPtr.Zero, 0);
}
catch (Exception e)
{
Console.WriteLine("Could not inject our DLL into DotA 2. Please post the following message on /r/dotatranslator for help");
Console.WriteLine(
"Could not inject our DLL into DotA 2. Please post the following message on /r/dotatranslator for help");
Console.WriteLine(e.Message);
Console.ReadKey();
Environment.Exit(-1);
Expand All @@ -83,7 +90,7 @@ static void Main(string[] args)
Environment.Exit(0);
}

static async void ListenPipeAndTranslate(NamedPipeServerStream server1, NamedPipeServerStream server2)
private static async void ListenPipeAndTranslate(NamedPipeServerStream server1, NamedPipeServerStream server2)
{
do
{
Expand All @@ -92,18 +99,20 @@ static async void ListenPipeAndTranslate(NamedPipeServerStream server1, NamedPip
await server1.WaitForConnectionAsync();
await server2.WaitForConnectionAsync();
Console.WriteLine("GLHF!");
while (true && server1.IsConnected && server2.IsConnected)
while (server1.IsConnected && server2.IsConnected)
{
byte[] buffer = new byte[1000];
var buffer = new byte[1000];
server1.Read(buffer, 0, 2);
int numBytes = BitConverter.ToUInt16(buffer, 0);
var numBytes = BitConverter.ToUInt16(buffer, 0);
server1.Read(buffer, 0, numBytes);
Array.Resize(ref buffer, numBytes);

if (numBytes == 0)
{
continue;
}

byte[] messageParams = new byte[100];
var messageParams = new byte[100];
server1.Read(messageParams, 0, 2);
numBytes = BitConverter.ToUInt16(messageParams, 0);
server1.Read(messageParams, 0, numBytes);
Expand All @@ -121,28 +130,32 @@ static async void ListenPipeAndTranslate(NamedPipeServerStream server1, NamedPip
} while (true);
}

static async void TranslateAndPrint(string message, NamedPipeServerStream sw, byte[] messageParams)
private static async void TranslateAndPrint(string message, Stream sw, byte[] messageParams)
{
// These messages are already in the user's language
// They contain HTML too, so I'm not gonna bother translate them
if (message.Contains("<img") || message.Contains("<font")) { return; }
if (message.Contains("<img") || message.Contains("<font"))
{
return;
}

try
{
string url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=" + TargetLang + "&dt=t&q=" + WebUtility.UrlEncode(message);
HttpClient hc = new HttpClient();
HttpResponseMessage r = await hc.GetAsync(url);
String rs = await r.Content.ReadAsStringAsync();
var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=" + _targetLang +
"&dt=t&q=" + WebUtility.UrlEncode(message);
var hc = new HttpClient();
var r = await hc.GetAsync(url);
var rs = await r.Content.ReadAsStringAsync();
dynamic d = JArray.Parse(rs);
string translated = d[0][0][0];
string sourcelang = d[2];
var translated = d[0][0][0];
var sourcelang = d[2];
Console.WriteLine("Original: " + message);
string toSend = "(Translated from " + sourcelang + "): " + translated;
if (sourcelang != TargetLang)
var toSend = "(Translated from " + sourcelang + "): " + translated;
if (sourcelang != _targetLang)
{
UnicodeEncoding ue = new UnicodeEncoding(false, false, false);
byte[] sendBytes = ue.GetBytes(toSend);
byte[] size = BitConverter.GetBytes((UInt16)sendBytes.Length);
var ue = new UnicodeEncoding(false, false, false);
var sendBytes = ue.GetBytes(toSend);
var size = BitConverter.GetBytes((ushort) sendBytes.Length);
// Write the number of raw bytes in this message
sw.Write(size, 0, 2);
// Write the UTF-16 encoded message
Expand All @@ -163,4 +176,4 @@ static async void TranslateAndPrint(string message, NamedPipeServerStream sw, by
}
}
}
}
}
37 changes: 16 additions & 21 deletions Translator/Translator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,19 @@
<RunPostBuildEvent>Always</RunPostBuildEvent>
</PropertyGroup>
<ItemGroup>
<Reference Include="EasyHook, Version=2.7.6270.0, Culture=neutral, PublicKeyToken=4b580fca19d0b0c5, processorArchitecture=MSIL">
<HintPath>..\packages\EasyHook.2.7.6270\lib\net40\EasyHook.dll</HintPath>
<Reference Include="EasyHook, Version=2.7.6035.0, Culture=neutral, PublicKeyToken=4b580fca19d0b0c5, processorArchitecture=MSIL">
<HintPath>..\packages\EasyHook.2.7.6035\lib\net40\EasyHook.dll</HintPath>
</Reference>
<Reference Include="INIFileParser, Version=2.5.2.0, Culture=neutral, PublicKeyToken=79af7b307b65cf3c, processorArchitecture=MSIL">
<HintPath>..\packages\ini-parser.2.5.2\lib\net20\INIFileParser.dll</HintPath>
<Reference Include="INIFileParser, Version=2.3.0.0, Culture=neutral, PublicKeyToken=79af7b307b65cf3c, processorArchitecture=MSIL">
<HintPath>..\packages\ini-parser.2.3.0\lib\net20\INIFileParser.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
Expand All @@ -81,6 +76,18 @@
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.6.1">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.6.1 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Content Include="EasyHook32.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -101,18 +108,6 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.6.1">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.6.1 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>del $(ProjectDir)\bin\$(Configuration)\Injectee.dll
Expand Down
4 changes: 2 additions & 2 deletions Translator/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EasyHook" version="2.7.6270" targetFramework="net461" />
<package id="ini-parser" version="2.5.2" targetFramework="net461" />
<package id="EasyHook" version="2.7.6035" targetFramework="net461" />
<package id="ini-parser" version="2.3.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
</packages>