Skip to content

Commit

Permalink
lets use RtxtParser everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
dellis1972 committed Feb 21, 2022
1 parent bb1694a commit 6ce7304
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ bool Run(DirectoryAssemblyResolver res) {
var r = resource.Value;
switch (r.Type) {
case RType.Integer:
CreateIntProperty (resource.Key, r.Identifier, r.Id, resourceDesigner, module);
CreateIntProperty (r.ResourceType, r.Identifier, r.Id, resourceDesigner, module);
break;
case RType.Array:
CreateIntArrayProperty (resource.Key, r.Identifier, r.Ids, resourceDesigner, module);
CreateIntArrayProperty (r.ResourceType, r.Identifier, r.Ids, resourceDesigner, module);
break;
}
}
Expand Down
106 changes: 16 additions & 90 deletions src/Xamarin.Android.Build.Tasks/Utilities/ManagedResourceParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,98 +296,18 @@ public CodeTypeDeclaration Parse (string resourceDirectory, string rTxtFile, IEn

void ProcessRtxtFile (string file)
{
var lines = System.IO.File.ReadLines (file);
foreach (var line in lines) {
var items = line.Split (new char [] { ' ' }, 4);
int value = items [1] != "styleable" ? Convert.ToInt32 (items [3], 16) : -1;
string itemName = items [2];
switch (items [1]) {
case "anim":
CreateIntField (animation, itemName, value);
break;
case "animator":
CreateIntField (animator, itemName, value);
break;
case "attr":
CreateIntField (attrib, itemName, value);
break;
case "array":
CreateIntField (arrays, itemName, value);
break;
case "bool":
CreateIntField (boolean, itemName, value);
break;
case "color":
CreateIntField (colors, itemName, value);
break;
case "dimen":
CreateIntField (dimension, itemName, value);
break;
case "drawable":
CreateIntField (drawable, itemName, value);
break;
case "font":
CreateIntField (font, itemName, value);
break;
case "id":
CreateIntField (ids, itemName, value);
break;
case "integer":
CreateIntField (ints, itemName, value);
break;
case "interpolator":
CreateIntField (interpolators, itemName, value);
break;
case "layout":
CreateIntField (layout, itemName, value);
break;
case "menu":
CreateIntField (menu, itemName, value);
break;
case "mipmap":
CreateIntField (mipmaps, itemName, value);
break;
case "plurals":
CreateIntField (plurals, itemName, value);
break;
case "raw":
CreateIntField (raw, itemName, value);
break;
case "string":
CreateIntField (strings, itemName, value);
break;
case "style":
CreateIntField (style, itemName, value);
break;
case "styleable":
switch (items [0]) {
case "int":
CreateIntField (styleable, itemName, Convert.ToInt32 (items [3], 10));
var parser = new RtxtParser ();
var resources = parser.Parse (file, Log, map);
foreach (var resource in resources) {
var r = resource.Value;
var cl = CreateClass (r.ResourceType);
switch (r.Type) {
case RType.Integer:
CreateIntField (cl, r.Identifier, r.Id);
break;
case "int[]":
var arrayValues = items [3].Trim (new char [] { '{', '}' })
.Replace (" ", "")
.Split (new char [] { ',' });
CreateIntArrayField (styleable, itemName, arrayValues.Length,
arrayValues.Select (x => string.IsNullOrEmpty (x) ? -1 : Convert.ToInt32 (x, 16)).ToArray ());
case RType.Array:
CreateIntArrayField (cl, r.Identifier, r.Ids.Length, r.Ids);
break;
}
break;
case "transition":
CreateIntField (transition, itemName, value);
break;
case "xml":
CreateIntField (xml, itemName, value);
break;
// for custom views
default:
CodeTypeDeclaration customClass;
if (!custom_types.TryGetValue (items [1], out customClass)) {
customClass = CreateClass (items [1]);
custom_types.Add (items [1], customClass);
}
CreateIntField (customClass, itemName, value);
break;
}
}
}
Expand Down Expand Up @@ -435,15 +355,21 @@ CodeTypeDeclaration CreateResourceClass ()
return decl;
}

Dictionary<string, CodeTypeDeclaration> classes = new Dictionary<string, CodeTypeDeclaration> (StringComparer.OrdinalIgnoreCase);

CodeTypeDeclaration CreateClass (string type)
{
if (classes.ContainsKey (type)) {
return classes[type];
}
var t = new CodeTypeDeclaration (ResourceParser.GetNestedTypeName (type)) {
IsPartial = true,
TypeAttributes = TypeAttributes.Public,
};
t.Members.Add (new CodeConstructor () {
Attributes = MemberAttributes.Private,
});
classes.Add (type, t);
return t;
}

Expand Down
12 changes: 7 additions & 5 deletions src/Xamarin.Android.Build.Tasks/Utilities/RtxtParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum RType {
Array,
}
public struct R {
public RType Type = RType.Integer;
public RType Type;
public int Id;
public int [] Ids;
public string Identifier;
Expand All @@ -37,6 +37,7 @@ public Dictionary<string, R> Parse (string file, TaskLoggingHelper logger, Dicti
var result = new Dictionary<string, R> ();
if (File.Exists (file))
ProcessRtxtFile (file, result);

return result;
}

Expand All @@ -47,6 +48,7 @@ void ProcessRtxtFile (string file, Dictionary<string, R> result)
var items = line.Split (new char [] { ' ' }, 4);
int value = items [1] != "styleable" ? Convert.ToInt32 (items [3], 16) : -1;
string itemName = ResourceIdentifier.GetResourceName(items [1], items [2], resource_fixup, log);
string itemKey = $"{items [1]}:{itemName}";
switch (items [1]) {
case "anim":
case "animator":
Expand All @@ -69,7 +71,7 @@ void ProcessRtxtFile (string file, Dictionary<string, R> result)
case "style":
case "transition":
case "xml":
result[ items [1]] = new R () {
result [itemKey] = new R () {
ResourceType = items[1],
Identifier = itemName,
Id = value,
Expand All @@ -78,7 +80,7 @@ void ProcessRtxtFile (string file, Dictionary<string, R> result)
case "styleable":
switch (items [0]) {
case "int":
result[ items [1]] = new R () {
result [itemKey] = new R () {
ResourceType = items[1],
Identifier = itemName,
Id = Convert.ToInt32 (items [3], 10),
Expand All @@ -89,7 +91,7 @@ void ProcessRtxtFile (string file, Dictionary<string, R> result)
.Replace (" ", "")
.Split (new char [] { ',' });

result[ items [1]] = new R () {
result [itemKey] = new R () {
ResourceType = items[1],
Type = RType.Array,
Identifier = itemName,
Expand All @@ -100,7 +102,7 @@ void ProcessRtxtFile (string file, Dictionary<string, R> result)
break;
// for custom views
default:
result[ items [1]] = new R () {
result [itemKey] = new R () {
ResourceType = items[1],
Identifier = itemName,
Id = value,
Expand Down

0 comments on commit 6ce7304

Please sign in to comment.