Skip to content

Commit

Permalink
ff
Browse files Browse the repository at this point in the history
  • Loading branch information
dellis1972 committed May 3, 2022
1 parent 1fa8d13 commit a2ed4fa
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 44 deletions.
123 changes: 82 additions & 41 deletions src/Xamarin.Android.Build.Tasks/Utilities/FileResourceParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ class FileResourceParser : ResourceParser
public string ResourceFlagFile { get; set; }

Dictionary<R, R []> arrayMapping = new Dictionary<R, R []> ();
public IDictionary<string, R> Parse (string resourceDirectory, IEnumerable<string> additionalResourceDirectories, Dictionary<string, string> resourceMap)
List<string> custom_types = new List<string> ();
public IList<R> Parse (string resourceDirectory, IEnumerable<string> additionalResourceDirectories, Dictionary<string, string> resourceMap)
{
Log.LogDebugMessage ($"Parsing Directory {resourceDirectory}");
var result = new SortedDictionary<string, R> ();
Dictionary<string, SortedSet<R>> resources = new Dictionary<string, SortedSet<R>> ();
var result = new List<R> ();
Dictionary<string, ICollection<R>> resources = new Dictionary<string, ICollection<R>> ();
foreach (var kvp in RtxtParser.knownTypes) {
Log.LogDebugMessage ($"Adding {kvp.Key}");
resources.Add (kvp.Key, new SortedSet<R> ());
if (kvp.Key == "styleable") {
resources.Add (kvp.Key, new List<R> ());
continue;
}
resources.Add (kvp.Key, new SortedSet<R> (new RComparer ()));
}
foreach (var dir in Directory.EnumerateDirectories (resourceDirectory, "*", SearchOption.TopDirectoryOnly)) {
foreach (var file in Directory.EnumerateFiles (dir, "*.*", SearchOption.AllDirectories)) {
Expand All @@ -47,23 +52,77 @@ public IDictionary<string, R> Parse (string resourceDirectory, IEnumerable<strin
}
}
}

// now generate the Id's we need in a specific order
List<string> declarationIds = new List<string> ();
declarationIds.Add ("attrib");
declarationIds.Add ("drawable");
declarationIds.Add ("mipmaps");
declarationIds.Add ("font");
declarationIds.Add ("layout");
declarationIds.Add ("animation");
declarationIds.Add ("animator");
declarationIds.Add ("transition");
declarationIds.Add ("xml");
declarationIds.Add ("raw");
declarationIds.Add ("dimension");
declarationIds.Add ("strings");
declarationIds.Add ("arrays");
declarationIds.Add ("plurals");
declarationIds.Add ("boolean");
declarationIds.Add ("colors");
declarationIds.Add ("ints");
declarationIds.Add ("menu");
declarationIds.Add ("id");
// custom types
foreach (var customClass in custom_types) {
declarationIds.Add (customClass);
}

declarationIds.Add ("interpolators");
declarationIds.Add ("style");
declarationIds.Add ("styleable");

declarationIds.Sort ((a, b) => {
return string.Compare (a, b, StringComparison.OrdinalIgnoreCase);
});

string itemPackageId = "0x7f";

foreach (var t in declarationIds) {
int itemid = 0;
Log.LogDebugMessage ($"Processing {t}");
foreach (R r in resources[t].OrderBy(x => x.ToSortedString(), StringComparer.Ordinal)) {
int typeid = 0;
int id = Convert.ToInt32 (itemPackageId + typeid.ToString ("X2") + itemid.ToString ("X4"), fromBase: 16);
//r.Id = id;
}
}

foreach (var kvp in resources) {
foreach (R r in kvp.Value) {
if (!result.ContainsKey (r.ToSortedString()))
result.Add (r.ToSortedString(), r);
result.Add (r);
}
}
result.Sort (new RComparer ());

return result;
}

class RComparer : IComparer<R> {
public int Compare(R a, R b) {
return string.Compare (a.ToSortedString (), b.ToSortedString (), StringComparison.Ordinal);
}
}

HashSet<string> resourceNamesToUseDirectly = new HashSet<string> () {
"integer-array",
"string-array",
"declare-styleable",
"add-resource",
};

void ProcessResourceFile (string file, Dictionary<string, SortedSet<R>> resources)
void ProcessResourceFile (string file, Dictionary<string, ICollection<R>> resources)
{
Log.LogDebugMessage ($"{nameof(ProcessResourceFile)} {file}");
var fileName = Path.GetFileNameWithoutExtension (file);
Expand All @@ -87,14 +146,10 @@ void ProcessResourceFile (string file, Dictionary<string, SortedSet<R>> resource
default:
break;
}
// if (!resources.ContainsKey (path)) {
// Log.LogDebugMessage ($"Registering {path}");
// resources[path] = new SortedSet<R>();
// }
CreateResourceField (path, fileName, resources);
}

