Skip to content

Commit

Permalink
improve dat loading / startup speed, + add options menu to automatica…
Browse files Browse the repository at this point in the history
…lly load dats on startup (#50)

* improve dat loading / startup speed, + add options menu to automatically load dats on startup

* .
  • Loading branch information
gmriggs authored Jan 30, 2022
1 parent 7874f6b commit f115672
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 40 deletions.
12 changes: 11 additions & 1 deletion ACViewer/ACViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="ActionCommand.cs" />
<Compile Include="Config\Config.cs" />
<Compile Include="Config\ConfigManager.cs" />
<Compile Include="Data\DIDTable.cs" />
<Compile Include="Data\DIDTables.cs" />
<Compile Include="Data\TeleportLocationType.cs" />
Expand Down Expand Up @@ -593,13 +595,18 @@
</ItemGroup>
<ItemGroup>
<Resource Include="Icons\OpenFile_16x.png" />
<Resource Include="Icons\Search_16x.png" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\texture_clamp.fx" />
<Content Include="Data\Locations.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Resource Include="Icons\about.png" />
<Resource Include="Icons\HelpApplication_16x.png" />
<Resource Include="Icons\help.png" />
<Resource Include="Icons\Question_16x.png" />
<Resource Include="Icons\Settings_16x.png" />
<Resource Include="Icons\Search_16x.png" />
<Resource Include="Icons\export-file.png" />
<Resource Include="Icons\portal.png" />
<None Include="Content\Images\highres.png">
Expand All @@ -615,6 +622,9 @@
<PackageReference Include="MonoGame.Framework.WpfInterop">
<Version>1.7.1</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ACE\Source\ACE.Common\ACE.Common.csproj">
Expand Down
8 changes: 8 additions & 0 deletions ACViewer/Config/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ACViewer.Config
{
public class Config
{
public string ACFolder { get; set; }
public bool AutomaticallyLoadDATsOnStartup { get; set; }
}
}
51 changes: 51 additions & 0 deletions ACViewer/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.IO;

using Newtonsoft.Json;

namespace ACViewer.Config
{
public static class ConfigManager
{
private static string Filename = "ACViewer.json";

private static Config config;

public static Config Config
{
get
{
if (config == null)
config = new Config();

return config;
}
}

public static void SaveConfig()
{
var settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;

var json = JsonConvert.SerializeObject(config, settings);

File.WriteAllText(Filename, json);
}

public static void LoadConfig()
{
if (!File.Exists(Filename)) return;

var json = File.ReadAllText(Filename);

var _config = JsonConvert.DeserializeObject<Config>(json);

if (_config == null)
{
Console.WriteLine($"ConfigManager.LoadConfig() - failed to parse {Filename}");
return;
}
config = _config;
}
}
}
Binary file added ACViewer/Icons/HelpApplication_16x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ACViewer/Icons/Question_16x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ACViewer/Icons/Settings_16x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ACViewer/Icons/about.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ACViewer/Icons/help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ACViewer/Icons/question-mark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 4 additions & 5 deletions ACViewer/Image.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -32,12 +33,10 @@ public static class Image
return colors;
}

public static Texture2D GetTextureFromBitmap(GraphicsDevice device, Bitmap bitmap)
public static Texture2D GetTextureFromBitmap(GraphicsDevice device, string filename)
{
var colors = GetColors(bitmap).ToArray();
var texture = new Texture2D(device, bitmap.Width, bitmap.Height, false, SurfaceFormat.Color);
texture.SetData(colors);
return texture;
using (var fs = new FileStream(filename, FileMode.Open))
return Texture2D.FromStream(device, fs);
}

public static Texture2D GetTexture2DFromBitmap(GraphicsDevice device, Bitmap bitmap)
Expand Down
2 changes: 1 addition & 1 deletion ACViewer/MapViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public MapViewer()

