SDK_UnityMoney/Assets/EFSDK/Editor/SDKTool.cs

300 lines
9.9 KiB
C#
Executable File

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Microsoft.CSharp.RuntimeBinder;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
public class SDKTool
{
public static string GetPackageName()
{
return PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.Android);
}
public static string MavenStr = @"
maven {
url 'https://repo.dgtverse.cn/repository/maven-public/'
name 'maven_repo.dgtverse.cn'
}
";
public static Hashtable LoadProperties(string file)
{
var ht = new Hashtable(16);
string content = null;
try
{
content = file;
}
catch (Exception e)
{
return null;
}
var rows = content.Split('\n');
string[] kv = null;
foreach (var c in rows)
{
if (c.Trim().Length == 0)
continue;
kv = c.Split('=');
ht[kv[0].Trim()] = kv.Length switch
{
1 => "",
2 => kv[1].Trim(),
_ => ht[kv[0].Trim()]
};
}
return ht;
}
public static string GetSDKVersion()
{
var xmlText =
SDKEditorNetworkTool.GetText($"https://repo.dgtverse.cn/repository/tk_my/com/earn/money/{Application.identifier}/maven-metadata.xml");
if (string.IsNullOrEmpty(xmlText))
{
throw new RuntimeBinderException(
$"获取版本号失败 , 接口请求返回为空,或请求不到. https://repo.dgtverse.cn/repository/tk_my/com/earn/money/{Application.identifier}/maven-metadata.xml");
}
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlText);
XmlNodeList versions = xmlDoc.SelectNodes("//versioning/latest");
if (versions == null)
{
throw new RuntimeBinderException("获取版本号失败");
}
if (versions.Count > 0)
{
string latestVersion = versions[0].InnerText;
Debug.Log($"Latest version: {latestVersion}");
return latestVersion;
}
throw new RuntimeBinderException("解析xml失败");
}
catch (Exception e)
{
throw new RuntimeBinderException($"获取版本号失败 : XML parsing error: {e.Message}");
}
}
private void CopyGoogleServices(string path)
{
// var gpPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "SDKConfig" +
// Path.DirectorySeparatorChar + SDKTool.GetPackageName() + Path.DirectorySeparatorChar +
// "google-services.json";
string gpPath = Path.Combine(Application.streamingAssetsPath, "google-services.json");
if (!File.Exists(gpPath))
{
throw new BuildFailedException("Can't find google-services.json");
}
var targetPath = path + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "launcher" +
Path.DirectorySeparatorChar + "google-services.json";
Debug.Log("gpPath : " + gpPath + "\t targetPath : " + targetPath);
if (File.Exists(targetPath))
{
File.Delete(targetPath);
}
File.Copy(gpPath, targetPath);
}
private void AddFirebasePluginToGradle(string path)
{
var filePath =
$"{path}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}launcher{Path.DirectorySeparatorChar}build.gradle";
if (File.ReadAllText(filePath).Contains("com.google.gms.google-services"))
{
return;
}
var newLine = new List<string>
{
"apply plugin: 'com.google.gms.google-services'\n",
"apply plugin: 'com.google.firebase.crashlytics'\n"
};
newLine.AddRange(File.ReadLines(filePath));
File.WriteAllLines(filePath, newLine);
}
private void AddFirebasePlugin(string rootPath)
{
_AddFirebasePlugin($"{rootPath}{Path.DirectorySeparatorChar}build.gradle");
}
private void _AddFirebasePlugin(string filePath)
{
if (File.ReadAllText(filePath).Contains("com.google.gms:google-services"))
{
return;
}
var newLine = new List<string>();
foreach (var line in File.ReadLines(filePath))
{
var trim = line.Trim();
if (trim.Contains("com.google.gms.google-services") && trim.Contains("4.4.2"))
{
continue;
}
if (trim.Contains("com.google.firebase.crashlytics") && trim.Contains("2.9.4"))
{
continue;
}
if (trim.Contains("com.google.gms:google-services") && trim.Contains("4.3.10"))
{
continue;
}
if (trim.Contains("com.google.firebase:firebase-crashlytics-gradle") && trim.Contains("2.9.4"))
{
continue;
}
newLine.Add(line);
if (trim.StartsWith("google()"))
{
newLine.Add(MavenStr);
}
if (trim.StartsWith("classpath"))
{
newLine.Add("classpath 'com.google.gms:google-services:4.3.10'\n");
newLine.Add("classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.4'\n");
}
if (trim.StartsWith("id 'com.android.application'"))
{
newLine.Add("id 'com.google.gms.google-services' version '4.4.2' apply false\n");
newLine.Add("id 'com.google.firebase.crashlytics' version '2.9.4' apply false\n");
}
}
File.WriteAllLines(filePath, newLine);
}
private static string _manifestFilePath;
public static string GetManifestPath(string basePath)
{
if (string.IsNullOrEmpty(_manifestFilePath))
{
StringBuilder pathBuilder = new StringBuilder(basePath);
pathBuilder.Append(Path.DirectorySeparatorChar).Append("src");
pathBuilder.Append(Path.DirectorySeparatorChar).Append("main");
pathBuilder.Append(Path.DirectorySeparatorChar).Append("AndroidManifest.xml");
_manifestFilePath = pathBuilder.ToString();
Debug.Log($"_manifestFilePath = {_manifestFilePath}");
}
return _manifestFilePath;
}
internal class AndroidXmlDocument : XmlDocument
{
private string m_Path;
protected XmlNamespaceManager nsMgr;
public const string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
public AndroidXmlDocument(string path)
{
m_Path = path;
using (var reader = new XmlTextReader(m_Path))
{
reader.Read();
Load(reader);
}
nsMgr = new XmlNamespaceManager(NameTable);
nsMgr.AddNamespace("android", AndroidXmlNamespace);
}
public string Save()
{
return SaveAs(m_Path);
}
public string SaveAs(string path)
{
using (var writer = new XmlTextWriter(path, new UTF8Encoding(false)))
{
writer.Formatting = Formatting.Indented;
Save(writer);
}
return path;
}
}
internal class AndroidManifest : AndroidXmlDocument
{
private readonly XmlElement ApplicationElement;
public AndroidManifest(string path) : base(path)
{
ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
}
private XmlAttribute CreateAndroidAttribute(string key, string value, string prefix = "android", string namespaceURI = AndroidXmlNamespace)
{
XmlAttribute attr = CreateAttribute(prefix, key, namespaceURI);
attr.Value = value;
return attr;
}
internal XmlNode GetActivityWithLaunchIntent()
{
return SelectSingleNode(
"/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and " +
"intent-filter/category/@android:name='android.intent.category.LAUNCHER']", nsMgr);
}
internal void SetApplicationTheme(string appTheme)
{
ApplicationElement.Attributes.Append(CreateAndroidAttribute("theme", appTheme));
}
internal void SetStartingActivityName(string activityName)
{
GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("name", activityName));
}
internal void SetApplicationAttribute(string key, string value, string prefix = "android", string namespaceURI = AndroidXmlNamespace)
{
ApplicationElement.Attributes.Append(CreateAndroidAttribute(key, value, prefix, namespaceURI));
}
internal void RemoveApplicationAttribute(string key)
{
// ApplicationElement.Attributes.Remove(CreateAndroidAttribute(key, key));
var removeNamedItem = ApplicationElement.Attributes.RemoveNamedItem(key);
Debug.Log($"删除节点 key = {key} value = {removeNamedItem}");
}
internal void SetStartingActivityAttribute(string key, string value)
{
XmlNode node = GetActivityWithLaunchIntent();
Debug.Log($"Main节点 node = {node} key = {key} value = {value}");
if (node != null)
{
XmlAttributeCollection attributes = node.Attributes;
attributes.Append(CreateAndroidAttribute(key, value));
}
}
}
}