using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
namespace TyphoonUnitySDK
{
[CustomEditor(typeof(PublishVivoBatSetting))]
public class PublishVivoBatSettingInspector : Editor
{
public PublishVivoBatSetting Setting => target as PublishVivoBatSetting;
/*build gradle*/
public string GetBuildGradle()
{
return $@"{Setting.AndroidPublishProj}\proj\launcher_vivo\build.gradle";
}
/*AndroidManifest.xml*/
public string GetAndroidManifestXml()
{
return $@"{Setting.AndroidPublishProj}\proj\launcher_vivo\src\main\AndroidManifest.xml";
}
/*supplierconfig.json*/
public string GetSupplierConfigJson()
{
return $@"{Setting.AndroidPublishProj}\proj\launcher_vivo\src\main\assets\supplierconfig.json";
}
/*strings.xml*/
public string GetStringsXml()
{
return $@"{Setting.AndroidPublishProj}\proj\launcher_vivo\src\main\res\values\strings.xml";
}
/*icon*/
public string GetIconPath()
{
return $@"{Setting.AndroidPublishProj}\proj\launcher_vivo\src\main\res\mipmap-mdpi\app_icon.png";
}
public string GetLauncherVivoPath() => $@"{Setting.AndroidPublishProj}\proj\launcher_vivo";
public string GetBeeUnionSdkConfigJsonPath() =>
@$"{Setting.AndroidPublishProj}\proj\launcher_vivo\src\main\assets\bee_union_sdk_config.json";
public PublishVivoBatSetting Target => target as PublishVivoBatSetting;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var publish = GUILayout.Button("发布vivo", GUILayout.Height(32));
if (publish)
{
var sure = EditorUtility.DisplayDialog("提示", "发布?", "是");
if (sure)
{
Publish(Target);
}
}
}
public void Publish(PublishVivoBatSetting setting)
{
if (!Directory.Exists(setting.AndroidPublishProj))
{
throw new Exception($"找不到{setting.AndroidPublishProj}");
}
WriteUnityLibraryAAR();
WriteIcon();
WriteBuildGradle();
WriteStringsXml();
WriteAndroidManifestXml();
WriteSupplierConfigJson();
WriteBeeUnionSdkConfigJson();
Debug.Log("处理完毕!");
}
private void WriteBeeUnionSdkConfigJson()
{
var path = GetBeeUnionSdkConfigJsonPath();
var jsonObj = new
{
privacyUrl = $"{Setting.PrivacyPolicyYNoteUrl}",
orientation = Setting.Portrait ? 1 : 2,
appId = Setting.AppId,
debug = 1,
};
var json = jsonObj.ToXJson();
File.WriteAllText(path, json);
Debug.Log($"写入:{path}");
}
//写入build.gradle
public void WriteBuildGradle()
{
var path = GetBuildGradle();
var gradle = File.ReadAllText(path);
//替换包名,替换版本号
var pattern = "versionCode[\\s]*[0-9]*";
gradle = Regex.Replace(gradle, pattern, $"versionCode {Setting.VersionCode}");
pattern = "versionName[\\s]*'[0-9\\.]*'";
gradle = Regex.Replace(gradle, pattern, $"versionName '{Setting.Version}'");
pattern = "applicationId[\\s]*\\'[\\w\\.0-9]*\\'";
gradle = Regex.Replace(gradle, pattern, $"applicationId '{Setting.PackageName}'");
pattern = "targetSdkVersion[\\s]*[0-9]*";
gradle = Regex.Replace(gradle, pattern, $"targetSdkVersion {Setting.TargetSdkVersion}");
File.WriteAllText(path, gradle);
Debug.Log($"写入:{path}");
}
/*写入AndroidManifest.xml*/
private void WriteAndroidManifestXml()
{
var orientation = Setting.Portrait ? "portrait" : "landscape";
var path = GetAndroidManifestXml();
var xml = File.ReadAllText(path);
var pattern = "";
xml = Regex.Replace(xml, pattern,
$"");
File.WriteAllText(path, xml);
Debug.Log($"写入:{path}");
}
/*写入supplierconfig.json*/
private void WriteSupplierConfigJson()
{
var json = $@"{{
""supplier"":{{
""vivo"":{{
""appid"":""{Setting.AppId}""
}},
""xiaomi"":{{
}},
""huawei"":{{
}},
""oppo"":{{
}}
}}
}}
";
var path = GetSupplierConfigJson();
File.WriteAllText(path, json);
Debug.Log($"写入:{path}");
}
/*写入strings.xml*/
private void WriteStringsXml()
{
var path = GetStringsXml();
var xml = File.ReadAllText(path);
var pattern = ".*";
xml = Regex.Replace(xml, pattern, $"{Setting.GameName}");
File.WriteAllText(path, xml);
Debug.Log($"写入:{path}");
}
//写入unityLibrary-release.aar
private void WriteUnityLibraryAAR()
{
var unityProj = GetMatchChinaAndroidProj();
if (unityProj == null)
{
throw new Exception($"找不到unity导出工程");
}
//尝试获取unitylibrary.aar
var aar = $"{unityProj}/unityLibrary/build/outputs/aar/unityLibrary-release.aar";
if (!File.Exists(aar))
{
throw new Exception($"找不到{aar}");
}
//复制aar到指定目录
var to = @$"{Setting.AndroidPublishProj}\proj\unionsdk\libs\{Path.GetFileName(aar)}";
Debug.Log($"复制{aar}->{to}");
File.Copy(aar, to, true);
}
/*写入icon*/
private void WriteIcon()
{
var path = GetIconPath();
var iconPath = AssetDatabase.GetAssetPath(Setting.Icon);
File.Copy(iconPath, path, true);
Debug.Log($"写入:{path}");
}
///
/// 正则表达式更换
///
public static void RegexReplace(string file, string pattern, string replacement)
{
var txt = File.ReadAllText(file);
Regex.Replace(txt, pattern, replacement);
File.WriteAllText(file, txt);
}
public static void RegexReplace(string file, params PatternReplacement[] replacements)
{
var txt = File.ReadAllText(file);
foreach (var e in replacements)
{
txt = Regex.Replace(txt, e.Pattern, e.Replacement);
}
File.WriteAllText(file, txt);
}
public class PatternReplacement
{
public string Pattern;
public string Replacement;
public PatternReplacement(string pattern, string replacement)
{
Pattern = pattern;
Replacement = replacement;
}
}
public class ReplacementFileSettings
{
public string FilePath;
public PatternReplacement Replacement;
public ReplacementFileSettings(string filePath, string pattern, string replacement)
{
FilePath = filePath;
Replacement = new PatternReplacement(pattern, replacement);
}
}
public class ProjInfo
{
public string Path;
public long ExportTime;
public ProjInfo(string path, long exportTime)
{
Path = path;
ExportTime = exportTime;
}
}
//获取最匹配的导出文件夹
private static string GetMatchChinaAndroidProj()
{
var china_android_proj = $".typhoonoutput";
if (!Directory.Exists(china_android_proj))
{
throw new Exception($"找不到{china_android_proj}");
}
var dir = Directory.GetDirectories(china_android_proj);
var match = new List();
foreach (var item in dir)
{
var folderName = Path.GetFileName(item);
if (folderName.StartsWith("china_android_"))
{
var time = long.Parse(folderName.Replace("china_android_", ""));
match.Add(new ProjInfo(item, time));
}
}
match.Sort((a, b) => { return b.ExportTime.CompareTo(a.ExportTime); });
if (match.Count > 0)
{
return match.First().Path;
}
return null;
}
}
}