UNPKG

5.65 kBPlain TextView Raw
1using System;
2using Microsoft.Win32;
3
4namespace wstool
5{
6 class wstool
7 {
8 static int Main(string[] args) {
9 if (args.Length == 0) {
10 return showHelp();
11 }
12
13 string command = args[0];
14
15 if (command == "launch") {
16 string appid = null;
17 string version = null;
18 string windowsAppId = null;
19 for (var i = 1; i < args.Length; i++) {
20 string option = args[i++];
21 if (args.Length <= i || args[i].StartsWith("--")) {
22 return showHelp(String.Format("Invalid option for command '{0}'", command));
23 }
24 switch (option) {
25 case "--appid":
26 appid = args[i];
27 break;
28 case "--version":
29 version = args[i];
30 break;
31 case "--windowsAppId":
32 windowsAppId = args[i];
33 break;
34 default:
35 return showHelp(String.Format("Invalid option for command '{0}'", command));
36 }
37
38 }
39 if (appid == null) {
40 return showHelp(String.Format("Invalid option for command '{0}'", command));
41 }
42
43 return launchApp(appid, version, windowsAppId);
44 }
45
46 return showHelp(String.Format("Invalid command '{0}'", command));
47 }
48
49 static int launchApp(String appid, String version, String windowsAppId) {
50 string appUserModelId = null;
51 string windowsAppName = null;
52 var appListKey = Registry.CurrentUser.OpenSubKey("Software\\Classes\\ActivatableClasses\\Package");
53
54 // if we have the version, then this should be simple
55 if (version != null) {
56 foreach (var appKeyName in appListKey.GetSubKeyNames()) {
57 if (appKeyName.IndexOf(appid + "_" + version + "_") == 0) {
58 // Save the name, in case sub key doesn't exist
59 windowsAppName = appKeyName;
60 var appKey = appListKey.OpenSubKey(appKeyName);
61 var appClassKey = appKey.OpenSubKey("ActivatableClassId\\App");
62 if (appClassKey == null)
63 {
64 appClassKey = appKey.OpenSubKey("ActivatableClassId\\App.wwa");
65 }
66 String serverId = (String)appClassKey.GetValue("Server");
67 var subKey = appKey.OpenSubKey("Server\\" + serverId);
68 appUserModelId = (String)subKey.GetValue("AppUserModelId");
69 break;
70 }
71 }
72 } else {
73 // no version, so find all with the appid and the largest version number
74 string lastVersion = null;
75 foreach (var appKeyName in appListKey.GetSubKeyNames()) {
76 int p = appKeyName.IndexOf(appid + "_");
77 if (p == 0) {
78 int q = appKeyName.IndexOf("_", p + 1);
79 if (q != -1) {
80 string thisVersion = appKeyName.Substring(p + 1, q);
81 if (lastVersion == null || String.Compare(thisVersion, lastVersion, true) > 0) {
82 // Save the name, in case sub key doesn't exist
83 windowsAppName = appKeyName;
84 var appKey = appListKey.OpenSubKey(appKeyName);
85 var appClassKey = appKey.OpenSubKey("ActivatableClassId\\App");
86 if (appClassKey == null)
87 {
88 appClassKey = appKey.OpenSubKey("ActivatableClassId\\App.wwa");
89 }
90 if (appClassKey != null)
91 {
92 String serverId = (String)appClassKey.GetValue("Server");
93 var subKey = appKey.OpenSubKey("Server\\" + serverId);
94 if (subKey != null)
95 {
96 appUserModelId = (String)subKey.GetValue("AppUserModelId");
97 lastVersion = thisVersion;
98 }
99 }
100 }
101 }
102 }
103 }
104 }
105
106 // If there's no Server entry in the registry...we still have a way when Windows Store application id is provided
107 // You can find Windows Store application id in the "Application Id" value in the AppxManifest.xml.
108 if (appUserModelId == null && windowsAppName != null) {
109
110 // Extract "family name" from the entry name, by removing version and archtecture string
111 // For example: Microsoft.SkypeApp_3.2.1.0_x86__kzf8qxf38zg5c -> Microsoft.SkypeApp_kzf8qxf38zg5c
112 var first = windowsAppName.IndexOf("_");
113 var last = windowsAppName.LastIndexOf("_");
114 var familyName = windowsAppName.Substring(0, first) + "_" + windowsAppName.Substring(last + 1);
115
116 // we try to do our best...many apps are actually using "App" for application id
117 if (windowsAppId == null) {
118 windowsAppId = "App";
119 }
120
121 // then we conbine it with the Windows Store application id...wish it generates valid one
122 appUserModelId = String.Format("{0}!{1}", familyName, windowsAppId);
123 }
124
125 if (appUserModelId == null) {
126 Console.WriteLine("Could not find version " + version + " of application " + appid + " in the registry. Is the application installed?");
127 return 1;
128 }
129
130 try {
131 var aam = new ApplicationActivationManager();
132 UInt32 id;
133 aam.ActivateApplication(appUserModelId, null, ActivateOptions.None, out id);
134 Console.WriteLine(id);
135 } catch (System.Runtime.InteropServices.COMException) {
136 Console.WriteLine("Could not find version " + version + " of application " + appid + " in the registry. Is the application installed?");
137 return 1;
138 }
139
140 return 0;
141 }
142
143 static int showHelp(string msg = "") {
144 Console.WriteLine("Appcelerator Windows Store App Tool v1.0\n");
145
146 if (msg.Length > 0) {
147 Console.WriteLine("ERROR: " + msg + "\n");
148 }
149
150 Console.WriteLine("Usage:");
151 Console.WriteLine(" wstool <command> [options]");
152 Console.WriteLine("");
153 Console.WriteLine("Commands:");
154 Console.WriteLine(" launch --appid <Titanium application id> [--appversion <version>] [--windowsAppId <Windows Store application id>] Launch a Windows Store app");
155 Console.WriteLine("");
156
157 return msg.Length > 0 ? 1 : 0;
158 }
159 }
160}