Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
FileAssociationManager.cs
浏览该文件的文档.
1#if WINDOWS
2using System.Diagnostics;
3using System.Reflection;
4using System.Runtime.InteropServices;
5using Engine;
6using Microsoft.Win32;
7
8#pragma warning disable CA1416
9#endif
10
11namespace Game {
12 public static class FileAssociationManager {
13 public static string ProgIdBase = $"SurvivalcraftApi_{ModsManager.ShortAPIVersionString}";
14 public static string VersionedExeName = $"Survivalcraft_{ModsManager.ShortAPIVersionString}.exe";
15 public static string[] SupportedExtensions = [".scworld", ".scbtex", ".scskin", ".scfpack", ".scmod" /*, ".scmodList"*/];
16 public const string fName = "FileAssociationManager";
17
18 public static void Initialize() {
19#if WINDOWS
21 Task.Run(() => {
22 if (!IsRegistered()) {
23 Register();
24 }
25 }
26 );
27 }
28#endif
29 }
30
31 public static bool Register() {
32#if WINDOWS
33 try {
34 string appPath = Process.GetCurrentProcess().MainModule?.FileName;
35 if (string.IsNullOrEmpty(appPath)) {
37 return false;
38 }
39 string friendlyAppName =
40 $"{LanguageControl.Get("Usual", "gameName")} {ModsManager.ShortGameVersion} - API {ModsManager.APIVersionString}";
41 string command = $"\"{appPath}\" \"%1\"";
42 foreach (string extension in SupportedExtensions) {
43 using (RegistryKey progKey = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{ProgIdBase}{extension}")) {
44 if (progKey == null) {
46 return false;
47 }
48 progKey.SetValue("", LanguageControl.Get(out _, LanguageControl.englishJsonNode, fName, "ExtensionDescription", extension));
49 progKey.SetValue("FriendlyAppName", friendlyAppName);
50 progKey.SetValue("FriendlyTypeName", LanguageControl.Get(fName, "ExtensionDescription", extension));
51 using (RegistryKey iconKey = progKey.CreateSubKey("DefaultIcon")) {
52 iconKey?.SetValue("", $"{appPath},0");
53 }
54 using (RegistryKey commandKey = progKey.CreateSubKey(@"shell\open\command")) {
55 if (commandKey == null) {
57 return false;
58 }
59 commandKey.SetValue("", command);
60 }
61 }
62 using (RegistryKey openWithKey = Registry.CurrentUser.CreateSubKey(
63 $@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{extension}\OpenWithProgids"
64 )) {
65 if (openWithKey == null) {
66 Log.Error(string.Format(LanguageControl.Get(fName, "4"), extension));
67 return false;
68 }
69 openWithKey.SetValue($"{ProgIdBase}{extension}", Array.Empty<byte>(), RegistryValueKind.None);
70 }
71 }
72 SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
73 }
74 catch (Exception e) {
75 Log.Error($"{LanguageControl.Get(fName, "5")}\n{e.Message}");
76 return false;
77 }
79#endif
80 return true;
81 }
82
83 public static void Unregister() {
84#if WINDOWS
85 try {
86 foreach (string extension in SupportedExtensions) {
87 Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{ProgIdBase}", false);
88 Registry.CurrentUser.OpenSubKey($@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{extension}\OpenWithProgids", true)
89 ?.DeleteValue($"{ProgIdBase}{extension}", false);
90 }
91 SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
92 }
93 catch (Exception e) {
94 Log.Error($"{LanguageControl.Get(fName, "7")}\n{e.Message}");
95 }
97#endif
98 }
99
100 public static bool IsRegistered() {
101#if WINDOWS
102 try {
103 string appPath = Process.GetCurrentProcess().MainModule?.FileName;
104 if (appPath == null) {
105 return false;
106 }
107 string friendlyAppName =
108 $"{LanguageControl.Get("Usual", "gameName")} {ModsManager.ShortGameVersion} - API {ModsManager.APIVersionString}";
109 string command = $"\"{appPath}\" \"%1\"";
110 foreach (string extension in SupportedExtensions) {
111 using (RegistryKey progKey = Registry.CurrentUser.OpenSubKey($@"Software\Classes\{ProgIdBase}{extension}", false)) {
112 if (progKey == null) {
113 return false;
114 }
115 if (progKey.GetValue("FriendlyAppName")?.ToString() != friendlyAppName) {
116 return false;
117 }
118 if ((string)progKey.OpenSubKey($@"shell\open\command", false)?.GetValue("")
119 != command) {
120 return false;
121 }
122 }
123 if (Registry.CurrentUser.OpenSubKey(
124 $@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{extension}\OpenWithProgids",
125 false
126 )
127 ?.GetValue($"{ProgIdBase}{extension}")
128 == null) {
129 return false;
130 }
131 }
132 }
133 catch {
134 return false;
135 }
136#endif
137 return true;
138 }
139#if WINDOWS
140 [DllImport("shell32.dll")]
141 static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
142#endif
143 }
144}
static void Error(object message)
定义 Log.cs:80
static void Information(object message)
定义 Log.cs:56
static JsonNode englishJsonNode
static string Get(string className, int key)
获取在当前语言类名键对应的字符串