| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using UnityEditor;
- using UnityEngine;
- namespace Pack
- {
- public static class PackRun
- {
- private static PackPlatforms s_Configs = null;
- public static PackPlatforms GetConfigs(bool forceRefresh = false)
- {
- if (s_Configs == null || forceRefresh)
- {
- s_Configs = null;
- try
- {
- string path = Path.GetFullPath(Application.dataPath + PackConstant.PackPlatformConfigPath);
- if (File.Exists(path))
- {
- UTF8Encoding uTF8Encoding = new UTF8Encoding(false);
- string configStr = File.ReadAllText(path, uTF8Encoding);
- s_Configs = JsonUtility.FromJson<PackPlatforms>(configStr);
- }
- }
- catch (Exception e)
- {
- s_Configs = null;
- Debug.LogException(e);
- }
- }
- return s_Configs;
- }
- public static void SaveConfigs(PackPlatforms packPlatforms)
- {
- if (packPlatforms == null) return;
- try
- {
- string path = Path.GetFullPath(Application.dataPath + PackConstant.PackPlatformConfigPath);
- if (!File.Exists(path))
- {
- if (!Directory.Exists(Path.GetDirectoryName(path)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- }
- var fs = File.Create(path);
- fs.Dispose();
- }
- s_Configs = packPlatforms;
- string configStr = JsonUtility.ToJson(packPlatforms, true);
- UTF8Encoding uTF8Encoding = new UTF8Encoding(false);
- File.WriteAllText(path, configStr, uTF8Encoding);
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- }
- }
- public static void ChangePlatform(string channelUniqueId)
- {
- PackPlatforms packPlatforms = GetConfigs(true);
- foreach (PackPlatformBase packPlatformBase in packPlatforms.packPlatforms)
- {
- if (channelUniqueId == packPlatformBase.channelUniqueId)
- {
- ChangePlatform(packPlatformBase);
- return;
- }
- }
- throw new PackException("目标打包平台是错误的");
- }
- public static void ChangePlatform(PackPlatformBase packPlatformBase)
- {
- if (packPlatformBase == null)
- {
- throw new PackException("目标打包平台是错误的");
- }
- if (!packPlatformBase.CanChangePlatform())
- {
- packPlatformBase.ChangePlatform();
- return;
- }
- if (EditorApplication.isCompiling)
- {
- throw new PackException("编辑器正在编译脚本文件,不能开始切换平台");
- }
- ClearCurEnv();
- RecordCurEnv(packPlatformBase);
- packPlatformBase.ChangePlatform();
- }
- public static void BuildAB(string channelUniqueId)
- {
- PackPlatforms packPlatforms = GetConfigs(true);
- foreach (PackPlatformBase packPlatformBase in packPlatforms.packPlatforms)
- {
- if (channelUniqueId == packPlatformBase.channelUniqueId)
- {
- BuildAB(packPlatformBase);
- return;
- }
- }
- throw new PackException("目标打包平台是错误的");
- }
- public static void BuildAB(PackPlatformBase packPlatformBase)
- {
- if (packPlatformBase == null)
- {
- throw new PackException("目标打包平台是错误的");
- }
- if (!packPlatformBase.CanBuildAssetBundles())
- {
- packPlatformBase.BuildAssetBundles();
- return;
- }
- if (EditorApplication.isCompiling)
- {
- throw new PackException("编辑器正在编译脚本文件,不能开始编译AB");
- }
- ClearCurEnv();
- RecordCurEnv(packPlatformBase);
- packPlatformBase.BuildAssetBundles();
- }
- public static void BuildLuaAB(PackPlatformBase packPlatformBase)
- {
- if (packPlatformBase == null)
- {
- throw new PackException("目标打包平台是错误的");
- }
- if (!packPlatformBase.CanBuildAssetBundles())
- {
- packPlatformBase.BuildOnlyLua();
- return;
- }
- if (EditorApplication.isCompiling)
- {
- throw new PackException("编辑器正在编译脚本文件,不能开始编译AB");
- }
- ClearCurEnv();
- RecordCurEnv(packPlatformBase);
- packPlatformBase.BuildOnlyLua();
- }
- public static void BuildApp(string channelUniqueId)
- {
- PackPlatforms packPlatforms = GetConfigs(true);
- foreach (PackPlatformBase packPlatformBase in packPlatforms.packPlatforms)
- {
- if (channelUniqueId == packPlatformBase.channelUniqueId)
- {
- BuildApp(packPlatformBase);
- return;
- }
- }
- throw new PackException("目标打包平台是错误的");
- }
- public static void BuildApp(PackPlatformBase packPlatformBase)
- {
- if (packPlatformBase == null)
- {
- throw new PackException("目标打包平台是错误的");
- }
- if (!packPlatformBase.CanBuildApp())
- {
- packPlatformBase.BuildApp();
- return;
- }
- if (EditorApplication.isCompiling)
- {
- throw new PackException("编辑器正在编译脚本文件,不能开始编译App");
- }
- ClearCurEnv();
- RecordCurEnv(packPlatformBase);
- packPlatformBase.BuildApp();
- }
- public static string GetCurChannelUniqueId()
- {
- if (packToolsSettings != null)
- {
- return packToolsSettings.channelUniqueId;
- }
- return "";
- }
- public static void ClearBuildAndResetDefault()
- {
- if (EditorApplication.isCompiling)
- {
- throw new PackException("编辑器正在编译脚本文件,不能开始编译App");
- }
- ClearCurEnv();
- new PackPlatformDefault().ChangePlatform();
- }
- private static void ClearCurEnv()
- {
- PackPlatforms packPlatforms = GetConfigs();
- string channelUniqueId = string.Empty;
- string buildTargetStr = string.Empty;
- if (packToolsSettings != null)
- {
- channelUniqueId = packToolsSettings.channelUniqueId;
- buildTargetStr = packToolsSettings.buildClassName;
- }
- PackPlatformBase curPackPlatformBase = null;
- if (!string.IsNullOrEmpty(channelUniqueId))
- {
- curPackPlatformBase = packPlatforms.packPlatforms.FindFirst((x) => x.channelUniqueId == channelUniqueId);
- }
- if (curPackPlatformBase != null)
- {
- curPackPlatformBase.BuildClear();
- }
- else
- {
- curPackPlatformBase = packPlatforms.packPlatforms.FindFirst((x) => x.GetType().FullName == buildTargetStr);
- if (curPackPlatformBase != null)
- {
- curPackPlatformBase.BuildClear();
- }
- }
- RecordCurEnv(null);
- }
- private static void RecordCurEnv(PackPlatformBase packPlatformBase)
- {
- if (packPlatformBase == null)
- {
- if (packToolsSettings != null)
- {
- packToolsSettings.channelUniqueId = string.Empty;
- packToolsSettings.buildClassName = string.Empty;
- }
- }
- else
- {
- if (packToolsSettings != null)
- {
- packToolsSettings.channelUniqueId = packPlatformBase.channelUniqueId;
- packToolsSettings.buildClassName = packPlatformBase.GetType().FullName;
- }
- }
- SavePackToolsSettings();
- }
- private static void CommandLineBuildApp()
- {
- if (EditorApplication.isCompiling)
- {
- throw new PackException("编辑器正在编译脚本文件,不能开始执行命令行");
- }
- Dictionary<string, string> args = GetValidCommandLineArgs(Environment.GetCommandLineArgs());
- if (args.Count <= 0)
- {
- throw new PackException("参数为空,无法打包");
- }
- if (args.ContainsKey("-h"))
- {
- string content = GetCanPackPlatformStr();
- throw new PackException(content);
- }
- if (args.ContainsKey("-buildAB"))
- {
- if (string.IsNullOrEmpty(args["-buildAB"]))
- throw new PackException("目标平台不能为空,请使用-h来查看可编译平台");
- else
- {
- BuildAB(args["-buildAB"]);
- return;
- }
- }
- if (args.ContainsKey("-buildApp"))
- {
- if (string.IsNullOrEmpty(args["-buildApp"]))
- throw new PackException("目标平台不能为空,请使用-h来查看可编译平台");
- else
- {
- BuildApp(args["-buildApp"]);
- return;
- }
- }
- throw new PackException("无可执行的命令");
- }
- private static void CommandLineLog(string message)
- {
- Debug.Log(PackConstant.TAG_START + message + PackConstant.TAG_END);
- }
- private static List<string> s_ValidCommandLineArgNames = new List<string>() {
- "-h",
- "-buildAB",
- "-buildApp",
- "-androidEnv"
- };
- private static Dictionary<string, string> GetValidCommandLineArgs(string[] args)
- {
- Dictionary<string, string> maps = new Dictionary<string, string>();
- for (int i = 0, iMax = args.Length; i < iMax; i++)
- {
- if (s_ValidCommandLineArgNames.Contains(args[i]))
- {
- if (i + 1 >= iMax)
- maps.Add(args[i], string.Empty);
- else
- maps.Add(args[i], args[i + 1]);
- i++;
- }
- }
- return maps;
- }
- private static string GetCanPackPlatformStr()
- {
- PackPlatforms packPlatforms = GetConfigs(true);
- if (packPlatforms == null || packPlatforms.packPlatforms == null ||
- packPlatforms.packPlatforms.Length <= 0)
- {
- return "无可打包的平台,请检查";
- }
- StringBuilder sb = new StringBuilder();
- sb.Append("ID");
- sb.Append("\t\t\t\t");
- sb.Append("Name");
- sb.Append("\n");
- for (int i = 0, iMax = packPlatforms.packPlatforms.Length; i < iMax; i++)
- {
- PackPlatformBase packPlatformBase = packPlatforms.packPlatforms[i];
- sb.Append(packPlatformBase.channelUniqueId);
- sb.Append("\t\t\t");
- sb.Append(packPlatformBase.GetChannelUniqueName());
- sb.Append("\n");
- }
- string content = sb.ToString();
- if (string.IsNullOrEmpty(content))
- {
- return "无可打包的平台,请检查";
- }
- return content;
- }
- private static void BuildPlayerHandler(BuildPlayerOptions options)
- {
- PackWindow.OpenPackWindow();
- EditorUtility.DisplayDialog("提示", "请使用打包工具来打包", "好的");
- }
- private static void OnQuitting()
- {
- SavePackToolsSettings();
- }
- //[InitializeOnLoadMethod]
- private static void RegisterBuildPlayerHandler()
- {
- BuildPlayerWindow.RegisterBuildPlayerHandler(BuildPlayerHandler);
- EditorApplication.quitting += OnQuitting;
- }
- private static PackToolsSettings s_PackToolsSettings = null;
- public static PackToolsSettings packToolsSettings
- {
- get
- {
- if (s_PackToolsSettings == null)
- {
- if (File.Exists(Application.dataPath + PackConstant.CurPackEnvPath))
- {
- string content = File.ReadAllText(Application.dataPath + PackConstant.CurPackEnvPath);
- s_PackToolsSettings = JsonUtility.FromJson<PackToolsSettings>(content);
- }
- if (s_PackToolsSettings == null)
- s_PackToolsSettings = new PackToolsSettings();
- }
- return s_PackToolsSettings;
- }
- }
- public static void SavePackToolsSettings()
- {
- if (packToolsSettings != null && packToolsSettings.IsDirty())
- {
- File.WriteAllText(Application.dataPath + PackConstant.CurPackEnvPath, JsonUtility.ToJson(packToolsSettings));
- }
- }
- [Serializable]
- public class PackToolsSettings : ISerializationCallbackReceiver
- {
- [SerializeField]
- private string m_ChannelUniqueId = "";
- [SerializeField]
- private string m_BuildClassName = "";
- public string channelUniqueId
- {
- get; set;
- }
- public string buildClassName
- {
- get; set;
- }
- public PackToolsSettings()
- {
- OnAfterDeserialize();
- }
- public bool IsDirty()
- {
- if (m_ChannelUniqueId != channelUniqueId)
- return true;
- if (m_BuildClassName != buildClassName)
- return true;
- return false;
- }
- public void OnAfterDeserialize()
- {
- channelUniqueId = m_ChannelUniqueId;
- buildClassName = m_BuildClassName;
- }
- public void OnBeforeSerialize()
- {
- m_ChannelUniqueId = channelUniqueId;
- m_BuildClassName = buildClassName;
- }
- }
- }
- }
|