public static void FindAssetInProject() { var file = EditorUtility.OpenFilePanel("Select File to Find", null, null); if (!string.IsNullOrEmpty(file) && PlatDependant.IsFileExist(file)) { var md5 = CapsEditorUtils.GetFileMD5(file); string found = null; foreach (var asset in AssetDatabase.GetAllAssetPaths()) { if (CapsEditorUtils.GetFileMD5(asset) == md5) { found = asset; break; } } if (found == null) { EditorUtility.DisplayDialog("Result", "Not Found.", "OK"); } else { var asset = AssetDatabase.LoadMainAssetAtPath(found); EditorUtility.FocusProjectWindow(); EditorGUIUtility.PingObject(asset); Selection.activeObject = asset; } } }
public static void CheckModsAndMakeLink() { if (System.IO.Directory.Exists("Assets/Mods")) { var dirs = System.IO.Directory.GetDirectories("Assets/Mods"); if (dirs != null) { foreach (var dir in dirs) { var mod = System.IO.Path.GetFileName(dir); var linkdir = dir + "/Link"; if (System.IO.Directory.Exists(linkdir)) { CapsEditorUtils.HideFile(linkdir); if (System.IO.File.Exists(linkdir + ".meta")) { System.IO.File.Delete(linkdir + ".meta"); } foreach (var usdir in UniqueSpecialFolders) { var srcdir = linkdir + "/" + usdir; if (System.IO.Directory.Exists(srcdir)) { var phdir = "Assets/" + usdir + "/Mods/" + mod + "/Content"; if (!CapsEditorUtils.IsDirLink(phdir)) { if (System.IO.Directory.Exists(phdir)) { System.IO.Directory.Delete(phdir, true); } else if (System.IO.File.Exists(phdir)) { System.IO.File.Delete(phdir); } System.IO.Directory.CreateDirectory("Assets/" + usdir + "/Mods/" + mod); CapsEditorUtils.MakeDirLink(phdir, "../../../../" + srcdir); } } } } } } } AssetDatabase.Refresh(); CheckModsVisibility(); }
public static void ResetModsLink() { foreach (var usdir in UniqueSpecialFolders) { var dir = "Assets/" + usdir + "/Mods"; if (System.IO.Directory.Exists(dir)) { var subs = System.IO.Directory.GetDirectories(dir); foreach (var sub in subs) { if (CapsEditorUtils.IsDirLink(sub + "/Content")) { CapsEditorUtils.DeleteDirLink(sub + "/Content"); } } System.IO.Directory.Delete(dir, true); } } CheckModsAndMakeLink(); }
public static bool IsModOptional(string mod) { if (string.IsNullOrEmpty(mod)) { return(false); } var package = GetPackageName(mod); if (!string.IsNullOrEmpty(package)) { var path = "Packages/" + package; var descpath = path + "/Runtime/Resources/resdesc.asset"; bool descPathExists = false; if (!(descPathExists = System.IO.File.Exists(descpath))) { descpath = path + "/Resources/resdesc.asset"; } if (descPathExists || System.IO.File.Exists(descpath)) { var desc = AssetDatabase.LoadAssetAtPath <CapsModDesc>(descpath); if (desc != null) { var rv = desc.IsOptional; Resources.UnloadAsset(desc); return(rv); } else { bool inMain; string[] deps; if (TryParseModDesc(descpath, out inMain, out deps)) { return(!inMain || (deps != null && deps.Length > 0)); } } } return(false); } else { var descpath = "Assets/Mods/" + mod + "/Resources/resdesc.asset"; if (!System.IO.File.Exists(descpath)) { return(false); } if (CapsEditorUtils.IsFileHidden("Assets/Mods/" + mod)) { return(true); } var desc = AssetDatabase.LoadAssetAtPath <CapsModDesc>(descpath); if (desc == null) { bool inMain; string[] deps; if (TryParseModDesc(descpath, out inMain, out deps)) { return(!inMain || (deps != null && deps.Length > 0)); } return(false); } bool rv = desc.IsOptional; Resources.UnloadAsset(desc); return(rv); } }
public static void CheckModsVisibility() { HashSet <string> compilerOpLines = new HashSet <string>(); System.Xml.Linq.XDocument linkxml = new System.Xml.Linq.XDocument(); var flags = new HashSet <string>(ResManager.PreRuntimeDFlags); var mods = GetAllModsOrPackages(); for (int i = 0; i < mods.Length; ++i) { var mod = mods[i]; if (!IsModOptional(mod) || flags.Contains(mod)) { // enable string defpath; bool defPathExists = false; var pdir = GetModRootInPackage(mod); if (!string.IsNullOrEmpty(pdir)) { defpath = pdir + "/mcs.rsp"; if (defPathExists = System.IO.File.Exists(defpath)) { var pname = GetPackageName(mod); compilerOpLines.Add("-define:MOD_" + pname.ToUpper().Replace(".", "_")); } else { defpath = "Assets/Mods/" + mod + "/Link/mcs.rsp"; } } else { defpath = "Assets/Mods/" + mod + "/Link/mcs.rsp"; } if (defPathExists || System.IO.File.Exists(defpath)) { compilerOpLines.Add("-define:MOD_" + mod.ToUpper().Replace(".", "_")); try { compilerOpLines.UnionWith(System.IO.File.ReadAllLines(defpath)); } catch (Exception e) { Debug.LogException(e); } } string linkxmlpath; bool linkxmlPathExists = false; if (!string.IsNullOrEmpty(pdir)) { linkxmlpath = pdir + "/link.xml"; if (linkxmlPathExists = System.IO.File.Exists(linkxmlpath)) { } else { linkxmlpath = "Assets/Mods/" + mod + "/Link/link.xml"; } } else { linkxmlpath = "Assets/Mods/" + mod + "/Link/link.xml"; } if (linkxmlPathExists || System.IO.File.Exists(linkxmlpath)) { CapsEditorUtils.MergeXml(linkxml, linkxmlpath); } CapsEditorUtils.UnhideFile("Assets/Mods/" + mod); if (System.IO.File.Exists("Assets/Mods/" + mod + ".meta")) { CapsEditorUtils.UnhideFile("Assets/Mods/" + mod + ".meta"); } foreach (var sdir in UniqueSpecialFolders) { var moddir = "Assets/" + sdir + "/Mods/" + mod; if (System.IO.Directory.Exists(moddir)) { CapsEditorUtils.UnhideFile(moddir); if (System.IO.File.Exists(moddir + ".meta")) { CapsEditorUtils.UnhideFile(moddir + ".meta"); } } } } else { // disable CapsEditorUtils.HideFile("Assets/Mods/" + mod); if (System.IO.File.Exists("Assets/Mods/" + mod + ".meta")) { System.IO.File.Delete("Assets/Mods/" + mod + ".meta"); } foreach (var sdir in UniqueSpecialFolders) { var moddir = "Assets/" + sdir + "/Mods/" + mod; if (System.IO.Directory.Exists(moddir)) { CapsEditorUtils.HideFile(moddir); if (System.IO.File.Exists(moddir + ".meta")) { System.IO.File.Delete(moddir + ".meta"); } } } } } if (linkxml.Root != null) { linkxml.Save("Assets/link.xml"); } else { System.IO.File.Delete("Assets/link.xml"); } compilerOpLines.Remove(""); HashSet <string> existCompilerOpLines = new HashSet <string>(); if (System.IO.File.Exists("Assets/mcs.rsp")) { try { existCompilerOpLines.UnionWith(System.IO.File.ReadAllLines("Assets/mcs.rsp")); existCompilerOpLines.Remove(""); } catch (Exception e) { Debug.LogException(e); } } bool hasdiff = true; if (existCompilerOpLines.Count == compilerOpLines.Count) { var diff = new HashSet <string>(compilerOpLines); diff.ExceptWith(existCompilerOpLines); hasdiff = diff.Count > 0; } if (hasdiff) { if (System.IO.File.Exists("Assets/mcs.rsp")) { System.IO.File.Delete("Assets/mcs.rsp"); } if (System.IO.File.Exists("Assets/csc.rsp")) { System.IO.File.Delete("Assets/csc.rsp"); } var lines = compilerOpLines.ToArray(); Array.Sort(lines); System.IO.File.WriteAllLines("Assets/mcs.rsp", lines); System.IO.File.WriteAllLines("Assets/csc.rsp", lines); AssetDatabase.ImportAsset("Assets/mcs.rsp"); AssetDatabase.ImportAsset("Assets/csc.rsp"); EditorApplication.LockReloadAssemblies(); try { AssetDatabase.ImportAsset(CapsEditorUtils.__ASSET__, ImportAssetOptions.ForceUpdate); } catch { } AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(ScriptableObject.CreateInstance <CapsModDesc>())), ImportAssetOptions.ForceUpdate); // Update all package... //foreach (var kvp in _PackageName2ModName) //{ // var pname = kvp.Key; // AssetDatabase.ImportAsset("Packages/" + pname, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ImportRecursive); //} ReimportAllAssemblyDefinitions(); EditorApplication.UnlockReloadAssemblies(); } AssetDatabase.Refresh(); }
/// <remarks>not recommend</remarks> public static bool BuildRawUpdate(string oldz, string newz, string diff) { LinkedList<IDisposable> lstToDispose = new LinkedList<IDisposable>(); try { ZipArchive olda = null, newa = null, diffa = null; try { var olds = File.OpenRead(oldz); lstToDispose.AddFirst(olds); olda = new ZipArchive(olds, ZipArchiveMode.Read); lstToDispose.AddFirst(olda); } catch { } try { var news = File.OpenRead(newz); lstToDispose.AddFirst(news); newa = new ZipArchive(news, ZipArchiveMode.Read); lstToDispose.AddFirst(newa); } catch { } if (newa == null) { return false; } else if (olda == null) { File.Copy(newz, diff, true); return true; } else { var entries = newa.Entries; List<string> diffEntries = new List<string>(); for (int i = 0; i < entries.Count; ++i) { var entry = entries[i]; try { var oentry = olda.GetEntry(entry.FullName); if (oentry != null && oentry.Length == entry.Length) { string md5old = ""; string md5new = ""; try { using (var sold = oentry.Open()) { md5old = CapsEditorUtils.GetStreamMD5(sold); } } catch { } try { using (var snew = entry.Open()) { md5new = CapsEditorUtils.GetStreamMD5(snew); } } catch { } if (md5new == md5old) { continue; } } diffEntries.Add(entry.FullName); } catch { } } if (diffEntries.Count > 0) { try { var streama = PlatDependant.OpenWrite(diff); if (streama != null) { lstToDispose.AddFirst(streama); diffa = new ZipArchive(streama, ZipArchiveMode.Create); if (diffa != null) { lstToDispose.AddFirst(diffa); for (int i = 0; i < diffEntries.Count; ++i) { var ename = diffEntries[i]; try { var entrys = newa.GetEntry(ename); if (entrys != null) { var entryd = diffa.CreateEntry(ename, CompressionLevel.Optimal); if (entryd != null) { using (var streams = entrys.Open()) { using (var streamd = entryd.Open()) { streams.CopyTo(streamd); } } } } } catch { } } } } } catch { } return true; } } } catch { } finally { foreach (var dis in lstToDispose) { if (dis != null) { dis.Dispose(); } } } return false; }