Skip to content

Commit

Permalink
added dependencygraphsettings window
Browse files Browse the repository at this point in the history
  • Loading branch information
kruumy committed Mar 10, 2023
1 parent 5e5698a commit 02187d4
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 21 deletions.
22 changes: 18 additions & 4 deletions EasyZoneBuilder.Core/DependencyGraphUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ namespace EasyZoneBuilder.Core
{
public static class DependencyGraphUtil
{
public const string FILENAME = "dependency_graph.json";

public static async Task GenerateDependencyGraphJson()
{
Dictionary<string, Dictionary<string, AssetType>> zone_asset_assetType = await ZoneBuilder.ListAssets(Core.Settings.IW4.Zones);
string[] zones = Core.Settings.IW4.Zones;
Console.WriteLine($"Reading {zones.Count()} zones...");
Dictionary<string, Dictionary<string, AssetType>> zone_asset_assetType = await ZoneBuilder.ListAssets(zones);
Dictionary<string, List<string>> asset_zones = new Dictionary<string, List<string>>();
foreach ( KeyValuePair<string, Dictionary<string, AssetType>> item in zone_asset_assetType )
{
Expand All @@ -23,13 +27,13 @@ public static async Task GenerateDependencyGraphJson()
asset_zones[ $"{item1.Value}:{item1.Key}" ].Add(item.Key); // TODO: might need to remove duplicates
}
}
File.WriteAllText("dependency_graph.json", Core.TinyJson.JSONWriter.ToJson(asset_zones));
File.WriteAllText(FILENAME, Core.TinyJson.JSONWriter.ToJson(asset_zones));
Console.WriteLine("Done!");
}
public static IEnumerable<string> GetRequiredZones( ModCSV csv )
{
// Took a lot of inspiration from https://github.com/XLabsProject/iw4-zone-asset-finder/blob/main/iw4-zone-asset-finder/Commands/BuildRequirements.cs
string rawdepgraph = File.ReadAllText("dependency_graph.json");
Dictionary<string, List<string>> dependency_graph = Core.TinyJson.JSONParser.FromJson<Dictionary<string, List<string>>>(rawdepgraph);
Dictionary<string, List<string>> dependency_graph = Get();
List<KeyValuePair<string, List<string>>> assets_zones = new List<KeyValuePair<string, List<string>>>();
foreach ( KeyValuePair<string, AssetType> asset in csv )
{
Expand Down Expand Up @@ -69,5 +73,15 @@ public static async Task<IEnumerable<string>> GetRequiredZonesAsync( ModCSV csv
{
return await Task.Run(() => GetRequiredZones(csv));
}

public static Dictionary<string, List<string>> Get()
{
return Core.TinyJson.JSONParser.FromJson<Dictionary<string, List<string>>>(File.ReadAllText(FILENAME));
}

public static async Task<Dictionary<string, List<string>>> GetAsync()
{
return await Task.Run(() => Get());
}
}
}
26 changes: 25 additions & 1 deletion EasyZoneBuilder.Core/IW4.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
using EasyZoneBuilder.Core.Interfaces;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace EasyZoneBuilder.Core
{
public class IW4 : IDirectoryInfo
{
public DirectoryInfo Directory { get; }
public Mod[] Mods { get; }

public readonly string[] BlacklistedZones =
{
"mp_ambush_sh",
"mp_bloc",
"mp_cargoship",
"mp_fav_tropical",
"mp_killhouse",
"mp_nuked",
"mp_nuked_shaders",
"mp_storm_spring",
"team_opfor",
"team_opforce_airborne",
"team_opforce_composite",
"team_rangers",
"team_spetsnaz",
"team_tf141",
"team_us_army",
"iw4x_code_post_gfx_mp"
};
// TODO: check why these zones dont work in zonebuilder,
// my guess is that it needs some other zone loaded before it

public string[] Zones
{
get
Expand All @@ -17,7 +41,7 @@ public string[] Zones
for ( int i = 0; i < fullPath.Length; i++ )
{
string name = Path.GetFileNameWithoutExtension(fullPath[ i ]);
if ( name != "mp_ambush_sh" && name != "mp_bloc" && name != "mp_cargoship" ) // TODO: check why these zones dont work in zonebuilder, my guess is that it needs some other zone loaded before it
if ( !BlacklistedZones.Any(z => name == z) )
{
result.Add(name);
}
Expand Down
13 changes: 9 additions & 4 deletions EasyZoneBuilder.Core/ZoneBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public static class ZoneBuilder
{
public static FileInfo TargetExecutable { get; private set; }

public static string LastError { get; private set; } = string.Empty;

private static readonly int MAX_COMMANDS_PER_EXECUTATION = 30;

public static void Initialize( FileInfo iw4x )
Expand All @@ -24,8 +22,11 @@ public static async Task<string> Execute( params string[] commands )
{
StringBuilder ret = new StringBuilder();
IEnumerable<string[]> batchCommands = commands.SplitIntoChunks(MAX_COMMANDS_PER_EXECUTATION);
int i = 0;
foreach ( string[] bcommands in batchCommands )
{
i++;
Console.WriteLine($"{i}/{batchCommands.Count()} with {bcommands.Count()} zones");
StringBuilder args = new StringBuilder();
args.Append("-nosteam -zonebuilder -stdout");
foreach ( string com in bcommands )
Expand All @@ -46,7 +47,11 @@ public static async Task<string> Execute( params string[] commands )
p.StartInfo.CreateNoWindow = true;
p.Start();
string raw = await p.StandardOutput.ReadToEndAsync();
LastError = await p.StandardError.ReadToEndAsync();
string err = await p.StandardError.ReadToEndAsync();
if ( !string.IsNullOrEmpty(err.Trim()) )
{
throw new Exception("Zonebuilder: " + err);
}
ret.AppendLine(raw.Substring(raw.LastIndexOf('"') + 3).Replace("\r", string.Empty));
}
}
Expand Down Expand Up @@ -169,7 +174,7 @@ public static async Task BuildZone( ModCSV csv, FileInfo destination )
}
else
{
throw new Exception(LastError);
throw new FileNotFoundException();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion EasyZoneBuilder.Core/dependency_graph.json

Large diffs are not rendered by default.

79 changes: 78 additions & 1 deletion EasyZoneBuilder.GUI/DependencyGraphSettings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,85 @@
xmlns:local="clr-namespace:EasyZoneBuilder.GUI"
mc:Ignorable="d"
Style="{DynamicResource CustomWindowStyle}"
Title="Dependency Graph Settings" Height="225" Width="400" ResizeMode="NoResize" Icon="/Resources/settings.ico" WindowStartupLocation="CenterOwner">
Title="Dependency Graph Settings" Height="300" Width="450" ResizeMode="NoResize" Icon="/Resources/settings.ico" WindowStartupLocation="CenterOwner">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="125"></ColumnDefinition>
<ColumnDefinition Width="125"></ColumnDefinition>
</Grid.ColumnDefinitions>

<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label
Content="Dependency Graph"
HorizontalAlignment="Center">

</Label>
<ListBox
x:Name="DependencyGraphInfoBox"
Grid.Row="1"
Margin="5"
FontSize="11"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Loaded="DependencyGraphInfoBox_Loaded">
</ListBox>
</Grid>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label
Content="Detected Zones"
HorizontalAlignment="Center">
</Label>
<ListBox
x:Name="ZonesListBox"
Grid.Row="1"
Margin="5"
FontSize="11"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Loaded="ZonesListBox_Loaded">
</ListBox>
</Grid>





<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Button
x:Name="RegenerateDependencyGraphBtn"
Content="Regenerate Dependency Graph"
Click="RegenerateDependencyGraphBtn_Click"
Margin="5"
Grid.Row="2">
</Button>
<Label
Grid.Row="0"
Content="Console Output"
HorizontalAlignment="Center">

</Label>
<TextBox
x:Name="ConsoleOutputBox"
Grid.Row="1"
Margin="5"
IsReadOnly="True"
Loaded="ConsoleOutputBox_Loaded"
Unloaded="ConsoleOutputBox_Unloaded">
</TextBox>

</Grid>

</Grid>
</Window>
77 changes: 67 additions & 10 deletions EasyZoneBuilder.GUI/DependencyGraphSettings.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EasyZoneBuilder.Core;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EasyZoneBuilder.GUI
{
Expand All @@ -23,5 +16,69 @@ public DependencyGraphSettings()
{
InitializeComponent();
}

private void ZonesListBox_Loaded( object sender, RoutedEventArgs e )
{
ZonesListBox.ItemsSource = Settings.IW4.Zones;
}

private ConsoleWriter ConsoleWriter;

private void ConsoleOutputBox_Loaded( object sender, RoutedEventArgs e )
{
ConsoleWriter = new ConsoleWriter(ConsoleOutputBox);
}

private void ConsoleOutputBox_Unloaded( object sender, RoutedEventArgs e )
{
ConsoleWriter.Dispose();
ConsoleWriter = null;
}

private async void RegenerateDependencyGraphBtn_Click( object sender, RoutedEventArgs e )
{
if ( MessageBoxResult.OK == MessageBox.Show("Are you sure? This will take a few minutes.", "Warning", MessageBoxButton.OKCancel, MessageBoxImage.Warning) )
{
RegenerateDependencyGraphBtn.IsEnabled = false;
object oldContent = RegenerateDependencyGraphBtn.Content;
RegenerateDependencyGraphBtn.Content = "Generating...";
await DependencyGraphUtil.GenerateDependencyGraphJson();
DependencyGraphInfoBox_Loaded(sender, e);
RegenerateDependencyGraphBtn.Content = oldContent;
RegenerateDependencyGraphBtn.IsEnabled = true;
MessageBox.Show($"Successfully written to '{DependencyGraphUtil.FILENAME}'!");
}
}

private async void DependencyGraphInfoBox_Loaded( object sender, RoutedEventArgs e )
{
DependencyGraphInfoBox.Items.Clear();
System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>> graph = await DependencyGraphUtil.GetAsync();
DependencyGraphInfoBox.Items.Add($"Asset Count = {graph.Count}");
}
}

internal class ConsoleWriter : TextWriter, IDisposable
{
private readonly TextBox _output;

public ConsoleWriter( TextBox output )
{
_output = output;
Console.SetOut(this);
}

public override void Write( char value )
{
_output.Dispatcher.Invoke(() => _output.AppendText(value.ToString()));
}

public override Encoding Encoding => Encoding.Unicode;

protected override void Dispose( bool disposing )
{
Console.SetOut(Console.Out);
base.Dispose(disposing);
}
}
}

0 comments on commit 02187d4

Please sign in to comment.