public void LoadContent()
{
WorldMap = Image.GetTextureFromBitmap(GraphicsDevice, new Bitmap(new FileStream(@"Content\Images\highres.png", FileMode.Open)));
WorldMap = Image.GetTextureFromBitmap(GraphicsDevice, @"Content\Images\highres.png");

Highlight = new Texture2D(GraphicsDevice, 1, 1);
Highlight.SetData(new Microsoft.Xna.Framework.Color[1] { Microsoft.Xna.Framework.Color.Red });
Expand Down
2 changes: 1 addition & 1 deletion ACViewer/View/About.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ACViewer.View"
mc:Ignorable="d"
Title="About" Height="170" Width="280" WindowStyle="ToolWindow" ResizeMode="NoResize">
Title="About" Height="170" Width="280" ResizeMode="NoResize" Icon="../Icons/Question_16x.png">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="7.5,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top">
<Run FontWeight="Bold">ACViewer - build 2022.01.28</Run><LineBreak /><LineBreak />
Expand Down
7 changes: 2 additions & 5 deletions ACViewer/View/FileExplorer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public partial class FileExplorer : UserControl, INotifyPropertyChanged

public List<string> FileIDs
{
get
{
return _fileIDs;
}
get => _fileIDs;
set
{
_fileIDs = value;
Expand Down Expand Up @@ -177,7 +174,7 @@ private void FileType_SelectionChanged(object sender, SelectionChangedEventArgs
MainWindow.Status.WriteLine($"{selected.Name}s: {FileIDs.Count:N0}");
}

private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Expand Down
18 changes: 15 additions & 3 deletions ACViewer/View/MainMenu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
</MenuItem>
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="_Options" Click="Options_Click" />
<MenuItem Header="_Options" Click="Options_Click">
<MenuItem.Icon>
<Image Source="../Icons/Settings_16x.png" />
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_Find DID" Click="FindDID_Click">
Expand All @@ -43,8 +47,16 @@
<MenuItem x:Name="optionShowParticles" Header="Show _Particles in World Viewer" Click="ShowParticles_Click" />
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_View Help" Click="Guide_Click" />
<MenuItem Header="_About" Click="About_Click"/>
<MenuItem Header="_View Help" Click="Guide_Click">
<MenuItem.Icon>
<Image Source="../Icons/help.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_About" Click="About_Click">
<MenuItem.Icon>
<Image Source="../Icons/about.png" />
</MenuItem.Icon>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
Expand Down
53 changes: 37 additions & 16 deletions ACViewer/View/MainMenu.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Diagnostics;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

Expand Down Expand Up @@ -42,29 +42,48 @@ public MainMenu()
Instance = this;
}

private async void OpenFile_Click(object sender, RoutedEventArgs e)
private void OpenFile_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "DAT files (*.dat)|*.dat|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
var files = openFileDialog.FileNames;
if (files.Length < 1) return;
var file = files[0];

MainWindow.Status.WriteLine("Reading " + file);
var success = openFileDialog.ShowDialog();

if (success != true) return;

var filenames = openFileDialog.FileNames;

if (filenames.Length < 1) return;

var filename = filenames[0];

LoadDATs(filename);
}

public void LoadDATs(string filename)
{
MainWindow.Status.WriteLine("Reading " + filename);

var worker = new BackgroundWorker();

await Task.Run(() => ReadDATFile(file));
worker.DoWork += (sender, doWorkEventArgs) =>
{
ReadDATFile(filename);
};

//var cellFiles = DatManager.CellDat.AllFiles.Count;
//var portalFiles = DatManager.PortalDat.AllFiles.Count;
worker.RunWorkerCompleted += (sender, runWorkerCompletedEventArgs) =>
{
/*var cellFiles = DatManager.CellDat.AllFiles.Count;
var portalFiles = DatManager.PortalDat.AllFiles.Count;
//MainWindow.Status.WriteLine($"CellFiles={cellFiles}, PortalFiles={portalFiles}");
MainWindow.Status.WriteLine($"CellFiles={cellFiles}, PortalFiles={portalFiles}");*/
MainWindow.Status.WriteLine("Done");
GameView.PostInit();
}
};

worker.RunWorkerAsync();
}

private void Export_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -132,10 +151,10 @@ private void Export_Click(object sender, RoutedEventArgs e)
FileExport.ExportRaw(DatType.Portal, selectedFileID, saveFilename);
}

public void ReadDATFile(string filename)
public static void ReadDATFile(string filename)
{
var fi = new System.IO.FileInfo(filename);
var di = fi.Directory;
var di = fi.Attributes.HasFlag(System.IO.FileAttributes.Directory) ? new System.IO.DirectoryInfo(filename) : fi.Directory;

var loadCell = true;
DatManager.Initialize(di.FullName, true, loadCell);
Expand All @@ -144,6 +163,7 @@ public void ReadDATFile(string filename)
private void Options_Click(object sender, RoutedEventArgs e)
{
Options = new Options();
Options.WindowStartupLocation = WindowStartupLocation.CenterScreen;
Options.ShowDialog();
}

Expand Down Expand Up @@ -206,6 +226,7 @@ private void ShowLocation_Click(object sender, RoutedEventArgs e)
private void About_Click(object sender, RoutedEventArgs e)
{
var about = new About();
about.WindowStartupLocation = WindowStartupLocation.CenterScreen;
about.ShowDialog();
}

Expand Down
16 changes: 16 additions & 0 deletions ACViewer/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Windows;
using System.Windows.Input;

using ACViewer.Config;

namespace ACViewer.View
{
/// <summary>
Expand All @@ -20,6 +22,20 @@ public MainWindow()
DataContext = this;

//WpfGame.UseASingleSharedGraphicsDevice = true;

LoadConfig();
}

private static Config.Config Config => ConfigManager.Config;

private static void LoadConfig()
{
ConfigManager.LoadConfig();

if (Config.AutomaticallyLoadDATsOnStartup)
{
MainMenu.Instance.LoadDATs(Config.ACFolder);
}
}

private DateTime lastUpdateTime { get; set; }
Expand Down
12 changes: 9 additions & 3 deletions ACViewer/View/Options.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ACViewer.View"
mc:Ignorable="d"
Title="Options" Height="280" Width="380">
<Grid>

Title="Options" Height="156" Width="600" Icon="../Icons/Settings_16x.png">
<Grid Margin="0,0,-8,0">
<Label Content="AC Folder:" HorizontalAlignment="Left" Margin="17,11,0,0" VerticalAlignment="Top"/>
<TextBox Text="{Binding Path=ACFolder}" HorizontalAlignment="Left" Height="23" Margin="86,15,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370"/>
<Button Content="Select Folder" HorizontalAlignment="Left" Margin="461,15,0,0" VerticalAlignment="Top" Width="94" Height="23" Click="SelectACFolderButton_Click"/>
<CheckBox Content="Automatically Load DATs on Startup" IsChecked="{Binding Path=AutomaticallyLoadDATsOnStartup}" HorizontalAlignment="Left" Margin="32,47,0,0" VerticalAlignment="Top"/>
<Button Content="OK" HorizontalAlignment="Left" Margin="395,83,0,0" VerticalAlignment="Top" Width="75" Click="OKButton_Click"/>
<Button Content="Cancel" HorizontalAlignment="Left" Margin="480,83,0,0" VerticalAlignment="Top" Width="75" Click="CancelButton_Click"/>

</Grid>
</Window>
Loading

0 comments on commit f115672

Please sign in to comment.