-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacroManager.cs
More file actions
166 lines (152 loc) · 5.71 KB
/
MacroManager.cs
File metadata and controls
166 lines (152 loc) · 5.71 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace VirtualController
{
public class MacroManager
{
private readonly string macroFolder;
public MacroManager(string macroFolder)
{
this.macroFolder = macroFolder;
if (!Directory.Exists(macroFolder))
{
Directory.CreateDirectory(macroFolder);
}
}
// 新: マクロ一覧エントリ(ファイル or フォルダ)
public class MacroEntry
{
public string Name { get; set; }
public string RelativePath { get; set; } // relative to macroFolder, directories use relative path
public bool IsFolder { get; set; }
public override string ToString()
{
return Name;
}
}
// ルート/指定フォルダ内のディレクトリ(先)→ファイル(後)を返す
public List<MacroEntry> GetMacroEntries(string relativePath = "")
{
var entries = new List<MacroEntry>();
try
{
string target = string.IsNullOrEmpty(relativePath) ? macroFolder : Path.Combine(macroFolder, relativePath);
if (!Directory.Exists(target))
return entries;
// directories first
var dirs = Directory.GetDirectories(target);
Array.Sort(dirs, StringComparer.CurrentCultureIgnoreCase);
foreach (var d in dirs)
{
var dirName = Path.GetFileName(d);
var rel = string.IsNullOrEmpty(relativePath) ? dirName : Path.Combine(relativePath, dirName);
entries.Add(new MacroEntry { Name = dirName, RelativePath = rel, IsFolder = true });
}
// then files
var files = Directory.GetFiles(target, "*.csv");
Array.Sort(files, StringComparer.CurrentCultureIgnoreCase);
foreach (var f in files)
{
var name = Path.GetFileNameWithoutExtension(f);
var rel = string.IsNullOrEmpty(relativePath) ? name : Path.Combine(relativePath, name);
entries.Add(new MacroEntry { Name = name, RelativePath = rel, IsFolder = false });
}
}
catch
{
// IO エラーは空リストでフォールバック
}
return entries;
}
// 互換: 既存呼び出し向けにフォルダ指定版の名前取得
public List<string> GetMacroNames(string relativePath = "")
{
var result = new List<string>();
try
{
string target = string.IsNullOrEmpty(relativePath) ? macroFolder : Path.Combine(macroFolder, relativePath);
if (!Directory.Exists(target))
return result;
var files = Directory.GetFiles(target, "*.csv");
foreach (var file in files)
{
var name = Path.GetFileNameWithoutExtension(file);
result.Add(name);
}
}
catch
{
}
return result;
}
// 既存: ルートのマクロテキスト読み込み(互換)
public string LoadMacroText(string macroName)
{
string path = Path.Combine(macroFolder, macroName + ".csv");
if (File.Exists(path))
return File.ReadAllText(path, Encoding.UTF8);
return "";
}
// 新: 相対フォルダ内のマクロテキスト読み込み
public string LoadMacroText(string relativePath, string macroName)
{
try
{
string target = string.IsNullOrEmpty(relativePath) ? macroFolder : Path.Combine(macroFolder, relativePath);
string path = Path.Combine(target, macroName + ".csv");
if (File.Exists(path))
return File.ReadAllText(path, Encoding.UTF8);
}
catch
{
}
return "";
}
// 既存: ルートへ保存(互換)
public void SaveMacro(string macroName, string text)
{
string path = Path.Combine(macroFolder, macroName + ".csv");
File.WriteAllText(path, text, Encoding.UTF8);
}
// 新: 相対フォルダへ保存(存在しなければ作成)
public void SaveMacroInFolder(string relativePath, string macroName, string text)
{
try
{
string target = string.IsNullOrEmpty(relativePath) ? macroFolder : Path.Combine(macroFolder, relativePath);
if (!Directory.Exists(target))
Directory.CreateDirectory(target);
string path = Path.Combine(target, macroName + ".csv");
File.WriteAllText(path, text, Encoding.UTF8);
}
catch
{
// 失敗は呼び出し元で処理
throw;
}
}
// 既存: ルート削除
public void DeleteMacro(string macroName)
{
string path = Path.Combine(macroFolder, macroName + ".csv");
if (File.Exists(path))
File.Delete(path);
}
// 新: 相対フォルダ内削除
public void DeleteMacroInFolder(string relativePath, string macroName)
{
try
{
string target = string.IsNullOrEmpty(relativePath) ? macroFolder : Path.Combine(macroFolder, relativePath);
string path = Path.Combine(target, macroName + ".csv");
if (File.Exists(path))
File.Delete(path);
}
catch
{
}
}
}
}