Skip to content

Commit

Permalink
Revert registry interface method, fix SN error, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Sep 27, 2020
1 parent fd3d6de commit d71ada7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 102 deletions.
92 changes: 25 additions & 67 deletions SmartImage/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ internal static void HandleContextMenu(IntegrationOption option)
case IntegrationOption.Add:
string fullPath = RuntimeInfo.ExeLocation;

if (!RuntimeInfo.IsExeInAppFolder) {
bool v = CliOutput.ReadConfirm("Could not find exe in system path. Add now?");
// Add command and icon to command
string[] commandCode =
{
"@echo off",
$"reg.exe add {REG_SHELL_CMD} /ve /d \"{fullPath} \"\"%%1\"\"\" /f >nul",
$"reg.exe add {REG_SHELL} /v Icon /d \"{fullPath}\" /f >nul"
};

if (v) {
Setup();
return;
}
}


RegistrySubKey.SetValue("Icon", fullPath);

var cmd = RegistrySubKey.CreateSubKey("command");
cmd.SetValue(null, String.Format("\"{0}\" \"%1\"", fullPath));
Cli.CreateRunBatchFile("add_to_menu.bat", commandCode);

break;
case IntegrationOption.Remove:

Registry.CurrentUser.DeleteSubKeyTree(REG_HKCU_SHELL_SMARTIMAGE);
string[] code =
{
"@echo off",
$@"reg.exe delete {REG_SHELL} /f >nul"
};

Cli.CreateRunBatchFile("rem_from_menu.bat", code);

break;
default:
Expand Down Expand Up @@ -88,53 +89,6 @@ internal static void HandlePath(IntegrationOption option)
}
}

/// <summary>
/// Remove old legacy registry integrations
/// </summary>
internal static void RemoveOldRegistry()
{
const string REG_SHELL_LEGACY = @"HKEY_CLASSES_ROOT\*\shell\SmartImage\";

const string REG_SHELL_CMD_LEGACY = @"HKEY_CLASSES_ROOT\*\shell\SmartImage\command";

bool added = IsAdded();

if (added) {
Remove();
}
else {
return;
}

bool success = !IsAdded();


static void Remove()
{
string[] code =
{
"@echo off",
$@"reg.exe delete {REG_SHELL_LEGACY} /f >nul"
};

Cli.CreateRunBatchFile("rem_from_menu.bat", code);
}

static bool IsAdded()
{
string cmdStr = String.Format(@"reg query {0}", REG_SHELL_CMD_LEGACY);
var cmd = Cli.Shell(cmdStr, true);

string[] stdOut = Cli.ReadAllLines(cmd.StandardOutput);

bool b = stdOut.Any(s => s.Contains(RuntimeInfo.NAME));
return b;
}

if (!success) {
throw new SmartImageException();
}
}

internal static void ResetIntegrations()
{
Expand Down Expand Up @@ -182,23 +136,27 @@ internal static void Uninstall()

File.WriteAllText(dir, commands.QuickJoin("\n"));


// Runs in background
Process.Start(dir);
}

private const string REG_HKCU_SHELL_ROOT = @"Software\Classes\*\shell\";
private const string REG_SHELL = @"HKEY_CLASSES_ROOT\*\shell\SmartImage\";

private const string REG_HKCU_SHELL_SMARTIMAGE = REG_HKCU_SHELL_ROOT + @"SmartImage\";
private const string REG_SHELL_CMD = @"HKEY_CLASSES_ROOT\*\shell\SmartImage\command";

internal static RegistryKey RegistrySubKey => Registry.CurrentUser.CreateSubKey(REG_HKCU_SHELL_SMARTIMAGE);

internal static bool IsContextMenuAdded
{
get
{
var shell = Registry.CurrentUser.OpenSubKey(REG_HKCU_SHELL_ROOT)!;
return shell.GetSubKeyNames().Contains(RuntimeInfo.NAME);
string cmdStr = String.Format(@"reg query {0}", REG_SHELL_CMD);
var cmd = Cli.Shell(cmdStr, true);

string[] stdOut = Cli.ReadAllLines(cmd.StandardOutput);

bool b = stdOut.Any(s => s.Contains(RuntimeInfo.NAME));
return b;
}
}

Expand Down
6 changes: 3 additions & 3 deletions SmartImage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ private static void Main(string[] args)
// return;
// }

using var s = new SearchClient(img);
s.Start();
using var searchClient = new SearchClient(img);
searchClient.Start();

