Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically fetch Script folders and files #978

Merged
merged 4 commits into from
Jun 17, 2022
Merged
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
2 changes: 1 addition & 1 deletion UndertaleModTests/GameScriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public async Task<bool> GenerateGMLCache(ThreadLocal<GlobalDecompileContext> dec

protected async Task<object> RunScript(string path)
{
string scriptpath = Path.Combine("../../../UndertaleModTool/Scripts/SampleScripts/", path);
string scriptpath = Path.Combine("../../../UndertaleModTool/Scripts/Builtin Scripts/", path);
using (var loader = new InteractiveAssemblyLoader())
{
loader.RegisterDependency(typeof(UndertaleObject).GetTypeInfo().Assembly);
Expand Down
22 changes: 2 additions & 20 deletions UndertaleModTool/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,8 @@
<MenuItem Header="S_ettings" Command="Properties"/>
<MenuItem Header="_Close" Command="Close" InputGestureText="Ctrl+Q"/>
</MenuItem>
<MenuItem Header="_Scripts">
<MenuItem Header="_Run builtin script" SubmenuOpened="MenuItem_RunBuiltinScript_SubmenuOpened">
<MenuItem Header="(...loading...)" IsEnabled="False"/>
</MenuItem>
<MenuItem Header="_Run community script" SubmenuOpened="MenuItem_RunCommunityScript_SubmenuOpened">
<MenuItem Header="(...loading...)" IsEnabled="False"/>
</MenuItem>
<MenuItem Header="_Unpack assets" SubmenuOpened="MenuItem_RunUnpackScript_SubmenuOpened">
<MenuItem Header="(...loading...)" IsEnabled="False"/>
</MenuItem>
<MenuItem Header="_Repack assets" SubmenuOpened="MenuItem_RunRepackScript_SubmenuOpened">
<MenuItem Header="(...loading...)" IsEnabled="False"/>
</MenuItem>
<MenuItem Header="_Run technical script" SubmenuOpened="MenuItem_RunTechnicalScript_SubmenuOpened">
<MenuItem Header="(...loading...)" IsEnabled="False"/>
</MenuItem>
<MenuItem Header="_Run demo script" SubmenuOpened="MenuItem_RunDemoScript_SubmenuOpened">
<MenuItem Header="(...loading...)" IsEnabled="False"/>
</MenuItem>
<MenuItem Header="Run _other script..." Click="MenuItem_RunOtherScript_Click"/>
<MenuItem Name="RootScriptItem" Header="_Scripts" SubmenuOpened="RootMenuItem_SubmenuOpened">
<MenuItem Header="(...loading...)" IsEnabled="False"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_GitHub" Click="MenuItem_GitHub_Click"/>
Expand Down
125 changes: 66 additions & 59 deletions UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ private async Task LoadFile(string filename, bool preventClose = false)

// Clear "GC holes" left in the memory in process of data unserializing
// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.gcsettings.largeobjectheapcompactionmode?view=net-6.0
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
}

Expand Down Expand Up @@ -1133,7 +1133,7 @@ private async Task SaveFile(string filename, bool suppressDebug = false)
{
this.ShowError("An error occured while trying to save:\n" + e.Message, "Save error");
});

SaveSucceeded = false;
}
// Don't make any changes unless the save succeeds.
Expand Down Expand Up @@ -1169,7 +1169,7 @@ private async Task SaveFile(string filename, bool suppressDebug = false)
{
this.ShowError("An error occured while trying to save:\n" + exc.Message, "Save error");
});

SaveSucceeded = false;
}
if (Data != null)
Expand Down Expand Up @@ -1900,63 +1900,89 @@ private void MenuItem_Add_Click(object sender, RoutedEventArgs e)
OpenInTab(obj, true);
}

private void MenuItem_RunBuiltinScript_SubmenuOpened(object sender, RoutedEventArgs e)
{
MenuItem_RunScript_SubmenuOpened(sender, e, "SampleScripts");
}
private void MenuItem_RunCommunityScript_SubmenuOpened(object sender, RoutedEventArgs e)
{
MenuItem_RunScript_SubmenuOpened(sender, e, "CommunityScripts");
}
private void MenuItem_RunTechnicalScript_SubmenuOpened(object sender, RoutedEventArgs e)
{
MenuItem_RunScript_SubmenuOpened(sender, e, "TechnicalScripts");
}
private void MenuItem_RunUnpackScript_SubmenuOpened(object sender, RoutedEventArgs e)
private void RootMenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
{
MenuItem_RunScript_SubmenuOpened(sender, e, "Unpackers");
MenuItem_RunScript_SubmenuOpened(sender, e, Path.Combine(ExePath, "Scripts"));
}
private void MenuItem_RunRepackScript_SubmenuOpened(object sender, RoutedEventArgs e)
{
MenuItem_RunScript_SubmenuOpened(sender, e, "Repackers");
}
private void MenuItem_RunDemoScript_SubmenuOpened(object sender, RoutedEventArgs e)
{
MenuItem_RunScript_SubmenuOpened(sender, e, "DemoScripts");
}
private void MenuItem_RunScript_SubmenuOpened(object sender, RoutedEventArgs e, string folderName)

