Skip to content

Commit

Permalink
refactor(Weasel.Setup): improve registry setting and getting
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 13, 2025
1 parent 39d541e commit 7857423
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
50 changes: 31 additions & 19 deletions Weasel.Setup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static void Run(string arg)
var dir = arg.Substring(arg.IndexOf(':') + 1);
if (dir != null)
{
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "RimeUserDir", dir);
Registry.CurrentUser.SetValue($@"{WEASEL_PROG_REG_KEY}\RimeUserDir", dir);
}
return;
}
Expand All @@ -69,31 +69,31 @@ private static void Run(string arg)
switch (arg) // 无需管理员权限的操作
{
case "/ls": // 简体中文
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "Language", "chs");
Registry.CurrentUser.SetValue($@"{WEASEL_PROG_REG_KEY}\Language", "chs");
return;
case "/lt": // 繁体中文
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "Language", "cht");
Registry.CurrentUser.SetValue($@"{WEASEL_PROG_REG_KEY}\Language", "cht");
return;
case "/le": // 英语
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "Language", "eng");
Registry.CurrentUser.SetValue($@"{WEASEL_PROG_REG_KEY}\Language", "eng");
return;
case "/eu": // 启用更新
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_UPDATE_REG_KEY, "CheckForUpdates", 1);
Registry.CurrentUser.SetValue($@"{WEASEL_UPDATE_REG_KEY}\CheckForUpdates", "1");
return;
case "/du": // 禁用更新
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_UPDATE_REG_KEY, "CheckForUpdates", 0);
Registry.CurrentUser.SetValue($@"{WEASEL_UPDATE_REG_KEY}\CheckForUpdates", "0");
return;
case "/toggleime":
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "ToggleImeOnOpenClose", 1);
Registry.CurrentUser.SetValue($@"{WEASEL_PROG_REG_KEY}\ToggleImeOnOpenClose", "yes");
return;
case "/toggleascii":
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "ToggleImeOnOpenClose", 0);
Registry.CurrentUser.SetValue($@"{WEASEL_PROG_REG_KEY}\ToggleImeOnOpenClose", "no");
return;
case "/testing": // 测试通道
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "UpdateChannel", "testing");
Registry.CurrentUser.SetValue($@"{WEASEL_PROG_REG_KEY}\UpdateChannel", "testing");
return;
case "/release": // 正式通道
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "UpdateChannel", "release");
Registry.CurrentUser.SetValue($@"{WEASEL_PROG_REG_KEY}\UpdateChannel", "release");
return;
default: // 需要管理员权限的操作
if (!IsRunAsAdmin())
Expand Down Expand Up @@ -130,15 +130,24 @@ private static void Run(string arg)

private static void CustomInstall(bool isInstalling)
{
var isSilentMode = isInstalling;
var isSilentMode = false;
var isInstalled = Setup.IsWeaselInstalled;

var isHant = Convert.ToBoolean(
Utils.Reg.GetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "Hant", 0)
);
var userDir = Convert.ToString(
Utils.Reg.GetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "RimeUserDir", string.Empty)
);
bool isHant = false;
string userDir = string.Empty;
using (var user = Registry.CurrentUser.OpenSubKey(WEASEL_PROG_REG_KEY))
{
if (user != null)
{
userDir = Convert.ToString(user.GetValue("RimeUserDir"));
var value = user.GetValue("Hant");
if (value != null && value is int)
{
isHant = Convert.ToBoolean(value);
if (isInstalling) isSilentMode = true;
}
}
}

if (!isSilentMode)
{
Expand All @@ -154,8 +163,11 @@ private static void CustomInstall(bool isInstalling)
isHant = dialog.IsHant;
userDir = dialog.UserDir;

Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "RimeUserDir", userDir);
Utils.Reg.SetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "Hant", isHant ? 1 : 0);
using (var user = Registry.CurrentUser.CreateSubKey(WEASEL_PROG_REG_KEY))
{
user.SetValue($@"{WEASEL_PROG_REG_KEY}\RimeUserDir", userDir);
user.SetValue($@"{WEASEL_PROG_REG_KEY}\Hant", isHant ? 1 : 0);
}
}
else
{
Expand Down
13 changes: 8 additions & 5 deletions Weasel.Setup/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public static void NormalInstall(bool isHant, bool isSilentMode = true)
});

// 登记注册表
Utils.Reg.SetValue(Registry.LocalMachine, WEASEL_PROG_REG_KEY, "WeaselRoot", productDir);
Utils.Reg.SetValue(Registry.LocalMachine, WEASEL_PROG_REG_KEY, "ServerExecutable", WEASEL_SERVER_EXE);
using (var local = Registry.LocalMachine.CreateSubKey(WEASEL_PROG_REG_KEY))
{
local.SetValue("WeaselRoot", productDir);
local.SetValue("ServerExecutable", WEASEL_SERVER_EXE);
}

// 启用键盘布局和文本服务
var psz = isHant ? PSZTITLE_HANT : PSZTITLE_HANS;
Expand All @@ -86,7 +89,7 @@ public static void Uninstall(bool isSilentMode)
{
// 停用键盘布局和文本服务
var isHant = Convert.ToBoolean(
Utils.Reg.GetValue(Registry.CurrentUser, WEASEL_PROG_REG_KEY, "Hant", 0)
Registry.CurrentUser.GetValue($@"{WEASEL_PROG_REG_KEY}\Hant")
);
var psz = isHant ? PSZTITLE_HANT : PSZTITLE_HANS;
PInvoke.Input.InstallLayoutOrTip(psz, PInvoke.Input.ILOT.ILOT_UNINSTALL);
Expand Down Expand Up @@ -237,8 +240,8 @@ private static void UpdateServiceState(string libPath, bool enable, bool isHant)
var value = isHant ? "hant" : "hans";
Environment.SetEnvironmentVariable("TEXTSERVICE_PROFILE", value);
var sysarm32Dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "SysArm32");
var regsvr32Path = Directory.Exists(sysarm32Dir)
? Path.Combine(sysarm32Dir, "regsvr32.exe")
var regsvr32Path = Directory.Exists(sysarm32Dir)
? Path.Combine(sysarm32Dir, "regsvr32.exe")
: "regsvr32.exe";
var args = enable ? $"/s \"{libPath}\"" : $"/s /u \"{libPath}\"";
var updateInfo = new ProcessStartInfo
Expand Down
19 changes: 0 additions & 19 deletions Weasel.Setup/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,6 @@ public static void SetProcessApiAwareness()
}
}

public static class Reg
{
public static void SetValue(RegistryKey baseKey, string subKeyName, string valueName, object value)
{
using (var reg = baseKey.CreateSubKey(subKeyName))
{
reg.SetValue(valueName, value);
}
}

public static object GetValue(RegistryKey baseKey, string subKeyName, string valueName, object defaultValue)
{
using (var reg = baseKey.CreateSubKey(subKeyName))
{
return reg.GetValue(valueName, defaultValue);
}
}
}

public static class Path
{
public static string LogPath
Expand Down

0 comments on commit 7857423

Please sign in to comment.