Skip to content

Commit

Permalink
Optimize Mutiple Database Search + Bug Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussacana committed Jan 31, 2021
1 parent 92edb1a commit 878067e
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 15 deletions.
4 changes: 2 additions & 2 deletions StringReloads/AutoInstall/ExHIBIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ExHIBIT : IAutoInstall
{
public string Name => "ExHIBIT";

lstrcpyA lstrcpyAHook;
ExHIBIT_lstrcpyA lstrcpyAHook;

ExHIBIT_Say10 Say10Hook;
ExHIBIT_PrintSub3 PrintSub3Hook;
Expand All @@ -28,7 +28,7 @@ public void Install()
var Resident = (from x in Modules where x.ModuleName.ToLowerInvariant() == "resident.dll" select x.BaseAddress).Single();

if (lstrcpyAHook == null)
lstrcpyAHook = new lstrcpyA(Resident);
lstrcpyAHook = new ExHIBIT_lstrcpyA(Resident);

Say10Hook.Install();
PrintSub3Hook.Install();
Expand Down
6 changes: 6 additions & 0 deletions StringReloads/Engine/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public string[] IniLines {
string _Workspace = null;
public string Workspace => _Workspace ??= GetValue("Workspace");

bool? _Hashset = null;
public bool Hashset => _Hashset ??= GetValue("Hashset").ToBoolean();

bool? _FastMode = null;
public bool FastMode => _FastMode ??= GetValue("FastMode").ToBoolean();


string _WorkingDirectory = null;
public string WorkingDirectory {
Expand Down
24 changes: 23 additions & 1 deletion StringReloads/Engine/Initializer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using StringReloads.Engine.Unmanaged;
using StringReloads.Engine.Interface;
using StringReloads.Engine.Match;
using StringReloads.Engine.Unmanaged;
using StringReloads.StringModifier;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Configuration;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -35,9 +39,20 @@ internal void Initialize(SRL Engine)
else
BuildCache(Engine);

if (Engine.Settings.Hashset) {
Log.Debug("Generating Hashset...");
foreach (var Database in Engine.Databases) {
foreach (var Entry in Database) {
Engine.Hashset.Add(Entry.OriginalLine);
Engine.Hashset.Add(Minify.Default.Apply(Entry.OriginalLine, null));
}
}
}

Log.Debug($"{Engine.Databases.Count} Database(s) Loaded");
Log.Debug($"{Engine.CharRemap.Count} Remap(s) Loaded");
Log.Debug($"{Engine.CharRemapAlt.Count} Alternative Remap(s) Loaded");
Log.Debug($"{Engine.Hashset.Count} Hashes Ready");

PluginsInitializer(Engine);

Expand All @@ -52,6 +67,13 @@ internal void Initialize(SRL Engine)

EnableExceptionHandler(Engine);

if (Engine.Settings.FastMode)
{
Log.Debug("Fast Mode Enabled, Disabling Cool Features...");
IMatch Basic = (from x in Engine.Matchs where x is BasicMatch select x).First();
Engine._Matchs = new IMatch[] { Basic };
}

Engine.Initialized = true;
Log.Information("SRL Initialized");
}
Expand Down
4 changes: 2 additions & 2 deletions StringReloads/Engine/LSTParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public LSTEntry(string LineA, string LineB)
LineB = LineB.Substring(LineB.IndexOf("::", 2) + 2);
}

OriginalLine = LineA.Replace(LSTParser.BreakLine, "\n");
TranslationLine = LineB.Replace(LSTParser.ReturnLine, "\r");
OriginalLine = LineA.Replace(LSTParser.BreakLine, "\n").Replace(LSTParser.ReturnLine, "\r");
TranslationLine = LineB.Replace(LSTParser.BreakLine, "\n").Replace(LSTParser.ReturnLine, "\r");
}

