Skip to content

Commit d6377cc

Browse files
committed
chore: remove unused assemblies, standardizes code
1 parent 0768a6b commit d6377cc

15 files changed

+374
-419
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Sinden Companion
22

33
## TODO
4-
- Add menu item to disable window monitoring?
5-
- Remove unused references
4+
- Support varying bytes for injection
65

76
## Getting started
87
- Download the most recent release from the release page

SindenCompanion/AppForm.cs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
11
using System;
2-
using System.Windows.Controls;
2+
using System.Diagnostics;
3+
using System.IO;
34
using System.Windows;
5+
using System.Windows.Controls;
46
using System.Windows.Forms;
57
using System.Windows.Forms.Integration;
6-
using System.Diagnostics;
7-
using System.IO;
8+
using System.Windows.Media;
89
using SindenCompanionShared;
10+
using Application = System.Windows.Forms.Application;
11+
using RichTextBox = System.Windows.Controls.RichTextBox;
912

1013
namespace SindenCompanion
1114
{
1215
public partial class AppForm : Form
1316
{
14-
public readonly System.Windows.Controls.RichTextBox WpfRichTextBox;
15-
16-
private bool _userRequestedClose = false;
17-
1817
private readonly Config _conf;
18+
public readonly RichTextBox WpfRichTextBox;
1919

2020
private Action<RecoilProfile> _callback;
21+
22+
private bool _userRequestedClose;
23+
2124
public AppForm(Config conf)
2225
{
2326
_conf = conf;
2427
InitializeComponent();
2528
var richTextBoxHost = new ElementHost
2629
{
27-
Dock = DockStyle.Fill,
30+
Dock = DockStyle.Fill
2831
};
2932

3033
_richTextBoxPanel.Controls.Add(richTextBoxHost);
3134

32-
var wpfRichTextBox = new System.Windows.Controls.RichTextBox
35+
var wpfRichTextBox = new RichTextBox
3336
{
34-
Background = System.Windows.Media.Brushes.Black,
35-
Foreground = System.Windows.Media.Brushes.LightGray,
36-
FontFamily = new System.Windows.Media.FontFamily("Cascadia Mono, Consolas, Courier New, monospace"),
37+
Background = Brushes.Black,
38+
Foreground = Brushes.LightGray,
39+
FontFamily = new FontFamily("Cascadia Mono, Consolas, Courier New, monospace"),
3740
FontSize = 14,
3841
IsReadOnly = true,
3942
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
4043
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
41-
Margin = new Thickness(0),
44+
Margin = new Thickness(0)
4245
};
4346

4447
wpfRichTextBox.TextChanged += wpfRichTextBox_TextChanged;
@@ -54,7 +57,6 @@ public void SetCallback(Action<RecoilProfile> callback)
5457

5558
private void AppForm_Load(object sender, EventArgs e)
5659
{
57-
5860
}
5961

6062
private void wpfRichTextBox_TextChanged(object sender, EventArgs e)
@@ -73,22 +75,22 @@ private void AppForm_FormClosing(object sender, FormClosingEventArgs e)
7375

7476
private void notificationIcon_MouseDoubleClick(object sender, MouseEventArgs e)
7577
{
76-
this.WindowState = FormWindowState.Minimized;
78+
WindowState = FormWindowState.Minimized;
7779
Show();
78-
this.WindowState = FormWindowState.Normal;
80+
WindowState = FormWindowState.Normal;
7981
}
8082

8183
private void showMenuItem_Click(object sender, EventArgs e)
8284
{
83-
this.WindowState = FormWindowState.Minimized;
85+
WindowState = FormWindowState.Minimized;
8486
Show();
85-
this.WindowState = FormWindowState.Normal;
87+
WindowState = FormWindowState.Normal;
8688
}
8789

8890
private void exitMenuItem_Click(object sender, EventArgs e)
8991
{
9092
_userRequestedClose = true;
91-
System.Windows.Forms.Application.Exit();
93+
Application.Exit();
9294
}
9395

9496
private void configFileMenuItem_Click(object sender, EventArgs e)
@@ -99,13 +101,9 @@ private void configFileMenuItem_Click(object sender, EventArgs e)
99101
private void bootMenuItem_Click(object sender, EventArgs e)
100102
{
101103
if (Startup.IsInStartup())
102-
{
103104
Startup.RemoveFromStartup();
104-
}
105105
else
106-
{
107106
Startup.RunOnStartup();
108-
}
109107

110108
bootMenuItem.Checked = Startup.IsInStartup();
111109
}
@@ -117,22 +115,18 @@ private void NotificationIconMenu_Opened(object sender, EventArgs e)
117115
foreach (var profile in _conf.RecoilProfiles)
118116
{
119117
var item = new ToolStripMenuItem(profile.Name);
120-
item.Click += (s, a) =>
121-
{
122-
_callback(profile);
123-
};
118+
item.Click += (s, a) => { _callback(profile); };
124119
changeProfileMenuItem.DropDownItems.Add(item);
125-
}
120+
}
126121
}
127122

128123
private void AppForm_Resize(object sender, EventArgs e)
129124
{
130-
if (this.WindowState == FormWindowState.Minimized)
125+
if (WindowState == FormWindowState.Minimized)
131126
{
132127
Hide();
133128
NotificationIcon.Visible = true;
134129
}
135130
}
136-
137131
}
138-
}
132+
}

