using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
namespace UnityEditor.AddressableAssets.Build
{
///
/// This can be used to create a LinkXml for your build. This will ensure that the desired runtime types are packed into the build.
///
public class LinkXmlGenerator
{
Dictionary m_TypeConversion = new Dictionary();
HashSet m_Types = new HashSet();
///
/// Add runtime type to the LinkXml Generator.
///
/// The desired runtime types.
public void AddTypes(params Type[] types)
{
if (types == null)
return;
foreach (var t in types)
AddTypeInternal(t);
}
///
/// Add runtime type to the LinkXml Generator.
///
/// The desired runtime types.
public void AddTypes(IEnumerable types)
{
if (types == null)
return;
foreach (var t in types)
AddTypeInternal(t);
}
private void AddTypeInternal(Type t)
{
if (t == null)
return;
Type convertedType;
if (m_TypeConversion.TryGetValue(t, out convertedType))
m_Types.Add(convertedType);
else
m_Types.Add(t);
}
///
/// Setup runtime type conversion
///
/// Convert from type.
/// Convert to type.
public void SetTypeConversion(Type a, Type b)
{
m_TypeConversion[a] = b;
}
///
/// Save the LinkXml to the specified path.
///
/// The path to save the linker xml file.
public void Save(string path)
{
var assemblyMap = new Dictionary> ();
foreach (var t in m_Types)
{
var a = t.Assembly;
List types;
if (!assemblyMap.TryGetValue(a, out types))
assemblyMap.Add(a, types = new List());
types.Add(t);
}
XmlDocument doc = new XmlDocument();
var linker = doc.AppendChild(doc.CreateElement("linker"));
foreach (var k in assemblyMap)
{
var assembly = linker.AppendChild(doc.CreateElement("assembly"));
var attr = doc.CreateAttribute("fullname");
attr.Value = k.Key.FullName;
if (assembly.Attributes != null)
{
assembly.Attributes.Append(attr);
//TODO - this is a temporary fix to deal with iOS stripping.
if (k.Key.FullName.Contains("Addressables") || k.Key.FullName.Contains("ResourceManager"))
{
var preserveAssembly = doc.CreateAttribute("preserve");
preserveAssembly.Value = "all";
assembly.Attributes.Append(preserveAssembly);
}
foreach (var t in k.Value)
{
var typeEl = assembly.AppendChild(doc.CreateElement("type"));
var tattr = doc.CreateAttribute("fullname");
tattr.Value = t.FullName;
if (typeEl.Attributes != null)
{
typeEl.Attributes.Append(tattr);
var pattr = doc.CreateAttribute("preserve");
pattr.Value = "all";
typeEl.Attributes.Append(pattr);
}
}
}
}
doc.Save(path);
}
}
}