-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
executable file
·177 lines (152 loc) · 5.78 KB
/
Form1.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Data.SQLite;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace PowerYangyang
{
public partial class Form1 : Form
{
private readonly int[] fpsArray = new int[] { 30, 60, 90, 120 };
private string connStr = "";
private JsonNode? gameQualitySettingNode;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Display version
var ver = Assembly.GetEntryAssembly()?.GetName().Version;
this.Text = $"{this.Text} v{ver?.Major}.{ver?.Minor}.{ver?.Build}";
if (Properties.Settings.Default.InstallPath == "")
{
var installPathDialog = new InstallPathDialog();
var result = installPathDialog.ShowDialog();
if (result != DialogResult.OK)
{
this.Close();
}
}
if (File.Exists($@"{Properties.Settings.Default.InstallPath}\Client\Saved\LocalStorage\LocalStorage.db") == false)
{
var result = MessageBox.Show(
"LocalStorage.db is not found.\nIf you have never launch the game, please launch the game first and retry.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
if (result != null)
{
this.Close();
}
}
var connStrBuilder = new SQLiteConnectionStringBuilder()
{
DataSource = $@"{Properties.Settings.Default.InstallPath}\Client\Saved\LocalStorage\LocalStorage.db"
};
connStr = connStrBuilder.ToString();
// Read current value
GetGameQualitySettingNode();
var currentFps = GetFps();
if (currentFps == -1)
{
var result = MessageBox.Show(
"Failed to get current fps from db.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
if (result != null)
{
this.Close();
}
}
else
{
var currentIndex = Array.IndexOf(fpsArray, currentFps);
FPSTrackBar.Value = currentIndex != -1 ? currentIndex : 1;
WriteLog($"Successfully initialized, current FPS is {currentFps}");
}
}
private void FPSTrackBar_MouseCaptureChanged(object sender, EventArgs e)
{
gameQualitySettingNode!["KeyCustomFrameRate"] = fpsArray[FPSTrackBar.Value];
var result = WriteFameQualitySettings();
if (result == true)
WriteLog($"Set FPS to {fpsArray[FPSTrackBar.Value]}.");
else
WriteLog("Failed to change FPS value.");
}
private void GetGameQualitySettingNode()
{
string settingsJson = "";
using (var conn = new SQLiteConnection(connStr))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from LocalStorage where key = @key";
cmd.Parameters.Add(new SQLiteParameter("@key", "GameQualitySetting"));
var result = cmd.ExecuteNonQuery();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
settingsJson = reader["value"].ToString() ?? "";
}
reader.Close();
}
}
conn.Close();
}
gameQualitySettingNode = JsonNode.Parse(settingsJson);
}
private int GetFps()
{
bool parsed = Int32.TryParse(gameQualitySettingNode?["KeyCustomFrameRate"]?.ToString(), out int fpsInt);
return parsed ? fpsInt : -1;
}
private bool WriteFameQualitySettings()
{
var result = false;
using (var conn = new SQLiteConnection(connStr))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
var tran = conn.BeginTransaction();
cmd.CommandText = "update LocalStorage set value = @value where key = @key";
cmd.Parameters.Add(new SQLiteParameter("@value", JsonSerializer.Serialize(gameQualitySettingNode)));
cmd.Parameters.Add(new SQLiteParameter("@key", "GameQualitySetting"));
if (cmd.ExecuteNonQuery() != 1)
{
tran.Rollback();
}
else
{
tran.Commit();
result = true;
}
}
conn.Close();
}
return result;
}
private void WriteLog(string message)
{
if (!string.IsNullOrWhiteSpace(LogTextBox.Text))
{
LogTextBox.AppendText("\r\n" + message);
}
else
{
LogTextBox.AppendText(message);
}
LogTextBox.ScrollToCaret();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}