Skip to content

Commit d9228ee

Browse files
Dan WagerDan Wager
authored andcommitted
Added AAPT Wrapper to build
1 parent 61244d8 commit d9228ee

File tree

6 files changed

+408
-4
lines changed

6 files changed

+408
-4
lines changed

AndroidLib/AndroidLib.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
<EmbeddedResource Include="Resources\AndroidController\AdbWinApi.dll" />
7878
<EmbeddedResource Include="Resources\AndroidController\AdbWinUsbApi.dll" />
7979
</ItemGroup>
80-
<ItemGroup />
80+
<ItemGroup>
81+
<Folder Include="Resources\Other\" />
82+
</ItemGroup>
8183
<ItemGroup>
8284
<EmbeddedResource Include="Resources\Signer\testkey.pk8" />
8385
<EmbeddedResource Include="Resources\Signer\testkey.x509.pem" />
@@ -87,7 +89,7 @@
8789
<EmbeddedResource Include="Resources\Signer\signapk.jar" />
8890
</ItemGroup>
8991
<ItemGroup>
90-
<EmbeddedResource Include="Resources\Other\aapt.exe" />
92+
<EmbeddedResource Include="Resources\AAPT\aapt.exe" />
9193
</ItemGroup>
9294
<ItemGroup>
9395
<None Include="regaw_leinad.pfx" />
Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace RegawMOD.Android
6+
{
7+
public partial class AAPT
8+
{
9+
/// <summary>
10+
/// Contains Apk Badging Dump Information
11+
/// </summary>
12+
public class Badging
13+
{
14+
#region Constants
15+
private const string APOSTROPHE = "'";
16+
17+
private const string PACKAGE = "package:";
18+
private const string PACKAGE_NAME = "name='";
19+
private const string PACKAGE_VERSION_CODE = "versionCode='";
20+
private const string PACKAGE_VERSION_NAME = "versionName='";
21+
22+
private const string APPLICATION = "application:";
23+
private const string APPLICATION_LABEL = "label='";
24+
private const string APPLICATION_ICON = "icon='";
25+
26+
private const string ACTIVITY = "launchable-activity:";
27+
private const string ACTIVITY_NAME = "name='";
28+
private const string ACTIVITY_LABEL = "label='";
29+
private const string ACTIVITY_ICON = "icon='";
30+
31+
private const string SDK_VERSION = "sdkVersion:'";
32+
private const string SDK_TARGET = "targetSdkVersion:'";
33+
34+
private const string USES_PERMISSION = "uses-permission:'";
35+
36+
private const string DENSITIES = "densities:";
37+
#endregion
38+
39+
#region Fields
40+
private FileInfo source;
41+
42+
private PackageInfo package;
43+
private ApplicationInfo application;
44+
private LaunchableActivity activity;
45+
46+
private string sdkVersion;
47+
private string targetSdkVersion;
48+
49+
private List<string> usesPermission;
50+
private List<int> densities;
51+
#endregion
52+
53+
#region Properties
54+
/// <summary>
55+
/// Gets a <c>FileInfo</c> containing information about the source Apk
56+
/// </summary>
57+
public FileInfo Source { get { return this.source; } }
58+
59+
/// <summary>
60+
/// Gets a <see cref="PackageInfo"/> containing infomation about the Apk
61+
/// </summary>
62+
public PackageInfo Package { get { return this.package; } }
63+
64+
/// <summary>
65+
/// Gets a <see cref="ApplicationInfo"/> containing infomation about the Apk
66+
/// </summary>
67+
public ApplicationInfo Application { get { return this.application; } }
68+
69+
/// <summary>
70+
/// Gets a <see cref="LaunchableActivity"/> containing infomation about the Apk
71+
/// </summary>
72+
public LaunchableActivity Activity { get { return this.activity; } }
73+
74+
/// <summary>
75+
/// Gets a value indicating the Android Sdk Version of the Apk
76+
/// </summary>
77+
public string SdkVersion { get { return this.sdkVersion; } }
78+
79+
/// <summary>
80+
/// Gets a value indicating the Target Android Sdk Version of the Apk
81+
/// </summary>
82+
public string TargetSdkVersion { get { return this.targetSdkVersion; } }
83+
84+
/// <summary>
85+
/// Gets a <c>List&lt;string&gt;</c> containing the Android Permissions used by the Apk
86+
/// </summary>
87+
public List<string> Permissions { get { return this.usesPermission; } }
88+
89+
/// <summary>
90+
/// Gets a <c>List&lt;int&gt;</c> containing the supported screen densities of the Apk
91+
/// </summary>
92+
public List<int> ScreenDensities { get { return this.densities; } }
93+
#endregion
94+
95+
internal Badging(FileInfo source, string dump)
96+
{
97+
this.source = source;
98+
99+
this.package = new PackageInfo();
100+
this.application = new ApplicationInfo();
101+
this.activity = new LaunchableActivity();
102+
103+
this.sdkVersion = "";
104+
this.targetSdkVersion = "";
105+
106+
this.usesPermission = new List<string>();
107+
this.densities = new List<int>();
108+
109+
ProcessDump(dump);
110+
}
111+
112+
private void ProcessDump(string dump)
113+
{
114+
using (StringReader r = new StringReader(dump))
115+
{
116+
string line;
117+
118+
while (r.Peek() != -1)
119+
{
120+
line = r.ReadLine();
121+
122+
if (line.StartsWith(PACKAGE))
123+
{
124+
//find name
125+
int nameStart = line.IndexOf(PACKAGE_NAME) + PACKAGE_NAME.Length;
126+
int nameLength = line.IndexOf(APOSTROPHE, nameStart) - nameStart;
127+
string name = line.Substring(nameStart, nameLength);
128+
129+
//find versionCode
130+
int versionCodeStart = line.IndexOf(PACKAGE_VERSION_CODE) + PACKAGE_VERSION_CODE.Length;
131+
int versionCodeLength = line.IndexOf(APOSTROPHE, versionCodeStart) - versionCodeStart;
132+
string versionCode = line.Substring(versionCodeStart, versionCodeLength);
133+
134+
//find versionName
135+
int versionNameStart = line.IndexOf(PACKAGE_VERSION_NAME) + PACKAGE_VERSION_NAME.Length;
136+
int versionNameLength = line.IndexOf(APOSTROPHE, versionNameStart) - versionNameStart;
137+
string versionName = line.Substring(versionNameStart, versionNameLength);
138+
139+
this.package = new PackageInfo(name, versionCode, versionName);
140+
}
141+
else if (line.StartsWith(APPLICATION))
142+
{
143+
//find label
144+
int labelStart = line.IndexOf(APPLICATION_LABEL) + APPLICATION_LABEL.Length;
145+
int labelLength = line.IndexOf(APOSTROPHE, labelStart) - labelStart;
146+
string label = line.Substring(labelStart, labelLength);
147+
148+
//find icon
149+
int iconStart = line.IndexOf(APPLICATION_ICON) + APPLICATION_ICON.Length;
150+
int iconLength = line.IndexOf(APOSTROPHE, iconStart) - iconStart;
151+
string icon = line.Substring(iconStart, iconLength);
152+
153+
this.application = new ApplicationInfo(label, icon);
154+
}
155+
else if (line.StartsWith(ACTIVITY))
156+
{
157+
//find name
158+
int nameStart = line.IndexOf(ACTIVITY_NAME) + ACTIVITY_NAME.Length;
159+
int nameLength = line.IndexOf(APOSTROPHE, nameStart) - nameStart;
160+
string name = line.Substring(nameStart, nameLength);
161+
162+
//find label
163+
int labelStart = line.IndexOf(ACTIVITY_LABEL) + ACTIVITY_LABEL.Length;
164+
int labelLength = line.IndexOf(APOSTROPHE, labelStart) - labelStart;
165+
string label = line.Substring(labelStart, labelLength);
166+
167+
//find icon
168+
int iconStart = line.IndexOf(ACTIVITY_ICON) + ACTIVITY_ICON.Length;
169+
int iconLength = line.IndexOf(APOSTROPHE, iconStart) - iconStart;
170+
string icon = line.Substring(iconStart, iconLength);
171+
172+
this.activity = new LaunchableActivity(name, label, icon);
173+
}
174+
else if (line.StartsWith(SDK_VERSION))
175+
{
176+
this.sdkVersion = line.Substring(SDK_VERSION.Length).Replace(APOSTROPHE, "");
177+
}
178+
else if (line.StartsWith(SDK_TARGET))
179+
{
180+
this.targetSdkVersion = line.Substring(SDK_TARGET.Length).Replace(APOSTROPHE, "");
181+
}
182+
else if (line.StartsWith(USES_PERMISSION))
183+
{
184+
this.usesPermission.Add(line.Substring(USES_PERMISSION.Length).Replace(APOSTROPHE, ""));
185+
}
186+
else if (line.StartsWith(DENSITIES))
187+
{
188+
string[] densities = line.Substring(DENSITIES.Length + 2).Split(new char[] { '\'', ' ' }, StringSplitOptions.RemoveEmptyEntries);
189+
190+
for (int i = 0; i < densities.Length; i++)
191+
this.densities.Add(int.Parse(densities[i]));
192+
}
193+
}
194+
}
195+
}
196+
197+
/// <summary>
198+
/// Contains information about an Apk's package
199+
/// </summary>
200+
public class PackageInfo
201+
{
202+
private string name;
203+
private string versionCode;
204+
private string versionName;
205+
206+
internal PackageInfo() : this(null, null, null) { }
207+
internal PackageInfo(string name, string versionCode, string versionName)
208+
{
209+
this.name = name;
210+
this.versionCode = versionCode;
211+
this.versionName = versionName;
212+
}
213+
214+
/// <summary>
215+
/// Gets a value indicating the Apk's package name
216+
/// </summary>
217+
public string Name
218+
{
219+
get { return this.name; }
220+
}
221+
222+
/// <summary>
223+
/// Gets a value indicating the Version Code of the Apk's package
224+
/// </summary>
225+
public string VersionCode
226+
{
227+
get { return this.versionCode; }
228+
}
229+
230+
/// <summary>
231+
/// Gets a value indicating the Version Name of the Apk's package
232+
/// </summary>
233+
public string VersionName
234+
{
235+
get { return this.versionName; }
236+
}
237+
}
238+
239+
/// <summary>
240+
/// Contains general information about an Apk
241+
/// </summary>
242+
public class ApplicationInfo
243+
{
244+
private string label;
245+
private string icon;
246+
247+
internal ApplicationInfo() : this(null, null) { }
248+
internal ApplicationInfo(string label, string icon)
249+
{
250+
this.label = label;
251+
this.icon = icon;
252+
}
253+
254+
/// <summary>
255+
/// Gets a value indicating the Application's Label
256+
/// </summary>
257+
public string Label
258+
{
259+
get { return this.label; }
260+
}
261+
262+
/// <summary>
263+
/// Gets a value indicating the path inside the apk to the Application's default icon
264+
/// </summary>
265+
public string Icon
266+
{
267+
get { return this.icon; }
268+
}
269+
}
270+
271+
/// <summary>
272+
/// Contains information about an Apk's main Activity
273+
/// </summary>
274+
public class LaunchableActivity
275+
{
276+
private string name;
277+
private string label;
278+
private string icon;
279+
280+
internal LaunchableActivity() : this(null, null, null) { }
281+
internal LaunchableActivity(string name, string label, string icon)
282+
{
283+
this.name = name;
284+
this.label = label;
285+
this.icon = icon;
286+
}
287+
288+
/// <summary>
289+
/// Gets a value indicating the name of the Apk's main Activity
290+
/// </summary>
291+
public string Name
292+
{
293+
get { return this.name; }
294+
}
295+
296+
/// <summary>
297+
/// Gets a value indicating the label of the Apk's main Activity
298+
/// </summary>
299+
public string Label
300+
{
301+
get { return this.label; }
302+
}
303+
304+
/// <summary>
305+
/// Gets a value indicating the path to the default icon of the Apk's main Activity
306+
/// </summary>
307+
public string Icon
308+
{
309+
get { return this.icon; }
310+
}
311+
}
312+
}
313+
}
314+
}