SindenCompanion/Config.cs

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,77 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
4+
using System.Reflection;
35
using System.Text;
46
using SindenCompanionShared;
5-
using YamlDotNet.Serialization.NamingConventions;
67
using YamlDotNet.Serialization;
7-
using System;
8+
using YamlDotNet.Serialization.NamingConventions;
89

910
namespace SindenCompanion
1011

11-
{ public class Config
12+
{
13+
public class Config
1214
{
13-
private static Config _instance = null;
14-
private static FileSystemWatcher _fw = null;
15+
private static Config _instance;
16+
private static FileSystemWatcher _fw;
17+
18+
public List<RecoilProfile> RecoilProfiles { get; set; } = new List<RecoilProfile>();
19+
public List<GameProfile> GameProfiles { get; set; } = new List<GameProfile>();
20+
21+
public Global Global { get; set; }
22+
1523
public static Config GetInstance()
1624
{
17-
if (_instance != null) {
18-
return _instance;
19-
}
20-
if (_fw == null) {
25+
if (_instance != null) return _instance;
26+
if (_fw == null)
27+
{
2128
_fw = new FileSystemWatcher();
22-
_fw.Path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
29+
_fw.Path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
2330
_fw.Filter = "config.yaml";
24-
_fw.Changed += new FileSystemEventHandler((e, a) => {
31+
_fw.Changed += new FileSystemEventHandler((e, a) =>
32+
{
2533
Console.WriteLine("Detected change to config file.");
26-
_instance = null;
34+
_instance = null;
2735
});
2836
_fw.EnableRaisingEvents = true;
2937
}
3038

3139
Console.WriteLine("Loading configuration.");
32-
using (StreamReader streamReader = new StreamReader(".\\config.yaml", Encoding.UTF8))
40+
using (var streamReader = new StreamReader(".\\config.yaml", Encoding.UTF8))
3341
{
3442
try
3543
{
3644
var deserializer = new DeserializerBuilder()
37-
.WithNamingConvention(UnderscoredNamingConvention.Instance)
38-
.Build();
45+
.WithNamingConvention(UnderscoredNamingConvention.Instance)
46+
.Build();
3947
_instance = deserializer.Deserialize<Config>(streamReader.ReadToEnd());
40-
} catch (Exception ex)
48+
}
49+
catch (Exception ex)
4150
{
4251
Console.WriteLine($"Failed to instantiate configuration: {ex}");
4352
throw ex;
4453
}
4554
}
55+
4656
return _instance;
4757
}
58+
}
4859

49-
public List<RecoilProfile> RecoilProfiles { get; set; }
50-
public List<GameProfile> GameProfiles { get; set; }
5160

52-
public Global Global { get; set; }
53-
public Config()
61+
public class Global
62+
{
63+
public Global()
5464
{
55-
RecoilProfiles = new List<RecoilProfile>();
56-
GameProfiles = new List<GameProfile>();
65+
IpcPort = 5557;
5766
}
58-
}
5967

60-
public class Global
61-
{
6268
public bool RecoilOnSwitch { get; set; }
6369
public int IpcPort { get; set; }
64-
public string Lightgun { get; set; }
70+
public string Lightgun { get; set; }
6571

6672
public bool Debug { get; set; }
67-
68-
public Global()
69-
{
70-
IpcPort = 5557;
71-
}
7273
}
74+
7375
public class GameProfile
7476
{
7577
public string Name { get; set; }
@@ -80,27 +82,18 @@ public class GameProfile
8082

8183
public MemScan Memscan { get; set; }
8284

83-
public bool Matches (ForegroundProcess fp)
85+
public bool Matches(ForegroundProcess fp)
8486
{
8587
// If both exe and title are set, AND them
8688
if (Match.Exe != null && Match.Title != null)
8789
{
88-
if ($"{fp.ProcessName}.exe" == Match.Exe && Match.Title == fp.WindowTitle)
89-
{
90-
return true;
91-
}
90+
if ($"{fp.ProcessName}.exe" == Match.Exe && Match.Title == fp.WindowTitle) return true;
9291
return false;
9392
}
94-
95-
if ($"{fp.ProcessName}.exe" == Match.Exe)
96-
{
97-
return true;
98-
}
9993

100-
if (Match.Title == fp.WindowTitle)
101-
{
102-
return true;
103-
}
94+
if ($"{fp.ProcessName}.exe" == Match.Exe) return true;
95+
96+
if (Match.Title == fp.WindowTitle) return true;
10497

10598
return false;
10699
}
@@ -119,4 +112,4 @@ public class MemScan
119112

120113
public Dictionary<int, string> Match { get; set; }
121114
}
122-
}
115+
}
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
4-
using System.Linq;
53
using System.Runtime.InteropServices;
64
using System.Text;
7-
using System.Threading.Tasks;
85

96
namespace SindenCompanion
107
{
118
public class ForegroundProcess
129
{
13-
[DllImport("user32.dll")]
14-
static extern IntPtr GetForegroundWindow();
15-
16-
[DllImport("user32.dll")]
17-
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
18-
19-
[DllImport("user32.dll", SetLastError = true)]
20-
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
10+
public readonly uint ProcessId;
11+
public readonly string ProcessName;
2112

2213
public readonly string WindowTitle;
23-
public readonly uint ProcessId = 0;
24-
public readonly string ProcessName;
2514

2615
public ForegroundProcess()
2716
{
2817
const int nChars = 256;
29-
StringBuilder Buff = new StringBuilder(nChars);
30-
IntPtr handle = GetForegroundWindow();
18+
var buff = new StringBuilder(nChars);
19+
var handle = GetForegroundWindow();
3120

32-
if (GetWindowText(handle, Buff, nChars) > 0)
33-
{
34-
WindowTitle = Buff.ToString();
35-
}
21+
if (GetWindowText(handle, buff, nChars) > 0) WindowTitle = buff.ToString();
3622

3723
GetWindowThreadProcessId(handle, out ProcessId);
38-
Process process = Process.GetProcessById((int)ProcessId);
24+
var process = Process.GetProcessById((int)ProcessId);
3925
ProcessName = process.ProcessName;
4026
}
41-
}
4227

28+
[DllImport("user32.dll")]
29+
private static extern IntPtr GetForegroundWindow();
30+
31+
[DllImport("user32.dll")]
32+
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
4333

44-
}
34+
[DllImport("user32.dll", SetLastError = true)]
35+
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
36+
}
37+
}

SindenCompanion/Logger.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
using Serilog;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using System.Windows.Controls;
2+
using Serilog;
73

84
namespace SindenCompanionShared
95
{
106
public partial class Logger
117
{
12-
public static ILogger CreateDesktopLogger(bool debug, System.Windows.Controls.RichTextBox rtb)
8+
public static ILogger CreateDesktopLogger(bool debug, RichTextBox rtb)
139
{
14-
LoggerConfiguration config = new LoggerConfiguration().WriteTo.RichTextBox(rtb);
10+
var config = new LoggerConfiguration().WriteTo.RichTextBox(rtb);
1511

16-
if (debug)
17-
{
18-
config = config.MinimumLevel.Debug();
19-
}
12+
if (debug) config = config.MinimumLevel.Debug();
2013

2114
Log.Logger = config.CreateLogger();
2215

2316
return Log.Logger;
2417
}
2518
}
26-
}
19+
}

0 commit comments

Comments
 (0)