WXBundleSelectorWindow.cs 11.4 KB
Newer Older
J
junkunzhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
using System;

namespace WeChatWASM
{
	public class WXBundleSelectorWindow : EditorWindow
	{
		public static WXEditorScriptObject miniGameConf;
		private int totalPage; //页面总数
		private
		const int countPerPage = 15; //定义每一页的数量
		private int page = 1; //当前page
		private
		const int pathMaxLength = 64;
		private Vector2 scrollRoot;
		private string[] ignoreRule;
		private string[] noneIgnoreRule;
		private string searchText = "";
		private string searchTextCache = ""; //做一个Cache 因为 OnGUI 是频繁调用的,不应该频繁处理
		private void OnEnable()
		{
			this.init();
		}
		private void init()
		{
			miniGameConf = UnityUtil.GetEditorConf();
			this.loadIgnore();

			//扫描微信小游戏导出目录
			this.scanMiniGameDirBundle(true);
		}
		private List<string> fileList = new List<string>();
		private List<string> showFileList = new List<string>();     //搜索显示状态下的列表
		private List<string> selectedFileList = new List<string>();
		/**
		    扫描微信小游戏目录内的资源包信息
		    */
		private void scanMiniGameDirBundle(bool userule = false)
		{
			if (string.IsNullOrEmpty(miniGameConf.CompressTexture.bundleSuffix))
			{
				this.showToast("bundle后缀不能为空", true);
				return;
			}
			if (string.IsNullOrEmpty(miniGameConf.ProjectConf.DST))
			{
				this.showToast("请先转换为小游戏", true);
				return;
			}
			if (!File.Exists(miniGameConf.ProjectConf.DST + "/webgl/index.html"))
			{
				this.showToast("请先转换为小游戏,并确保导出目录下存在webgl目录");
				return;
			}
			string bundleSuffixArg = miniGameConf.CompressTexture.bundleSuffix;
			string[] bundleSuffix = bundleSuffixArg.Split(';');
			string sourceDir = miniGameConf.ProjectConf.DST + "/webgl".Replace('\\', '/');
			string bundleDir = miniGameConf.CompressTexture.bundleDir;
			this.fileList.Clear();
			this.fileList.Add("首包资源");
			this.page = 1;
			this.recFile(sourceDir, bundleSuffix);
			if (!string.IsNullOrEmpty(bundleDir))
			{
				bundleDir = bundleDir.Replace('\\', '/');
				this.recFile(bundleDir, bundleSuffix);
			}

			if (userule) //规则中存在一些特定的要加入的
			{
				for (int i = 0; i < this.ignoreRule.Length; i++)
				{
					string path = this.ignoreRule[i];
					if (path.Equals("首包资源"))
					{
						if (!this.selectedFileList.Contains(path))
						{
							this.selectedFileList.Add(path);
						}
						continue;
					}
					FileInfo info = new FileInfo(this.ignoreRule[i]);
					if (info.Exists)
					{
						if (!this.fileList.Contains(path))
						{
							this.fileList.Add(path);
						}
						if (!this.selectedFileList.Contains(path))
						{
							this.selectedFileList.Add(path);
						}
					}
				}
			}

			//重新扫描后对已选资源删除不再扫描结果内的元素
			foreach (string item in this.selectedFileList)
			{
				if (!this.fileList.Contains(item))
				{
					this.selectedFileList.Remove(item);
				}
			}

			if (userule) //此时排除一些不忽略的
			{
				for (int i = 0; i < this.noneIgnoreRule.Length; i++)
				{
					string path = this.noneIgnoreRule[i];
					if (this.selectedFileList.Contains(path))
					{
						this.selectedFileList.Remove(path);
					}
				}
			}


			this.search();
			if (this.fileList.Count == 1)
			{
				this.showToast("请检查bundle后缀以及资源目录内容,未搜索到相关资源", true);
				return;
			}
			if (!userule)
				this.showToast($"搜索完成,共计 {this.fileList.Count - 1} 个bundle资源");
		}
		/**
			递归搜素
			*/
		private void recFile(string dir, string[] bundleSuffix)
		{
			if (!Directory.Exists(dir))
			{
				this.showToast($"目录无效【{dir}】", true);
				return;
			}
			DirectoryInfo dirInfo = new DirectoryInfo(dir);
			FileSystemInfo[] fileinfo = dirInfo.GetFileSystemInfos();
			foreach (FileSystemInfo i in fileinfo)
			{
				if (i is DirectoryInfo)
				{
					this.recFile(i.FullName, bundleSuffix);
				}
				else
				{
					this.addFileList(i.FullName, bundleSuffix);
				}
			}
		}
		private void addFileList(string path, string[] bundleSuffix)
		{
			for (int i = 0; i < bundleSuffix.Length; i++)
			{
				if (Regex.Match(path, @"\." + bundleSuffix[i] + "$").Success)
				{
					if (!this.fileList.Contains(path))
						this.fileList.Add(path);
					return;
				}
			}
		}
		private void showToast(string content, bool err = false)
		{
			if (err)
			{
				UnityEngine.Debug.LogError(content);
			}
			else
			{
				UnityEngine.Debug.LogFormat(content);
			}
			ShowNotification(new GUIContent(content));
		}
		private bool isSelected(string path)
		{
			return this.selectedFileList.Contains(path);
		}
		private void change(string path, bool selected)
		{
			if (selected)
			{
				if (!this.isSelected(path))
				{
					this.selectedFileList.Add(path);
				}
			}
			else
			{
				if (this.isSelected(path))
				{
					this.selectedFileList.Remove(path);
				}
			}
		}
		/**
            对已有的结果进行关键词检索更新新的列表
            搜索显示不会影响已选中的项目内容,但是会影响总条数与页面关系

            已忽略的将其置顶
             */
		private void search()
		{
			this.showFileList.Clear();
			List<string> unselectedfileList = new List<string>();
			string search = searchText.Trim();
			if (search.Equals(""))
			{
				foreach (string item in this.fileList)
				{
					if (item.Equals("首包资源") || this.selectedFileList.Contains(item))
					{
						this.showFileList.Add(item);
					}
					else
					{
						unselectedfileList.Add(item);
					}
				}
			}
			else
			{
				foreach (string item in this.fileList)
				{
					if (item.IndexOf(this.searchText) != -1)
					{
						if (item.Equals("首包资源") || this.selectedFileList.Contains(item))
						{
							this.showFileList.Add(item);
						}
						else
						{
							unselectedfileList.Add(item);
						}
					}
				}
			}
			foreach (string item in unselectedfileList)
			{
				this.showFileList.Add(item);
			}
			totalPage = (int)Mathf.Ceil((float)showFileList.Count / (float)countPerPage); //计算总页数
		}
		/**
			路径字符串截断处理
			*/
		private string stringSub(string path)
		{
			if (path.Length <= pathMaxLength)
			{
				return path;
			}
			string tempPath = path.Substring(path.Length - pathMaxLength);
			return "..." + tempPath;
		}