ConsoleIO.HandleOptions(s.Results);
ConsoleIO.HandleOptions(searchClient.Results);
}
catch (Exception exception) {

Expand Down
25 changes: 12 additions & 13 deletions SmartImage/Searching/Engines/SauceNao/SauceNaoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ private SearchResult GetBestResultWithApi(string url)
return new SearchResult(this, null);
}

// todo: IExtendedSearchResult

private readonly struct SauceNaoSimpleResult
{
public string Title { get; }
Expand Down Expand Up @@ -186,7 +188,7 @@ private static string FindCreator(HtmlNode resultcontent)
return i;

}

// TODO: organize like Yandex


Expand All @@ -196,10 +198,8 @@ private static List<SauceNaoSimpleResult> ParseResults(HtmlDocument doc)

var images = new List<SauceNaoSimpleResult>();

foreach (var result in results)
{
if (result.GetAttributeValue("id", string.Empty) == "result-hidden-notification")
{
foreach (var result in results) {
if (result.GetAttributeValue("id", string.Empty) == "result-hidden-notification") {
continue;
}

Expand Down Expand Up @@ -232,7 +232,7 @@ private static List<SauceNaoSimpleResult> ParseResults(HtmlDocument doc)

var title = FindCreator(resultcontent);
var similarity = float.Parse(resultsimilarityinfo.InnerText.Replace("%", String.Empty));


var i = new SauceNaoSimpleResult(title, link, similarity);
images.Add(i);
Expand All @@ -243,22 +243,21 @@ private static List<SauceNaoSimpleResult> ParseResults(HtmlDocument doc)

private SearchResult GetBestResultWithoutApi(string url)
{


SearchResult? sr = null;

var resUrl = BASIC_RESULT + url;


var sz = Network.GetString(resUrl);
var doc = new HtmlDocument();
doc.LoadHtml(sz);

try {

var sz = Network.GetString(resUrl);
var doc = new HtmlDocument();
doc.LoadHtml(sz);

var img = ParseResults(doc);

var best = img.OrderByDescending(i=>i.Similarity).First(i => i.Url!=null);
var best = img.OrderByDescending(i => i.Similarity).First(i => i.Url != null);

sr = new SearchResult(this, best.Url, best.Similarity);
sr.ExtendedInfo.Add(best.Title);
Expand Down
9 changes: 6 additions & 3 deletions SmartImage/Searching/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

namespace SmartImage.Searching
{
/// <summary>
/// Searching client
/// </summary>
public class SearchClient : IDisposable
{
/// <summary>
Expand All @@ -36,7 +39,7 @@ public class SearchClient : IDisposable

public SearchClient(string img)
{


string auth = SearchConfig.Config.ImgurAuth;
bool useImgur = !String.IsNullOrWhiteSpace(auth);
Expand All @@ -55,7 +58,7 @@ public SearchClient(string img)

m_imgUrl = Upload(img, useImgur);


m_threads = CreateSearchThreads();
}

Expand Down Expand Up @@ -86,7 +89,7 @@ public void Start()
private Thread[] CreateSearchThreads()
{
// todo: improve


var availableEngines = GetAllEngines()
.Where(e => m_engines.HasFlag(e.Engine))
Expand Down
14 changes: 0 additions & 14 deletions SmartImage/Shell/ConsoleMainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,6 @@ private static ConsoleOption[] AllOptions
}
};


private static readonly ConsoleOption LegacyCleanupOption = new ConsoleOption()
{
Name = "Legacy cleanup",
Function = () =>
{
Integration.RemoveOldRegistry();
ConsoleIO.WaitForInput();
return null;
}
};

private static readonly ConsoleOption UninstallOption = new ConsoleOption()
{
Name = "Uninstall",
Expand Down
2 changes: 1 addition & 1 deletion SmartImage/Shell/ConsoleOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ConsoleOption()
/// </summary>
public static readonly ConsoleOption Wait = new ConsoleOption()
{
Name = "Wait",
Name = "Wait",

Color = ConsoleColor.Yellow,

Expand Down
2 changes: 1 addition & 1 deletion SmartImage/SmartImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

<PropertyGroup>
<PackageId>SmartImage</PackageId>
<Version>1.9.5</Version>
<Version>1.9.6</Version>
<Authors>Read Stanton (Decimation)</Authors>
<PackageTags>Image reverse search identification source sauce</PackageTags>
<RepositoryUrl>https://github.com/Decimation/SmartImage</RepositoryUrl>
Expand Down

0 comments on commit d71ada7

Please sign in to comment.