cc-pets 2.2.1 → 2.4.0
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/CC_BRIDGE.md +24 -2
- package/CC_BRIDGE.zh-CN.md +19 -2
- package/CHANGELOG.md +111 -0
- package/CHANGELOG.zh-CN.md +111 -0
- package/CONTRIBUTING.md +35 -10
- package/CONTRIBUTING.zh-CN.md +28 -10
- package/README.md +212 -32
- package/README.zh-CN.md +167 -36
- package/Resources/AgentIcons/claude.png +0 -0
- package/Resources/AgentIcons/codex.icns +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.vsix +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.zip +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +699 -20
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +90 -25
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +17 -3
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +71 -23
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/{CCPetsAppDelegate+Settings.m → App/CCPetsAppDelegate+Settings.m} +87 -102
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +121 -51
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +17 -2
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +107 -11
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +74 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +43 -23
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +832 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +59 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +576 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +507 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +106 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +897 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
- package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.h +21 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.m +503 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +367 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +2 -1
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +148 -36
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.m +64 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.h +23 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.m +475 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
- package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +11 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +19 -2
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +4 -1
- package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
- package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
- package/Sources/CCPets/Pet/PetView+Private.h +81 -0
- package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
- package/Sources/CCPets/Pet/PetView.m +635 -0
- package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
- package/Sources/CCPets/Support/CCPetsDoctor.m +646 -0
- package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
- package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
- package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
- package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +33 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +117 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +44 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +279 -0
- package/Sources/CCPets/UI/CCPetsGlassView.h +57 -0
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +60 -11
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +64 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +1412 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +135 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +865 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +288 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +712 -0
- package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
- package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
- package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
- package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
- package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
- package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexBackground.m +193 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +97 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +323 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +174 -19
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +598 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +713 -0
- package/THIRD_PARTY_NOTICES.md +8 -4
- package/THIRD_PARTY_NOTICES.zh-CN.md +6 -2
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +12 -2
- package/bin/codex-with-pet +15 -5
- package/package.json +5 -1
- package/plugins/jetbrains/README.md +62 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalBridge.java +379 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalIdentity.java +24 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalStartup.java +12 -0
- package/plugins/jetbrains/src/main/resources/META-INF/plugin.xml +23 -0
- package/plugins/jetbrains/src/main/resources/META-INF/pluginIcon.svg +380 -0
- package/plugins/vscode/README.md +63 -0
- package/plugins/vscode/extension.js +210 -0
- package/plugins/vscode/package.json +18 -0
- package/scripts/build-idea-plugin.sh +101 -0
- package/scripts/build-vscode-extension.sh +112 -0
- package/scripts/build.sh +31 -5
- package/scripts/compile-cached.zsh +133 -0
- package/scripts/i18n.zsh +9 -52
- package/scripts/install-app.sh +4 -0
- package/scripts/install-shell-integration.sh +17 -10
- package/scripts/uninstall-shell-integration.sh +6 -1
- package/Sources/CCPets/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsGlassView.h +0 -31
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1839
- package/Sources/CCPets/QuotaDashboardView.m +0 -1145
- package/scripts/bridge/cli.mjs +0 -237
- package/scripts/bridge/core.mjs +0 -228
- package/scripts/bridge/hook.mjs +0 -150
- package/scripts/bridge/install.mjs +0 -311
- package/scripts/bridge/mcp-server.mjs +0 -308
- package/scripts/bridge/options.mjs +0 -87
- package/scripts/bridge/process.mjs +0 -151
- package/scripts/bridge/reservations.mjs +0 -248
- package/scripts/bridge/store.mjs +0 -433
- package/scripts/bridge/watch.mjs +0 -69
- package/scripts/check-l10n.mjs +0 -168
- package/scripts/configure-updater.mjs +0 -43
- package/scripts/detect-cli.mjs +0 -68
- package/scripts/doctor.mjs +0 -470
- package/scripts/i18n.mjs +0 -122
- package/scripts/install-claude-hooks.mjs +0 -253
- package/scripts/install-codex-hooks.mjs +0 -78
- package/scripts/integration-markers.mjs +0 -51
- package/scripts/locales/zh-Hans.json +0 -366
- package/scripts/pet-store.mjs +0 -415
- package/scripts/uninstall-integrations.mjs +0 -205
- /package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.h → Pet/CCPetsImageLoader.h} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.h → Pet/CCPetsPetSize.h} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.m → Pet/CCPetsPetSize.m} +0 -0
- /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
- /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +0 -0
- /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.h → UI/MenuPetSizeView.h} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.m → UI/MenuPetSizeView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// 保序 JSON,用于改写用户自己的配置文件(~/.claude/settings.json、~/.codex/hooks.json)。
|
|
4
|
+
// NSJSONSerialization 不保留键顺序,输出格式也和 JSON.stringify 不同("key" : value、转义 /),
|
|
5
|
+
// 拿它回写会把用户的文件整个打乱。这里的约定与原先 Node 安装器的 JSON.parse + JSON.stringify(…, null, 2)
|
|
6
|
+
// 一致:对象按键的首次出现顺序输出,重复键后者覆盖前者的值但不改变位置。
|
|
7
|
+
//
|
|
8
|
+
// 值的表示:对象 CCPetsJSONObject、数组 NSMutableArray、字符串 NSString、null NSNull、
|
|
9
|
+
// 布尔 @YES/@NO、解析出的数字 CCPetsJSONNumber(保留原文字面量,不经过 double 往返)。
|
|
10
|
+
// 代码里新建的数字可以直接用非布尔的 NSNumber;普通 NSDictionary 也能写出,键按字母序排列。
|
|
11
|
+
|
|
12
|
+
@interface CCPetsJSONObject : NSObject <NSCopying>
|
|
13
|
+
@property (nonatomic, readonly) NSArray<NSString *> *keys;
|
|
14
|
+
- (id)objectForKeyedSubscript:(NSString *)key;
|
|
15
|
+
// 已有的键原位替换值,新键追加到末尾,与 JS 对象的属性顺序规则相同。
|
|
16
|
+
- (void)setObject:(id)value forKeyedSubscript:(NSString *)key;
|
|
17
|
+
- (void)removeObjectForKey:(NSString *)key;
|
|
18
|
+
@end
|
|
19
|
+
|
|
20
|
+
@interface CCPetsJSONNumber : NSObject
|
|
21
|
+
@property (nonatomic, readonly, copy) NSString *literal;
|
|
22
|
+
@end
|
|
23
|
+
|
|
24
|
+
// 解析失败返回 nil,并通过 errorMessage 给出出错位置。
|
|
25
|
+
id CCPetsJSONParse(NSData *data, NSString **errorMessage);
|
|
26
|
+
// 等价于 JSON.stringify(value, null, 2)。
|
|
27
|
+
NSString *CCPetsJSONStringify(id value);
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
#import "CCPetsJSON.h"
|
|
2
|
+
|
|
3
|
+
@implementation CCPetsJSONObject {
|
|
4
|
+
NSMutableArray<NSString *> *_keys;
|
|
5
|
+
NSMutableDictionary<NSString *, id> *_values;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
- (instancetype)init {
|
|
9
|
+
if ((self = [super init])) {
|
|
10
|
+
_keys = [NSMutableArray array];
|
|
11
|
+
_values = [NSMutableDictionary dictionary];
|
|
12
|
+
}
|
|
13
|
+
return self;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
- (NSArray<NSString *> *)keys {
|
|
17
|
+
return [_keys copy];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
- (id)objectForKeyedSubscript:(NSString *)key {
|
|
21
|
+
return _values[key];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
- (void)setObject:(id)value forKeyedSubscript:(NSString *)key {
|
|
25
|
+
if (!_values[key]) [_keys addObject:key];
|
|
26
|
+
_values[key] = value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
- (void)removeObjectForKey:(NSString *)key {
|
|
30
|
+
if (!_values[key]) return;
|
|
31
|
+
[_keys removeObject:key];
|
|
32
|
+
[_values removeObjectForKey:key];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 浅拷贝,对应 JS 的 { ...object }。
|
|
36
|
+
- (id)copyWithZone:(NSZone *)zone {
|
|
37
|
+
CCPetsJSONObject *copy = [CCPetsJSONObject new];
|
|
38
|
+
for (NSString *key in _keys) copy[key] = _values[key];
|
|
39
|
+
return copy;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@end
|
|
43
|
+
|
|
44
|
+
@implementation CCPetsJSONNumber
|
|
45
|
+
|
|
46
|
+
- (instancetype)initWithLiteral:(NSString *)literal {
|
|
47
|
+
if ((self = [super init])) _literal = [literal copy];
|
|
48
|
+
return self;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@end
|
|
52
|
+
|
|
53
|
+
// 按 UTF-16 码元解析,和 JS 字符串的模型一致:\uD800 这类孤立代理项也能原样保留。
|
|
54
|
+
typedef struct {
|
|
55
|
+
const unichar *characters;
|
|
56
|
+
NSUInteger length;
|
|
57
|
+
NSUInteger index;
|
|
58
|
+
NSUInteger depth;
|
|
59
|
+
NSString *error;
|
|
60
|
+
} JSONParser;
|
|
61
|
+
|
|
62
|
+
static const NSUInteger JSONMaximumDepth = 512;
|
|
63
|
+
|
|
64
|
+
static void JSONFail(JSONParser *parser, NSString *reason) {
|
|
65
|
+
if (!parser->error) {
|
|
66
|
+
parser->error = [NSString stringWithFormat:@"%@ at position %lu", reason, (unsigned long)parser->index];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static void JSONSkipWhitespace(JSONParser *parser) {
|
|
71
|
+
while (parser->index < parser->length) {
|
|
72
|
+
unichar character = parser->characters[parser->index];
|
|
73
|
+
if (character != ' ' && character != '\t' && character != '\n' && character != '\r') return;
|
|
74
|
+
parser->index++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static BOOL JSONConsumeLiteral(JSONParser *parser, NSString *literal) {
|
|
79
|
+
NSUInteger length = literal.length;
|
|
80
|
+
if (parser->index + length > parser->length) return NO;
|
|
81
|
+
for (NSUInteger offset = 0; offset < length; offset++) {
|
|
82
|
+
if (parser->characters[parser->index + offset] != [literal characterAtIndex:offset]) return NO;
|
|
83
|
+
}
|
|
84
|
+
parser->index += length;
|
|
85
|
+
return YES;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static int JSONHexValue(unichar character) {
|
|
89
|
+
if (character >= '0' && character <= '9') return character - '0';
|
|
90
|
+
if (character >= 'a' && character <= 'f') return character - 'a' + 10;
|
|
91
|
+
if (character >= 'A' && character <= 'F') return character - 'A' + 10;
|
|
92
|
+
return -1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
static NSString *JSONParseString(JSONParser *parser) {
|
|
96
|
+
parser->index++;
|
|
97
|
+
NSMutableString *result = [NSMutableString string];
|
|
98
|
+
while (parser->index < parser->length) {
|
|
99
|
+
unichar character = parser->characters[parser->index++];
|
|
100
|
+
if (character == '"') return result;
|
|
101
|
+
if (character < 0x20) {
|
|
102
|
+
parser->index--;
|
|
103
|
+
JSONFail(parser, @"Bad control character in string literal");
|
|
104
|
+
return nil;
|
|
105
|
+
}
|
|
106
|
+
if (character != '\\') {
|
|
107
|
+
[result appendFormat:@"%C", character];
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (parser->index >= parser->length) break;
|
|
111
|
+
unichar escape = parser->characters[parser->index++];
|
|
112
|
+
switch (escape) {
|
|
113
|
+
case '"': [result appendString:@"\""]; break;
|
|
114
|
+
case '\\': [result appendString:@"\\"]; break;
|
|
115
|
+
case '/': [result appendString:@"/"]; break;
|
|
116
|
+
case 'b': [result appendString:@"\b"]; break;
|
|
117
|
+
case 'f': [result appendString:@"\f"]; break;
|
|
118
|
+
case 'n': [result appendString:@"\n"]; break;
|
|
119
|
+
case 'r': [result appendString:@"\r"]; break;
|
|
120
|
+
case 't': [result appendString:@"\t"]; break;
|
|
121
|
+
case 'u': {
|
|
122
|
+
if (parser->index + 4 > parser->length) {
|
|
123
|
+
JSONFail(parser, @"Bad Unicode escape");
|
|
124
|
+
return nil;
|
|
125
|
+
}
|
|
126
|
+
unichar value = 0;
|
|
127
|
+
for (NSUInteger offset = 0; offset < 4; offset++) {
|
|
128
|
+
int digit = JSONHexValue(parser->characters[parser->index + offset]);
|
|
129
|
+
if (digit < 0) {
|
|
130
|
+
JSONFail(parser, @"Bad Unicode escape");
|
|
131
|
+
return nil;
|
|
132
|
+
}
|
|
133
|
+
value = (unichar)(value * 16 + digit);
|
|
134
|
+
}
|
|
135
|
+
parser->index += 4;
|
|
136
|
+
[result appendFormat:@"%C", value];
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
default:
|
|
140
|
+
parser->index--;
|
|
141
|
+
JSONFail(parser, @"Bad escaped character");
|
|
142
|
+
return nil;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
JSONFail(parser, @"Unterminated string");
|
|
146
|
+
return nil;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static BOOL JSONIsDigit(JSONParser *parser) {
|
|
150
|
+
if (parser->index >= parser->length) return NO;
|
|
151
|
+
unichar character = parser->characters[parser->index];
|
|
152
|
+
return character >= '0' && character <= '9';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static CCPetsJSONNumber *JSONParseNumber(JSONParser *parser) {
|
|
156
|
+
NSUInteger start = parser->index;
|
|
157
|
+
if (parser->characters[parser->index] == '-') parser->index++;
|
|
158
|
+
if (!JSONIsDigit(parser)) {
|
|
159
|
+
JSONFail(parser, @"No number after minus sign");
|
|
160
|
+
return nil;
|
|
161
|
+
}
|
|
162
|
+
if (parser->characters[parser->index] == '0') {
|
|
163
|
+
parser->index++;
|
|
164
|
+
} else {
|
|
165
|
+
while (JSONIsDigit(parser)) parser->index++;
|
|
166
|
+
}
|
|
167
|
+
if (parser->index < parser->length && parser->characters[parser->index] == '.') {
|
|
168
|
+
parser->index++;
|
|
169
|
+
if (!JSONIsDigit(parser)) {
|
|
170
|
+
JSONFail(parser, @"Unterminated fractional number");
|
|
171
|
+
return nil;
|
|
172
|
+
}
|
|
173
|
+
while (JSONIsDigit(parser)) parser->index++;
|
|
174
|
+
}
|
|
175
|
+
if (parser->index < parser->length &&
|
|
176
|
+
(parser->characters[parser->index] == 'e' || parser->characters[parser->index] == 'E')) {
|
|
177
|
+
parser->index++;
|
|
178
|
+
if (parser->index < parser->length &&
|
|
179
|
+
(parser->characters[parser->index] == '+' || parser->characters[parser->index] == '-')) {
|
|
180
|
+
parser->index++;
|
|
181
|
+
}
|
|
182
|
+
if (!JSONIsDigit(parser)) {
|
|
183
|
+
JSONFail(parser, @"Exponent part is missing a number");
|
|
184
|
+
return nil;
|
|
185
|
+
}
|
|
186
|
+
while (JSONIsDigit(parser)) parser->index++;
|
|
187
|
+
}
|
|
188
|
+
NSString *literal = [NSString stringWithCharacters:parser->characters + start length:parser->index - start];
|
|
189
|
+
return [[CCPetsJSONNumber alloc] initWithLiteral:literal];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static id JSONParseValue(JSONParser *parser);
|
|
193
|
+
|
|
194
|
+
static id JSONParseArray(JSONParser *parser) {
|
|
195
|
+
parser->index++;
|
|
196
|
+
NSMutableArray *array = [NSMutableArray array];
|
|
197
|
+
JSONSkipWhitespace(parser);
|
|
198
|
+
if (parser->index < parser->length && parser->characters[parser->index] == ']') {
|
|
199
|
+
parser->index++;
|
|
200
|
+
return array;
|
|
201
|
+
}
|
|
202
|
+
while (YES) {
|
|
203
|
+
id value = JSONParseValue(parser);
|
|
204
|
+
if (!value) return nil;
|
|
205
|
+
[array addObject:value];
|
|
206
|
+
JSONSkipWhitespace(parser);
|
|
207
|
+
if (parser->index >= parser->length) break;
|
|
208
|
+
unichar character = parser->characters[parser->index++];
|
|
209
|
+
if (character == ']') return array;
|
|
210
|
+
if (character != ',') {
|
|
211
|
+
parser->index--;
|
|
212
|
+
JSONFail(parser, @"Expected ',' or ']' after array element");
|
|
213
|
+
return nil;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
JSONFail(parser, @"Unterminated array");
|
|
217
|
+
return nil;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
static id JSONParseObject(JSONParser *parser) {
|
|
221
|
+
parser->index++;
|
|
222
|
+
CCPetsJSONObject *object = [CCPetsJSONObject new];
|
|
223
|
+
JSONSkipWhitespace(parser);
|
|
224
|
+
if (parser->index < parser->length && parser->characters[parser->index] == '}') {
|
|
225
|
+
parser->index++;
|
|
226
|
+
return object;
|
|
227
|
+
}
|
|
228
|
+
while (YES) {
|
|
229
|
+
JSONSkipWhitespace(parser);
|
|
230
|
+
if (parser->index >= parser->length || parser->characters[parser->index] != '"') {
|
|
231
|
+
JSONFail(parser, @"Expected double-quoted property name");
|
|
232
|
+
return nil;
|
|
233
|
+
}
|
|
234
|
+
NSString *key = JSONParseString(parser);
|
|
235
|
+
if (!key) return nil;
|
|
236
|
+
JSONSkipWhitespace(parser);
|
|
237
|
+
if (parser->index >= parser->length || parser->characters[parser->index] != ':') {
|
|
238
|
+
JSONFail(parser, @"Expected ':' after property name");
|
|
239
|
+
return nil;
|
|
240
|
+
}
|
|
241
|
+
parser->index++;
|
|
242
|
+
id value = JSONParseValue(parser);
|
|
243
|
+
if (!value) return nil;
|
|
244
|
+
object[key] = value;
|
|
245
|
+
JSONSkipWhitespace(parser);
|
|
246
|
+
if (parser->index >= parser->length) break;
|
|
247
|
+
unichar character = parser->characters[parser->index++];
|
|
248
|
+
if (character == '}') return object;
|
|
249
|
+
if (character != ',') {
|
|
250
|
+
parser->index--;
|
|
251
|
+
JSONFail(parser, @"Expected ',' or '}' after property value");
|
|
252
|
+
return nil;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
JSONFail(parser, @"Unterminated object");
|
|
256
|
+
return nil;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
static id JSONParseValue(JSONParser *parser) {
|
|
260
|
+
JSONSkipWhitespace(parser);
|
|
261
|
+
if (parser->index >= parser->length) {
|
|
262
|
+
JSONFail(parser, @"Unexpected end of JSON input");
|
|
263
|
+
return nil;
|
|
264
|
+
}
|
|
265
|
+
unichar character = parser->characters[parser->index];
|
|
266
|
+
if (character == '{' || character == '[') {
|
|
267
|
+
if (++parser->depth > JSONMaximumDepth) {
|
|
268
|
+
JSONFail(parser, @"Nesting too deep");
|
|
269
|
+
return nil;
|
|
270
|
+
}
|
|
271
|
+
id value = character == '{' ? JSONParseObject(parser) : JSONParseArray(parser);
|
|
272
|
+
parser->depth--;
|
|
273
|
+
return value;
|
|
274
|
+
}
|
|
275
|
+
if (character == '"') return JSONParseString(parser);
|
|
276
|
+
if (character == '-' || (character >= '0' && character <= '9')) return JSONParseNumber(parser);
|
|
277
|
+
if (JSONConsumeLiteral(parser, @"true")) return @YES;
|
|
278
|
+
if (JSONConsumeLiteral(parser, @"false")) return @NO;
|
|
279
|
+
if (JSONConsumeLiteral(parser, @"null")) return NSNull.null;
|
|
280
|
+
JSONFail(parser, [NSString stringWithFormat:@"Unexpected token '%C'", character]);
|
|
281
|
+
return nil;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
id CCPetsJSONParse(NSData *data, NSString **errorMessage) {
|
|
285
|
+
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
286
|
+
if (!text) {
|
|
287
|
+
if (errorMessage) *errorMessage = @"File is not valid UTF-8";
|
|
288
|
+
return nil;
|
|
289
|
+
}
|
|
290
|
+
// 编辑器偶尔会写入 BOM。它不属于 JSON,跳过比整份配置拒收更合理。
|
|
291
|
+
if ([text hasPrefix:@""]) text = [text substringFromIndex:1];
|
|
292
|
+
NSUInteger length = text.length;
|
|
293
|
+
unichar *characters = malloc(MAX(length, 1) * sizeof(unichar));
|
|
294
|
+
[text getCharacters:characters range:NSMakeRange(0, length)];
|
|
295
|
+
JSONParser parser = {characters, length, 0, 0, nil};
|
|
296
|
+
id value = JSONParseValue(&parser);
|
|
297
|
+
if (value) {
|
|
298
|
+
JSONSkipWhitespace(&parser);
|
|
299
|
+
if (parser.index < parser.length) {
|
|
300
|
+
JSONFail(&parser, @"Unexpected non-whitespace character after JSON");
|
|
301
|
+
value = nil;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
free(characters);
|
|
305
|
+
if (!value && errorMessage) *errorMessage = parser.error ?: @"Invalid JSON";
|
|
306
|
+
return value;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// 与 JSON.stringify 的转义规则一致:只转义引号、反斜杠和控制字符,孤立代理项写成 \uXXXX,
|
|
310
|
+
// 其余(包括中文、/、U+2028)原样输出。
|
|
311
|
+
static void JSONAppendString(NSMutableString *output, NSString *string) {
|
|
312
|
+
[output appendString:@"\""];
|
|
313
|
+
NSUInteger length = string.length;
|
|
314
|
+
for (NSUInteger index = 0; index < length; index++) {
|
|
315
|
+
unichar character = [string characterAtIndex:index];
|
|
316
|
+
switch (character) {
|
|
317
|
+
case '"': [output appendString:@"\\\""]; continue;
|
|
318
|
+
case '\\': [output appendString:@"\\\\"]; continue;
|
|
319
|
+
case '\b': [output appendString:@"\\b"]; continue;
|
|
320
|
+
case '\f': [output appendString:@"\\f"]; continue;
|
|
321
|
+
case '\n': [output appendString:@"\\n"]; continue;
|
|
322
|
+
case '\r': [output appendString:@"\\r"]; continue;
|
|
323
|
+
case '\t': [output appendString:@"\\t"]; continue;
|
|
324
|
+
default: break;
|
|
325
|
+
}
|
|
326
|
+
if (character < 0x20) {
|
|
327
|
+
[output appendFormat:@"\\u%04x", character];
|
|
328
|
+
} else if (CFStringIsSurrogateHighCharacter(character) && index + 1 < length &&
|
|
329
|
+
CFStringIsSurrogateLowCharacter([string characterAtIndex:index + 1])) {
|
|
330
|
+
[output appendFormat:@"%C%C", character, [string characterAtIndex:index + 1]];
|
|
331
|
+
index++;
|
|
332
|
+
} else if (CFStringIsSurrogateHighCharacter(character) || CFStringIsSurrogateLowCharacter(character)) {
|
|
333
|
+
[output appendFormat:@"\\u%04x", character];
|
|
334
|
+
} else {
|
|
335
|
+
[output appendFormat:@"%C", character];
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
[output appendString:@"\""];
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
static void JSONAppendValue(NSMutableString *output, id value, NSString *indent) {
|
|
342
|
+
NSString *inner = [indent stringByAppendingString:@" "];
|
|
343
|
+
if ([value isKindOfClass:CCPetsJSONObject.class]) {
|
|
344
|
+
CCPetsJSONObject *object = value;
|
|
345
|
+
NSArray<NSString *> *keys = object.keys;
|
|
346
|
+
if (keys.count == 0) {
|
|
347
|
+
[output appendString:@"{}"];
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
[output appendString:@"{\n"];
|
|
351
|
+
[keys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger index, BOOL *stop) {
|
|
352
|
+
[output appendString:inner];
|
|
353
|
+
JSONAppendString(output, key);
|
|
354
|
+
[output appendString:@": "];
|
|
355
|
+
JSONAppendValue(output, object[key], inner);
|
|
356
|
+
[output appendString:index + 1 < keys.count ? @",\n" : @"\n"];
|
|
357
|
+
}];
|
|
358
|
+
[output appendFormat:@"%@}", indent];
|
|
359
|
+
} else if ([value isKindOfClass:NSDictionary.class]) {
|
|
360
|
+
// 普通字典没有顺序,按键排序输出,至少保证同样的内容写出来一模一样。
|
|
361
|
+
CCPetsJSONObject *object = [CCPetsJSONObject new];
|
|
362
|
+
NSDictionary *dictionary = value;
|
|
363
|
+
for (NSString *key in [dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)]) object[key] = dictionary[key];
|
|
364
|
+
JSONAppendValue(output, object, indent);
|
|
365
|
+
} else if ([value isKindOfClass:NSArray.class]) {
|
|
366
|
+
NSArray *array = value;
|
|
367
|
+
if (array.count == 0) {
|
|
368
|
+
[output appendString:@"[]"];
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
[output appendString:@"[\n"];
|
|
372
|
+
[array enumerateObjectsUsingBlock:^(id element, NSUInteger index, BOOL *stop) {
|
|
373
|
+
[output appendString:inner];
|
|
374
|
+
JSONAppendValue(output, element, inner);
|
|
375
|
+
[output appendString:index + 1 < array.count ? @",\n" : @"\n"];
|
|
376
|
+
}];
|
|
377
|
+
[output appendFormat:@"%@]", indent];
|
|
378
|
+
} else if ([value isKindOfClass:NSString.class]) {
|
|
379
|
+
JSONAppendString(output, value);
|
|
380
|
+
} else if ([value isKindOfClass:CCPetsJSONNumber.class]) {
|
|
381
|
+
[output appendString:((CCPetsJSONNumber *)value).literal];
|
|
382
|
+
} else if ([value isKindOfClass:NSNumber.class]) {
|
|
383
|
+
if (CFGetTypeID((__bridge CFTypeRef)value) == CFBooleanGetTypeID()) {
|
|
384
|
+
[output appendString:[value boolValue] ? @"true" : @"false"];
|
|
385
|
+
} else {
|
|
386
|
+
[output appendString:[value stringValue]];
|
|
387
|
+
}
|
|
388
|
+
} else {
|
|
389
|
+
[output appendString:@"null"];
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
NSString *CCPetsJSONStringify(id value) {
|
|
394
|
+
NSMutableString *output = [NSMutableString string];
|
|
395
|
+
JSONAppendValue(output, value, @"");
|
|
396
|
+
return output;
|
|
397
|
+
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// - 其他语言查 <语言>.lproj/Localizable.strings,查不到就退回英文原文。
|
|
8
8
|
// 英文是源语言:漏翻的条目显示英文,比让别的语言用户看到某种第三语言好接受;
|
|
9
9
|
// 新语言也从英文译。代价是改了英文原文要同步改各语言表的 key,
|
|
10
|
-
//
|
|
10
|
+
// tests/check-l10n.mjs 会把漏掉的报出来。
|
|
11
11
|
//
|
|
12
12
|
// 带参数的文案照常写 %@ / %ld;参数顺序和译文语序不同时,两边都用 %1$@ / %2$@ 标位置。
|
|
13
13
|
//
|
|
@@ -40,6 +40,8 @@ void CCPetsSetLanguagePreference(NSString *preference);
|
|
|
40
40
|
// 环境变量 CC_PETS_LANGUAGE 优先(测试用,也方便临时切换),其次是偏好,最后跟随系统:
|
|
41
41
|
// 系统首选语言里第一个能匹配上的说了算,都匹配不上就用源语言。
|
|
42
42
|
NSString *CCPetsCurrentLanguage(void);
|
|
43
|
+
// 命令行子命令用:语言按 CC_PETS_LANGUAGE → ~/.cc-pets/language → LC_ALL / LC_MESSAGES / LANG → 系统 决定。
|
|
44
|
+
void CCPetsUseCommandLineLanguage(BOOL enabled);
|
|
43
45
|
BOOL CCPetsLanguageIsSource(void);
|
|
44
46
|
// 按给定的支持列表解析,不读任何全局状态,测试用。匹配规则:先找完全一致或前缀一致的
|
|
45
47
|
// (zh-Hans-CN → zh-Hans),再退到只比主语言(zh-Hant → zh-Hans、ja_JP → ja)。
|
|
@@ -156,10 +156,38 @@ static NSArray<NSString *> *CCPetsSystemLanguages(void) {
|
|
|
156
156
|
return NSLocale.preferredLanguages;
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
+
static BOOL CCPetsCommandLineMode;
|
|
160
|
+
|
|
161
|
+
void CCPetsUseCommandLineLanguage(BOOL enabled) {
|
|
162
|
+
@synchronized(CCPetsLanguageKey) {
|
|
163
|
+
CCPetsCommandLineMode = enabled;
|
|
164
|
+
CCPetsCachedLanguage = nil;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 命令行:桌宠写下的语言文件优先(终端输出与桌宠界面同一种语言),其次是 locale 环境变量,最后跟随系统。
|
|
169
|
+
// 裸跑的二进制和 App 用的不是同一个 defaults 域,读不到 App 里选的语言偏好,所以不看偏好。
|
|
170
|
+
static NSString *CCPetsCommandLineLanguage(NSArray<NSString *> *supported) {
|
|
171
|
+
NSString *file = [NSString stringWithContentsOfFile:CCPetsLanguageFilePath() encoding:NSUTF8StringEncoding error:nil];
|
|
172
|
+
NSString *match = CCPetsMatchLanguage([file stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet], supported);
|
|
173
|
+
if (match) return match;
|
|
174
|
+
NSDictionary<NSString *, NSString *> *environment = NSProcessInfo.processInfo.environment;
|
|
175
|
+
for (NSString *name in @[@"LC_ALL", @"LC_MESSAGES", @"LANG"]) {
|
|
176
|
+
match = CCPetsMatchLanguage(environment[name], supported);
|
|
177
|
+
if (match) return match;
|
|
178
|
+
}
|
|
179
|
+
return CCPetsResolveLanguageAmong(CCPetsLanguageSystem, CCPetsSystemLanguages(), supported);
|
|
180
|
+
}
|
|
181
|
+
|
|
159
182
|
NSString *CCPetsCurrentLanguage(void) {
|
|
160
183
|
@synchronized(CCPetsLanguageKey) {
|
|
161
184
|
if (CCPetsCachedLanguage) return CCPetsCachedLanguage;
|
|
162
185
|
NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_LANGUAGE"];
|
|
186
|
+
if (CCPetsCommandLineMode) {
|
|
187
|
+
CCPetsCachedLanguage = CCPetsMatchLanguage(override, CCPetsSupportedLanguages()) ?:
|
|
188
|
+
CCPetsCommandLineLanguage(CCPetsSupportedLanguages());
|
|
189
|
+
return CCPetsCachedLanguage;
|
|
190
|
+
}
|
|
163
191
|
NSString *preference = override.length > 0 ? override : CCPetsLanguagePreference();
|
|
164
192
|
CCPetsCachedLanguage = CCPetsResolveLanguageAmong(preference, CCPetsSystemLanguages(),
|
|
165
193
|
CCPetsSupportedLanguages());
|
|
@@ -182,7 +210,8 @@ NSString *CCPetsLocalized(NSString *text) {
|
|
|
182
210
|
NSString *CCPetsLanguageFilePath(void) {
|
|
183
211
|
NSString *home = NSProcessInfo.processInfo.environment[@"CC_PETS_HOME"];
|
|
184
212
|
if (home.length == 0) {
|
|
185
|
-
|
|
213
|
+
NSString *userHome = NSProcessInfo.processInfo.environment[@"HOME"];
|
|
214
|
+
home = [(userHome.length > 0 ? userHome : NSHomeDirectory()) stringByAppendingPathComponent:@".cc-pets"];
|
|
186
215
|
}
|
|
187
216
|
return [home.stringByStandardizingPath stringByAppendingPathComponent:@"language"];
|
|
188
217
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// 用户设置的统一写入口。右键菜单和主窗口设置页都走这里,保证两边写进去的值经过同一套
|
|
4
|
+
// 钳制,且任何一边改了都会发出 CCPetsPreferencesDidChangeNotification,另一边据此刷新。
|
|
5
|
+
//
|
|
6
|
+
// 只管 defaults 本身:写值、规整、广播。改完之后要让界面生效(换主题、重排气泡、重新聚合
|
|
7
|
+
// 用量……)是 AppDelegate (Settings) 里 update* 方法的事,这一层不认识任何视图。
|
|
8
|
+
|
|
9
|
+
// userInfo[CCPetsPreferencesChangedKeyKey] 是被改的 defaults 键。只有值真的变了才发。
|
|
10
|
+
extern NSNotificationName const CCPetsPreferencesDidChangeNotification;
|
|
11
|
+
extern NSString *const CCPetsPreferencesChangedKeyKey;
|
|
12
|
+
|
|
13
|
+
extern NSString *const PetSpeechEnabledKey;
|
|
14
|
+
extern NSString *const PetInteractionEnabledKey;
|
|
15
|
+
extern NSString *const PetInteractionHeartThresholdKey;
|
|
16
|
+
extern NSString *const PetInteractionAnnoyedThresholdKey;
|
|
17
|
+
extern NSString *const PetInteractionIntervalKey;
|
|
18
|
+
// 启动保护按代理软件分别保存;增加软件时定义独立的布尔键。
|
|
19
|
+
extern NSString *const CCPetsShadowrocketLaunchCheckKey;
|
|
20
|
+
|
|
21
|
+
// 布尔设置。碎碎念这类"没写过就算开"的键在这里统一兜底,调用方不必各自判 nil。
|
|
22
|
+
BOOL CCPetsPrefBool(NSString *key);
|
|
23
|
+
void CCPetsPrefSetBool(NSString *key, BOOL value);
|
|
24
|
+
|
|
25
|
+
// 写字符串或数字。数值键会先钳到合法范围(宠物缩放、点击互动阈值与间隔);
|
|
26
|
+
// 规整后不合法(NaN 之类)时不写,返回 NO。value 为 nil 表示删除该键,交回默认值。
|
|
27
|
+
BOOL CCPetsPrefSetObject(NSString *key, id value);
|
|
28
|
+
|
|
29
|
+
// 规整后的值,不写入。供测试和需要回显钳制结果的控件使用;不合法时返回 nil。
|
|
30
|
+
id CCPetsPrefNormalizedValue(NSString *key, id value);
|
|
31
|
+
|
|
32
|
+
// 不经过上面两个 setter、由其他模块自己写 defaults 的键(台词来源、界面语言)改完后补发广播。
|
|
33
|
+
void CCPetsPrefPostChange(NSString *key);
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
#import "CCPetsPreferences.h"
|
|
2
|
+
#import "CCPetsPetSize.h"
|
|
3
|
+
#import <math.h>
|
|
4
|
+
|
|
5
|
+
NSNotificationName const CCPetsPreferencesDidChangeNotification = @"CCPetsPreferencesDidChange";
|
|
6
|
+
NSString *const CCPetsPreferencesChangedKeyKey = @"key";
|
|
7
|
+
|
|
8
|
+
NSString *const PetSpeechEnabledKey = @"CCPetsSpeechEnabled";
|
|
9
|
+
NSString *const PetInteractionEnabledKey = @"CCPetsInteractionEnabled";
|
|
10
|
+
NSString *const PetInteractionHeartThresholdKey = @"CCPetsInteractionHeartThreshold";
|
|
11
|
+
NSString *const PetInteractionAnnoyedThresholdKey = @"CCPetsInteractionAnnoyedThreshold";
|
|
12
|
+
NSString *const PetInteractionIntervalKey = @"CCPetsInteractionInterval";
|
|
13
|
+
NSString *const CCPetsShadowrocketLaunchCheckKey = @"CCPetsLaunchProtection.Shadowrocket";
|
|
14
|
+
|
|
15
|
+
// 包装器运行的是裸二进制,默认 preferences domain 与 App bundle 不同。
|
|
16
|
+
// 启动保护的读取和写入统一走 CFPreferences 的同一个域、用户和主机。
|
|
17
|
+
// 不混用临时 suite 的写入与 standardUserDefaults 的快照回读,避免控件刷新后弹回关闭。
|
|
18
|
+
static NSString *LaunchPreferencesDomain(void) {
|
|
19
|
+
NSString *domain = NSProcessInfo.processInfo.environment[@"CC_PETS_PREFERENCES_DOMAIN"];
|
|
20
|
+
return domain.length ? domain : @"com.universewang.cc-pets";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static BOOL IsLaunchProtectionKey(NSString *key) {
|
|
24
|
+
return [key hasPrefix:@"CCPetsLaunchProtection."];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static id LaunchProtectionValue(NSString *key) {
|
|
28
|
+
return CFBridgingRelease(CFPreferencesCopyValue((__bridge CFStringRef)key,
|
|
29
|
+
(__bridge CFStringRef)LaunchPreferencesDomain(),
|
|
30
|
+
kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 没有注册默认值、缺省又是开启的键。碎碎念从第一版起就是"读不到就算开",
|
|
34
|
+
// 改成 registerDefaults 的话 CLI 子命令(没走 applicationDidFinishLaunching)会读成关。
|
|
35
|
+
static BOOL DefaultsToEnabled(NSString *key) {
|
|
36
|
+
return [key isEqualToString:PetSpeechEnabledKey];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
BOOL CCPetsPrefBool(NSString *key) {
|
|
40
|
+
id value = IsLaunchProtectionKey(key)
|
|
41
|
+
? LaunchProtectionValue(key)
|
|
42
|
+
: [NSUserDefaults.standardUserDefaults objectForKey:key];
|
|
43
|
+
// 兼容第一版总开关;一旦保存了 Shadowrocket 的独立开关(包括关闭),就以新值为准。
|
|
44
|
+
if (value == nil && [key isEqualToString:CCPetsShadowrocketLaunchCheckKey]) {
|
|
45
|
+
value = LaunchProtectionValue(@"CCPetsLaunchProxyCheck");
|
|
46
|
+
}
|
|
47
|
+
if (value == nil) return DefaultsToEnabled(key);
|
|
48
|
+
return [value respondsToSelector:@selector(boolValue)] ? [value boolValue] : NO;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
void CCPetsPrefPostChange(NSString *key) {
|
|
52
|
+
if (key.length == 0) return;
|
|
53
|
+
[NSNotificationCenter.defaultCenter
|
|
54
|
+
postNotificationName:CCPetsPreferencesDidChangeNotification object:nil
|
|
55
|
+
userInfo:@{CCPetsPreferencesChangedKeyKey: key}];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
void CCPetsPrefSetBool(NSString *key, BOOL value) {
|
|
59
|
+
if (key.length == 0) return;
|
|
60
|
+
BOOL changed = CCPetsPrefBool(key) != value;
|
|
61
|
+
if (IsLaunchProtectionKey(key)) {
|
|
62
|
+
CFStringRef domain = (__bridge CFStringRef)LaunchPreferencesDomain();
|
|
63
|
+
CFPreferencesSetValue((__bridge CFStringRef)key, value ? kCFBooleanTrue : kCFBooleanFalse,
|
|
64
|
+
domain, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
|
|
65
|
+
CFPreferencesSynchronize(domain, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
|
|
66
|
+
} else {
|
|
67
|
+
[NSUserDefaults.standardUserDefaults setBool:value forKey:key];
|
|
68
|
+
}
|
|
69
|
+
if (changed) CCPetsPrefPostChange(key);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static NSNumber *ClampedNumber(id value, double minimum, double maximum) {
|
|
73
|
+
if (![value isKindOfClass:NSNumber.class]) return nil;
|
|
74
|
+
double number = [value doubleValue];
|
|
75
|
+
if (!isfinite(number)) return nil;
|
|
76
|
+
return @(MAX(minimum, MIN(maximum, number)));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 下限与右键菜单原先各 setter 里的钳制逐条对应,改这里等于改菜单行为。
|
|
80
|
+
id CCPetsPrefNormalizedValue(NSString *key, id value) {
|
|
81
|
+
if (value == nil) return nil;
|
|
82
|
+
if ([key isEqualToString:CCPetsPetScaleKey]) {
|
|
83
|
+
NSNumber *scale = ClampedNumber(value, CCPetsPetMinimumScale, CCPetsPetMaximumScale);
|
|
84
|
+
// 滑动条连续拖动会给出 0.8734 这种值,按百分比取整,免得 defaults 里存一串小数。
|
|
85
|
+
return scale ? @(round(scale.doubleValue * 100) / 100) : nil;
|
|
86
|
+
}
|
|
87
|
+
if ([key isEqualToString:PetInteractionHeartThresholdKey]) {
|
|
88
|
+
NSNumber *count = ClampedNumber(value, 2, INT_MAX);
|
|
89
|
+
return count ? @(count.integerValue) : nil;
|
|
90
|
+
}
|
|
91
|
+
if ([key isEqualToString:PetInteractionAnnoyedThresholdKey]) {
|
|
92
|
+
// 生气必须在比心之后,否则连点永远到不了比心那一档。
|
|
93
|
+
NSInteger heart = MAX((NSInteger)2, [NSUserDefaults.standardUserDefaults
|
|
94
|
+
integerForKey:PetInteractionHeartThresholdKey]);
|
|
95
|
+
NSNumber *count = ClampedNumber(value, heart + 1, INT_MAX);
|
|
96
|
+
return count ? @(count.integerValue) : nil;
|
|
97
|
+
}
|
|
98
|
+
if ([key isEqualToString:PetInteractionIntervalKey]) return ClampedNumber(value, 0.4, 3.0);
|
|
99
|
+
if ([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class]) return value;
|
|
100
|
+
return nil;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
BOOL CCPetsPrefSetObject(NSString *key, id value) {
|
|
104
|
+
if (key.length == 0) return NO;
|
|
105
|
+
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
106
|
+
id previous = [defaults objectForKey:key];
|
|
107
|
+
if (value == nil) {
|
|
108
|
+
[defaults removeObjectForKey:key];
|
|
109
|
+
if (previous != nil) CCPetsPrefPostChange(key);
|
|
110
|
+
return YES;
|
|
111
|
+
}
|
|
112
|
+
id normalized = CCPetsPrefNormalizedValue(key, value);
|
|
113
|
+
if (!normalized) return NO;
|
|
114
|
+
[defaults setObject:normalized forKey:key];
|
|
115
|
+
if (![previous isEqual:normalized]) CCPetsPrefPostChange(key);
|
|
116
|
+
return YES;
|
|
117
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
// 主窗口的「诊断」页:渲染 `cc-pets doctor` 的同一套检查(CCPetsDoctorSectionsForApp:PATH 顺序那一项
|
|
4
|
+
// 在桌宠进程里看不准,改成去终端确认的提示,其余相同)。
|
|
5
|
+
// 要处理的问题置顶;已登记账号的 Hooks / status line 支持备份后自动修复,其他问题保留修复命令。
|
|
6
|
+
// 正常项折叠成一行,修复结束后重新检查。
|
|
7
|
+
@interface CCPetsDiagnosticsPage : NSViewController
|
|
8
|
+
@end
|
|
9
|
+
|
|
10
|
+
// 检查结果的缓存,诊断页、侧边栏角标、概览的提醒共用一份。检查要读配置、探测 CLI,在后台队列做;
|
|
11
|
+
// 主窗口打开时检查一次(结果十分钟内算新鲜),之后点"重新检查"或修复完成后再跑,不开定时器。
|
|
12
|
+
extern NSNotificationName const CCPetsDiagnosticsDidChangeNotification;
|
|
13
|
+
@interface CCPetsDiagnosticsStore : NSObject
|
|
14
|
+
// 还没检查过时为 nil。
|
|
15
|
+
@property(class, nonatomic, readonly) NSArray<NSDictionary *> *sections;
|
|
16
|
+
@property(class, nonatomic, readonly) BOOL running;
|
|
17
|
+
@property(class, nonatomic, readonly) BOOL repairing;
|
|
18
|
+
@property(class, nonatomic, readonly) NSString *repairMessage;
|
|
19
|
+
@property(class, nonatomic, readonly) NSDate *checkedAt;
|
|
20
|
+
// warn + fail 的条数:侧边栏角标和概览提醒的数字。
|
|
21
|
+
@property(class, nonatomic, readonly) NSUInteger issueCount;
|
|
22
|
+
+ (void)refreshIfStale;
|
|
23
|
+
+ (void)refresh;
|
|
24
|
+
+ (void)repairIssues:(NSArray<NSDictionary *> *)issues;
|
|
25
|
+
@end
|