public static void WriteClass(TextWriter writer, string ns, List <QABItemInfo> assetBundleInfos, string projectTag = null) { var compileUnit = new CodeCompileUnit(); var codeNamespace = new CodeNamespace(ns); compileUnit.Namespaces.Add(codeNamespace); for (int i = 0; i < assetBundleInfos.Count; i++) { QABItemInfo assetBundleInfo = assetBundleInfos [i]; string className = assetBundleInfo.name.ToUpper(); string bundleName = className.Substring(0, 1).ToUpper() + className.Substring(1); className = className.Substring(0, 1).ToUpper() + className.Substring(1).Replace("/", "_").Replace("@", "_").Replace("!", "_"); if (!string.IsNullOrEmpty(projectTag)) { className = className.Replace("_project_" + projectTag, ""); bundleName = bundleName.Replace("_project_" + projectTag, ""); } var codeType = new CodeTypeDeclaration(className); codeNamespace.Types.Add(codeType); CodeMemberField bundleNameField = new CodeMemberField(); bundleNameField.Attributes = MemberAttributes.Public | MemberAttributes.Const; bundleNameField.Name = "BUNDLENAME"; bundleNameField.Type = new CodeTypeReference(typeof(System.String)); codeType.Members.Add(bundleNameField); bundleNameField.InitExpression = new CodePrimitiveExpression(assetBundleInfo.path.ToLowerInvariant()); foreach (var asset in assetBundleInfo.assets) { CodeMemberField assetField = new CodeMemberField(); assetField.Attributes = MemberAttributes.Public | MemberAttributes.Const; string content = Path.GetFileNameWithoutExtension(asset).ToUpperInvariant(); assetField.Name = content; assetField.Type = new CodeTypeReference(typeof(System.String)); codeType.Members.Add(assetField); assetField.InitExpression = new CodePrimitiveExpression(content.ToLowerInvariant()); } } var provider = new CSharpCodeProvider(); var options = new CodeGeneratorOptions(); options.BlankLinesBetweenMembers = false; provider.GenerateCodeFromCompileUnit(compileUnit, writer, options); }
private static void GenerateVersionConfig(string outputPath, List <string> finalZips, List <string> finalFiles) { // string abManifestFile; string abManifestFile = Path.Combine(outputPath, QPlatform.GetPlatformName()); if (projectTag != "") { abManifestFile = Path.Combine(outputPath, projectTag); } else { abManifestFile = Path.Combine(outputPath, QPlatform.GetPlatformName()); } AssetBundle ab = AssetBundle.LoadFromFile(abManifestFile); AssetBundleManifest abMainfest = (AssetBundleManifest)ab.LoadAllAssets() [0]; string[] allABNames = abMainfest.GetAllAssetBundles(); XmlDocument xmlDoc = new XmlDocument(); XmlElement xmlRoot = xmlDoc.CreateElement("config"); xmlRoot.SetAttribute("res_version", PTAssetBundleBuilder.resVersion); xmlDoc.AppendChild(xmlRoot); mABInfos.Clear(); for (int i = 0; i < allABNames.Length; i++) { Hash128 hash = abMainfest.GetAssetBundleHash(allABNames [i]); XmlElement xmlItem = CreateConfigItem(xmlDoc, Path.Combine(outputPath, allABNames [i]), allABNames [i], allABNames [i]); xmlRoot.AppendChild(xmlItem); AssetBundle assetBundle = AssetBundle.LoadFromFile(Path.Combine(outputPath, allABNames [i])); QABItemInfo abInfo = new QABItemInfo(allABNames [i], Path.Combine(outputPath, allABNames [i])); abInfo.assets = assetBundle.GetAllAssetNames(); mABInfos.Add(abInfo); assetBundle.Unload(true); } // 这里要加上平台相关的xml string platformBundleName = QPlatform.GetPlatformName(); XmlElement platformItem; if (projectTag == "") { platformItem = CreateConfigItem(xmlDoc, abManifestFile, platformBundleName, platformBundleName); } else { platformItem = CreateConfigItem(xmlDoc, abManifestFile, projectTag, projectTag); } xmlRoot.AppendChild(platformItem); foreach (var zipPath in finalZips) { XmlElement zipItem = CreateConfigItem(xmlDoc, zipPath, Path.GetFileName(zipPath), Path.GetFileName(zipPath)); xmlRoot.AppendChild(zipItem); } foreach (var filePath in finalFiles) { XmlElement fileItem = CreateConfigItem(xmlDoc, filePath, Path.GetFileName(filePath), Path.GetFileName(filePath)); xmlRoot.AppendChild(fileItem); } ab.Unload(true); platformItem = CreateConfigItem(xmlDoc, Path.Combine(outputPath, QPlatform.GetPlatformName()), QPlatform.GetPlatformName(), QPlatform.GetPlatformName()); xmlRoot.AppendChild(platformItem); xmlDoc.Save(outputPath + "/resitems.xml"); AssetDatabase.Refresh(); if (PTAssetBundleBuilder.isEnableGenerateClass) { if (!Directory.Exists(Application.dataPath + Path.DirectorySeparatorChar + "QData")) { Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + "QData"); } var path = Path.GetFullPath(Application.dataPath + Path.DirectorySeparatorChar + "QData/QAB/QAssets.cs"); StreamWriter writer = new StreamWriter(File.Open(path, FileMode.Create)); QABCodeGenerator.WriteClass(writer, "QAB", mABInfos); writer.Close(); AssetDatabase.Refresh(); } }
private static void GenerateVersionConfig(string outputPath) { string abManifestFile = Path.Combine(outputPath, QABUtil.GetPlatformName()); AssetBundle ab = AssetBundle.LoadFromFile(abManifestFile); AssetBundleManifest abMainfest = (AssetBundleManifest)ab.LoadAllAssets() [0]; string[] allABNames = abMainfest.GetAllAssetBundles(); XmlDocument xmlDoc = new XmlDocument(); XmlNode xmlRoot = xmlDoc.CreateElement("config"); xmlDoc.AppendChild(xmlRoot); mABInfos.Clear(); for (int i = 0; i < allABNames.Length; i++) { Hash128 hash = abMainfest.GetAssetBundleHash(allABNames [i]); byte[] fileBytes = QEditorUtil.GetFileBytes(Path.Combine(outputPath, allABNames [i])); string md5 = QEditorUtil.MD5(fileBytes); string size = QEditorUtil.Size(fileBytes); fileBytes = null; XmlElement xmlItem = xmlDoc.CreateElement("item"); string abName = QABConfigMgr.Instance.markItems4AbsPath [allABNames [i]].name; string absPath = allABNames [i]; xmlItem.SetAttribute("name", abName); xmlItem.SetAttribute("abspath", absPath); xmlItem.SetAttribute("md5", md5); xmlItem.SetAttribute("size", size); xmlRoot.AppendChild(xmlItem); AssetBundle assetBundle = AssetBundle.LoadFromFile(Path.Combine(outputPath, allABNames [i])); QABItemInfo abInfo = new QABItemInfo(abName, absPath); abInfo.assets = assetBundle.GetAllAssetNames(); mABInfos.Add(abInfo); } ab.Unload(true); byte[] platformBytes = QEditorUtil.GetFileBytes(Path.Combine(outputPath, QABUtil.GetPlatformName())); string platformMD5 = QEditorUtil.MD5(platformBytes); string platformSize = QEditorUtil.Size(platformBytes); platformBytes = null; XmlElement platformItem = xmlDoc.CreateElement("item"); platformItem.SetAttribute("name", QABUtil.GetPlatformName()); platformItem.SetAttribute("abspath", QABUtil.GetPlatformName()); platformItem.SetAttribute("md5", platformMD5); platformItem.SetAttribute("size", platformSize); xmlRoot.AppendChild(platformItem); xmlDoc.Save(outputPath + "/resitems.xml"); AssetDatabase.Refresh(); if (!Directory.Exists(Application.dataPath + Path.DirectorySeparatorChar + "QData")) { Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + "QData"); } var path = Path.GetFullPath(Application.dataPath + Path.DirectorySeparatorChar + "QData/QAB/QAssets.cs"); StreamWriter writer = new StreamWriter(File.Open(path, FileMode.Create)); QABCodeGenerator.WriteClass(writer, "QAB", mABInfos); writer.Close(); AssetDatabase.Refresh(); }
private static void GenerateVersionConfig(string outputPath){ string abManifestFile = Path.Combine (outputPath, QPlatform.GetPlatformName ()); AssetBundle ab = AssetBundle.LoadFromFile (abManifestFile); AssetBundleManifest abMainfest = (AssetBundleManifest)ab.LoadAllAssets () [0]; string[] allABNames = abMainfest.GetAllAssetBundles (); XmlDocument xmlDoc = new XmlDocument (); XmlNode xmlRoot = xmlDoc.CreateElement ("config"); xmlDoc.AppendChild (xmlRoot); mABInfos.Clear (); for (int i = 0; i < allABNames.Length; i++) { Hash128 hash = abMainfest.GetAssetBundleHash (allABNames [i]); byte[] fileBytes = QEditorUtil.GetFileBytes (Path.Combine (outputPath, allABNames [i])); string md5 = QEditorUtil.MD5(fileBytes); string size = QEditorUtil.Size (fileBytes); fileBytes = null; XmlElement xmlItem = xmlDoc.CreateElement ("item"); string abName = QABConfigMgr.Instance.markItems4AbsPath [allABNames [i]].name; string absPath = allABNames [i]; xmlItem.SetAttribute ("name", abName); xmlItem.SetAttribute ("abspath", absPath); xmlItem.SetAttribute ("md5", md5); xmlItem.SetAttribute ("size", size); xmlRoot.AppendChild (xmlItem); AssetBundle assetBundle = AssetBundle.LoadFromFile (Path.Combine (outputPath, allABNames [i])); QABItemInfo abInfo= new QABItemInfo (abName,absPath); abInfo.assets = assetBundle.GetAllAssetNames (); mABInfos.Add (abInfo); assetBundle.Unload (true); } ab.Unload (true); byte[] platformBytes = QEditorUtil.GetFileBytes (Path.Combine (outputPath, QPlatform.GetPlatformName ())); string platformMD5 = QEditorUtil.MD5 (platformBytes); string platformSize = QEditorUtil.Size (platformBytes); platformBytes = null; XmlElement platformItem = xmlDoc.CreateElement ("item"); platformItem.SetAttribute ("name", QPlatform.GetPlatformName()); platformItem.SetAttribute ("abspath", QPlatform.GetPlatformName()); platformItem.SetAttribute ("md5", platformMD5); platformItem.SetAttribute ("size", platformSize); xmlRoot.AppendChild (platformItem); xmlDoc.Save (outputPath + "/resitems.xml"); AssetDatabase.Refresh (); if (!Directory.Exists (Application.dataPath + Path.DirectorySeparatorChar + "QData")) { Directory.CreateDirectory (Application.dataPath + Path.DirectorySeparatorChar + "QData"); } var path = Path.GetFullPath (Application.dataPath + Path.DirectorySeparatorChar + "QData/QAB/QAssets.cs"); StreamWriter writer = new StreamWriter(File.Open (path, FileMode.Create)); QABCodeGenerator.WriteClass (writer,"QAB",mABInfos); writer.Close (); AssetDatabase.Refresh (); }