		/**
			配置文件保存在 webgl 目录内的 .wxbundleignore 文件内
			例:
				xxxx/a.bundle
				!xxxx/b.bundle
			意为对 a.bundler 忽略;
				   b.bundle 不忽略;
		 */
		private void confirm()
		{
			string path = miniGameConf.ProjectConf.DST + "/webgl".Replace('\\', '/') + "/.wxbundleignore";
			using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
			{
				using (StreamWriter writer = new StreamWriter(file))
				{
					foreach (string item in this.fileList)
					{
						string row = item.Equals("首包资源") ? "#" : item;
						if (this.selectedFileList.Contains(item))
						{
							writer.WriteLine(row);
						}
						else
						{
							writer.WriteLine($"!{row}");
						}
					}
				}
			}

			this.showToast("保存成功");
		}

		/**
			读取 .wxbundleignore 文件 若该文件不存在则视为默认全选
		 */
		private void loadIgnore()
		{
			string path = miniGameConf.ProjectConf.DST + "/webgl".Replace('\\', '/') + "/.wxbundleignore";
			FileInfo info = new FileInfo(path);
			if (!info.Exists)
			{
				this.ignoreRule = new string[] { };
				this.noneIgnoreRule = new string[] { };
				return;
			}
			string content = "";
			using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
			{
				using (StreamReader reader = new StreamReader(file))
				{
					content = reader.ReadToEnd();
				}
			}
			string[] rule = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
			List<string> ignore = new List<string>();
			List<string> noneIgnore = new List<string>();

			for (int i = 0; i < rule.Length; i++)
			{
				string row = rule[i];
				if (string.IsNullOrWhiteSpace(row))
					continue;
				if (row.Length > 0 && row[0] == '!')
				{
					noneIgnore.Add(row.Equals("!#") ? "首包资源" : row.Substring(1));
				}
				else
				{
					ignore.Add(row.Equals("#") ? "首包资源" : row);
				}
			}
			this.ignoreRule = ignore.ToArray();
			this.noneIgnoreRule = noneIgnore.ToArray();

		}

