
运行子程序(VersionCompatible)失败。解析表达式出错。
内部错误:未将对象引用设置到对象的实例。
原始表达式:
public static string GetTranslatorType(string id){
var index = id.IndexOf("@@");
if (index == -1) return id;
return id.Substring(0, index);
}
public static JArray ConvertToJArray(JToken tkn){
if (tkn.Type == JTokenType.Array){
return (JArray)tkn;
} else if (tkn.Type == JTokenType.String) {
return JArray.FromObject(tkn.ToString().SplitToList("\n"));
} else {
throw new InvalidOperationException("[Custom] Unsupported translators/dictionaries field format.");
}
}
var newSettings = JObject.FromObject({defaultSettings});
var oldSettings = JObject.FromObject({setting});
// 添加自定义接口
var newTranslatorsUnable = (JArray)newSettings["translators"]["unable"];
var newDictionariesUnable = (JArray)newSettings["dictionaries"]["unable"];
// name要替换成这些代码
//_context.RunSp("AssistiveTools", new Dictionary<string, object>() {
// { "toolName", "GetActionName" },
// { "input", rowData["actionId"] }
// })["output"]?.ToString()
for(int i=0; i<{customInterfaces}.Rows.Count; i++) {
var rowData = {customInterfaces}.Rows[i];
var customJObject = JObject.FromObject(new {
id = "Custom@@"+i,
isDeletable = true,
displayName = rowData["translator"],
icon = string.IsNullOrWhiteSpace(rowData["icon"] as string)?"action:"+rowData["actionId"]:rowData["icon"],
action = new {
id = rowData["actionId"],
name = _context.RunSp("AssistiveTools", new Dictionary<string, object>() {
{ "toolName", "GetActionName" },
{ "input", rowData["actionId"] }
})["output"]?.ToString(),
icon = "action:"+rowData["actionId"]
},
additionalParams = rowData["param"]
});
if ((bool)rowData["isDictionary"]) {
newDictionariesUnable.Add(customJObject);
} else {
newTranslatorsUnable.Add(customJObject);
}
}
// 填充translators的设置项
JArray oldTranslators = ConvertToJArray(oldSettings["translators"]);
JArray oldDictionaries = ConvertToJArray(oldSettings["dictionaries"]);
JArray newTranslatorsAble = (JArray)newSettings["translators"]["able"];
JObject AISetting;
string AIDisplayName;
if (oldSettings["AIService"].ToString() == "ChatGPT-chat") {
AISetting = (JObject)newTranslatorsUnable[0];
AISetting["apiUrl"] = oldSettings["AIUrl"];
AIDisplayName = "OpenAI";
} else {
AISetting = (JObject)newTranslatorsUnable[1];
AIDisplayName = "谷歌AI";
}
var AITranslatorIndex = oldTranslators.ToObject<List<string>>().IndexOf("AI");
if (AITranslatorIndex != -1){
oldTranslators.RemoveAt(AITranslatorIndex);
oldTranslators.Insert(AITranslatorIndex, JToken.FromObject(AIDisplayName));
}
AISetting["AIModel"] = oldSettings["AIModel"];
AISetting["apiKey"] = oldSettings["AIAPIKey"];
AISetting["prompt"] = oldSettings["AIPrompt"];
AISetting["temperature"] = oldSettings["AITemperature"];
AISetting["proxy"] = oldSettings["AIProxy"];
var AIDictionaryIndex = oldDictionaries.ToObject<List<string>>().IndexOf("AI");
if (AIDictionaryIndex != -1){
newDictionariesUnable.Add(AISetting);
oldDictionaries.RemoveAt(AIDictionaryIndex);
oldDictionaries.Insert(AIDictionaryIndex, JToken.FromObject(AIDisplayName));
}
JObject deeplSetting = (JObject)newTranslatorsAble[1];
if (oldSettings["DeepLAuthKey"]==null || string.IsNullOrWhiteSpace(oldSettings["DeepLAuthKey"].ToString())) {
deeplSetting["apiMode"] = "free";
} else if (oldSettings["DeepLIsPro"].ToObject<bool>()) {
deeplSetting["apiMode"] = "apiPro";
} else {
deeplSetting["apiMode"] = "apiFree";
}
deeplSetting["apiKey"] = oldSettings["DeepLAuthKey"];
JObject baiduSetting = (JObject)newTranslatorsUnable[3];
baiduSetting["apiId"] = oldSettings["baiduAppid"];
baiduSetting["apiKey"] = oldSettings["baiduKey"];
JObject tencentSetting = (JObject)newTranslatorsUnable[7];
tencentSetting["apiId"] = oldSettings["tencentSecretId"];
tencentSetting["apiKey"] = oldSettings["tencentSecretKey"];
JObject niuTransSetting = (JObject)newTranslatorsUnable[8];
niuTransSetting["apiKey"] = oldSettings["niuApiKey"];
// 填充OCR的设置项
var newOcrServiceUnable = (JArray)newSettings["ocrService"]["unable"];
JObject baiduOcrSetting = (JObject)newOcrServiceUnable[0];
baiduOcrSetting["apiKey"] = oldSettings["baiduOcrApiKey"];
baiduOcrSetting["secretKey"] = oldSettings["baiduOcrSecretKey"];
JObject quickerOcrSetting = (JObject)newOcrServiceUnable[1];
quickerOcrSetting["ocrLan"] = oldSettings["ocrLan"];
// name要替换成这些代码
//_context.RunSp("AssistiveTools", new Dictionary<string, object>() {
// { "toolName", "GetActionName" },
// { "input", oldSettings["customOCRAction"] }
// })["output"]?.ToString()
if(!string.IsNullOrWhiteSpace((string)oldSettings["customOCRAction"])){
JObject customOcrSetting = (JObject)newOcrServiceUnable[3];
customOcrSetting["action"]["id"] = oldSettings["customOCRAction"];
customOcrSetting["action"]["icon"] = "action:"+(string)oldSettings["customOCRAction"];
customOcrSetting["action"]["name"] = _context.RunSp("AssistiveTools", new Dictionary<string, object>() {
{ "toolName", "GetActionName" },
{ "input", oldSettings["customOCRAction"] }
})["output"]?.ToString();
customOcrSetting["ocrLan"] = oldSettings["ocrLan"];
}
// 填充TTS的设置项
var newTtsServiceUnable = (JArray)newSettings["ttsService"]["unable"];
JObject DUITtsSetting = (JObject)newTtsServiceUnable[1];
DUITtsSetting["ttsVoiceId"] = oldSettings["ttsVoiceId"];
DUITtsSetting["ttsVoiceSpeed"] = oldSettings["ttsVoiceSpeed"];
// 更新translators和dictionaries
var transMap = new Dictionary<string, string>() {
{ "腾讯交互翻译", "TranSmart" },
{ "有道翻译", "Youdao" },
{ "百度翻译", "Baidu" },
{ "必应翻译", "Bing" },
{ "DeepL", "DeepL" },
{ "谷歌翻译", "Google" },
{ "谷歌镜像", "GoogleMirror" },
{ "CNKI翻译", "CNKI" },
{ "OpenAI", "OpenAI" },
{ "谷歌AI", "GoogleAI" },
{ "腾讯翻译君", "Tencent" },
{ "小牛翻译", "NiuTrans" },
{ "火山翻译", "Volctrans" },
{ "阿里翻译", "Alibaba" },
{ "彩云小译", "Caiyun" },
{ "有道词典", "YoudaoDict" },
{ "必应词典", "BingDict" },
{ "Qk英汉词典", "QkDict" },
{ "海词词典", "HaiciDict" },
{ "爱词霸", "IcibaDict" },
{ "翻译服务", "AdditionalTranslators" }
};
foreach(var transKey in new string[] {"translators", "dictionaries"}) {
var oldValues = ConvertToJArray(oldSettings[transKey]);
var newValuesAble = (JArray)newSettings[transKey]["able"];
var newValuesUnable = (JArray)newSettings[transKey]["unable"];
newValuesUnable.AddRange(newValuesAble);
newValuesAble.RemoveAll();
foreach (var oldValue in oldValues) {
var id = transMap.ContainsKey(oldValue.ToString()) ? transMap[oldValue.ToString()] : "";
for (int i=0; i<newValuesUnable.Count; i++) {
var item = (JObject)newValuesUnable[i];
if (item.ContainsKey("displayName")
&& item["displayName"].ToString() == oldValue.ToString()
|| GetTranslatorType(item["id"].ToString()) == id) {
newValuesAble.Add(item);
newValuesUnable.RemoveAt(i);
break;
}
}
}
}
// 更新ocrService和ttsService
foreach(var transKey in new string[] {"ocrService", "ttsService"}) {
var oldValues = (transKey == "ttsService" ? oldSettings["ttsSource"] : oldSettings[transKey]);
var newValuesAble = (JArray)newSettings[transKey]["able"];
var newValuesUnable = (JArray)newSettings[transKey]["unable"];
newValuesUnable.AddRange(newValuesAble);
newValuesAble.RemoveAll();
for (int i=0; i<newValuesUnable.Count; i++) {
var item = (JObject)newValuesUnable[i];
if (item["id"].ToString() == oldValues.ToString()) {
newValuesAble.Add(item);
newValuesUnable.RemoveAt(i);
break;
}
}
}
// 更新buttonVisibleDict
var oldButtonVisibleList = ConvertToJArray(oldSettings["buttonVisibleList"]).ToObject<List<string>>();
var newButtonVisibleDict = newSettings["buttonVisibleDict"];
foreach (var part in new string[] {"left", "right"}) {
var currentPart = newButtonVisibleDict[part] as JObject;
foreach (var property in currentPart.Properties()) {
currentPart[property.Name] = oldButtonVisibleList.Contains(property.Name);
}
}
// 更新其他直接映射的项
var directMapping = new Dictionary<string, string>() {
{ "isPinTopmost", "isPin" },
{ "displayPanelMode", "displayMode" },
{ "themeMode", "colorMethod" },
{ "backgroundImage", "backgroundIMGPath" },
{ "XfVoiceAPPID", "XfVoiceAPPID" },
{ "XfVoiceAPIKey", "XfVoiceAPIKey" },
{ "XfVoiceAPISecret", "XfVoiceAPISecret" },
{ "secondTransMode", "secondTransMode" },
{ "expandNum", "expandNum" },
{ "historyMaxCount", "historyMaxCount" },
{ "isMergePara", "isMergePara" },
{ "foldDelay", "foldDelay" },
{ "winLocation", "winLocation" },
{ "winMaxHeightPercent", "winMaxHeightPercent" },
{ "showInTaskbar", "showInTaskbar" },
{ "titleFontFamily", "titleFontFamily" },
{ "textFontFamily", "textFontFamily" },
{ "fontSize", "fontSize" },
{ "backgroundColor", "backgroundColor" },
{ "backgroundColor2", "backgroundColor2" },
{ "buttonColor", "buttonColor" },
{ "borderColor", "borderColor" },
{ "textColor", "textColor" },
{ "badgeIndex", "badgeIndex" },
{ "showEditBox", "showEditBox" },
{ "ocrIsAppend", "ocrIsAppend" },
{ "origLan", "origLan" },
{ "transLan", "transLan" },
{ "transLan2", "transLan2" },
{ "closeFlag", "closeFlag" },
{ "autoLocationX", "autoLocationX" },
{ "autoLocationY", "autoLocationY" },
{ "isConsecutive", "isConsecutive" },
{ "isClipChangeTrans", "isClipChangeTrans" },
{ "width", "width" }
};
foreach (var item in directMapping) {
newSettings[item.Key] = oldSettings[item.Value];
}
// 更新其他简单变动的项
newSettings["transShortCut"] = oldSettings["isCtrlEnter"].ToObject<bool>() ? "CtrlEnter" : "Enter";
newSettings["loseFocusAction"] = oldSettings["topFoldMode"].ToString() == "noFold" ? "none" : "fold";
return newSettings;(赋值)
(----Translatorv53:运行子程序----)