cc-pets 1.4.6 → 1.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +43 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +8 -1
- package/Sources/CCPets/CCPetsPhrases.h +40 -0
- package/Sources/CCPets/CCPetsPhrases.m +135 -16
- package/Sources/CCPets/CCPetsPhrasesEditor.m +238 -21
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,24 @@
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
- 台词可以按宠物分别配置了。`~/.cc-pets/speech.txt` 仍是所有宠物的通用台词,某一只想换
|
|
8
|
+
口吻时在 `~/.cc-pets/speech/<宠物名>.txt` 里**只写想改的小节**,没写的照旧走通用。
|
|
9
|
+
台词编辑器顶部多了「通用 / 当前宠物」的切换,切换宠物时自动跟随。
|
|
10
|
+
- 回落按**小节整体**做,不按行合并:专属写了 `[idle]` 就只说专属那几句,通用那一节
|
|
11
|
+
一句都不参与。按行合并的话,用户没有任何写法能表达"这只宠物这句话不要说"。
|
|
12
|
+
- 判据是"这一节在不在"而不是"有没有内容":专属里写了标签却不写台词,表示这只宠物在
|
|
13
|
+
这个情境闭嘴。当成"空的等于没写"去回落的话,这个意图就再也写不出来了。
|
|
14
|
+
- 专属词库的校验规则单独一套:缺小节是常态(缺就回落),所以"标签不能删"和
|
|
15
|
+
"`[state_*]` 至少留一句"都不适用。套通用规则的话,用户想改一句话得先抄一整份
|
|
16
|
+
22 节的文件过来。同理,保存时不给专属文件补标签——补完之后每一节都"存在",
|
|
17
|
+
这只宠物就再也不会回落了。
|
|
18
|
+
- 专属页右上角有「填入出厂模板」,解决空白页无从下手。填进来的是整份**注释状态**的
|
|
19
|
+
出厂台词:用户看得见每一节能写什么,而注释一行都不算数,所以填完之后仍然全部回落到
|
|
20
|
+
通用,想让哪一节归这只宠物管就去掉那一节的 `#`。直接填未注释的完整出厂台词是错的——
|
|
21
|
+
那样 22 节全被接管,再也不会回落;只填小节头的空骨架更糟,等于这只宠物全情境闭嘴。
|
|
22
|
+
- 专属文件懒创建,不预先给每只宠物生成空文件:空文件和没有文件语义完全一样,
|
|
23
|
+
凭空造一堆只会让目录全是噪音。删掉宠物素材也不删它的台词文件,装回来还在。
|
|
24
|
+
|
|
7
25
|
- 修复台词编辑器里 ⌘C/⌘V/⌘X/⌘A/⌘Z/⌘⇧Z 全部没反应。这些快捷键不是 `NSTextView` 自己
|
|
8
26
|
处理的:按键先走 `NSApp.mainMenu` 的 `performKeyEquivalent:`,命中菜单项之后才沿响应者链
|
|
9
27
|
发 `copy:` / `paste:` 这些消息。这个 app 是 `LSUIElement`,此前从没设过主菜单,所以全部
|
package/README.md
CHANGED
|
@@ -166,6 +166,49 @@ Agent **正在干活**时(思考、调用工具、等待审批、子 Agent 工
|
|
|
166
166
|
- **保存**(⌘S)/ **保存并关闭**(⏎)—— 保存时校验,问题当场列出来。
|
|
167
167
|
校验没过或被 mtime 冲突取消时不会关窗,改动不会跟着窗口一起消失
|
|
168
168
|
|
|
169
|
+
### 给单只宠物写专属台词
|
|
170
|
+
|
|
171
|
+
上面那份 `speech.txt` 是**所有**宠物的通用台词。想让某一只换个口吻,编辑器顶部有一个
|
|
172
|
+
「**通用** / **当前宠物 · ×××**」的切换——切到右边,写的就只属于当前这只宠物,
|
|
173
|
+
存在 `~/.cc-pets/speech/<宠物名>.txt`(内置素材是 `builtin-默认.txt`)。
|
|
174
|
+
|
|
175
|
+
这一页**初次打开是空白的,这是正常的**:空白 = 这只宠物全部沿用通用台词。
|
|
176
|
+
不知道能写什么就点右上角 **填入出厂模板**,它会把整份出厂台词以**注释状态**填进来
|
|
177
|
+
(每行都以 `#` 开头,一句都不生效,这只宠物说的还是通用台词)。想让哪一节归它自己管,
|
|
178
|
+
把那一节行首的 `#` 去掉再改字就行。
|
|
179
|
+
|
|
180
|
+
手写的话,**只写想改的小节**就行:
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
# ~/.cc-pets/speech/external-哆啦A梦.txt
|
|
184
|
+
[idle]
|
|
185
|
+
铜锣烧…
|
|
186
|
+
口袋里好像有点东西。
|
|
187
|
+
|
|
188
|
+
[done]
|
|
189
|
+
诺,任意门给你。
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
这样它闲着和完工时说自己的话,其余情境照旧用通用的。三种写法对应三种结果:
|
|
193
|
+
|
|
194
|
+
| 专属文件里 | 结果 |
|
|
195
|
+
| --- | --- |
|
|
196
|
+
| 没写这一节 | 用通用的 |
|
|
197
|
+
| 写了,下面有台词 | **整节接管**,通用的那一节一句都不参与 |
|
|
198
|
+
| 写了标签,下面空着 | 这只宠物在这个情境不吭声 |
|
|
199
|
+
|
|
200
|
+
注意是**整节替换而不是混着说**:写了 `[idle]` 的三句,它闲着时就只在这三句里挑。
|
|
201
|
+
|
|
202
|
+
专属这一页的规则比通用那份松得多——缺小节本来就是常态,所以「标签不能删」和
|
|
203
|
+
「`[state_*]` 至少留一句」两条都不适用,只剩逐行的那些检查(超长、槽位拼错、小节名写错)。
|
|
204
|
+
「试说一句」在专属没写该小节时会照实说出通用的那一句,并注明「这一节来自通用台词」。
|
|
205
|
+
按钮此时也变成 **清空这一页…**:清空即全部回落到通用,专属词库没有「出厂」这个概念。
|
|
206
|
+
「填入出厂模板」填的是注释版而不是直接可用的整份台词,也是同一个道理——直接填进
|
|
207
|
+
22 节未注释的台词,这只宠物就每一节都自己管、再也不会回落了,那正好毁掉这一页的用法。
|
|
208
|
+
|
|
209
|
+
切换宠物后编辑器会自动跟到新那只身上;这一页有没保存的改动时会先问一句。
|
|
210
|
+
删掉某只宠物的素材不会删它的专属台词文件,重新装回同名素材时台词还在。
|
|
211
|
+
|
|
169
212
|
校验分两级:
|
|
170
213
|
|
|
171
214
|
标签行是配置项,22 个一个都不能删。删掉之后编辑器里就看不见它了,用户既不知道少的是
|
|
@@ -988,6 +988,9 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
988
988
|
self.petView = [[PetView alloc] initWithFrame:NSMakeRect(43, 0, 144, 150)
|
|
989
989
|
sheet:image rowCount:spriteRowCount];
|
|
990
990
|
self.petView.currentPetID = selected;
|
|
991
|
+
// 台词层要知道现在是哪只宠物,才能去找它的专属词库。启动这一处和 switchPetToID:
|
|
992
|
+
// 那一处是仅有的两个入口——删除宠物后的回落也走 switchPetToID:。
|
|
993
|
+
PetPhrasesSetCurrentPetID(selected);
|
|
991
994
|
__weak typeof(self) weakSelf = self;
|
|
992
995
|
self.petView.pocketHoverChanged = ^(BOOL hovering) {
|
|
993
996
|
weakSelf.pocketHovering = hovering;
|
|
@@ -1759,6 +1762,7 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1759
1762
|
NSImage *image = LoadPetSpriteImage(option[@"path"], NSMakeSize(140, 150), spriteRowCount);
|
|
1760
1763
|
if (!image) return;
|
|
1761
1764
|
[self.petView applySheet:image petID:petID rowCount:spriteRowCount];
|
|
1765
|
+
PetPhrasesSetCurrentPetID(petID);
|
|
1762
1766
|
[NSUserDefaults.standardUserDefaults setObject:petID forKey:@"CCPetsSelectedSprite"];
|
|
1763
1767
|
}
|
|
1764
1768
|
- (BOOL)deletePetWithID:(NSString *)petID {
|
|
@@ -1787,7 +1791,10 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1787
1791
|
if ([self.petView.currentPetID isEqualToString:petID]) {
|
|
1788
1792
|
NSDictionary *fallback = [self petOptions].firstObject;
|
|
1789
1793
|
if (fallback) [self switchPetToID:fallback[@"id"]];
|
|
1790
|
-
else
|
|
1794
|
+
else {
|
|
1795
|
+
PetPhrasesSetCurrentPetID(nil);
|
|
1796
|
+
[NSUserDefaults.standardUserDefaults removeObjectForKey:@"CCPetsSelectedSprite"];
|
|
1797
|
+
}
|
|
1791
1798
|
}
|
|
1792
1799
|
return YES;
|
|
1793
1800
|
}
|
|
@@ -52,6 +52,34 @@ NSString *PetPhraseTagDescription(NSString *tag);
|
|
|
52
52
|
// 纯文本而不是 JSON:普通用户写不了 JSON,而且少个逗号整个文件失效、我们静默降级,
|
|
53
53
|
// 用户只会觉得"改了没用"。纯文本一行一句,写错只丢那一行。
|
|
54
54
|
NSString *PetPhrasesFilePath(void);
|
|
55
|
+
|
|
56
|
+
// 宠物专属词库。通用词库(上面那个 speech.txt)是所有宠物的兜底,某只宠物想换口吻时,
|
|
57
|
+
// 在 ~/.cc-pets/speech/<petKey>.txt 里**只写想改的小节**,没写的照旧用通用的。
|
|
58
|
+
//
|
|
59
|
+
// 回落按小节(tag)整体做,不按行合并:专属文件里写了 [idle] 就只说专属那几句,
|
|
60
|
+
// 通用的那节一句都不参与。这和"文件里有什么就说什么"是同一条语义,只是多了一层。
|
|
61
|
+
// 小节写了标签但底下空着,表示这只宠物在这个情境闭嘴——所以判据是"这一节在不在",
|
|
62
|
+
// 不是"这一节有没有内容"。
|
|
63
|
+
//
|
|
64
|
+
// 专属目录:~/.cc-pets/speech/。可用 CC_PETS_PHRASES_PET_DIR 覆盖(测试用)。
|
|
65
|
+
NSString *PetPhrasesPetDirectory(void);
|
|
66
|
+
// petID(builtin:默认.webp / external:哆啦A梦)转成文件名用的 key。
|
|
67
|
+
//
|
|
68
|
+
// petID 里的冒号、素材名里的斜杠都不能直接进文件名,内置素材的 .webp 后缀留着也只是
|
|
69
|
+
// 噪音,所以统一规整成 builtin-默认 / external-哆啦A梦。中文原样保留——用户要能在
|
|
70
|
+
// Finder 里一眼认出哪份是哪只的,转义成百分号编码就白瞎了。
|
|
71
|
+
NSString *PetPhrasesKeyForPetID(NSString *petID);
|
|
72
|
+
// 某只宠物的专属词库路径。petID 为空返回 nil。
|
|
73
|
+
NSString *PetPhrasesFilePathForPetID(NSString *petID);
|
|
74
|
+
|
|
75
|
+
// 当前是哪只宠物。取词层本来是纯函数,这里是唯一的外部状态——桌宠启动和每次切换
|
|
76
|
+
// 宠物时由 AppDelegate 推进来。做成 setter 而不是让 PetPhraseForTag 多带一个参数:
|
|
77
|
+
// 说话的调用点散在十几处,每处都传一遍 petID 只会让人漏传,而漏传的表现是"专属台词
|
|
78
|
+
// 偶尔不生效",最难查。
|
|
79
|
+
void PetPhrasesSetCurrentPetID(NSString *petID);
|
|
80
|
+
NSString *PetPhrasesCurrentPetID(void);
|
|
81
|
+
// 当前宠物的专属词库路径。还没设过宠物时返回 nil。
|
|
82
|
+
NSString *PetPhrasesCurrentPetFilePath(void);
|
|
55
83
|
// 出厂词库路径:app bundle 里的 phrases.default.txt。
|
|
56
84
|
// 可用 CC_PETS_PHRASES_DEFAULT_FILE 覆盖(测试用)。
|
|
57
85
|
NSString *PetPhrasesDefaultFilePath(void);
|
|
@@ -126,6 +154,18 @@ typedef NS_ENUM(NSInteger, PetPhraseIssueLevel) {
|
|
|
126
154
|
NSString *PetPhraseTextWithRestoredTags(NSString *text, NSArray<NSString *> **restored);
|
|
127
155
|
|
|
128
156
|
// 校验一段文本。保存时调用,不做实时校验——边打边标红只会干扰输入。
|
|
157
|
+
// 这是通用词库的规则:22 个标签一个都不能少,state_ 那组还必须留一句。
|
|
129
158
|
NSArray<PetPhraseIssue *> *PetPhraseValidateText(NSString *text);
|
|
159
|
+
// 专属词库的校验。规则松得多:缺小节是正常状态(缺就回落到通用),所以"标签不能删"
|
|
160
|
+
// 和"state_ 至少留一句"这两条一律不适用。留下的只有逐行的那些——超长、槽位拼错、
|
|
161
|
+
// 小节名写错、散句无处安放。
|
|
162
|
+
//
|
|
163
|
+
// 不共用一个函数是因为两边的"错误"含义相反:专属文件只写一节是常态,套通用规则的话
|
|
164
|
+
// 用户想改一句话就得先抄一整份 22 节的文件过来。
|
|
165
|
+
NSArray<PetPhraseIssue *> *PetPhraseValidateTextForPet(NSString *text);
|
|
166
|
+
|
|
167
|
+
// 这段文本里有没有 [tag] 这一节。编辑器判断"专属文件是否接管了这个情境"用,
|
|
168
|
+
// 判据和取词一致:标签在就算接管,底下有没有台词不影响。
|
|
169
|
+
BOOL PetPhraseTextContainsTag(NSString *text, NSString *tag);
|
|
130
170
|
// 有没有致命错误。有就不能保存。
|
|
131
171
|
BOOL PetPhraseIssuesHaveError(NSArray<PetPhraseIssue *> *issues);
|
|
@@ -45,6 +45,79 @@ NSString *PetPhrasesFilePath(void) {
|
|
|
45
45
|
return [NSHomeDirectory() stringByAppendingPathComponent:@".cc-pets/speech.txt"];
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
NSString *PetPhrasesPetDirectory(void) {
|
|
49
|
+
NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_PHRASES_PET_DIR"];
|
|
50
|
+
if (override.length > 0) return override.stringByStandardizingPath;
|
|
51
|
+
return [NSHomeDirectory() stringByAppendingPathComponent:@".cc-pets/speech"];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 文件名安全化。斜杠和冒号会让路径跑到别处去,控制字符在 Finder 里根本看不见,
|
|
55
|
+
// 前导点会把文件藏起来——用户看不见的文件等于不存在,他会以为自己写的没保存上。
|
|
56
|
+
static NSString *PetPhrasesSafeFileComponent(NSString *name) {
|
|
57
|
+
NSMutableString *safe = [NSMutableString string];
|
|
58
|
+
for (NSUInteger index = 0; index < name.length; index++) {
|
|
59
|
+
unichar character = [name characterAtIndex:index];
|
|
60
|
+
if (character == '/' || character == ':' || character == '\\' || character < 0x20) {
|
|
61
|
+
[safe appendString:@"_"];
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
[safe appendFormat:@"%C", character];
|
|
65
|
+
}
|
|
66
|
+
// "." 和 ".." 是目录本身,拼出来的路径会指向 speech 目录而不是文件。
|
|
67
|
+
if ([safe isEqualToString:@"."] || [safe isEqualToString:@".."]) return @"_";
|
|
68
|
+
if ([safe hasPrefix:@"."]) [safe replaceCharactersInRange:NSMakeRange(0, 1) withString:@"_"];
|
|
69
|
+
// 素材名理论上可以任意长,而文件名有 255 字节上限。中文一个字三字节,截到 60 个字符
|
|
70
|
+
// 稳稳落在限内;截断带来的重名概率不值得再挂个哈希后缀去防。
|
|
71
|
+
if (safe.length > 60) return [safe substringToIndex:60];
|
|
72
|
+
return safe;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
NSString *PetPhrasesKeyForPetID(NSString *petID) {
|
|
76
|
+
if (petID.length == 0) return nil;
|
|
77
|
+
NSString *scheme = @"";
|
|
78
|
+
NSString *name = petID;
|
|
79
|
+
NSRange colon = [petID rangeOfString:@":"];
|
|
80
|
+
if (colon.location != NSNotFound) {
|
|
81
|
+
scheme = [petID substringToIndex:colon.location];
|
|
82
|
+
name = [petID substringFromIndex:colon.location + 1];
|
|
83
|
+
}
|
|
84
|
+
// 内置素材的 petID 是文件名,扩展名对用户没有任何意义,留着只会让 builtin-默认.webp.txt
|
|
85
|
+
// 这种双扩展名看起来像出了错。
|
|
86
|
+
if ([scheme isEqualToString:@"builtin"]) {
|
|
87
|
+
NSString *extension = name.pathExtension.lowercaseString;
|
|
88
|
+
if ([extension isEqualToString:@"webp"] || [extension isEqualToString:@"png"]) {
|
|
89
|
+
name = name.stringByDeletingPathExtension;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (name.length == 0) return nil;
|
|
93
|
+
NSString *safe = PetPhrasesSafeFileComponent(name);
|
|
94
|
+
if (safe.length == 0) return nil;
|
|
95
|
+
if (scheme.length == 0) return safe;
|
|
96
|
+
return [NSString stringWithFormat:@"%@-%@", PetPhrasesSafeFileComponent(scheme), safe];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
NSString *PetPhrasesFilePathForPetID(NSString *petID) {
|
|
100
|
+
NSString *key = PetPhrasesKeyForPetID(petID);
|
|
101
|
+
if (key.length == 0) return nil;
|
|
102
|
+
return [PetPhrasesPetDirectory() stringByAppendingPathComponent:
|
|
103
|
+
[key stringByAppendingPathExtension:@"txt"]];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 当前宠物。取词层唯一的外部状态,见头文件里为什么不走参数。
|
|
107
|
+
static NSString *PetPhrasesCurrentPet;
|
|
108
|
+
|
|
109
|
+
void PetPhrasesSetCurrentPetID(NSString *petID) {
|
|
110
|
+
PetPhrasesCurrentPet = petID.length > 0 ? [petID copy] : nil;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
NSString *PetPhrasesCurrentPetID(void) {
|
|
114
|
+
return PetPhrasesCurrentPet;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
NSString *PetPhrasesCurrentPetFilePath(void) {
|
|
118
|
+
return PetPhrasesFilePathForPetID(PetPhrasesCurrentPet);
|
|
119
|
+
}
|
|
120
|
+
|
|
48
121
|
NSString *PetPhrasesLegacyFilePath(void) {
|
|
49
122
|
NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_PHRASES_LEGACY_FILE"];
|
|
50
123
|
if (override.length > 0) return override.stringByStandardizingPath;
|
|
@@ -292,30 +365,51 @@ BOOL PetPhrasesEnsureInteractionSections(void) {
|
|
|
292
365
|
return [updated writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
|
|
293
366
|
}
|
|
294
367
|
|
|
368
|
+
// 一个缓存槽:只存 path / stamp / groups 三样。用可变字典而不是结构体,
|
|
369
|
+
// 纯粹是为了让 ARC 全程管住这几个对象——结构体里存对象要么写 __unsafe_unretained
|
|
370
|
+
// 再自己找地方续命,要么加 __strong 让结构体不能当静态变量用,两条路都比这难读。
|
|
371
|
+
//
|
|
372
|
+
// 路径也参与失效判断:切换宠物时专属词库换的是路径而不是内容,只比 mtime 的话会一直
|
|
373
|
+
// 拿着上一只宠物的台词——两只宠物的文件 mtime 恰好相同并不罕见(同一次编辑里先后存的)。
|
|
374
|
+
//
|
|
295
375
|
// 从磁盘读。mtime 没变就直接用上次的结果——取句子本来就低频,这点开销可以忽略,
|
|
296
376
|
// 换来的是用户改完保存立刻生效,不用重启桌宠。
|
|
297
|
-
static NSDictionary<NSString *, NSArray<NSString *> *> *
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
static NSString *cachedPath;
|
|
301
|
-
|
|
302
|
-
NSString *path = PetPhrasesFilePath();
|
|
377
|
+
static NSDictionary<NSString *, NSArray<NSString *> *> *LoadPhrasesAtPath(NSString *path,
|
|
378
|
+
NSMutableDictionary *slot) {
|
|
379
|
+
if (path.length == 0) return nil;
|
|
303
380
|
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path error:nil];
|
|
304
381
|
NSDate *stamp = attributes[NSFileModificationDate];
|
|
305
|
-
if (stamp &&
|
|
306
|
-
[path isEqualToString:
|
|
307
|
-
return
|
|
382
|
+
if (stamp && slot[@"stamp"] && [stamp isEqualToDate:slot[@"stamp"]] &&
|
|
383
|
+
[path isEqualToString:slot[@"path"]]) {
|
|
384
|
+
return slot[@"groups"];
|
|
308
385
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
cached = nil;
|
|
386
|
+
[slot removeAllObjects];
|
|
387
|
+
slot[@"path"] = path;
|
|
312
388
|
if (!stamp) return nil;
|
|
389
|
+
slot[@"stamp"] = stamp;
|
|
313
390
|
|
|
314
391
|
NSString *text = [NSString stringWithContentsOfFile:path
|
|
315
392
|
encoding:NSUTF8StringEncoding error:nil];
|
|
316
393
|
if (text.length == 0) return nil;
|
|
317
|
-
|
|
318
|
-
|
|
394
|
+
NSDictionary *groups = ParsePhrasesText(text, nil);
|
|
395
|
+
slot[@"groups"] = groups;
|
|
396
|
+
return groups;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
static NSDictionary<NSString *, NSArray<NSString *> *> *LoadUserPhrases(void) {
|
|
400
|
+
static NSMutableDictionary *slot;
|
|
401
|
+
static dispatch_once_t once;
|
|
402
|
+
dispatch_once(&once, ^{ slot = [NSMutableDictionary dictionary]; });
|
|
403
|
+
return LoadPhrasesAtPath(PetPhrasesFilePath(), slot);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// 当前宠物的专属词库。没设过宠物、或这只宠物没有专属文件时返回 nil——
|
|
407
|
+
// 两种情况的结果都是"全部走通用",调用方不需要区分。
|
|
408
|
+
static NSDictionary<NSString *, NSArray<NSString *> *> *LoadPetPhrases(void) {
|
|
409
|
+
static NSMutableDictionary *slot;
|
|
410
|
+
static dispatch_once_t once;
|
|
411
|
+
dispatch_once(&once, ^{ slot = [NSMutableDictionary dictionary]; });
|
|
412
|
+
return LoadPhrasesAtPath(PetPhrasesCurrentPetFilePath(), slot);
|
|
319
413
|
}
|
|
320
414
|
|
|
321
415
|
// 模板本身是数据,不是格式串。**绝不能把它交给 stringWithFormat:**——
|
|
@@ -335,8 +429,14 @@ static NSString *FillSlots(NSString *template, NSDictionary<NSString *, NSString
|
|
|
335
429
|
}
|
|
336
430
|
|
|
337
431
|
// 候选就是文件里那一节,没有别的来源。
|
|
432
|
+
//
|
|
433
|
+
// 两层:这只宠物的专属词库里**有这一节**就整节接管,通用词库那节一句都不参与;
|
|
434
|
+
// 没有这一节才落到通用。判据是 entries != nil 而不是 count > 0——专属文件里写了
|
|
435
|
+
// 标签却不写台词,是用户在明确表达"这只宠物在这个情境闭嘴",那时必须返回空,
|
|
436
|
+
// 落回通用的话这个表达就没有任何写法能实现了。
|
|
338
437
|
static NSArray<NSString *> *CandidatesForTag(NSString *tag) {
|
|
339
|
-
NSArray *entries =
|
|
438
|
+
NSArray *entries = LoadPetPhrases()[tag];
|
|
439
|
+
if (entries == nil) entries = LoadUserPhrases()[tag];
|
|
340
440
|
if (![entries isKindOfClass:NSArray.class]) return @[];
|
|
341
441
|
NSMutableArray<NSString *> *result = [NSMutableArray array];
|
|
342
442
|
for (id entry in (NSArray *)entries) {
|
|
@@ -591,7 +691,9 @@ NSString *PetPhraseTextWithRestoredTags(NSString *text,
|
|
|
591
691
|
return [lines componentsJoinedByString:@"\n"];
|
|
592
692
|
}
|
|
593
693
|
|
|
594
|
-
|
|
694
|
+
// forPet=YES 时只跑逐行那部分:专属词库缺小节是常态(缺就回落到通用),
|
|
695
|
+
// 拿通用规则去套的话,用户想改一句话得先抄一整份 22 节的文件过来。
|
|
696
|
+
static NSArray<PetPhraseIssue *> *ValidateText(NSString *text, BOOL forPet) {
|
|
595
697
|
NSMutableArray<PetPhraseIssue *> *issues = [NSMutableArray array];
|
|
596
698
|
NSDictionary *groups = ParsePhrasesText(text ?: @"", ^(NSInteger line, NSString *reason) {
|
|
597
699
|
[issues addObject:MakeIssue(line, PetPhraseIssueLevelWarning, reason)];
|
|
@@ -627,6 +729,7 @@ NSArray<PetPhraseIssue *> *PetPhraseValidateText(NSString *text) {
|
|
|
627
729
|
// 会满文件去找一个不存在的小节。
|
|
628
730
|
NSSet<NSString *> *stateTags = [NSSet setWithArray:PetPhraseStateTags()];
|
|
629
731
|
for (NSString *tag in PetPhraseAllTags()) {
|
|
732
|
+
if (forPet) break;
|
|
630
733
|
NSArray *entries = groups[tag];
|
|
631
734
|
if (entries == nil) {
|
|
632
735
|
[issues addObject:MakeIssue(0, PetPhraseIssueLevelError, [NSString stringWithFormat:
|
|
@@ -655,6 +758,22 @@ NSArray<PetPhraseIssue *> *PetPhraseValidateText(NSString *text) {
|
|
|
655
758
|
return issues;
|
|
656
759
|
}
|
|
657
760
|
|
|
761
|
+
NSArray<PetPhraseIssue *> *PetPhraseValidateText(NSString *text) {
|
|
762
|
+
return ValidateText(text, NO);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
NSArray<PetPhraseIssue *> *PetPhraseValidateTextForPet(NSString *text) {
|
|
766
|
+
return ValidateText(text, YES);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// 判据必须和取词一致:取词看的是"这一节在不在"(ParsePhrasesText 给空小节也建了条目),
|
|
770
|
+
// 这里另写一套字符串匹配的话,两边迟早对不上——用户会看到编辑器说"这句来自通用",
|
|
771
|
+
// 桌宠却闭着嘴。
|
|
772
|
+
BOOL PetPhraseTextContainsTag(NSString *text, NSString *tag) {
|
|
773
|
+
if (tag.length == 0 || text.length == 0) return NO;
|
|
774
|
+
return ParsePhrasesText(text, nil)[tag] != nil;
|
|
775
|
+
}
|
|
776
|
+
|
|
658
777
|
BOOL PetPhraseIssuesHaveError(NSArray<PetPhraseIssue *> *issues) {
|
|
659
778
|
for (PetPhraseIssue *issue in issues) {
|
|
660
779
|
if (issue.level == PetPhraseIssueLevelError) return YES;
|
|
@@ -9,8 +9,19 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
9
9
|
@property NSTextField *statusLabel;
|
|
10
10
|
@property NSButton *saveButton;
|
|
11
11
|
@property NSButton *saveAndCloseButton;
|
|
12
|
+
@property NSButton *revertButton;
|
|
13
|
+
// 只在专属页出现:把出厂台词整份填进来,给空白页一个起点。
|
|
14
|
+
@property NSButton *fillButton;
|
|
15
|
+
// 编辑的是通用词库还是某只宠物的专属词库。
|
|
16
|
+
@property NSSegmentedControl *scopeControl;
|
|
17
|
+
@property BOOL petScope;
|
|
18
|
+
// 打开这一页时锁定的 petID。不是每次都现取 PetPhrasesCurrentPetID():用户完全可能
|
|
19
|
+
// 一边开着编辑器一边切宠物,现取的话保存会落到另一只身上,而他看着的还是这一只的文本。
|
|
20
|
+
@property(copy) NSString *scopePetID;
|
|
12
21
|
// 载入时文件的 mtime。保存前比一次,防止把外部编辑器的改动直接盖掉。
|
|
13
22
|
@property NSDate *loadedStamp;
|
|
23
|
+
// 载入时的全文。判断"有没有改过"用,切换 scope 前要靠它决定问不问。
|
|
24
|
+
@property(copy) NSString *loadedText;
|
|
14
25
|
@end
|
|
15
26
|
|
|
16
27
|
@implementation PetPhrasesEditorController
|
|
@@ -29,6 +40,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
29
40
|
+ (void)present {
|
|
30
41
|
PetPhrasesEditorController *controller = [self shared];
|
|
31
42
|
[controller buildWindowIfNeeded];
|
|
43
|
+
[controller syncScopeToCurrentPet];
|
|
32
44
|
[controller reloadFromDisk];
|
|
33
45
|
[NSApp activateIgnoringOtherApps:YES];
|
|
34
46
|
[controller.window makeKeyAndOrderFront:nil];
|
|
@@ -143,13 +155,37 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
143
155
|
NSButton *revert = [NSButton buttonWithTitle:@"恢复出厂台词…" target:self
|
|
144
156
|
action:@selector(revertToDefault:)];
|
|
145
157
|
revert.translatesAutoresizingMaskIntoConstraints = NO;
|
|
146
|
-
|
|
147
|
-
|
|
158
|
+
self.revertButton = revert;
|
|
159
|
+
|
|
160
|
+
// 两段式而不是下拉菜单:一共就两个去处,而且"我现在改的是哪一份"必须一眼可见——
|
|
161
|
+
// 藏进下拉里的话,用户很容易把只想给一只宠物写的台词存进通用词库。
|
|
162
|
+
NSSegmentedControl *scope = [NSSegmentedControl segmentedControlWithLabels:@[@"通用", @"当前宠物"]
|
|
163
|
+
trackingMode:NSSegmentSwitchTrackingSelectOne target:self action:@selector(scopeChanged:)];
|
|
164
|
+
scope.translatesAutoresizingMaskIntoConstraints = NO;
|
|
165
|
+
scope.selectedSegment = 0;
|
|
166
|
+
self.scopeControl = scope;
|
|
167
|
+
|
|
168
|
+
// 专属页初次打开是空白的——空白在语义上是对的(全部回落到通用),但一个空白文本框
|
|
169
|
+
// 不告诉任何人该往里写什么。这个按钮就是那个起点:把出厂台词整份填进来,
|
|
170
|
+
// 用户在上面改字、删掉不想接管的小节即可。
|
|
171
|
+
NSButton *fill = [NSButton buttonWithTitle:@"填入出厂模板" target:self
|
|
172
|
+
action:@selector(fillFromDefault:)];
|
|
173
|
+
fill.translatesAutoresizingMaskIntoConstraints = NO;
|
|
174
|
+
fill.hidden = YES;
|
|
175
|
+
self.fillButton = fill;
|
|
176
|
+
|
|
177
|
+
for (NSView *view in @[scroll, status, picker, speak, save, saveAndClose, revert, scope, fill]) {
|
|
148
178
|
[root addSubview:view];
|
|
149
179
|
}
|
|
150
180
|
|
|
151
181
|
[NSLayoutConstraint activateConstraints:@[
|
|
152
|
-
[
|
|
182
|
+
[scope.topAnchor constraintEqualToAnchor:root.topAnchor constant:12],
|
|
183
|
+
[scope.leadingAnchor constraintEqualToAnchor:root.leadingAnchor constant:16],
|
|
184
|
+
[fill.leadingAnchor constraintGreaterThanOrEqualToAnchor:scope.trailingAnchor constant:12],
|
|
185
|
+
[fill.trailingAnchor constraintEqualToAnchor:root.trailingAnchor constant:-16],
|
|
186
|
+
[fill.centerYAnchor constraintEqualToAnchor:scope.centerYAnchor],
|
|
187
|
+
|
|
188
|
+
[scroll.topAnchor constraintEqualToAnchor:scope.bottomAnchor constant:10],
|
|
153
189
|
[scroll.leadingAnchor constraintEqualToAnchor:root.leadingAnchor],
|
|
154
190
|
[scroll.trailingAnchor constraintEqualToAnchor:root.trailingAnchor],
|
|
155
191
|
[scroll.bottomAnchor constraintEqualToAnchor:status.topAnchor constant:-8],
|
|
@@ -175,16 +211,106 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
175
211
|
self.window = window;
|
|
176
212
|
}
|
|
177
213
|
|
|
214
|
+
#pragma mark - 编辑范围
|
|
215
|
+
|
|
216
|
+
// 菜单里显示的宠物名。petID 是 external:哆啦A梦 / builtin:默认.webp,
|
|
217
|
+
// 前缀和扩展名对用户没有意义。
|
|
218
|
+
- (NSString *)displayNameForPetID:(NSString *)petID {
|
|
219
|
+
NSRange colon = [petID rangeOfString:@":"];
|
|
220
|
+
NSString *name = colon.location == NSNotFound ? petID :
|
|
221
|
+
[petID substringFromIndex:colon.location + 1];
|
|
222
|
+
NSString *extension = name.pathExtension.lowercaseString;
|
|
223
|
+
if ([extension isEqualToString:@"webp"] || [extension isEqualToString:@"png"]) {
|
|
224
|
+
name = name.stringByDeletingPathExtension;
|
|
225
|
+
}
|
|
226
|
+
return name;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// 把"当前宠物"那一段的标题换成真名,并处理没有宠物的情况。
|
|
230
|
+
//
|
|
231
|
+
// 每次打开窗口都要跑:窗口是单例且不随关闭销毁,用户完全可能关掉编辑器、切一只宠物、
|
|
232
|
+
// 再打开——那时段标题还停在上一只身上,他会以为自己在给这只写。
|
|
233
|
+
- (void)syncScopeToCurrentPet {
|
|
234
|
+
NSString *petID = PetPhrasesCurrentPetID();
|
|
235
|
+
BOOL hasPet = petID.length > 0;
|
|
236
|
+
[self.scopeControl setLabel:hasPet ?
|
|
237
|
+
[NSString stringWithFormat:@"当前宠物 · %@", [self displayNameForPetID:petID]] :
|
|
238
|
+
@"当前宠物" forSegment:1];
|
|
239
|
+
[self.scopeControl setEnabled:hasPet forSegment:1];
|
|
240
|
+
|
|
241
|
+
// 宠物变了(或没了)而这一页正停在专属范围上:跟着走,但别把用户没存的东西冲掉。
|
|
242
|
+
if (self.petScope && (!hasPet || ![petID isEqualToString:self.scopePetID])) {
|
|
243
|
+
if ([self isDirty] && ![self confirmDiscardChanges]) {
|
|
244
|
+
// 用户选择留在原地。此时 scopePetID 还是老那只,保存仍然落在他看着的那份上。
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
self.petScope = hasPet;
|
|
248
|
+
}
|
|
249
|
+
self.scopePetID = petID;
|
|
250
|
+
if (!hasPet) self.petScope = NO;
|
|
251
|
+
self.scopeControl.selectedSegment = self.petScope ? 1 : 0;
|
|
252
|
+
[self syncRevertButtonTitle];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
- (void)syncRevertButtonTitle {
|
|
256
|
+
// 专属词库没有"出厂"这一说,它的出厂状态就是空文件(全部回落到通用)。
|
|
257
|
+
self.revertButton.title = self.petScope ? @"清空这一页…" : @"恢复出厂台词…";
|
|
258
|
+
// 通用页不需要这个按钮:它本来就是从出厂台词拷出来的,不存在空白无从下手的问题,
|
|
259
|
+
// 想拿回出厂内容有"恢复出厂台词…"。
|
|
260
|
+
self.fillButton.hidden = !self.petScope;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
- (void)scopeChanged:(id)sender {
|
|
264
|
+
BOOL wantPet = self.scopeControl.selectedSegment == 1;
|
|
265
|
+
if (wantPet == self.petScope) return;
|
|
266
|
+
if ([self isDirty] && ![self confirmDiscardChanges]) {
|
|
267
|
+
self.scopeControl.selectedSegment = self.petScope ? 1 : 0;
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
self.petScope = wantPet;
|
|
271
|
+
[self syncRevertButtonTitle];
|
|
272
|
+
[self reloadFromDisk];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// 当前这一页对应磁盘上的哪个文件。
|
|
276
|
+
- (NSString *)currentFilePath {
|
|
277
|
+
if (!self.petScope) return PetPhrasesFilePath();
|
|
278
|
+
return PetPhrasesFilePathForPetID(self.scopePetID);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
- (BOOL)isDirty {
|
|
282
|
+
return ![(self.textView.string ?: @"") isEqualToString:self.loadedText ?: @""];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
- (BOOL)confirmDiscardChanges {
|
|
286
|
+
NSAlert *alert = [NSAlert new];
|
|
287
|
+
alert.messageText = @"这一页还没保存";
|
|
288
|
+
alert.informativeText = @"切换之后,没保存的改动会丢掉。";
|
|
289
|
+
[alert addButtonWithTitle:@"丢弃改动"];
|
|
290
|
+
[alert addButtonWithTitle:@"留在这一页"];
|
|
291
|
+
return [alert runModal] == NSAlertFirstButtonReturn;
|
|
292
|
+
}
|
|
293
|
+
|
|
178
294
|
#pragma mark - 读写
|
|
179
295
|
|
|
180
296
|
- (void)reloadFromDisk {
|
|
181
|
-
|
|
182
|
-
|
|
297
|
+
// 专属词库不预先生成:空文件和"没有这个文件"在语义上完全一样(都是全部回落到通用),
|
|
298
|
+
// 凭空造一堆空文件只会让 speech/ 目录里全是噪音,用户还得挨个点开确认是不是空的。
|
|
299
|
+
if (!self.petScope) PetPhrasesEnsureFileExists();
|
|
300
|
+
NSString *path = [self currentFilePath];
|
|
183
301
|
NSString *text = [NSString stringWithContentsOfFile:path
|
|
184
302
|
encoding:NSUTF8StringEncoding error:nil] ?: @"";
|
|
185
303
|
self.textView.string = text;
|
|
304
|
+
self.loadedText = text;
|
|
186
305
|
self.loadedStamp = [NSFileManager.defaultManager attributesOfItemAtPath:path
|
|
187
306
|
error:nil][NSFileModificationDate];
|
|
307
|
+
if (self.petScope && text.length == 0) {
|
|
308
|
+
[self showStatus:[NSString stringWithFormat:
|
|
309
|
+
@"%@ · 还是空的,这只宠物现在说的全是通用台词。"
|
|
310
|
+
"只写想改的小节即可,或点右上角「填入出厂模板」看看能写些什么。", path]
|
|
311
|
+
warning:NO];
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
188
314
|
[self showStatus:[NSString stringWithFormat:@"%@", path] warning:NO];
|
|
189
315
|
}
|
|
190
316
|
|
|
@@ -214,7 +340,9 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
214
340
|
// 修复结果就已经覆盖了文件。必须让用户确认恢复位置和内容,再主动保存一次。
|
|
215
341
|
NSArray<NSString *> *restored = nil;
|
|
216
342
|
NSString *text = self.textView.string ?: @"";
|
|
217
|
-
|
|
343
|
+
// 专属词库跳过这一步:那份文件本来就该只有用户想改的那几节,把 22 个标签全补进去
|
|
344
|
+
// 正好毁掉它的用法——补完之后每一节都"存在",等于这只宠物再也不会回落到通用了。
|
|
345
|
+
NSString *repaired = self.petScope ? text : PetPhraseTextWithRestoredTags(text, &restored);
|
|
218
346
|
if (restored.count > 0) {
|
|
219
347
|
// 走 shouldChangeTextInRange: 让这一步能被 ⌘Z 撤销,用户不满意可以退回去。
|
|
220
348
|
NSRange all = NSMakeRange(0, text.length);
|
|
@@ -227,17 +355,25 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
227
355
|
return NO;
|
|
228
356
|
}
|
|
229
357
|
|
|
230
|
-
NSArray<PetPhraseIssue *> *issues =
|
|
358
|
+
NSArray<PetPhraseIssue *> *issues = self.petScope ?
|
|
359
|
+
PetPhraseValidateTextForPet(text) : PetPhraseValidateText(text);
|
|
231
360
|
if (PetPhraseIssuesHaveError(issues)) {
|
|
232
361
|
[self presentIssues:issues blocking:YES restored:restored];
|
|
233
362
|
return NO;
|
|
234
363
|
}
|
|
235
364
|
if (![self confirmNoExternalChange]) return NO;
|
|
236
365
|
|
|
237
|
-
NSString *path =
|
|
366
|
+
NSString *path = [self currentFilePath];
|
|
367
|
+
if (path.length == 0) {
|
|
368
|
+
[self showStatus:@"没有选中的宠物,无法保存专属台词。" warning:YES];
|
|
369
|
+
return NO;
|
|
370
|
+
}
|
|
238
371
|
NSError *error = nil;
|
|
239
372
|
// 结尾补一个换行:没有的话下次追加小节会直接粘在最后一句后面。
|
|
240
373
|
NSString *normalized = [text hasSuffix:@"\n"] ? text : [text stringByAppendingString:@"\n"];
|
|
374
|
+
// 专属目录是懒创建的,第一次给某只宠物写台词时才出现。
|
|
375
|
+
[NSFileManager.defaultManager createDirectoryAtPath:path.stringByDeletingLastPathComponent
|
|
376
|
+
withIntermediateDirectories:YES attributes:nil error:nil];
|
|
241
377
|
if (![normalized writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
|
|
242
378
|
[self showStatus:[NSString stringWithFormat:@"保存失败:%@",
|
|
243
379
|
error.localizedDescription ?: @"未知原因"] warning:YES];
|
|
@@ -245,6 +381,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
245
381
|
}
|
|
246
382
|
self.loadedStamp = [NSFileManager.defaultManager attributesOfItemAtPath:path
|
|
247
383
|
error:nil][NSFileModificationDate];
|
|
384
|
+
self.loadedText = normalized;
|
|
248
385
|
|
|
249
386
|
if (issues.count > 0) {
|
|
250
387
|
[self presentIssues:issues blocking:NO restored:nil];
|
|
@@ -259,7 +396,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
259
396
|
// 外部编辑器可能同时改了这个文件。直接覆盖会静默吃掉别人的改动,所以问一次。
|
|
260
397
|
- (BOOL)confirmNoExternalChange {
|
|
261
398
|
NSDate *current = [NSFileManager.defaultManager
|
|
262
|
-
attributesOfItemAtPath:
|
|
399
|
+
attributesOfItemAtPath:[self currentFilePath] error:nil][NSFileModificationDate];
|
|
263
400
|
if (!current || !self.loadedStamp || [current isEqualToDate:self.loadedStamp]) return YES;
|
|
264
401
|
|
|
265
402
|
NSAlert *alert = [NSAlert new];
|
|
@@ -328,13 +465,23 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
328
465
|
// 对着编辑器里【还没保存】的文本取,这样用户改完不用先保存就能听到效果。
|
|
329
466
|
- (void)speakSample:(id)sender {
|
|
330
467
|
NSString *tag = self.tagPicker.selectedItem.representedObject;
|
|
331
|
-
NSString *
|
|
468
|
+
NSString *draft = self.textView.string ?: @"";
|
|
469
|
+
// 专属这一页没写这一节时,桌宠实际会说通用词库里的那一节——预览必须走同一条路,
|
|
470
|
+
// 否则用户看到"[idle] 里没有能说的句子",会以为这只宠物闲着的时候是哑的。
|
|
471
|
+
BOOL fellBack = NO;
|
|
472
|
+
if (self.petScope && !PetPhraseTextContainsTag(draft, tag)) {
|
|
473
|
+
draft = [NSString stringWithContentsOfFile:PetPhrasesFilePath()
|
|
474
|
+
encoding:NSUTF8StringEncoding error:nil] ?: @"";
|
|
475
|
+
fellBack = YES;
|
|
476
|
+
}
|
|
477
|
+
NSString *text = PetPhraseForTagInText(tag, draft, [self sampleSlots]);
|
|
332
478
|
if (text.length == 0) {
|
|
333
479
|
[self showStatus:[NSString stringWithFormat:@"[%@] 里没有能说的句子。", tag] warning:YES];
|
|
334
480
|
return;
|
|
335
481
|
}
|
|
336
482
|
if (PetPhrasesSpeakHandler) PetPhrasesSpeakHandler(text);
|
|
337
|
-
[self showStatus:[NSString stringWithFormat:@"
|
|
483
|
+
[self showStatus:[NSString stringWithFormat:@"试说:%@%@", text,
|
|
484
|
+
fellBack ? @"(这一节来自通用台词)" : @""] warning:NO];
|
|
338
485
|
}
|
|
339
486
|
|
|
340
487
|
// 预览用的假数据。用真实数据的话,没在跑 agent 时 {toolName} 之类全是空的,
|
|
@@ -346,28 +493,98 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
346
493
|
};
|
|
347
494
|
}
|
|
348
495
|
|
|
349
|
-
#pragma mark -
|
|
496
|
+
#pragma mark - 填入出厂台词
|
|
497
|
+
|
|
498
|
+
// 给空白的专属页一个起点,同时**不破坏回落**。
|
|
499
|
+
//
|
|
500
|
+
// 直接填入完整的出厂台词是错的:那样 22 个小节全都"存在",这只宠物从此每一节都自己
|
|
501
|
+
// 管,再也不会回落到通用词库——正好毁掉专属词库的全部用法。
|
|
502
|
+
//
|
|
503
|
+
// 所以填进来的是整份**注释状态**的出厂台词。用户看得见每一节可以写什么(这才是空白页
|
|
504
|
+
// 真正缺的东西),而注释行在解析时一行都不算数,所以填完之后这只宠物说的仍然全是通用
|
|
505
|
+
// 台词,什么都没变。想让哪一节归它自己管,把那一节的 # 去掉再改字。
|
|
506
|
+
//
|
|
507
|
+
// 也不做成"只填小节头、不填台词"的骨架:那样每一节都存在且为空,按语义等于这只宠物
|
|
508
|
+
// 在所有情境下全部闭嘴,比空白页更糟——用户点完初始化,桌宠反而一句话都不说了。
|
|
509
|
+
- (NSString *)commentedDefaultText {
|
|
510
|
+
NSString *defaults = [NSString stringWithContentsOfFile:PetPhrasesDefaultFilePath()
|
|
511
|
+
encoding:NSUTF8StringEncoding error:nil];
|
|
512
|
+
if (defaults.length == 0) return nil;
|
|
513
|
+
|
|
514
|
+
NSString *petName = self.scopePetID.length > 0 ?
|
|
515
|
+
[self displayNameForPetID:self.scopePetID] : @"这只宠物";
|
|
516
|
+
NSMutableString *text = [NSMutableString stringWithFormat:
|
|
517
|
+
@"# ↓↓↓ %@ 的专属台词。下面整份都是注释行,未生效状态 ↓↓↓\n"
|
|
518
|
+
"#\n"
|
|
519
|
+
"# 想让某个情境归 %@ 自己说:把对应标签取消注释后配置对应台词即可\n"
|
|
520
|
+
"# 开启标签但无台词配置则没有任何台词互动\n"
|
|
521
|
+
"# 未开启标签则会命中通用台词\n"
|
|
522
|
+
"\n", petName, petName];
|
|
523
|
+
|
|
524
|
+
for (NSString *line in [defaults componentsSeparatedByString:@"\n"]) {
|
|
525
|
+
// 已经是注释的行不再叠一层 #,双井号只是噪音。空行保持空行,
|
|
526
|
+
// 不然满屏 "#" 会把小节之间的呼吸感全填满。
|
|
527
|
+
if (line.length == 0) { [text appendString:@"\n"]; continue; }
|
|
528
|
+
[text appendFormat:[line hasPrefix:@"#"] ? @"%@\n" : @"# %@\n", line];
|
|
529
|
+
}
|
|
530
|
+
return text;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
- (void)fillFromDefault:(id)sender {
|
|
534
|
+
NSString *text = [self commentedDefaultText];
|
|
535
|
+
if (text.length == 0) {
|
|
536
|
+
[self showStatus:@"找不到出厂台词文件。" warning:YES];
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
// 空白页直接填,不打断——那正是这个按钮存在的理由。已经写了东西才问一句。
|
|
540
|
+
if ((self.textView.string ?: @"").length > 0) {
|
|
541
|
+
NSAlert *alert = [NSAlert new];
|
|
542
|
+
alert.messageText = @"覆盖这一页?";
|
|
543
|
+
alert.informativeText = @"这一页现在的内容会被整份出厂台词替换(全部是注释状态)。"
|
|
544
|
+
"还没保存,可以按 ⌘Z 撤销。";
|
|
545
|
+
[alert addButtonWithTitle:@"覆盖"];
|
|
546
|
+
[alert addButtonWithTitle:@"取消"];
|
|
547
|
+
if ([alert runModal] != NSAlertFirstButtonReturn) return;
|
|
548
|
+
}
|
|
549
|
+
// 走 shouldChangeTextInRange: 而不是直接设 string,这样这一步能被 ⌘Z 撤销。
|
|
550
|
+
NSRange all = NSMakeRange(0, self.textView.string.length);
|
|
551
|
+
if (![self.textView shouldChangeTextInRange:all replacementString:text]) return;
|
|
552
|
+
[self.textView.textStorage replaceCharactersInRange:all withString:text];
|
|
553
|
+
[self.textView didChangeText];
|
|
554
|
+
[self showStatus:@"已填入出厂台词,整份都是注释、还不生效。"
|
|
555
|
+
"把想改的那一节行首的 # 去掉,它才归这只宠物管。" warning:NO];
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
#pragma mark - 恢复出厂 / 清空
|
|
350
559
|
|
|
351
560
|
- (void)revertToDefault:(id)sender {
|
|
352
561
|
NSAlert *alert = [NSAlert new];
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
562
|
+
// 专属词库的"出厂状态"就是空白:清空 = 这只宠物全部回落到通用台词。
|
|
563
|
+
// 拿出厂词库去填它是错的——那等于把 22 个小节全部标成"专属接管",再也不会回落。
|
|
564
|
+
alert.messageText = self.petScope ? @"清空这只宠物的专属台词?" : @"恢复出厂台词?";
|
|
565
|
+
alert.informativeText = self.petScope ?
|
|
566
|
+
@"清空之后,这只宠物说的全部回到通用台词。还没保存,可以按 ⌘Z 撤销。" :
|
|
567
|
+
@"编辑器里的内容会被出厂台词替换。还没保存,可以按 ⌘Z 撤销。";
|
|
568
|
+
[alert addButtonWithTitle:self.petScope ? @"清空" : @"恢复"];
|
|
356
569
|
[alert addButtonWithTitle:@"取消"];
|
|
357
570
|
if ([alert runModal] != NSAlertFirstButtonReturn) return;
|
|
358
571
|
|
|
359
|
-
NSString *text =
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
572
|
+
NSString *text = @"";
|
|
573
|
+
if (!self.petScope) {
|
|
574
|
+
text = [NSString stringWithContentsOfFile:PetPhrasesDefaultFilePath()
|
|
575
|
+
encoding:NSUTF8StringEncoding error:nil];
|
|
576
|
+
if (text.length == 0) {
|
|
577
|
+
[self showStatus:@"找不到出厂台词文件。" warning:YES];
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
364
580
|
}
|
|
365
581
|
// 走 shouldChangeTextInRange: 而不是直接设 string,这样这一步能被 ⌘Z 撤销。
|
|
366
582
|
NSRange all = NSMakeRange(0, self.textView.string.length);
|
|
367
583
|
if (![self.textView shouldChangeTextInRange:all replacementString:text]) return;
|
|
368
584
|
[self.textView.textStorage replaceCharactersInRange:all withString:text];
|
|
369
585
|
[self.textView didChangeText];
|
|
370
|
-
[self showStatus
|
|
586
|
+
[self showStatus:self.petScope ? @"已清空,按保存才会写进文件。" :
|
|
587
|
+
@"已填入出厂台词,按保存才会写进文件。" warning:NO];
|
|
371
588
|
}
|
|
372
589
|
|
|
373
590
|
@end
|