update: 更新 MainManifestDoc 修改工具
parent
2d05040807
commit
a240dc4e2c
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1429f4e79961470ba0597d4f3caceee8
|
||||||
|
timeCreated: 1711525393
|
||||||
|
|
@ -0,0 +1,252 @@
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Guru.Editor
|
||||||
|
{
|
||||||
|
using System.Xml;
|
||||||
|
using System.IO;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Android 配置修改器
|
||||||
|
/// </summary>
|
||||||
|
public class MainManifestDoc
|
||||||
|
{
|
||||||
|
private const string TargetPath = "Plugins/Android/AndroidManifest.xml";
|
||||||
|
private const string XmlnsAndroid = "xmlns:android";
|
||||||
|
private const string NamespaceAndroid = "http://schemas.android.com/apk/res/android";
|
||||||
|
private const string XmlnsTools= "xmlns:tools";
|
||||||
|
private const string NamespaceTools = "http://schemas.android.com/tools";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private XmlDocument _doc;
|
||||||
|
public XmlDocument Doc => _doc;
|
||||||
|
|
||||||
|
private string _docPath;
|
||||||
|
private bool _isReady = false;
|
||||||
|
|
||||||
|
private XmlElement _manifestNode;
|
||||||
|
private XmlElement _applicationNode;
|
||||||
|
private XmlNodeList _metadataList;
|
||||||
|
private XmlNodeList _permissionList;
|
||||||
|
|
||||||
|
|
||||||
|
#region Initiallize
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="docPath"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static MainManifestDoc Load(string docPath = "")
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(docPath))
|
||||||
|
{
|
||||||
|
docPath = Path.GetFullPath(Path.Combine(Application.dataPath, TargetPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!File.Exists(docPath))
|
||||||
|
{
|
||||||
|
Debug.LogError($"--- File not found: {docPath}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mod = new MainManifestDoc();
|
||||||
|
mod.ReadFromPath(docPath);
|
||||||
|
return mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MainManifestDoc Read(string xmlStr, string docPath = "")
|
||||||
|
{
|
||||||
|
var mod = new MainManifestDoc();
|
||||||
|
mod.ReadFromXml(xmlStr, docPath);
|
||||||
|
return mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从文件路径读取
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="docPath"></param>
|
||||||
|
public void ReadFromPath(string docPath)
|
||||||
|
{
|
||||||
|
_isReady = false;
|
||||||
|
if (File.Exists(_docPath))
|
||||||
|
{
|
||||||
|
_docPath = docPath;
|
||||||
|
var xmlStr = File.ReadAllText(docPath);
|
||||||
|
ReadFromXml(xmlStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void ReadFromXml(string xmlStr, string docPath = "")
|
||||||
|
{
|
||||||
|
_doc = new XmlDocument();
|
||||||
|
_doc.LoadXml(xmlStr);
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes the Doc
|
||||||
|
/// </summary>
|
||||||
|
private void Init()
|
||||||
|
{
|
||||||
|
// --- Root Nodes ---
|
||||||
|
_manifestNode = _doc.SelectSingleNode("manifest") as XmlElement;
|
||||||
|
_applicationNode = _doc.SelectSingleNode("manifest/application") as XmlElement;
|
||||||
|
// --- Metadatas ---
|
||||||
|
_metadataList = _applicationNode.SelectNodes("meta-data");
|
||||||
|
// --- Permissions ---
|
||||||
|
_permissionList = _applicationNode.SelectNodes("uses-permission");
|
||||||
|
|
||||||
|
AddXmlnsAndroid();
|
||||||
|
AddXmlnsTools();
|
||||||
|
_isReady = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Save Doc
|
||||||
|
/// </summary>
|
||||||
|
public void Save(string docPath = "")
|
||||||
|
{
|
||||||
|
if (_isReady)
|
||||||
|
{
|
||||||
|
if(string.IsNullOrEmpty(docPath)) docPath = _docPath;
|
||||||
|
if (!string.IsNullOrEmpty(docPath))
|
||||||
|
{
|
||||||
|
var dir = Directory.GetParent(docPath);
|
||||||
|
if(!dir.Exists) dir.Create();
|
||||||
|
_doc.Save(_docPath);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Node Opreation
|
||||||
|
|
||||||
|
|
||||||
|
public static bool AddAttribute(XmlElement node, string key, string value)
|
||||||
|
{
|
||||||
|
if (node != null)
|
||||||
|
{
|
||||||
|
if (node.HasAttribute(key))
|
||||||
|
{
|
||||||
|
node.Attributes[key].Value = value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
node.SetAttribute(key, value);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region API
|
||||||
|
|
||||||
|
public bool AddXmlnsAndroid()
|
||||||
|
{
|
||||||
|
return AddAttribute(_manifestNode, XmlnsAndroid, NamespaceAndroid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool AddXmlnsTools()
|
||||||
|
{
|
||||||
|
return AddAttribute(_manifestNode, XmlnsTools, NamespaceTools);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add Replace Item
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
public void AddReplaceItem(string item)
|
||||||
|
{
|
||||||
|
if (_manifestNode != null)
|
||||||
|
{
|
||||||
|
List<string> items = new List<string>(5);
|
||||||
|
if (_manifestNode.HasAttribute("replace", NamespaceTools))
|
||||||
|
{
|
||||||
|
var arr = _manifestNode.GetAttribute("replace",NamespaceTools).Split(',');
|
||||||
|
if(arr != null && arr.Length > 0)
|
||||||
|
{
|
||||||
|
items.AddRange(arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!items.Contains(item)) items.Add(item);
|
||||||
|
|
||||||
|
_manifestNode.SetAttribute("replace", NamespaceTools, string.Join(",", items));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set metadata
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
public void SetMetadata(string key, string value)
|
||||||
|
{
|
||||||
|
_metadataList = _applicationNode?.SelectNodes("meta-data") ?? null;
|
||||||
|
|
||||||
|
XmlElement item = null;
|
||||||
|
if (_metadataList != null)
|
||||||
|
{
|
||||||
|
foreach (XmlElement e in _metadataList)
|
||||||
|
{
|
||||||
|
// var nm = e.GetAttribute("name", NamespaceAndroid);
|
||||||
|
if (e.GetAttribute("name", NamespaceAndroid) is string k && k == key)
|
||||||
|
{
|
||||||
|
item = e;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item == null)
|
||||||
|
{
|
||||||
|
item = _doc.CreateElement("meta-data");
|
||||||
|
_applicationNode?.AppendChild(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
item.SetAttribute("name", NamespaceAndroid, key);
|
||||||
|
item.SetAttribute("value", NamespaceAndroid, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region Output
|
||||||
|
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
if (_doc != null) return _doc.InnerXml;
|
||||||
|
return this.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 89a1c7f77fcf4982adbbaf6dc61bd62d
|
||||||
|
timeCreated: 1711505949
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
fileFormatVersion: 2
|
|
||||||
guid: 4bac8424332c40739e56d3637f75ef73
|
|
||||||
timeCreated: 1711084911
|
|
||||||
|
|
@ -373,7 +373,7 @@ namespace Guru
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Analytics.LogCrashlytics(ex);
|
Analytics.LogCrashlytics($"{Tag} --- Json:{msg} Ex:{ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue