Skip to content

Commit

Permalink
Implement basic search
Browse files Browse the repository at this point in the history
  • Loading branch information
Grossley committed Mar 5, 2021
1 parent ab8ce07 commit 02d9454
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
68 changes: 68 additions & 0 deletions UndertaleModTool/SampleScripts/Search.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Text;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

int progress = 0;
string results = "";
int result_count = 0;
int code_count = 0;
ThreadLocal<DecompileContext> DECOMPILE_CONTEXT = new ThreadLocal<DecompileContext>(() => new DecompileContext(Data, false));

UpdateProgress();
bool case_sensitive = ScriptQuestion("Case sensitive?");
String keyword = SimpleTextInput("Enter your search", "Search box below", "", false);

await DumpCode();
HideProgressBar();
SimpleTextInput("Search results.", result_count.ToString() + " results in " + code_count.ToString() + " code entries.", results, true);

void UpdateProgress()
{
UpdateProgressBar(null, "Code Entries", progress++, Data.Code.Count);
}

string GetFolder(string path)
{
return Path.GetDirectoryName(path) + Path.DirectorySeparatorChar;
}


async Task DumpCode()
{
await Task.Run(() => Parallel.ForEach(Data.Code, DumpCode));
}

void DumpCode(UndertaleCode code)
{
try
{
var line_number = 1;
string decompiled_text = (code != null ? Decompiler.Decompile(code, DECOMPILE_CONTEXT.Value) : "");
string[] splitted = decompiled_text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
bool name_written = false;
foreach (string lineInt in splitted) {
if (case_sensitive ? lineInt.Contains(keyword) : lineInt.ToLower().Contains(keyword.ToLower()))
{
if (name_written == false)
{
results += "Results in " + code.Name.Content + ": \n==========================\n";
name_written = true;
code_count += 1;
}
results += "Line " + line_number.ToString() + ": " + lineInt + "\n";
result_count += 1;
}
line_number += 1;
}
if (name_written == true)
results += "\n";

}
catch (Exception e)
{
}

UpdateProgress();
}
3 changes: 3 additions & 0 deletions UndertaleModTool/UndertaleModTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@
<None Include="SampleScripts\RoomOfDetermination.csx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SampleScripts\Search.csx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SampleScripts\ShowRoomName.csx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 02d9454

Please sign in to comment.