-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.cs
120 lines (104 loc) · 3.77 KB
/
Helper.cs
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
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using Newtonsoft.Json.Linq;
namespace osu_launcher
{
internal class Helper
{
public static void SetControlText(Control control, string text, string defaultText = "")
{
control.Text = text ?? defaultText;
}
public static void SetControlChecked(CheckBox checkBox, bool? isChecked)
{
checkBox.Checked = isChecked ?? false;
}
public static void SetControlValue(TrackBar trackBar, int value)
{
trackBar.Value = value;
}
public static void SetControlSelectedIndex(ListControl comboBox, int selectedIndex)
{
comboBox.SelectedIndex = selectedIndex;
}
public static bool InitializeCefSharp()
{
var settings = new CefSettings
{
RootCachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")
};
return Cef.Initialize(settings);
}
public static void ValidateRequiredFiles()
{
if (!File.Exists("./src/Fonts/Quicksand-Light.ttf") || !File.Exists("./src/Fonts/NotoSansJP-Light.ttf"))
{
ShowErrorMessage("The font file was not found. Download this software again.");
Environment.Exit(1);
}
if (!File.Exists("./src/data.json"))
{
ShowErrorMessage("The data file was not found. Download this software again.");
Environment.Exit(1);
}
}
public static void ShowErrorMessage(string message)
{
MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static bool ArrayContains(IEnumerable<string> array, string value) => array.Any(item => item == value);
public static void ChangeConfigValue(string osuFolder, Dictionary<string, string> param)
{
try
{
string username = Environment.UserName;
string path = Path.Combine(osuFolder, $"osu!.{username}.cfg");
string[] lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
string key = lines[i].Split('=')[0].Trim();
for (int j = 0; j < param.Count; j++)
{
if (key != param.ElementAt(j).Key) continue;
lines[i] = $"{param.ElementAt(j).Key} = {param.ElementAt(j).Value}";
break;
}
}
File.WriteAllLines(path, lines);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public static void AddValueToArray<T>(ref IEnumerable<T> array, T value)
{
array = array.Append(value).ToArray();
}
public static void AddParameterIfNotEmpty(IDictionary<string, string> parameters, string key, string value)
{
if (!string.IsNullOrEmpty(value))
{
parameters.Add(key, value);
}
}
public static bool IsEnglish(string text)
{
return text.All(c => c < 128);
}
public static void InitializeCombobox(ComboBox comboBox, JToken items)
{
if (items == null) items = new JArray();
foreach (var item in items)
{
comboBox.Items.Add(item);
}
if (comboBox.Items.Count > 0) comboBox.SelectedIndex = 0;
}
}
}