public LSTFlag[] Flags => OriginalFlags.Union(TranslationFlags).ToArray();
Expand Down
33 changes: 24 additions & 9 deletions StringReloads/Engine/Match/BasicMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ public bool HasMatch(string String)
{
string Minified = String;

foreach (var Modifier in Engine.ReloadModifiers)
if (!Engine.Settings.FastMode)
{
if (Modifier.CanRestore)
Minified = Modifier.Restore(Minified);
foreach (var Modifier in Engine.ReloadModifiers)
{
if (Modifier.CanRestore)
Minified = Modifier.Restore(Minified);
}
}

Minified = Engine.Minify(String);

if (Engine.CurrentDatabase.HasKey(Minified))
return true;

if (Engine.Settings.Hashset && !Engine.Hashset.Contains(String) && !Engine.Hashset.Contains(Minified))
return false;

for (int i = 0; i < Engine.Databases.Count; i++)
{
if (Engine.Databases[i].HasKey(Minified))
Expand All @@ -48,10 +54,13 @@ public bool HasValue(string String)
{
string Minified = String;

foreach (var Modifier in Engine.ReloadModifiers)
if (!Engine.Settings.FastMode)
{
if (Modifier.CanRestore)
Minified = Modifier.Restore(Minified);
foreach (var Modifier in Engine.ReloadModifiers)
{
if (Modifier.CanRestore)
Minified = Modifier.Restore(Minified);
}
}

Minified = Engine.Minify(String);
Expand All @@ -72,17 +81,23 @@ public bool HasValue(string String)

string Minified = String;

foreach (var Modifier in Engine.ReloadModifiers)
if (!Engine.Settings.FastMode)
{
if (Modifier.CanRestore)
Minified = Modifier.Restore(Minified);
foreach (var Modifier in Engine.ReloadModifiers)
{
if (Modifier.CanRestore)
Minified = Modifier.Restore(Minified);
}
}

Minified = Engine.Minify(Minified);

if (Engine.CurrentDatabase.HasKey(Minified))
return Engine.CurrentDatabase[Minified];

if (Engine.Settings.Hashset && !Engine.Hashset.Contains(String) && !Engine.Hashset.Contains(Minified))
return null;

for (int i = 0; i < Engine.Databases.Count; i++)
{
if (Engine.Databases[i].HasKey(Minified))
Expand Down
3 changes: 3 additions & 0 deletions StringReloads/Engine/SRL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ where typeof(IPlugin).IsAssignableFrom(Typ) && !Typ.IsInterface

public List<Database> Databases = new List<Database>();

public HashSet<string> Hashset = new HashSet<string>();

internal IStringModifier[] _ReloadModifiers = null;
public IStringModifier[] ReloadModifiers => _ReloadModifiers ??= new IStringModifier[] {
new MonoWordWrap(this),
Expand Down Expand Up @@ -178,6 +180,7 @@ public Database CurrentDatabase {

return Output;
}

public string ProcessString(string String)
{
if (Settings.CacheOutput && RecentOutput.Contains(String))
Expand Down
56 changes: 56 additions & 0 deletions StringReloads/Hook/Others/ExHIBIT_lstrcpyA.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections;
using System.Text;
using StringReloads.Engine.String;
using StringReloads.Hook.Base;

namespace StringReloads.Hook.Others
{
unsafe class ExHIBIT_lstrcpyA : Hook<lstrcpyDelegate>
{
public ExHIBIT_lstrcpyA()
{
HookDelegate = hlstrcpyA;
Compile();
}
public ExHIBIT_lstrcpyA(void* TargetModule) : this (new IntPtr(TargetModule)) { }
public ExHIBIT_lstrcpyA(IntPtr TargetModule)
{
HookDelegate = hlstrcpyA;
Compile(true, TargetModule);
}
public override string Library => "kernel32.dll";

public override string Export => "lstrcpyA";

public override void Initialize() { }

private unsafe byte* hlstrcpyA(byte* lpString1, byte* lpString2)
{
if (!EntryPoint.SRL.Initialized)
return Bypass(lpString1, lpString2);

string Str = (CString)lpString2;

if (Minify(Str).Length == 0)
return Bypass(lpString1, lpString2);

lpString2 = EntryPoint.SRL.ProcessString((CString)lpString2);
return Bypass(lpString1, lpString2);
}

private string Minify(string Input) {
StringBuilder Builder = new StringBuilder();
foreach (var Char in Input) {
if (Char >= '0' && Char <= '9')
continue;
if (Char >= ',' && Char <= '.')
continue;
if (Char == ';' || char.IsWhiteSpace(Char))
continue;
Builder.Append(Char);
}
return Builder.ToString().Replace("NULL", "").Trim();
}
}
}
4 changes: 3 additions & 1 deletion StringReloads/SRL.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ Debug=false
Dump=false
DumpRegex=false
Encoding=SJIS
FastMode=false
Hashset=false
HeapAlloc=false
ImportHook=false
LoadLocalFonts=false
Log=true
LogFile=false
LogLevel=INF
Expand All @@ -16,7 +19,6 @@ ReloadRegexCaptures=true
SanityCheck=true
TrimMatch=false
Workspace=StringReloads
LoadLocalFonts=false

[Wordwrap]
UseRelativeWidth=false
Expand Down

0 comments on commit 878067e

Please sign in to comment.