private void MenuItem_RunScript_SubmenuOpened(object sender, RoutedEventArgs e, string folderDir)
{
MenuItem item = sender as MenuItem;

// DUMB Wpf behaviour. If a child submenu gets triggered, it triggers ALL parent events.
// So this is needed to prevent triggering parent events.
e.Handled = true;

DirectoryInfo directory = new DirectoryInfo(folderDir);
item.Items.Clear();
try
{
var appDir = Program.GetExecutableDirectory();
var folderDir = Path.Combine(appDir, "Scripts", folderName);

// exit out early if the path does not exist.
if (!Directory.Exists(folderDir))
if (!directory.Exists)
{
item.Items.Add(new MenuItem { Header = $"(Path {folderDir} does not exist, cannot search for files!)", IsEnabled = false });
item.Items.Add(new MenuItem {Header = $"(Path {folderDir} does not exist, cannot search for files!)", IsEnabled = false});
return;
}


foreach (var path in Directory.EnumerateFiles(folderDir))
// Go over each csx file
foreach (var file in directory.EnumerateFiles("*.csx"))
{
var filename = Path.GetFileName(path);
if (!filename.EndsWith(".csx"))
continue;
MenuItem subitem = new MenuItem() { Header = filename.Replace("_", "__") };
var filename = file.Name;
// Replace _ with __ because WPF uses _ for keyboard navigation
MenuItem subitem = new MenuItem {Header = filename.Replace("_", "__")};
subitem.Click += MenuItem_RunBuiltinScript_Item_Click;
subitem.CommandParameter = path;
subitem.CommandParameter = file.FullName;
item.Items.Add(subitem);
}

foreach (var subDirectory in directory.EnumerateDirectories())
{
// Don't add directories which don't have script files
if (!subDirectory.EnumerateFiles("*.csx").Any())
continue;

var subDirName = subDirectory.Name;
// In addition to the _ comment from above, we also need to add at least one item, so that WPF uses this as a submenuitem
MenuItem subItem = new MenuItem {Header = subDirName.Replace("_", "__"), Items = {new MenuItem {Header = "(loading...)", IsEnabled = false}}};
subItem.SubmenuOpened += (o, args) => MenuItem_RunScript_SubmenuOpened(o, args, subDirectory.FullName);
item.Items.Add(subItem);
}

if (item.Items.Count == 0)
item.Items.Add(new MenuItem() { Header = "(whoops, no scripts found?)", IsEnabled = false });
item.Items.Add(new MenuItem {Header = "(No scripts found!)", IsEnabled = false});
}
catch (Exception err)
{
item.Items.Add(new MenuItem() { Header = err.ToString(), IsEnabled = false });
item.Items.Add(new MenuItem {Header = err.ToString(), IsEnabled = false});
}

// If we're at the complete root, we need to add the "Run other script" button as well
if (item.Name != "RootScriptItem") return;

var otherScripts = new MenuItem {Header = "Run _other script..."};
otherScripts.Click += MenuItem_RunOtherScript_Click;
item.Items.Add(otherScripts);
}

private async void MenuItem_RunBuiltinScript_Item_Click(object sender, RoutedEventArgs e)
{
string path = (string)(sender as MenuItem).CommandParameter;
if (File.Exists(path))
await RunScript(path);
else
this.ShowError("The script file doesn't exist.");
}

private async void MenuItem_RunOtherScript_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();

dlg.DefaultExt = "csx";
dlg.Filter = "Scripts (.csx)|*.csx|All files|*";

if (dlg.ShowDialog() == true)
{
await RunScript(dlg.FileName);
}
}

Expand Down Expand Up @@ -2471,25 +2497,6 @@ public string ScriptInputDialog(string title, string label, string defaultInput,
return dlg.InputText;
}

private async void MenuItem_RunBuiltinScript_Item_Click(object sender, RoutedEventArgs e)
{
string path = (string)(sender as MenuItem).CommandParameter;
await RunScript(path);
}

private async void MenuItem_RunOtherScript_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();

dlg.DefaultExt = "csx";
dlg.Filter = "Scripts (.csx)|*.csx|All files|*";

if (dlg.ShowDialog() == true)
{
await RunScript(dlg.FileName);
}
}

private void MenuItem_GitHub_Click(object sender, RoutedEventArgs e)
{
OpenBrowser("https://github.com/krzys-h/UndertaleModTool");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ EnsureDataLoaded();

if (!((Data.GMS2_3 == false) && (Data.GMS2_3_1 == false) && (Data.GMS2_3_2 == false)))
{
bool x = RunUMTScript(Path.Combine(ExePath, "Scripts", "HelperScripts", "ConvertFrom17to16_for_2.3.csx"));
bool x = RunUMTScript(Path.Combine(ExePath, "Scripts", "Helper Scripts", "ConvertFrom17to16_for_2.3.csx"));
if (x == false)
ScriptError("ConvertFrom17to16_for_2.3.csx failed!");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//Need to test this, once I do then I can obsolete the other two scripts
if (!((Data.GMS2_3 == false) && (Data.GMS2_3_1 == false) && (Data.GMS2_3_2 == false)))
{
bool x = RunUMTScript(Path.Combine(ExePath, "Scripts", "HelperScripts", "ConvertFrom17to16_for_2.3.csx"));
bool x = RunUMTScript(Path.Combine(ExePath, "Scripts", "Helper Scripts", "ConvertFrom17to16_for_2.3.csx"));
if (x == false)
ScriptError("ConvertFrom17to16_for_2.3.csx failed!");
return;
Expand Down