-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddGameWindow.cs
More file actions
110 lines (95 loc) · 4.64 KB
/
Copy pathAddGameWindow.cs
File metadata and controls
110 lines (95 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Windows;
using System.IO;
using System.Linq;
using Microsoft.Win32;
namespace RomHub
{
public partial class AddGameWindow : Window
{
public AddGameWindow()
{
InitializeComponent();
}
private void BrowseRom_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog { Title = "Select ROM File", Filter = "ROM Files|*.*" };
if (dialog.ShowDialog() == true)
{
RomBox.Text = dialog.FileName;
if (string.IsNullOrWhiteSpace(TitleBox.Text))
TitleBox.Text = Path.GetFileNameWithoutExtension(dialog.FileName);
}
}
private void BrowseEmu_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog { Title = "Select Emulator", Filter = "Executable Files (*.exe)|*.exe|All Files|*.*" };
if (dialog.ShowDialog() == true) EmuBox.Text = dialog.FileName;
}
private void BrowseIcon_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog { Title = "Select Custom Cover Image", Filter = "Image Files|*.png;*.jpg;*.jpeg;*.bmp" };
if (dialog.ShowDialog() == true) IconBox.Text = dialog.FileName;
}
private void TextBox_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effects = DragDropEffects.Copy;
e.Handled = true;
}
private void RomBox_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetData(DataFormats.FileDrop) is string[] files && files.Length > 0)
{
RomBox.Text = files[0];
if (string.IsNullOrWhiteSpace(TitleBox.Text))
TitleBox.Text = Path.GetFileNameWithoutExtension(files[0]);
}
}
private void EmuBox_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetData(DataFormats.FileDrop) is string[] files && files.Length > 0) EmuBox.Text = files[0];
}
private void IconBox_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetData(DataFormats.FileDrop) is string[] files && files.Length > 0) IconBox.Text = files[0];
}
private void Cancel_Click(object sender, RoutedEventArgs e) => Close();
private void Add_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(TitleBox.Text) || string.IsNullOrWhiteSpace(RomBox.Text) || string.IsNullOrWhiteSpace(EmuBox.Text))
{
MessageBox.Show("Please fill out all required fields.", "Missing Information", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (!File.Exists(RomBox.Text) || !File.Exists(EmuBox.Text))
{
MessageBox.Show("The specified ROM file or Emulator executable does not exist.", "File Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
string platform = PlatformDetector.GetPlatform(RomBox.Text);
string romExtension = Path.GetExtension(RomBox.Text).ToLower();
string[] validRomExtensions = PlatformDetector.GetSupportedExtensions();
if (!validRomExtensions.Contains(romExtension))
{
var result = MessageBox.Show($"The extension '{romExtension}' is unrecognized. Add anyway?", "Format Verification", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.No) return;
}
string definitiveIconPath = IconBox.Text;
if (!string.IsNullOrWhiteSpace(definitiveIconPath) && File.Exists(definitiveIconPath))
{
try
{
string localDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UserCovers");
Directory.CreateDirectory(localDirectory);
string targetFile = Path.Combine(localDirectory, Guid.NewGuid().ToString() + Path.GetExtension(definitiveIconPath));
File.Copy(definitiveIconPath, targetFile, true);
definitiveIconPath = targetFile;
}
catch { }
}
new GameRepository().AddGame(TitleBox.Text, RomBox.Text, EmuBox.Text, definitiveIconPath, platform);
MessageBox.Show("Game successfully added!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
Close();
}
}
}