2020-12-19 19:14:40 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.Threading.Tasks;
|
2020-12-25 10:49:51 -05:00
|
|
|
using auto_creamapi.Models;
|
2020-12-19 19:14:40 -05:00
|
|
|
using auto_creamapi.Utils;
|
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
namespace auto_creamapi.Services
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
public interface ICreamDllService
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
public string TargetPath { get; set; }
|
2020-12-28 09:27:37 -05:00
|
|
|
public Task Initialize();
|
2020-12-25 10:49:51 -05:00
|
|
|
public void Save();
|
|
|
|
public void CheckIfDllExistsAtTarget();
|
|
|
|
public bool CreamApiApplied();
|
2021-01-01 08:53:02 -05:00
|
|
|
public bool CreamApiApplied(string arch);
|
2020-12-23 06:34:31 -05:00
|
|
|
}
|
2020-12-19 19:14:40 -05:00
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
public class CreamDllService : ICreamDllService
|
|
|
|
{
|
2020-12-19 19:14:40 -05:00
|
|
|
private const string X86Arch = "x86";
|
|
|
|
private const string X64Arch = "x64";
|
2020-12-28 09:27:37 -05:00
|
|
|
private static readonly string HashPath = Path.Combine(Directory.GetCurrentDirectory(), "cream_api.md5");
|
2020-12-19 19:14:40 -05:00
|
|
|
|
2021-01-01 08:53:02 -05:00
|
|
|
private Dictionary<string, CreamDll> _creamDlls;
|
2020-12-19 19:14:40 -05:00
|
|
|
private bool _x64Exists;
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
private bool _x86Exists;
|
2020-12-23 13:13:52 -05:00
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public string TargetPath { get; set; }
|
2020-12-25 10:49:51 -05:00
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public async Task Initialize()
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
|
|
|
MyLogger.Log.Debug("CreamDllService: Initialize begin");
|
2021-01-01 08:53:02 -05:00
|
|
|
_creamDlls = new Dictionary<string, CreamDll>
|
|
|
|
{
|
|
|
|
{X86Arch, new CreamDll("steam_api.dll", "steam_api_o.dll")},
|
|
|
|
{X64Arch, new CreamDll("steam_api64.dll", "steam_api64_o.dll")}
|
|
|
|
};
|
2020-12-19 19:14:40 -05:00
|
|
|
|
|
|
|
if (!File.Exists(HashPath))
|
|
|
|
{
|
|
|
|
MyLogger.Log.Information("Writing md5sum file...");
|
2020-12-28 09:27:37 -05:00
|
|
|
await File.WriteAllLinesAsync(HashPath,
|
|
|
|
new[]
|
|
|
|
{
|
|
|
|
$"{_creamDlls[X86Arch].Hash} {_creamDlls[X86Arch].Filename}",
|
|
|
|
$"{_creamDlls[X64Arch].Hash} {_creamDlls[X64Arch].Filename}"
|
2021-01-01 08:53:02 -05:00
|
|
|
}).ConfigureAwait(false);
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
2020-12-25 10:49:51 -05:00
|
|
|
|
|
|
|
MyLogger.Log.Debug("CreamDllService: Initialize end");
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
{
|
|
|
|
if (_x86Exists) CopyDll(X86Arch);
|
|
|
|
if (_x64Exists) CopyDll(X64Arch);
|
|
|
|
}
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public void CheckIfDllExistsAtTarget()
|
|
|
|
{
|
2021-01-01 08:53:02 -05:00
|
|
|
var x86File = Path.Combine(TargetPath, "steam_api.dll");
|
|
|
|
var x64File = Path.Combine(TargetPath, "steam_api64.dll");
|
|
|
|
_x86Exists = File.Exists(x86File);
|
|
|
|
_x64Exists = File.Exists(x64File);
|
|
|
|
if (_x86Exists) MyLogger.Log.Information($"x86 SteamAPI DLL found: {x86File}");
|
|
|
|
if (_x64Exists) MyLogger.Log.Information($"x64 SteamAPI DLL found: {x64File}");
|
2020-12-28 09:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool CreamApiApplied()
|
|
|
|
{
|
|
|
|
var a = CreamApiApplied("x86");
|
|
|
|
var b = CreamApiApplied("x64");
|
|
|
|
return a | b;
|
|
|
|
}
|
|
|
|
|
2020-12-19 19:14:40 -05:00
|
|
|
private void CopyDll(string arch)
|
|
|
|
{
|
|
|
|
var sourceSteamApiDll = _creamDlls[arch].Filename;
|
|
|
|
var targetSteamApiDll = Path.Combine(TargetPath, _creamDlls[arch].Filename);
|
|
|
|
var targetSteamApiOrigDll = Path.Combine(TargetPath, _creamDlls[arch].OrigFilename);
|
|
|
|
var targetSteamApiDllBackup = Path.Combine(TargetPath, $"{_creamDlls[arch].Filename}.backup");
|
2020-12-23 06:47:55 -05:00
|
|
|
MyLogger.Log.Information($"Setting up CreamAPI DLL @ {TargetPath} (arch :{arch})");
|
2020-12-19 19:14:40 -05:00
|
|
|
// Create backup of steam_api.dll
|
|
|
|
File.Copy(targetSteamApiDll, targetSteamApiDllBackup, true);
|
|
|
|
// Check if steam_api_o.dll already exists
|
|
|
|
// If missing rename original file
|
|
|
|
if (!File.Exists(targetSteamApiOrigDll))
|
|
|
|
File.Move(targetSteamApiDll, targetSteamApiOrigDll, true);
|
|
|
|
// Copy creamapi dll
|
|
|
|
File.Copy(sourceSteamApiDll, targetSteamApiDll, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CreamApiApplied(string arch)
|
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
var a = File.Exists(Path.Combine(TargetPath, _creamDlls[arch].OrigFilename));
|
|
|
|
var b = GetHash(Path.Combine(TargetPath, _creamDlls[arch].Filename)).Equals(_creamDlls[arch].Hash);
|
2020-12-19 19:14:40 -05:00
|
|
|
return a & b;
|
|
|
|
}
|
|
|
|
|
2021-01-01 08:53:02 -05:00
|
|
|
private static string GetHash(string filename)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2021-01-13 07:04:35 -05:00
|
|
|
if (!File.Exists(filename)) return "";
|
|
|
|
using var md5 = MD5.Create();
|
|
|
|
using var stream = File.OpenRead(filename);
|
|
|
|
return BitConverter
|
|
|
|
.ToString(md5.ComputeHash(stream))
|
|
|
|
.Replace("-", string.Empty);
|
2020-12-28 09:27:37 -05:00
|
|
|
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|