AndroidLib/Classes/AAPT/AAPT.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace RegawMOD.Android
6+
{
7+
/// <summary>
8+
/// Wrapper for the AAPT Android binary
9+
/// </summary>
10+
public partial class AAPT : IDisposable
11+
{
12+
private static Dictionary<string, string> RESOURCES = new Dictionary<string, string>
13+
{
14+
{"aapt.exe", "26a35ee028ed08d7ad0d18ffb6bb587a"}
15+
};
16+
17+
private string resDir;
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <c>AAPT</c> class
21+
/// </summary>
22+
public AAPT()
23+
{
24+
ResourceFolderManager.Register("AAPT");
25+
this.resDir = ResourceFolderManager.GetRegisteredFolderPath("AAPT");
26+
27+
ExtractResources(this.resDir);
28+
}
29+
30+
/// <summary>
31+
/// Dumps the specified Apk's badging information
32+
/// </summary>
33+
/// <param name="source">Source Apk on local machine</param>
34+
/// <returns><see cref="AAPT.Badging"/> object containing badging information</returns>
35+
public Badging DumpBadging(FileInfo source)
36+
{
37+
if (!source.Exists)
38+
throw new FileNotFoundException();
39+
40+
return new Badging(source, Command.RunProcessReturnOutput(Path.Combine(this.resDir, "aapt.exe"), "dump badging \"" + source.FullName + "\"", true));
41+
}
42+
43+
private void ExtractResources(string path)
44+
{
45+
string[] res = new string[RESOURCES.Count];
46+
RESOURCES.Keys.CopyTo(res, 0);
47+
48+
Extract.Resources("RegawMOD.Android", path, "Resources.AAPT", res);
49+
}
50+
51+
/// <summary>
52+
/// Call to free up resources after use of <c>AAPT</c>
53+
/// </summary>
54+
public void Dispose()
55+
{
56+
ResourceFolderManager.Unregister("AAPT");
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)