void CreateResourceField (string root, string id, Dictionary<string, SortedSet<R>> resources) {
void CreateResourceField (string root, string id, Dictionary<string, ICollection<R>> resources) {
var i = root.IndexOf ('-');
var item = i < 0 ? root : root.Substring (0, i);
item = resourceNamesToUseDirectly.Contains (root) ? root : item;
Expand Down Expand Up @@ -122,7 +177,7 @@ void CreateResourceField (string root, string id, Dictionary<string, SortedSet<R
resources[item].Add (r);
}

void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resources)
void ProcessStyleable (XmlReader reader, Dictionary<string, ICollection<R>> resources)
{
Log.LogDebugMessage ($"{nameof(ProcessStyleable)}");
string topName = null;
Expand Down Expand Up @@ -160,16 +215,6 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
reader.MoveToElement ();
if (reader.LocalName == "attr") {
attribs.Add (name);
} else {
if (name != null) {
var r = new R () {
ResourceTypeName = "id",
Identifier = name,
Id = -1,
};
Log.LogDebugMessage ($"Adding {r}");
resources [r.ResourceTypeName].Add (r);
}
}
}
var field = new R () {
Expand Down Expand Up @@ -205,19 +250,22 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
if (field.Type != RType.Array)
return;
arrayMapping.Add (field, fields.ToArray ());
field.Ids = new int [fields.Count];
field.Ids = new int [attribs.Count];
for (int idx =0; idx < field.Ids.Length; idx++)
field.Ids[idx] = -1;
resources [field.ResourceTypeName].Add (field);
foreach (R r in fields) {
int id = 0;
foreach (string r in attribs) {
resources [field.ResourceTypeName].Add (new R () {
ResourceTypeName = field.ResourceTypeName,
Identifier = $"{field.Identifier}_{r.Identifier.Replace (":", "_")}",
Id = 0,
Identifier = $"{field.Identifier}_{r.Replace (":", "_")}",
Id = id++,
});
}
}
}

void ProcessXmlFile (string file, Dictionary<string, SortedSet<R>> resources)
void ProcessXmlFile (string file, Dictionary<string, ICollection<R>> resources)
{
Log.LogDebugMessage ($"{nameof(ProcessXmlFile)}");
using (var reader = XmlReader.Create (file)) {
Expand All @@ -227,7 +275,7 @@ void ProcessXmlFile (string file, Dictionary<string, SortedSet<R>> resources)
if (reader.IsStartElement ()) {
var elementName = reader.Name;
if (elementName == "declare-styleable" || elementName == "configVarying" || elementName == "add-resource") {
ProcessStyleable (reader, resources);
ProcessStyleable (reader.ReadSubtree (), resources);
continue;
}
if (reader.HasAttributes) {
Expand Down Expand Up @@ -273,19 +321,12 @@ void ProcessXmlFile (string file, Dictionary<string, SortedSet<R>> resources)
if (!string.IsNullOrEmpty (name)) {
CreateResourceField (type ?? elementName, name, resources);
}
//if (!string.IsNullOrEmpty (custom_id) && !custom_types.TryGetValue (custom_id, out customClass)) {
//customClass = CreateClass (custom_id);
//custom_types.Add (custom_id, customClass);
//}
if (!string.IsNullOrEmpty (custom_id) && !resources.ContainsKey (custom_id)) {
resources.Add (custom_id, new SortedSet<R> (new RComparer ()));
custom_types.Add (custom_id);
}
if (!string.IsNullOrEmpty (id)) {
//CreateIntField (customClass ?? ids, id);
var r = new R () {
ResourceTypeName = custom_id ?? "id",
Identifier = id,
Id = -1,
};
Log.LogDebugMessage ($"Adding 2 {r}");
resources[r.ResourceTypeName].Add (r);
CreateResourceField (custom_id ?? "id", id.Replace ("-", "_"), resources);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Xamarin.Android.Build.Tasks/Utilities/RtxtParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public string ToSortedString () {
}

public int CompareTo(R other) {
return String.Compare (ToSortedString (), other.ToSortedString (), StringComparison.Ordinal);
return String.Compare (ToSortedString (), other.ToSortedString (), StringComparison.OrdinalIgnoreCase);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Utilities/RtxtWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace Xamarin.Android.Tasks
/// Write a list of Item to a file
///
public class RtxtWriter {
public void Write (string file, IDictionary<string, R> items) {
File.WriteAllLines (file, items.Values.Select (x => x.ToString ()), Encoding.UTF8);
public void Write (string file, IList<R> items) {
File.WriteAllLines (file, items.Select (x => x.ToString ()), Encoding.UTF8);
}
}
}

0 comments on commit a2ed4fa

Please sign in to comment.