2021-08-11 11:06:46 +03:00
|
|
|
using System;
|
2016-04-21 16:40:25 +03:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using Microsoft.Build.Framework;
|
|
|
|
|
2023-11-17 10:12:37 +03:00
|
|
|
// Disable until we get around to enable + fix any issues.
|
|
|
|
#nullable disable
|
|
|
|
|
2022-09-28 17:25:35 +03:00
|
|
|
namespace Xamarin.MacDev.Tasks {
|
|
|
|
public static class AssetPackUtils {
|
2016-04-21 16:40:25 +03:00
|
|
|
const string Base36Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
const int HashEverythingLimit = 96;
|
|
|
|
|
|
|
|
static unsafe ulong ComputeHash (string tags)
|
|
|
|
{
|
|
|
|
var value = (ulong) tags.Length;
|
2022-09-28 17:25:35 +03:00
|
|
|
char [] buffer;
|
2016-04-21 16:40:25 +03:00
|
|
|
|
|
|
|
if (tags.Length > HashEverythingLimit) {
|
2022-09-28 17:25:35 +03:00
|
|
|
buffer = new char [HashEverythingLimit];
|
2016-04-21 16:40:25 +03:00
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
while (index < 32) {
|
2022-09-28 17:25:35 +03:00
|
|
|
buffer [index] = tags [index];
|
2016-04-21 16:40:25 +03:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = (tags.Length >> 1) - 16; i < 32; i++)
|
2022-09-28 17:25:35 +03:00
|
|
|
buffer [index++] = tags [i];
|
2016-04-21 16:40:25 +03:00
|
|
|
|
|
|
|
for (int i = tags.Length - 32; i < tags.Length; i++)
|
2022-09-28 17:25:35 +03:00
|
|
|
buffer [index++] = tags [i];
|
2016-04-21 16:40:25 +03:00
|
|
|
} else {
|
|
|
|
buffer = tags.ToCharArray ();
|
|
|
|
}
|
|
|
|
|
|
|
|
fixed (char* bufptr = buffer) {
|
2022-09-28 17:25:35 +03:00
|
|
|
ushort* end4 = ((ushort*) bufptr) + (tags.Length & ~3);
|
|
|
|
ushort* end = ((ushort*) bufptr) + tags.Length;
|
|
|
|
ushort* cur = (ushort*) bufptr;
|
2016-04-21 16:40:25 +03:00
|
|
|
|
|
|
|
while (cur < end4) {
|
2022-09-28 17:25:35 +03:00
|
|
|
value = (value * 67503105) + (ulong) (cur [0] * 16974593) + (ulong) (cur [1] * 66049) + (ulong) (cur [2] * 257) + (ulong) cur [3];
|
2016-04-21 16:40:25 +03:00
|
|
|
cur += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cur < end) {
|
|
|
|
value = (value * 257) + (ulong) *cur;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value + (value << (tags.Length & 31));
|
|
|
|
}
|
|
|
|
|
|
|
|
static string Base36Encode (ulong value)
|
|
|
|
{
|
2022-09-28 17:25:35 +03:00
|
|
|
var encoded = new char [13];
|
2016-04-21 16:40:25 +03:00
|
|
|
int index = 12;
|
|
|
|
|
|
|
|
do {
|
2022-09-28 17:25:35 +03:00
|
|
|
encoded [index--] = Base36Alphabet [(int) (value % 36)];
|
2016-04-21 16:40:25 +03:00
|
|
|
value /= 36;
|
|
|
|
} while (value != 0);
|
|
|
|
|
|
|
|
while (index >= 0)
|
2022-09-28 17:25:35 +03:00
|
|
|
encoded [index--] = '0';
|
2016-04-21 16:40:25 +03:00
|
|
|
|
|
|
|
return new string (encoded, 0, 13);
|
|
|
|
}
|
|
|
|
|
2022-09-28 17:25:35 +03:00
|
|
|
public static string [] ParseTags (string value)
|
2016-04-21 16:40:25 +03:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty (value))
|
2022-09-28 17:25:35 +03:00
|
|
|
return new string [0];
|
2016-04-21 16:40:25 +03:00
|
|
|
|
|
|
|
return value.Split (new [] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select (tag => tag.Trim ()).ToArray ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IList<string> GetResourceTags (ITaskItem item)
|
|
|
|
{
|
|
|
|
var tags = ParseTags (item.GetMetadata ("ResourceTags"));
|
|
|
|
|
|
|
|
if (tags.Length == 0)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
Array.Sort (tags, StringComparer.Ordinal);
|
|
|
|
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string GetAssetPackDirectory (string outputPath, string bundleIdentifier, IList<string> tags, out string hash)
|
|
|
|
{
|
|
|
|
bool addHash = false;
|
|
|
|
string id;
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.Count; i++) {
|
2022-09-28 17:25:35 +03:00
|
|
|
if (tags [i].IndexOf ('+') != -1) {
|
2016-04-21 16:40:25 +03:00
|
|
|
addHash = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = Base36Encode (ComputeHash (string.Join (" ", tags)));
|
|
|
|
|
|
|
|
id = string.Join ("+", tags);
|
|
|
|
|
|
|
|
if (addHash)
|
|
|
|
id += "+" + hash;
|
|
|
|
|
|
|
|
return Path.Combine (outputPath, "OnDemandResources", bundleIdentifier + "." + id + ".assetpack");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|