		private void selectAll()
		{
			this.selectedFileList.Clear();
			foreach (string i in this.fileList)
			{
				this.selectedFileList.Add(i);
			}
		}

		private void OnGUI()
		{
			EditorGUILayout.BeginVertical("box");
			EditorGUILayout.LabelField("请选择需要忽略的bundle资源,若被忽略资源此前被压缩过,忽略后将被还原为未压缩");
			EditorGUILayout.BeginHorizontal();
			if (GUILayout.Button("扫描资源", GUILayout.Width(90)))
			{
				this.scanMiniGameDirBundle();
			}
			if (searchText.Trim().Equals(""))
			{
				if (GUILayout.Button("全部选择", GUILayout.Width(65)))
				{
					this.selectAll();
				}
				if (GUILayout.Button("全部反选", GUILayout.Width(65)))
				{
					foreach (string i in this.fileList)
					{
						this.change(i, !this.isSelected(i));
					}
				}
			}
			searchText = EditorGUILayout.TextArea(searchText, "SearchTextField", GUILayout.MaxWidth(300));

			if (!searchText.Equals(searchTextCache))
			{
				this.searchTextCache = this.searchText;
				this.search();
			}

			EditorGUILayout.EndHorizontal();
			scrollRoot = EditorGUILayout.BeginScrollView(scrollRoot);
			EditorGUILayout.BeginVertical("frameBox", GUILayout.MinHeight(200));
			string[] list = this.showFileList.ToArray();
			List<string> currentList = new List<string>();
			for (int i = (page - 1) * countPerPage; i < list.Length; i++)
			{
				if (i >= page * countPerPage) break;
				EditorGUILayout.BeginHorizontal("box");
				this.change(list[i], GUILayout.Toggle(this.isSelected(list[i]), this.stringSub(list[i])));
				currentList.Add(list[i]);
				if (!list[i].Equals("首包资源"))
				{
					if (GUILayout.Button("定位", GUILayout.Width(40)))
					{
						EditorUtility.RevealInFinder(list[i]);
					}
				}
				EditorGUILayout.EndHorizontal();
			}
			EditorGUILayout.EndVertical();
			EditorGUILayout.EndScrollView();
			EditorGUILayout.BeginHorizontal();
			EditorGUILayout.LabelField($"当前页:{page.ToString()}/{totalPage} 总条数:{this.showFileList.Count} 已选:{this.selectedFileList.Count}");
			if (GUILayout.Button("上一页"))
			{
				page -= 1;
				page = Mathf.Clamp(page, 1, totalPage);
			}
			if (GUILayout.Button("下一页"))
			{
				page += 1;
				page = Mathf.Clamp(page, 1, totalPage);
			}
			if (GUILayout.Button("本页选择"))
			{
				foreach (string i in currentList)
				{
					this.change(i, true);
				}
			}
			if (GUILayout.Button("本页反选"))
			{
				foreach (string i in currentList)
				{
					this.change(i, !this.isSelected(i));
				}
			}
			if (GUILayout.Button("确定"))
			{
				this.confirm();
			}
			EditorGUILayout.EndHorizontal();
			EditorGUILayout.EndVertical();
		}
	}
}