-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua-replacer.cs
More file actions
2288 lines (2128 loc) · 109 KB
/
lua-replacer.cs
File metadata and controls
2288 lines (2128 loc) · 109 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Threading.Tasks;
using System.Text.Json;
using System.Runtime.InteropServices;
using System.Net;
namespace StormworksLuaReplacer
{
public class ApplicationState
{
public bool IsReloading { get; set; }
public string ScriptDetectionPrefix { get; set; } = "-- autochanger";
public Point MouseLocation { get; set; }
public bool IsDarkTheme { get; set; } = true;
public bool SuppressPrefixPrompt { get; set; } = false;
public string SettingsFilePath { get; set; } = string.Empty;
public List<string> RecentFiles { get; set; } = new List<string>();
// mapping key: "<vehicleXmlFullPath>|<scriptIndex>" -> lua file path
public Dictionary<string, string> ScriptFileMappings { get; set; } = new Dictionary<string, string>();
}
public class PrefixConfirmDialog : Form
{
private CheckBox chkDontShow;
public bool DontShowAgain => chkDontShow?.Checked ?? false;
public PrefixConfirmDialog(List<string> displayNames)
{
this.Text = "プレフィックスを追加します";
this.Size = new System.Drawing.Size(520, 320);
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
var lbl = new Label { Text = "以下の置換されたスクリプトに検出プレフィックスを追加します。続行しますか?", Dock = DockStyle.Top, Height = 36, Padding = new Padding(8) };
var txt = new TextBox { Multiline = true, ReadOnly = true, ScrollBars = ScrollBars.Vertical, Dock = DockStyle.Fill, Font = new System.Drawing.Font("Consolas", 9) };
txt.Text = string.Join(Environment.NewLine, displayNames);
chkDontShow = new CheckBox { Text = "今後表示しない", Dock = DockStyle.Bottom, Height = 24, Padding = new Padding(6, 4, 0, 4) };
var btnOk = new Button { Text = "はい", DialogResult = DialogResult.OK, Width = 90, Height = 30 };
var btnCancel = new Button { Text = "いいえ", DialogResult = DialogResult.Cancel, Width = 90, Height = 30 };
var pnlButtons = new FlowLayoutPanel { FlowDirection = FlowDirection.RightToLeft, Dock = DockStyle.Bottom, Height = 44, Padding = new Padding(6) };
pnlButtons.Controls.Add(btnCancel);
pnlButtons.Controls.Add(btnOk);
this.Controls.Add(txt);
this.Controls.Add(lbl);
this.Controls.Add(chkDontShow);
this.Controls.Add(pnlButtons);
this.AcceptButton = btnOk;
this.CancelButton = btnCancel;
}
}
public partial class MainForm : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
private Point resizeStart;
private Rectangle resizeStartBounds;
private bool isResizing = false;
private int resizeMode = 0;
private XDocument? vehicleXml;
private string? currentFilePath;
private string? currentLuaFilePath;
private readonly List<LuaScriptNode> luaScripts = new List<LuaScriptNode>();
private readonly FileSystemWatcher fileWatcher;
private readonly FileSystemWatcher luaFileWatcher;
private readonly ApplicationState appState = new ApplicationState();
private bool suppressMessages = false;
private bool isHttpRequest = false;
private HttpListener? httpListener;
private Label? lblFilePath;
private ModernDropdown? cbRecentFiles;
private ModernDropdown? cbScriptFiles;
private ListBox? lstScripts;
private FlowLayoutPanel? topControlsPanel;
private System.Windows.Forms.RichTextBox? txtCurrentScript;
private System.Windows.Forms.RichTextBox? txtNewScript;
private System.Windows.Forms.Timer? highlightTimer;
private System.Windows.Forms.RichTextBox? highlightTarget;
private Panel? pnlTitleBar;
private Panel? titleRightSpacer;
private Panel? pnlCurrentBorder;
private Panel? pnlNewBorder;
private System.Windows.Forms.VScrollBar? vScrollList;
private System.Windows.Forms.VScrollBar? vScrollCurrent;
private System.Windows.Forms.VScrollBar? vScrollNew;
// status strip removed to avoid interfering with window resizing
private Color accentColor = Color.FromArgb(0, 122, 204);
private const int SB_VERT = 1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ReleaseCapture();
private const int WM_VSCROLL = 0x0115;
private const int SB_PAGEUP = 2;
private const int SB_PAGEDOWN = 3;
private const int SB_BOTTOM = 7;
private const int WM_NCHITTEST = 0x0084;
private const int WM_MOUSEWHEEL = 0x020A;
private const int HTCLIENT = 1;
private const int HTCAPTION = 2;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int RESIZE_BORDER = 8;
private System.Windows.Forms.Timer? reloadTimer;
private System.Windows.Forms.Timer? luaReloadTimer;
// Global keyboard hook
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private const int WM_SYSKEYDOWN = 0x0104;
private const int WM_SYSKEYUP = 0x0105;
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private IntPtr _hookID = IntPtr.Zero;
private LowLevelKeyboardProc _proc;
public MainForm()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += MainForm_KeyDown;
// Install global keyboard hook
_proc = HookCallback;
_hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, GetModuleHandle(null), 0);
LoadSettings();
// Populate recent-files dropdown after settings are loaded
UpdateRecentCombo();
// Apply theme from loaded settings (call again to ensure colors update)
ApplyTheme();
this.DoubleBuffered = true;
this.MinimumSize = new Size(400, 300);
AttachMouseHandlers(this);
fileWatcher = new FileSystemWatcher { NotifyFilter = NotifyFilters.LastWrite };
fileWatcher.SynchronizingObject = this;
fileWatcher.Changed += FileWatcher_Changed;
fileWatcher.Created += FileWatcher_Changed;
fileWatcher.Deleted += FileWatcher_Changed;
fileWatcher.Renamed += FileWatcher_Changed;
luaFileWatcher = new FileSystemWatcher { NotifyFilter = NotifyFilters.LastWrite };
luaFileWatcher.SynchronizingObject = this;
luaFileWatcher.Changed += LuaFileWatcher_Changed;
luaFileWatcher.Created += LuaFileWatcher_Changed;
luaFileWatcher.Deleted += LuaFileWatcher_Changed;
luaFileWatcher.Renamed += LuaFileWatcher_Changed;
// Start HTTP server
Task.Run(() => StartHttpServer());
}
private async Task StartHttpServer()
{
httpListener = new HttpListener();
httpListener.Prefixes.Add("http://127.0.0.1:2345/");
httpListener.Start();
while (true)
{
HttpListenerContext context;
try
{
context = await httpListener.GetContextAsync();
}
catch (HttpListenerException)
{
// Listener was stopped or suffered an error - exit loop
break;
}
catch (ObjectDisposedException)
{
// Listener disposed - exit loop
break;
}
catch (InvalidOperationException)
{
// Listener not in a valid state - exit loop
break;
}
var request = context.Request;
var response = context.Response;
if (request.Url.AbsolutePath == "/replace" && request.HttpMethod == "GET")
{
string? errorMessage = null;
// UI スレッド上での処理を非同期に実行し、その完了を待つ。
var asyncResult = this.BeginInvoke(new Action(() =>
{
try
{
isHttpRequest = true;
if (vehicleXml == null || lstScripts!.SelectedIndex < 0)
{
errorMessage = "XML file is not loaded or no script is selected.";
return;
}
suppressMessages = true;
BtnReplace_Click(null, EventArgs.Empty);
BtnSaveSync();
suppressMessages = false;
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
finally
{
isHttpRequest = false;
}
}));
try
{
// UI スレッド側の処理が終わるまで待機(HTTP スレッドをブロックするが、UI はブロックされない)
this.EndInvoke(asyncResult);
}
catch (Exception ex)
{
// EndInvoke による例外はここで拾っておく
if (string.IsNullOrEmpty(errorMessage)) errorMessage = ex.Message;
}
string responseString;
if (string.IsNullOrEmpty(errorMessage))
{
// 成功: Lua 側の判定が期待するパターンを含める
var payload = new { status = "success", message = "Files updated successfully." };
responseString = System.Text.Json.JsonSerializer.Serialize(payload);
}
else
{
// 失敗: success パターンを含めないようにする
var reason = TranslateErrorToEnglish(errorMessage);
var payload = new { status = "error", reason = reason };
responseString = System.Text.Json.JsonSerializer.Serialize(payload);
}
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentType = "text/plain; charset=utf-8";
response.ContentLength64 = buffer.Length;
await response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
}
else
{
response.StatusCode = 404;
}
try
{
response.OutputStream.Close();
}
catch { }
}
}
private async Task BtnSaveAsync()
{
if (vehicleXml == null || string.IsNullOrEmpty(currentFilePath)) { throw new Exception("XMLファイルが読み込まれていません。"); }
await SaveXmlFileAsync(currentFilePath);
}
private void BtnSaveSync()
{
if (vehicleXml == null || string.IsNullOrEmpty(currentFilePath)) { throw new Exception("XMLファイルが読み込まれていません。"); }
SaveXmlFileAsync(currentFilePath).Wait();
}
private void AttachMouseHandlers(Control parent)
{
foreach (Control c in parent.Controls)
{
c.MouseDown += ChildControl_MouseDown;
c.MouseMove += ChildControl_MouseMove;
c.MouseUp += ChildControl_MouseUp;
if (c.HasChildren) AttachMouseHandlers(c);
}
}
private void ChildControl_MouseDown(object? sender, MouseEventArgs e){
var ctrl = sender as Control;
if (ctrl == null) return;
var screenPt = ctrl.PointToScreen(e.Location);
var formPt = this.PointToClient(screenPt);
int mode = GetResizeMode(formPt);
if (mode != HTCLIENT)
{
isResizing = true;
resizeMode = mode;
resizeStart = screenPt;
resizeStartBounds = this.Bounds;
ctrl.Capture = true;
}
}
private void ChildControl_MouseMove(object? sender, MouseEventArgs e)
{
var ctrl = sender as Control;
if (ctrl == null) return;
var screenPt = ctrl.PointToScreen(e.Location);
if (isResizing)
{
ResizeWindow(screenPt);
return;
}
var formPt = this.PointToClient(screenPt);
int mode = GetResizeMode(formPt);
UpdateCursor(mode);
}
private void ChildControl_MouseUp(object? sender, MouseEventArgs e)
{
if (isResizing)
{
isResizing = false;
resizeMode = HTCLIENT;
this.Cursor = Cursors.Default;
var ctrl = sender as Control;
if (ctrl != null) ctrl.Capture = false;
}
}
private void ResizeWindow(Point currentScreenLocation)
{
if (!isResizing) return;
int deltaX = currentScreenLocation.X - resizeStart.X;
int deltaY = currentScreenLocation.Y - resizeStart.Y;
const int MIN_WIDTH = 400;
const int MIN_HEIGHT = 300;
int newLeft = resizeStartBounds.Left;
int newTop = resizeStartBounds.Top;
int newWidth = resizeStartBounds.Width;
int newHeight = resizeStartBounds.Height;
bool isLeft = (resizeMode == HTLEFT || resizeMode == HTTOPLEFT || resizeMode == HTBOTTOMLEFT);
bool isRight = (resizeMode == HTRIGHT || resizeMode == HTTOPRIGHT || resizeMode == HTBOTTOMRIGHT);
bool isTop = (resizeMode == HTTOP || resizeMode == HTTOPLEFT || resizeMode == HTTOPRIGHT);
bool isBottom = (resizeMode == HTBOTTOM || resizeMode == HTBOTTOMLEFT || resizeMode == HTBOTTOMRIGHT);
if (isLeft)
{
int proposedWidth = resizeStartBounds.Width - deltaX;
if (proposedWidth < MIN_WIDTH) proposedWidth = MIN_WIDTH;
newWidth = proposedWidth;
newLeft = (resizeStartBounds.Left + resizeStartBounds.Width) - newWidth;
}
else if (isRight)
{
newWidth = resizeStartBounds.Width + deltaX;
if (newWidth < MIN_WIDTH) newWidth = MIN_WIDTH;
}
if (isTop)
{
int proposedHeight = resizeStartBounds.Height - deltaY;
if (proposedHeight < MIN_HEIGHT) proposedHeight = MIN_HEIGHT;
newHeight = proposedHeight;
newTop = (resizeStartBounds.Top + resizeStartBounds.Height) - newHeight;
}
else if (isBottom)
{
newHeight = resizeStartBounds.Height + deltaY;
if (newHeight < MIN_HEIGHT) newHeight = MIN_HEIGHT;
}
this.Bounds = new Rectangle(newLeft, newTop, newWidth, newHeight);
}
private int GetResizeMode(Point clientPoint)
{
bool left = clientPoint.X <= RESIZE_BORDER;
bool right = clientPoint.X >= this.ClientSize.Width - RESIZE_BORDER;
bool top = clientPoint.Y <= RESIZE_BORDER;
bool bottom = clientPoint.Y >= this.ClientSize.Height - RESIZE_BORDER;
if (left && top) return HTTOPLEFT;
if (right && top) return HTTOPRIGHT;
if (left && bottom) return HTBOTTOMLEFT;
if (right && bottom) return HTBOTTOMRIGHT;
if (left) return HTLEFT;
if (right) return HTRIGHT;
if (top) return HTTOP;
if (bottom) return HTBOTTOM;
return HTCLIENT;
}
private void UpdateCursor(int mode)
{
// If window is maximized, do not show resize cursors
if (this.WindowState == FormWindowState.Maximized)
{
this.Cursor = Cursors.Default;
return;
}
switch (mode)
{
case HTLEFT:
case HTRIGHT:
this.Cursor = Cursors.SizeWE;
break;
case HTTOP:
case HTBOTTOM:
this.Cursor = Cursors.SizeNS;
break;
case HTTOPLEFT:
case HTBOTTOMRIGHT:
this.Cursor = Cursors.SizeNWSE;
break;
case HTTOPRIGHT:
case HTBOTTOMLEFT:
this.Cursor = Cursors.SizeNESW;
break;
default:
this.Cursor = Cursors.Default;
break;
}
}
private void InitializeComponent()
{
lblFilePath = new Label { Text = "ファイル: 未選択", Dock = DockStyle.Fill, AutoSize = false, TextAlign = System.Drawing.ContentAlignment.MiddleLeft };
lstScripts = new ListBox { Dock = DockStyle.Fill, Height = 300 };
txtCurrentScript = new System.Windows.Forms.RichTextBox { Multiline = true, Dock = DockStyle.Fill, Font = new System.Drawing.Font("Consolas", 10), ReadOnly = true, BorderStyle = BorderStyle.None, WordWrap = false, ScrollBars = RichTextBoxScrollBars.Horizontal };
txtNewScript = new System.Windows.Forms.RichTextBox { Multiline = true, Dock = DockStyle.Fill, Font = new System.Drawing.Font("Consolas", 10), BorderStyle = BorderStyle.None, WordWrap = false, ScrollBars = RichTextBoxScrollBars.Horizontal };
this.FormBorderStyle = FormBorderStyle.None;
this.Text = "";
pnlTitleBar = new Panel
{
Dock = DockStyle.Top,
Height = 30,
BackColor = System.Drawing.Color.FromArgb(45, 45, 48)
};
var lblTitle = new Label { Text = "Stormworks Lua Script Replacer", ForeColor = System.Drawing.Color.White, Location = new System.Drawing.Point(10, 8) };
lblTitle.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.Font = new System.Drawing.Font("Segoe UI", 9F);
var btnMaximize = new Button { Text = "🗖", Dock = DockStyle.Right, Width = 45, FlatStyle = FlatStyle.Flat, ForeColor = System.Drawing.Color.White, BackColor = System.Drawing.Color.FromArgb(60, 45, 72) };
btnMaximize.FlatAppearance.BorderSize = 0;
btnMaximize.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(63, 63, 70);
var btnMinimize = new Button { Text = "—", Dock = DockStyle.Right, Width = 45, FlatStyle = FlatStyle.Flat, ForeColor = System.Drawing.Color.White, BackColor = System.Drawing.Color.FromArgb(60, 45, 72) };
var btnClose = new Button { Text = "✕", Dock = DockStyle.Right, Width = 45, FlatStyle = FlatStyle.Flat, ForeColor = System.Drawing.Color.White, BackColor = System.Drawing.Color.FromArgb(60, 45, 72) };
btnClose.FlatAppearance.BorderSize = 0;
btnClose.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(212, 63, 63);
btnMinimize.FlatAppearance.BorderSize = 0;
btnMinimize.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(63, 63, 70);
pnlTitleBar.Controls.Add(lblTitle);
pnlTitleBar.Controls.Add(btnMinimize);
pnlTitleBar.Controls.Add(btnMaximize);
pnlTitleBar.Controls.Add(btnClose);
// spacer to the right of titlebar buttons to create a clickable margin
titleRightSpacer = new Panel { Dock = DockStyle.Right, Width = 10, BackColor = Color.Transparent };
pnlTitleBar.Controls.Add(titleRightSpacer);
// hide spacer when window is maximized to avoid extra offset
titleRightSpacer.Visible = this.WindowState != FormWindowState.Maximized;
// Ensure maximized bounds use the working area (so the taskbar isn't covered)
this.Load += (s, e) =>
{
try { this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; }
catch { }
if (titleRightSpacer != null) titleRightSpacer.Visible = this.WindowState != FormWindowState.Maximized;
};
this.Resize += (s, e) =>
{
try { this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; }
catch { }
if (titleRightSpacer != null) titleRightSpacer.Visible = this.WindowState != FormWindowState.Maximized;
};
btnClose.Click += (s, e) => this.Close();
btnMinimize.Click += (s, e) => this.WindowState = FormWindowState.Minimized;
btnMaximize.Click += (s, e) =>
{
this.WindowState = this.WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
btnMaximize.Text = this.WindowState == FormWindowState.Maximized ? "🗗" : "🗖";
};
pnlTitleBar.MouseDown += (s, e) =>
{
var screenPt = pnlTitleBar.PointToScreen(e.Location);
var formPt = this.PointToClient(screenPt);
if (GetResizeMode(formPt) != HTCLIENT) return;
// Use native window drag to avoid losing focus when moving quickly
try
{
ReleaseCapture();
SendMessage(this.Handle, 0x00A1, (IntPtr)HTCAPTION, IntPtr.Zero);
}
catch
{
// Fallback to manual movement if native message fails
try { pnlTitleBar.Capture = false; } catch { }
appState.MouseLocation = e.Location;
}
};
pnlTitleBar.MouseMove += (s, e) =>
{
if (e.Button == MouseButtons.Left && appState.MouseLocation != Point.Empty)
{
var screenPt = pnlTitleBar.PointToScreen(e.Location);
var formPt = this.PointToClient(screenPt);
if (GetResizeMode(formPt) != HTCLIENT) return;
this.Left += e.X - appState.MouseLocation.X;
this.Top += e.Y - appState.MouseLocation.Y;
}
};
lblTitle.MouseDown += (s, e) =>
{
var screenPt = lblTitle.PointToScreen(e.Location);
var formPt = this.PointToClient(screenPt);
if (GetResizeMode(formPt) != HTCLIENT) return;
pnlTitleBar.Capture = false;
Message msg = Message.Create(pnlTitleBar.Handle, 0x00A1, (IntPtr)0x0002, IntPtr.Zero);
this.DefWndProc(ref msg);
};
var menuStrip = new MenuStrip();
var fileMenu = new ToolStripMenuItem("ファイル");
var openXmlItem = new ToolStripMenuItem("ビークルXMLを開く...", null, BtnLoadXml_Click);
var saveXmlItem = new ToolStripMenuItem("XMLを保存", null, BtnSave_Click);
var saveAsXmlItem = new ToolStripMenuItem("名前を付けて保存...", null, BtnSaveAs_Click);
var exitItem = new ToolStripMenuItem("終了", null, (s, e) => this.Close());
fileMenu.DropDownItems.AddRange(new ToolStripItem[] { openXmlItem, saveXmlItem, saveAsXmlItem, new ToolStripSeparator(), exitItem });
var editMenu = new ToolStripMenuItem("編集");
var loadLuaItem = new ToolStripMenuItem("Luaファイルを読み込む...", null, BtnLoadLuaFile_Click);
var replaceItem = new ToolStripMenuItem("置換", null, BtnReplace_Click);
editMenu.DropDownItems.AddRange(new ToolStripItem[] { loadLuaItem, replaceItem });
var toolsMenu = new ToolStripMenuItem("ツール");
var settingsItem = new ToolStripMenuItem("設定...", null, BtnSettings_Click);
var toggleThemeItem = new ToolStripMenuItem("テーマ切替", null, BtnToggleTheme_Click);
toolsMenu.DropDownItems.AddRange(new ToolStripItem[] { settingsItem, toggleThemeItem });
((ToolStripDropDownMenu)fileMenu.DropDown).ShowImageMargin = false;
((ToolStripDropDownMenu)editMenu.DropDown).ShowImageMargin = false;
((ToolStripDropDownMenu)toolsMenu.DropDown).ShowImageMargin = false;
menuStrip.Items.AddRange(new ToolStripItem[] { fileMenu, editMenu, toolsMenu });
// Use professional renderer without rounded corners to remove rounded appearance
menuStrip.Renderer = new ToolStripProfessionalRenderer();
if (menuStrip.Renderer is ToolStripProfessionalRenderer msr) msr.RoundedEdges = false;
var toolStrip = new ToolStrip { GripStyle = ToolStripGripStyle.Hidden };
var openXmlBtn = new ToolStripButton("XMLを開く", null, BtnLoadXml_Click) { Margin = new Padding(5, 0, 0, 0) };
var loadLuaBtn = new ToolStripButton("Lua読込", null, BtnLoadLuaFile_Click);
var replaceBtn = new ToolStripButton("置換", null, BtnReplace_Click);
var saveBtn = new ToolStripButton("保存", null, BtnSave_Click);
var settingsBtn = new ToolStripButton("設定", null, BtnSettings_Click);
// subtle hover effect: change ForeColor to accent
foreach (ToolStripButton tsb in new[] { openXmlBtn, saveBtn, loadLuaBtn, replaceBtn, settingsBtn })
{
tsb.MouseEnter += (s, e) => tsb.ForeColor = accentColor;
tsb.MouseLeave += (s, e) => tsb.ForeColor = appState.IsDarkTheme ? Color.FromArgb(230, 230, 230) : Color.Black;
tsb.DisplayStyle = ToolStripItemDisplayStyle.Text;
}
// remove visual separators and use margins for spacing to avoid vertical lines
loadLuaBtn.Margin = new Padding(10, 1, 0, 2);
settingsBtn.Margin = new Padding(10, 1, 0, 2);
toolStrip.Items.AddRange(new ToolStripItem[] { openXmlBtn, loadLuaBtn, replaceBtn, saveBtn, settingsBtn });
// Use same renderer style as menu to avoid rounded corners
toolStrip.Renderer = new ToolStripProfessionalRenderer();
if (toolStrip.Renderer is ToolStripProfessionalRenderer tsr) tsr.RoundedEdges = false;
this.Text = "Stormworks Lua Script Replacer";
this.Size = new System.Drawing.Size(1000, 700);
this.StartPosition = FormStartPosition.CenterScreen;
lstScripts!.SelectedIndexChanged += LstScripts_SelectedIndexChanged;
lstScripts.DrawMode = DrawMode.OwnerDrawFixed;
lstScripts.DrawItem += LstScripts_DrawItem;
lstScripts.MouseWheel += (s, e) =>
{
if (vScrollList == null) return;
int delta = -e.Delta / 120;
vScrollList.Value = Math.Max(vScrollList.Minimum, Math.Min(vScrollList.Maximum, vScrollList.Value + delta * Math.Max(1, vScrollList.SmallChange)));
};
// Wrap textboxes in thin border panels so we can control border color in dark mode
pnlCurrentBorder = new Panel { Dock = DockStyle.Fill, Padding = new Padding(1), Tag = "border" };
pnlCurrentBorder.Controls.Add(txtCurrentScript);
txtCurrentScript!.MouseWheel += (s, e) =>
{
if (txtCurrentScript == null) return;
int pages = Math.Max(1, Math.Abs(e.Delta) / 120);
if (e.Delta < 0)
{
for (int i = 0; i < pages; i++) SendMessage(txtCurrentScript.Handle, WM_VSCROLL, (IntPtr)SB_PAGEDOWN, IntPtr.Zero);
}
else if (e.Delta > 0)
{
for (int i = 0; i < pages; i++) SendMessage(txtCurrentScript.Handle, WM_VSCROLL, (IntPtr)SB_PAGEUP, IntPtr.Zero);
}
UpdateTextScrollbars();
SyncVScrollFromText(txtCurrentScript, vScrollCurrent);
};
txtCurrentScript.TextChanged += (s, e) => { UpdateTextScrollbars(); highlightTarget = txtCurrentScript; highlightTimer?.Stop(); highlightTimer?.Start(); };
var grpCurrentScript = new GroupBox { Text = "現在のスクリプト", Dock = DockStyle.Fill };
grpCurrentScript.Controls.Add(pnlCurrentBorder);
pnlNewBorder = new Panel { Dock = DockStyle.Fill, Padding = new Padding(1), Tag = "border" };
pnlNewBorder.Controls.Add(txtNewScript);
txtNewScript!.MouseWheel += (s, e) =>
{
if (txtNewScript == null) return;
int pages = Math.Max(1, Math.Abs(e.Delta) / 120);
if (e.Delta < 0)
{
for (int i = 0; i < pages; i++) SendMessage(txtNewScript.Handle, WM_VSCROLL, (IntPtr)SB_PAGEDOWN, IntPtr.Zero);
}
else if (e.Delta > 0)
{
for (int i = 0; i < pages; i++) SendMessage(txtNewScript.Handle, WM_VSCROLL, (IntPtr)SB_PAGEUP, IntPtr.Zero);
}
UpdateTextScrollbars();
SyncVScrollFromText(txtNewScript, vScrollNew);
};
txtNewScript.TextChanged += (s, e) => { UpdateTextScrollbars(); highlightTarget = txtNewScript; highlightTimer?.Stop(); highlightTimer?.Start(); };
var grpNewScript = new GroupBox { Text = "新しいスクリプト", Dock = DockStyle.Fill };
grpNewScript.Controls.Add(pnlNewBorder);
var mainLayout = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 2, RowCount = 2, Padding = new Padding(10) };
mainLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 35F));
mainLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 65F));
mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 300F));
mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
// script list will be placed inside the XML area per user request
// XML preview area (left column, spans top row)
var xmlPanel = new Panel { Dock = DockStyle.Fill, Padding = new Padding(6), Tag = "xmlPanel" };
// Recent-files dropdown (replaces simple file label)
cbRecentFiles = new ModernDropdown { Width = 420, Height = 26 };
// Script-selection dropdown to the right of recent-files
cbScriptFiles = new ModernDropdown { Width = 260, Height = 26 };
// container for top controls (recent files + script selector)
this.topControlsPanel = new FlowLayoutPanel { Dock = DockStyle.Top, FlowDirection = FlowDirection.LeftToRight, Padding = new Padding(4), Margin = new Padding(0), WrapContents = true, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink };
cbRecentFiles.Margin = new Padding(4, 4, 4, 4);
cbScriptFiles.Margin = new Padding(4, 4, 4, 4);
this.topControlsPanel.Controls.Add(cbRecentFiles);
this.topControlsPanel.Controls.Add(cbScriptFiles);
cbRecentFiles.SelectedIndexChanged += async (s, e) =>
{
if (cbRecentFiles == null) return;
var sel = cbRecentFiles.SelectedItem;
if (string.IsNullOrWhiteSpace(sel)) return;
if (string.Equals(sel, currentFilePath, StringComparison.OrdinalIgnoreCase)) return;
try
{
currentFilePath = sel;
await LoadXmlFileAsync();
SetupFileWatcher();
AddToRecentFiles(sel);
}
catch (Exception ex) { MessageBox.Show($"XMLファイルの読み込みに失敗しました:\n{ex.Message}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); }
};
cbScriptFiles.SelectedIndexChanged += (s, e) =>
{
try
{
if (cbScriptFiles == null) return;
var sel = cbScriptFiles.SelectedItem;
if (string.IsNullOrWhiteSpace(sel)) return;
// Determine directory from currentLuaFilePath or luaFileWatcher path
string? dir = null;
if (!string.IsNullOrWhiteSpace(currentLuaFilePath)) dir = Path.GetDirectoryName(currentLuaFilePath);
if (string.IsNullOrWhiteSpace(dir) && luaFileWatcher != null) dir = luaFileWatcher.Path;
if (string.IsNullOrWhiteSpace(dir)) return;
var selectedFileName = sel;
var full = Path.Combine(dir, selectedFileName);
if (!File.Exists(full)) return;
// Load file into new-script editor
Task.Run(async () =>
{
try
{
var text = await File.ReadAllTextAsync(full).ConfigureAwait(false);
this.BeginInvoke(new Action(() =>
{
txtNewScript!.Text = text;
txtNewScript.Modified = false;
currentLuaFilePath = full;
SetupLuaFileWatcher(full);
UpdateTextScrollbars();
}));
}
catch { }
});
}
catch { }
};
// adjust widths initially and when panel size changes
this.topControlsPanel.SizeChanged += (s, e) => UpdateTopControlsWidths();
this.Resize += (s, e) => UpdateTopControlsWidths();
var xmlInner = new Panel { Dock = DockStyle.Fill, Padding = new Padding(5), Tag = "border" };
// place the detected scripts list inside the XML area (user requested)
xmlInner.Controls.Add(lstScripts);
var grpXml = new GroupBox { Text = "XML ファイル", Dock = DockStyle.Fill };
grpXml.Controls.Add(xmlInner);
// Add controls: group (fills), then clear button, then recent-files combobox docked at top
// Add group first, then topControlsPanel so the top panel stays visible (Dock = Top)
xmlPanel.Controls.Add(grpXml);
xmlPanel.Controls.Add(topControlsPanel);
// Ensure widths are computed initially
try { UpdateTopControlsWidths(); } catch { }
mainLayout.Controls.Add(xmlPanel, 0, 0);
mainLayout.SetColumnSpan(xmlPanel, 2);
// create custom scrollbars and add to respective containers
vScrollList = new System.Windows.Forms.VScrollBar { Dock = DockStyle.Right, Width = 14 };
// attach scrollbar to the scripts list inside the left XML area
xmlInner.Controls.Add(vScrollList);
vScrollList.ValueChanged += (s, e) => { if (lstScripts != null) lstScripts.TopIndex = vScrollList.Value; };
vScrollCurrent = new System.Windows.Forms.VScrollBar { Dock = DockStyle.Right, Width = 14 };
pnlCurrentBorder!.Controls.Add(vScrollCurrent);
vScrollCurrent.ValueChanged += (s, e) =>
{
if (txtCurrentScript == null) return;
int line = vScrollCurrent.Value;
if (line < 0) line = 0;
if (line >= txtCurrentScript.Lines.Length) line = Math.Max(0, txtCurrentScript.Lines.Length - 1);
int idx = txtCurrentScript.GetFirstCharIndexFromLine(line);
if (idx >= 0)
{
int prevSel = txtCurrentScript.SelectionStart;
txtCurrentScript.SelectionStart = idx;
txtCurrentScript.ScrollToCaret();
txtCurrentScript.SelectionStart = prevSel;
}
};
vScrollNew = new System.Windows.Forms.VScrollBar { Dock = DockStyle.Right, Width = 14 };
pnlNewBorder!.Controls.Add(vScrollNew);
vScrollNew.ValueChanged += (s, e) =>
{
if (txtNewScript == null) return;
int line = vScrollNew.Value;
if (line < 0) line = 0;
if (line >= txtNewScript.Lines.Length) line = Math.Max(0, txtNewScript.Lines.Length - 1);
int idx = txtNewScript.GetFirstCharIndexFromLine(line);
if (idx >= 0)
{
int prevSel = txtNewScript.SelectionStart;
txtNewScript.SelectionStart = idx;
txtNewScript.ScrollToCaret();
txtNewScript.SelectionStart = prevSel;
}
};
mainLayout.Controls.Add(grpCurrentScript, 0, 1);
mainLayout.Controls.Add(grpNewScript, 1, 1);
// thin separators between menu/tool/main areas (theme-aware via Tag = "borderline")
var menuSeparator = new Panel { Dock = DockStyle.Top, Height = 1, Tag = "borderline" };
var toolSeparator = new Panel { Dock = DockStyle.Top, Height = 1, Tag = "borderline" };
// Ensure menu/tool dock to top explicitly
menuStrip.Dock = DockStyle.Top;
toolStrip.Dock = DockStyle.Top;
// Add controls in reverse so Dock = Top stacks correctly (last added appears at the top):
// mainLayout (fills remaining), toolSeparator, toolStrip, menuSeparator, menuStrip, pnlTitleBar (top)
this.Controls.Add(mainLayout);
this.Controls.Add(toolSeparator);
this.Controls.Add(toolStrip);
this.Controls.Add(menuSeparator);
this.Controls.Add(menuStrip);
this.Controls.Add(pnlTitleBar);
this.MainMenuStrip = menuStrip;
ApplyTheme();
// initialize scrollbar ranges / hide native scrollbars
// create highlight timer
highlightTimer = new System.Windows.Forms.Timer { Interval = 250 };
highlightTimer.Tick += (s, e) =>
{
try
{
highlightTimer!.Stop();
if (highlightTarget != null) HighlightLua(highlightTarget);
}
catch { }
};
UpdateListScrollbar();
UpdateTextScrollbars();
}
private void BtnToggleTheme_Click(object? sender, EventArgs e)
{
appState.IsDarkTheme = !appState.IsDarkTheme;
ApplyTheme();
SaveSettings();
}
private void ApplyTheme()
{
bool dark = appState.IsDarkTheme;
// Form base
this.BackColor = dark ? System.Drawing.Color.FromArgb(37, 37, 38) : SystemColors.Control;
// Use a slightly off-white for dark-mode text/borders to reduce harsh contrast
this.ForeColor = dark ? Color.FromArgb(230, 230, 230) : Color.Black;
// Title bar must remain fixed color
if (pnlTitleBar != null) pnlTitleBar.BackColor = Color.FromArgb(60, 45, 72);
// Apply recursively to other controls (skip title bar)
foreach (Control c in this.Controls)
{
if (c == pnlTitleBar) continue;
ApplyThemeToControl(c, dark);
}
// ensure custom scrollbars use theme-friendly colors
if (vScrollList != null)
{
vScrollList.BackColor = dark ? Color.FromArgb(60, 45, 72) : SystemColors.Control;
vScrollList.ForeColor = dark ? Color.FromArgb(200, 200, 200) : Color.Black;
}
if (vScrollCurrent != null)
{
vScrollCurrent.BackColor = dark ? Color.FromArgb(60, 45, 72) : SystemColors.Control;
vScrollCurrent.ForeColor = dark ? Color.FromArgb(200, 200, 200) : Color.Black;
}
if (vScrollNew != null)
{
vScrollNew.BackColor = dark ? Color.FromArgb(60, 45, 72) : SystemColors.Control;
vScrollNew.ForeColor = dark ? Color.FromArgb(200, 200, 200) : Color.Black;
}
// status strip removed (avoids interfering with window resize)
// xml editor and line numbers (removed)
// header and header buttons (removed)
}
private void ApplyThemeToControl(Control ctrl, bool dark)
{
if (ctrl == pnlTitleBar) return;
// Determine sibling index for subtle alternating contrast
int siblingIndex = ctrl.Parent?.Controls.IndexOf(ctrl) ?? 0;
bool alternate = (siblingIndex % 2 == 0);
// Default colors
if (dark)
{
// Softer text/border color for dark mode
ctrl.ForeColor = Color.FromArgb(230, 230, 230);
if (ctrl is TextBox)
{
ctrl.BackColor = Color.FromArgb(30, 30, 30);
}
else if (ctrl is ListBox)
{
ctrl.BackColor = Color.FromArgb(30, 30, 30);
}
else if (ctrl is GroupBox)
{
// Use a consistent color for all GroupBoxes so paired panels match
ctrl.BackColor = Color.FromArgb(37, 37, 38);
}
else if (ctrl is Panel)
{
// Panels used as borders carry Tag == "border"
if (ctrl.Tag is string t && t == "border")
{
// use a soft light-gray border instead of pure white
ctrl.BackColor = Color.FromArgb(200, 200, 200);
}
// Panels used as thin separators carry Tag == "borderline"
else if (ctrl.Tag is string t2 && t2 == "borderline")
{
ctrl.BackColor = dark ? Color.FromArgb(80, 80, 80) : Color.FromArgb(200, 200, 200);
}
else
{
ctrl.BackColor = alternate ? Color.FromArgb(45, 45, 48) : Color.FromArgb(50, 50, 53);
}
}
else
{
ctrl.BackColor = Color.FromArgb(45, 45, 48);
}
}
else
{
ctrl.ForeColor = Color.Black;
if (ctrl is TextBox)
{
ctrl.BackColor = Color.White;
}
else if (ctrl is ListBox)
{
ctrl.BackColor = Color.White;
}
else if (ctrl is GroupBox)
{
// Use a consistent color for all GroupBoxes in light theme as well
ctrl.BackColor = SystemColors.Control;
}
else if (ctrl is Panel)
{
ctrl.BackColor = alternate ? SystemColors.Control : SystemColors.ControlLight;
}
else
{
ctrl.BackColor = SystemColors.Control;
}
}
// Specific control tweaks
switch (ctrl)
{
case TextBox tb:
tb.BackColor = dark ? Color.FromArgb(30, 30, 30) : Color.White;
tb.ForeColor = dark ? Color.FromArgb(230, 230, 230) : Color.Black;
break;
case ListBox lb:
lb.BackColor = dark ? Color.FromArgb(30, 30, 30) : Color.White;
lb.ForeColor = dark ? Color.FromArgb(230, 230, 230) : Color.Black;
break;
case GroupBox gb:
// already set above with alternation; ensure ForeColor
gb.ForeColor = dark ? Color.FromArgb(200, 200, 200) : Color.Black;
break;
case Label l:
l.BackColor = Color.Transparent;
l.ForeColor = dark ? Color.FromArgb(230, 230, 230) : Color.Black;
break;
case MenuStrip ms:
ms.BackColor = dark ? Color.FromArgb(45, 45, 48) : SystemColors.Control;
ms.ForeColor = dark ? Color.FromArgb(230, 230, 230) : Color.Black;
break;
case ToolStrip ts:
ts.BackColor = dark ? Color.FromArgb(45, 45, 48) : SystemColors.Control;
ts.ForeColor = dark ? Color.FromArgb(230, 230, 230) : Color.Black;
// Ensure contained items update their colors as well
foreach (ToolStripItem item in ts.Items)
{
item.ForeColor = dark ? Color.FromArgb(230, 230, 230) : Color.Black;
if (item is ToolStripButton b) b.DisplayStyle = ToolStripItemDisplayStyle.Text;
}
break;
default:
break;
}
// Recurse
foreach (Control child in ctrl.Controls)
{
ApplyThemeToControl(child, dark);
}
}
private void HighlightLua(System.Windows.Forms.RichTextBox rtb)
{
if (rtb == null) return;
int selStart = rtb.SelectionStart;
int selLen = rtb.SelectionLength;
try { rtb.SuspendLayout(); } catch { }
try { SuspendDrawing(rtb); } catch { }
try
{
var originalColor = rtb.ForeColor;
string text = rtb.Text;
if (string.IsNullOrEmpty(text)) return;
// Determine visible character range (visible lines only) with a small margin
int topChar = rtb.GetCharIndexFromPosition(new Point(0, 0));
int bottomChar = rtb.GetCharIndexFromPosition(new Point(0, Math.Max(0, rtb.ClientSize.Height - 1)));
int firstLine = Math.Max(0, rtb.GetLineFromCharIndex(topChar) - 2);
int lastLine = Math.Min(Math.Max(0, rtb.Lines.Length - 1), rtb.GetLineFromCharIndex(bottomChar) + 2);
int startIndex = (firstLine < rtb.Lines.Length) ? rtb.GetFirstCharIndexFromLine(firstLine) : 0;
int endIndex = (lastLine + 1 < rtb.Lines.Length) ? rtb.GetFirstCharIndexFromLine(lastLine + 1) : rtb.TextLength;
if (startIndex < 0) startIndex = 0;
if (endIndex < startIndex) endIndex = rtb.TextLength;
int length = Math.Max(0, Math.Min(rtb.TextLength - startIndex, endIndex - startIndex));
// If visible region is very small or text is tiny, fall back to full highlight
if (rtb.TextLength < 2000 || length == rtb.TextLength)
{
// small text - highlight whole content
rtb.SelectAll();
rtb.SelectionColor = originalColor;
startIndex = 0; length = rtb.TextLength;
}
else
{
// reset visible range only
rtb.Select(startIndex, length);
rtb.SelectionColor = originalColor;
}