QFramework.PackageKit.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /****************************************************************************
  2. * Copyright (c) 2020.10 liangxie
  3. *
  4. * https://qframework.cn
  5. * https://github.com/liangxiegame/QFramework
  6. * https://gitee.com/liangxiegame/QFramework
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. ****************************************************************************/
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Diagnostics;
  29. using System.IO;
  30. using System.Security.Cryptography;
  31. using UnityEditor;
  32. using UnityEditor.SceneManagement;
  33. using UnityEngine;
  34. using UnityEngine.SceneManagement;
  35. using Debug = UnityEngine.Debug;
  36. namespace QFramework
  37. {
  38. /// <summary>
  39. /// some net work util
  40. /// </summary>
  41. public static class Network
  42. {
  43. public static bool IsReachable
  44. {
  45. get { return Application.internetReachability != NetworkReachability.NotReachable; }
  46. }
  47. }
  48. // [InitializeOnLoad]
  49. // public class PackageCheck
  50. // {
  51. // enum CheckStatus
  52. // {
  53. // WAIT,
  54. // COMPARE,
  55. // NONE
  56. // }
  57. //
  58. // private CheckStatus mCheckStatus;
  59. //
  60. // private double mNextCheckTime = 0;
  61. //
  62. // private double mCheckInterval = 60;
  63. //
  64. // ControllerNode<PackageKit> mControllerNode = ControllerNode<PackageKit>.Allocate();
  65. //
  66. // static PackageCheck()
  67. // {
  68. // if (!EditorApplication.isPlayingOrWillChangePlaymode && Network.IsReachable)
  69. // {
  70. // PackageCheck packageCheck = new PackageCheck()
  71. // {
  72. // mCheckStatus = CheckStatus.WAIT,
  73. // mNextCheckTime = EditorApplication.timeSinceStartup,
  74. // };
  75. //
  76. // EditorApplication.update = packageCheck.CustomUpdate;
  77. // }
  78. // }
  79. //
  80. // private void CustomUpdate()
  81. // {
  82. // // 添加网络判断
  83. // if (!Network.IsReachable) return;
  84. //
  85. // switch (mCheckStatus)
  86. // {
  87. // case CheckStatus.WAIT:
  88. // if (EditorApplication.timeSinceStartup >= mNextCheckTime)
  89. // {
  90. // mCheckStatus = CheckStatus.COMPARE;
  91. // }
  92. //
  93. // break;
  94. //
  95. // case CheckStatus.COMPARE:
  96. //
  97. // ProcessCompare();
  98. //
  99. // break;
  100. // }
  101. // }
  102. //
  103. //
  104. // private void GoToWait()
  105. // {
  106. // mCheckStatus = CheckStatus.WAIT;
  107. //
  108. // mNextCheckTime = EditorApplication.timeSinceStartup + mCheckInterval;
  109. // }
  110. //
  111. // private bool ReCheckConfigDatas()
  112. // {
  113. // mCheckInterval = 60;
  114. //
  115. // return true;
  116. // }
  117. //
  118. // private void ProcessCompare()
  119. // {
  120. // if (Network.IsReachable)
  121. // {
  122. // new PackageManagerServer().GetAllRemotePackageInfoV5((packageDatas, res) =>
  123. // {
  124. // if (packageDatas == null)
  125. // {
  126. // return;
  127. // }
  128. //
  129. // if (new PackageManagerModel().VersionCheck)
  130. // {
  131. // CheckNewVersionDialog(packageDatas, PackageInfosRequestCache.Get().PackageRepositories);
  132. // }
  133. // });
  134. // }
  135. //
  136. // ReCheckConfigDatas();
  137. // GoToWait();
  138. // }
  139. //
  140. // private bool CheckNewVersionDialog(List<PackageRepository> requestPackageDatas,
  141. // List<PackageRepository> cachedPackageDatas)
  142. // {
  143. // var installedPackageVersionsModel = mControllerNode.GetModel<ILocalPackageVersionModel>();
  144. // foreach (var requestPackageData in requestPackageDatas)
  145. // {
  146. // var cachedPacakgeData =
  147. // cachedPackageDatas.Find(packageData => packageData.name == requestPackageData.name);
  148. //
  149. // var installedPackageVersion = installedPackageVersionsModel.GetByName(requestPackageData.name);
  150. //
  151. // if (installedPackageVersion == null)
  152. // {
  153. // }
  154. // else if (cachedPacakgeData == null &&
  155. // requestPackageData.VersionNumber > installedPackageVersion.VersionNumber ||
  156. // cachedPacakgeData != null && requestPackageData.Installed &&
  157. // requestPackageData.VersionNumber > cachedPacakgeData.VersionNumber &&
  158. // requestPackageData.VersionNumber > installedPackageVersion.VersionNumber)
  159. // {
  160. // ShowDisplayDialog(requestPackageData.name);
  161. // return false;
  162. // }
  163. // }
  164. //
  165. // return true;
  166. // }
  167. //
  168. //
  169. // private static void ShowDisplayDialog(string packageName)
  170. // {
  171. // var result = EditorUtility.DisplayDialog("PackageManager",
  172. // string.Format("{0} 有新版本更新,请前往查看(如需不再提示请点击前往查看,并取消勾选 Version Check)", packageName),
  173. // "前往查看", "稍后查看");
  174. //
  175. // if (result)
  176. // {
  177. // EditorApplication.ExecuteMenuItem(FrameworkMenuItems.Preferences);
  178. // }
  179. // }
  180. //
  181. // ~PackageCheck()
  182. // {
  183. // mControllerNode = null;
  184. // }
  185. // }
  186. internal static class User
  187. {
  188. public static BindableProperty<string> Username = new BindableProperty<string>(LoadString("username"));
  189. public static BindableProperty<string> Password = new BindableProperty<string>(LoadString("password"));
  190. public static BindableProperty<string> Token = new BindableProperty<string>(LoadString("token"));
  191. public static bool Logined
  192. {
  193. get
  194. {
  195. return !string.IsNullOrEmpty(Token.Value) &&
  196. !string.IsNullOrEmpty(Username.Value) &&
  197. !string.IsNullOrEmpty(Password.Value);
  198. }
  199. }
  200. public static void Save()
  201. {
  202. Username.SaveString("username");
  203. Password.SaveString("password");
  204. Token.SaveString("token");
  205. }
  206. public static void Clear()
  207. {
  208. Username.Value = string.Empty;
  209. Password.Value = string.Empty;
  210. Token.Value = string.Empty;
  211. Save();
  212. }
  213. public static void SaveString(this BindableProperty<string> selfProperty, string key)
  214. {
  215. EditorPrefs.SetString(key, selfProperty.Value);
  216. }
  217. public static string LoadString(string key)
  218. {
  219. return EditorPrefs.GetString(key, string.Empty);
  220. }
  221. }
  222. public class ReadmeWindow : EditorWindow
  223. {
  224. private Readme mReadme;
  225. private Vector2 mScrollPos = Vector2.zero;
  226. private PackageVersion mPackageVersion;
  227. public static void Init(Readme readme, PackageVersion packageVersion)
  228. {
  229. var readmeWin = (ReadmeWindow) GetWindow(typeof(ReadmeWindow), true, packageVersion.Name, true);
  230. readmeWin.mReadme = readme;
  231. readmeWin.mPackageVersion = packageVersion;
  232. readmeWin.position = new Rect(Screen.width / 2, Screen.height / 2, 600, 300);
  233. readmeWin.Show();
  234. }
  235. public void OnGUI()
  236. {
  237. mScrollPos = GUILayout.BeginScrollView(mScrollPos, true, true, GUILayout.Width(580), GUILayout.Height(300));
  238. GUILayout.Label("类型:" + mPackageVersion.Type);
  239. mReadme.items.ForEach(item =>
  240. {
  241. EasyIMGUI
  242. .Custom()
  243. .OnGUI(() =>
  244. {
  245. GUILayout.BeginHorizontal(EditorStyles.helpBox);
  246. GUILayout.BeginVertical();
  247. GUILayout.BeginHorizontal();
  248. GUILayout.Label("version: " + item.version, GUILayout.Width(130));
  249. GUILayout.Label("author: " + item.author);
  250. GUILayout.Label("date: " + item.date);
  251. if (item.author == User.Username.Value || User.Username.Value == "liangxie")
  252. {
  253. if (GUILayout.Button("删除"))
  254. {
  255. // RenderEndCommandExecuter.PushCommand(() =>
  256. // {
  257. new PackageManagerServer().DeletePackage(item.PackageId,
  258. () => { mReadme.items.Remove(item); });
  259. // });
  260. }
  261. }
  262. GUILayout.EndHorizontal();
  263. GUILayout.Label(item.content);
  264. GUILayout.EndVertical();
  265. GUILayout.EndHorizontal();
  266. }).DrawGUI();
  267. });
  268. GUILayout.EndScrollView();
  269. }
  270. }
  271. internal class InstallPackage : AbstractCommand
  272. {
  273. private readonly PackageRepository mRequestPackageData;
  274. private void OnProgressChanged(float progress)
  275. {
  276. EditorUtility.DisplayProgressBar("插件更新",
  277. string.Format("插件下载中 {0:P2}", progress), progress);
  278. }
  279. public InstallPackage(PackageRepository requestPackageData)
  280. {
  281. mRequestPackageData = requestPackageData;
  282. }
  283. protected override void OnExecute()
  284. {
  285. var tempFile = "Assets/" + mRequestPackageData.name + ".unitypackage";
  286. Debug.Log(mRequestPackageData.latestDownloadUrl + ">>>>>>:");
  287. EditorUtility.DisplayProgressBar("插件更新", "插件下载中 ...", 0.1f);
  288. //EditorHttp.Download(mRequestPackageData.latestDownloadUrl, response =>
  289. //{
  290. // if (response.Type == ResponseType.SUCCEED)
  291. // {
  292. // File.WriteAllBytes(tempFile, response.Bytes);
  293. // EditorUtility.ClearProgressBar();
  294. // AssetDatabase.ImportPackage(tempFile, false);
  295. // File.Delete(tempFile);
  296. // AssetDatabase.Refresh();
  297. // Debug.Log("PackageManager:插件下载成功");
  298. // this.GetModel<ILocalPackageVersionModel>()
  299. // .Reload();
  300. // }
  301. // else
  302. // {
  303. // EditorUtility.ClearProgressBar();
  304. // EditorUtility.DisplayDialog(mRequestPackageData.name,
  305. // "插件安装失败,请联系 liangxiegame@163.com 或者加入 QQ 群:623597263" + response.Error + ";", "OK");
  306. // }
  307. //}, OnProgressChanged);
  308. }
  309. }
  310. [Serializable]
  311. public class PackageInfosRequestCache
  312. {
  313. public List<PackageRepository> PackageRepositories = new List<PackageRepository>();
  314. private static string mFilePath
  315. {
  316. get
  317. {
  318. var dirPath = Application.dataPath + "/.qframework/PackageManager/";
  319. if (!Directory.Exists(dirPath))
  320. {
  321. Directory.CreateDirectory(dirPath);
  322. }
  323. return dirPath + "PackageInfosRequestCache.json";
  324. }
  325. }
  326. public static PackageInfosRequestCache Get()
  327. {
  328. if (File.Exists(mFilePath))
  329. {
  330. return JsonUtility.FromJson<PackageInfosRequestCache>(File.ReadAllText(mFilePath));
  331. }
  332. return new PackageInfosRequestCache();
  333. }
  334. public void Save()
  335. {
  336. File.WriteAllText(mFilePath, JsonUtility.ToJson(this));
  337. }
  338. }
  339. public static class FrameworkMenuItems
  340. {
  341. public const string Preferences = "QFramework/Preferences... %e";
  342. public const string PackageKit = "QFramework/PackageKit... %#e";
  343. public const string Feedback = "QFramework/Feedback";
  344. }
  345. public static class FrameworkMenuItemsPriorities
  346. {
  347. public const int Preferences = 1;
  348. public const int Feedback = 11;
  349. }
  350. public class Language
  351. {
  352. public static bool IsChinese
  353. {
  354. get
  355. {
  356. return Application.systemLanguage == SystemLanguage.Chinese ||
  357. Application.systemLanguage == SystemLanguage.ChineseSimplified;
  358. }
  359. }
  360. }
  361. public class SubWindow : EditorWindow, IMGUILayout
  362. {
  363. public string Id { get; set; }
  364. public bool Visible { get; set; }
  365. public Func<bool> VisibleCondition { get; set; }
  366. void IMGUIView.DrawGUI()
  367. {
  368. }
  369. IMGUILayout IMGUIView.Parent { get; set; }
  370. private GUIStyleProperty mStyle = new GUIStyleProperty(() => new GUIStyle());
  371. public GUIStyleProperty Style
  372. {
  373. get { return mStyle; }
  374. set { mStyle = value; }
  375. }
  376. Color IMGUIView.BackgroundColor { get; set; }
  377. private List<IMGUIView> mPrivateChildren = new List<IMGUIView>();
  378. private List<IMGUIView> mChildren
  379. {
  380. get { return mPrivateChildren; }
  381. set { mPrivateChildren = value; }
  382. }
  383. void IMGUIView.RefreshNextFrame()
  384. {
  385. }
  386. void IMGUIView.AddLayoutOption(GUILayoutOption option)
  387. {
  388. }
  389. void IMGUIView.RemoveFromParent()
  390. {
  391. }
  392. void IMGUIView.Refresh()
  393. {
  394. }
  395. public void Hide()
  396. {
  397. throw new NotImplementedException();
  398. }
  399. public IMGUILayout AddChild(IMGUIView view)
  400. {
  401. mChildren.Add(view);
  402. view.Parent = this;
  403. return this;
  404. }
  405. public void RemoveChild(IMGUIView view)
  406. {
  407. mChildren.Add(view);
  408. view.Parent = null;
  409. }
  410. public void Clear()
  411. {
  412. mChildren.Clear();
  413. }
  414. private void OnGUI()
  415. {
  416. mChildren.ForEach(view => view.DrawGUI());
  417. }
  418. public void Dispose()
  419. {
  420. }
  421. }
  422. public abstract class Window : EditorWindow, IDisposable
  423. {
  424. public static Window MainWindow { get; protected set; }
  425. public IMGUIViewController ViewController { get; set; }
  426. public T CreateViewController<T>() where T : IMGUIViewController, new()
  427. {
  428. var t = new T();
  429. t.SetUpView();
  430. return t;
  431. }
  432. public static void Open<T>(string title) where T : Window
  433. {
  434. MainWindow = GetWindow<T>(true);
  435. if (!MainWindow.mShowing)
  436. {
  437. MainWindow.position = new Rect(Screen.width / 2, Screen.height / 2, 800, 600);
  438. MainWindow.titleContent = new GUIContent(title);
  439. MainWindow.Init();
  440. MainWindow.mShowing = true;
  441. MainWindow.Show();
  442. }
  443. else
  444. {
  445. MainWindow.mShowing = false;
  446. MainWindow.Dispose();
  447. MainWindow.Close();
  448. MainWindow = null;
  449. }
  450. }
  451. public static SubWindow CreateSubWindow(string name = "SubWindow")
  452. {
  453. var window = GetWindow<SubWindow>(true, name);
  454. window.Clear();
  455. return window;
  456. }
  457. void Init()
  458. {
  459. OnInit();
  460. }
  461. public void PushCommand(System.Action command)
  462. {
  463. RenderEndCommandExecutor.PushCommand(command);
  464. }
  465. private void OnGUI()
  466. {
  467. if (ViewController != null)
  468. {
  469. ViewController.View.DrawGUI();
  470. }
  471. RenderEndCommandExecutor.ExecuteCommand();
  472. }
  473. public void Dispose()
  474. {
  475. OnDispose();
  476. }
  477. protected bool mShowing = false;
  478. protected abstract void OnInit();
  479. protected abstract void OnDispose();
  480. }
  481. public static class WindowExtension
  482. {
  483. public static T PushCommand<T>(this T view, System.Action command) where T : IMGUIView
  484. {
  485. RenderEndCommandExecutor.PushCommand(command);
  486. return view;
  487. }
  488. }
  489. public static class SubWindowExtension
  490. {
  491. public static T Postion<T>(this T subWindow, int x, int y) where T : SubWindow
  492. {
  493. var rect = subWindow.position;
  494. rect.x = x;
  495. rect.y = y;
  496. subWindow.position = rect;
  497. return subWindow;
  498. }
  499. public static T Size<T>(this T subWindow, int width, int height) where T : SubWindow
  500. {
  501. var rect = subWindow.position;
  502. rect.width = width;
  503. rect.height = height;
  504. subWindow.position = rect;
  505. return subWindow;
  506. }
  507. public static T PostionScreenCenter<T>(this T subWindow) where T : SubWindow
  508. {
  509. var rect = subWindow.position;
  510. rect.x = Screen.width / 2;
  511. rect.y = Screen.height / 2;
  512. subWindow.position = rect;
  513. return subWindow;
  514. }
  515. }
  516. public static class EditorUtils
  517. {
  518. public static string GetSelectedPathOrFallback()
  519. {
  520. var path = string.Empty;
  521. foreach (var obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
  522. {
  523. path = AssetDatabase.GetAssetPath(obj);
  524. if (!string.IsNullOrEmpty(path) && File.Exists(path))
  525. {
  526. return path;
  527. }
  528. }
  529. return path;
  530. }
  531. public static void MarkCurrentSceneDirty()
  532. {
  533. EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
  534. }
  535. public static string CurrentSelectPath
  536. {
  537. get { return Selection.activeObject == null ? null : AssetDatabase.GetAssetPath(Selection.activeObject); }
  538. }
  539. public static string AssetsPath2ABSPath(string assetsPath)
  540. {
  541. string assetRootPath = Path.GetFullPath(Application.dataPath);
  542. return assetRootPath.Substring(0, assetRootPath.Length - 6) + assetsPath;
  543. }
  544. public static string ABSPath2AssetsPath(string absPath)
  545. {
  546. string assetRootPath = Path.GetFullPath(Application.dataPath);
  547. Debug.Log(assetRootPath);
  548. Debug.Log(Path.GetFullPath(absPath));
  549. return "Assets" + Path.GetFullPath(absPath).Substring(assetRootPath.Length).Replace("\\", "/");
  550. }
  551. public static string AssetPath2ReltivePath(string path)
  552. {
  553. if (path == null)
  554. {
  555. return null;
  556. }
  557. return path.Replace("Assets/", "");
  558. }
  559. public static bool ExcuteCmd(string toolName, string args, bool isThrowExcpetion = true)
  560. {
  561. Process process = new Process();
  562. process.StartInfo.FileName = toolName;
  563. process.StartInfo.Arguments = args;
  564. process.StartInfo.CreateNoWindow = true;
  565. process.StartInfo.RedirectStandardOutput = true;
  566. process.StartInfo.RedirectStandardError = true;
  567. process.StartInfo.UseShellExecute = false;
  568. process.Start();
  569. OuputProcessLog(process, isThrowExcpetion);
  570. return true;
  571. }
  572. public static void OuputProcessLog(Process p, bool isThrowExcpetion)
  573. {
  574. string standardError = string.Empty;
  575. p.BeginErrorReadLine();
  576. p.ErrorDataReceived += (sender, outLine) => { standardError += outLine.Data; };
  577. string standardOutput = string.Empty;
  578. p.BeginOutputReadLine();
  579. p.OutputDataReceived += (sender, outLine) => { standardOutput += outLine.Data; };
  580. p.WaitForExit();
  581. p.Close();
  582. Debug.Log(standardOutput);
  583. if (standardError.Length > 0)
  584. {
  585. if (isThrowExcpetion)
  586. {
  587. Debug.Log(standardError);
  588. throw new Exception(standardError);
  589. }
  590. Debug.Log(standardError);
  591. }
  592. }
  593. public static Dictionary<string, string> ParseArgs(string argString)
  594. {
  595. int curPos = argString.IndexOf('-');
  596. Dictionary<string, string> result = new Dictionary<string, string>();
  597. while (curPos != -1 && curPos < argString.Length)
  598. {
  599. int nextPos = argString.IndexOf('-', curPos + 1);
  600. string item = string.Empty;
  601. if (nextPos != -1)
  602. {
  603. item = argString.Substring(curPos + 1, nextPos - curPos - 1);
  604. }
  605. else
  606. {
  607. item = argString.Substring(curPos + 1, argString.Length - curPos - 1);
  608. }
  609. item = StringTrim(item);
  610. int splitPos = item.IndexOf(' ');
  611. if (splitPos == -1)
  612. {
  613. string key = StringTrim(item);
  614. result[key] = "";
  615. }
  616. else
  617. {
  618. string key = StringTrim(item.Substring(0, splitPos));
  619. string value = StringTrim(item.Substring(splitPos + 1, item.Length - splitPos - 1));
  620. result[key] = value;
  621. }
  622. curPos = nextPos;
  623. }
  624. return result;
  625. }
  626. public static string GetFileMD5Value(string absPath)
  627. {
  628. if (!File.Exists(absPath))
  629. return "";
  630. MD5CryptoServiceProvider md5CSP = new MD5CryptoServiceProvider();
  631. FileStream file = new FileStream(absPath, FileMode.Open);
  632. byte[] retVal = md5CSP.ComputeHash(file);
  633. file.Close();
  634. string result = "";
  635. for (int i = 0; i < retVal.Length; i++)
  636. {
  637. result += retVal[i].ToString("x2");
  638. }
  639. return result;
  640. }
  641. public static string StringTrim(string str, params char[] trimer)
  642. {
  643. int startIndex = 0;
  644. int endIndex = str.Length;
  645. for (int i = 0; i < str.Length; ++i)
  646. {
  647. if (!IsInCharArray(trimer, str[i]))
  648. {
  649. startIndex = i;
  650. break;
  651. }
  652. }
  653. for (int i = str.Length - 1; i >= 0; --i)
  654. {
  655. if (!IsInCharArray(trimer, str[i]))
  656. {
  657. endIndex = i;
  658. break;
  659. }
  660. }
  661. if (startIndex == 0 && endIndex == str.Length)
  662. {
  663. return string.Empty;
  664. }
  665. return str.Substring(startIndex, endIndex - startIndex + 1);
  666. }
  667. public static string StringTrim(string str)
  668. {
  669. return StringTrim(str, ' ', '\t');
  670. }
  671. static bool IsInCharArray(char[] array, char c)
  672. {
  673. for (int i = 0; i < array.Length; ++i)
  674. {
  675. if (array[i] == c)
  676. {
  677. return true;
  678. }
  679. }
  680. return false;
  681. }
  682. }
  683. public static class MouseSelector
  684. {
  685. public static string GetSelectedPathOrFallback()
  686. {
  687. var path = string.Empty;
  688. foreach (var obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
  689. {
  690. path = AssetDatabase.GetAssetPath(obj);
  691. if (!string.IsNullOrEmpty(path) && File.Exists(path))
  692. {
  693. }
  694. }
  695. return path;
  696. }
  697. }
  698. internal class ColorView : View
  699. {
  700. public ColorView(Color color)
  701. {
  702. Color = new BindableProperty<Color>(color);
  703. }
  704. public BindableProperty<Color> Color { get; private set; }
  705. protected override void OnGUI()
  706. {
  707. Color.Value = EditorGUILayout.ColorField(Color.Value, LayoutStyles);
  708. }
  709. }
  710. internal class EnumPopupView : View
  711. {
  712. public BindableProperty<Enum> ValueProperty { get; set; }
  713. public EnumPopupView(Enum initValue)
  714. {
  715. ValueProperty = new BindableProperty<Enum>(initValue);
  716. ValueProperty.Value = initValue;
  717. Style = new GUIStyleProperty(() => EditorStyles.popup);
  718. }
  719. protected override void OnGUI()
  720. {
  721. Enum enumType = ValueProperty.Value;
  722. ValueProperty.Value = EditorGUILayout.EnumPopup(enumType, Style.Value, LayoutStyles);
  723. }
  724. }
  725. public abstract class IMGUIViewController
  726. {
  727. public VerticalLayout View = new VerticalLayout();
  728. public abstract void SetUpView();
  729. }
  730. }