public static void Generate(string ns, List <AssetBundleInfo> assetBundleInfos, string projectTag = null) { QNamespaceDefine nameSpace = new QNamespaceDefine(); nameSpace.Name = ns; nameSpace.GenerateDir = ABEditorPathConfig.ABCodeGeneratePath; nameSpace.FileName = "QAssets.cs"; for (int i = 0; i < assetBundleInfos.Count; i++) { AssetBundleInfo assetBundleInfo = assetBundleInfos [i]; string className = assetBundleInfo.name; 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, ""); } className = className.ToUpper(); QClassDefine classDefine = new QClassDefine(); nameSpace.Classes.Add(classDefine); classDefine.Name = className; QVariable variable = new QVariable(QAccessLimit.Public, QCompileType.Const, QTypeDefine.String, "BUNDLE_NAME", bundleName.ToUpperInvariant()); classDefine.Variables.Add(variable); foreach (var asset in assetBundleInfo.assets) { string content = Path.GetFileNameWithoutExtension(asset).ToUpperInvariant(); QVariable assetVariable = new QVariable(QAccessLimit.Public, QCompileType.Const, QTypeDefine.String, content.Replace("@", "_").Replace("!", "_"), content); classDefine.Variables.Add(assetVariable); } } QCodeGenerator.Generate(nameSpace); }
private static void GenerateVersionConfig(string outputPath) { var abManifestFile = Path.Combine(outputPath, GetPlatformName()); var ab = AssetBundle.LoadFromFile(abManifestFile); var abMainfest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest"); var allABNames = abMainfest.GetAllAssetBundles(); AssetBundleInfos.Clear(); foreach (var abName in allABNames) { var assetBundle = AssetBundle.LoadFromFile(Path.Combine(outputPath, abName)); var abInfo = new AssetBundleInfo(abName) { assets = assetBundle.GetAllAssetNames() }; AssetBundleInfos.Add(abInfo); assetBundle.Unload(true); } ab.Unload(true); AssetDatabase.Refresh(); if (ResKitEditorWindow.isEnableGenerateClass) { "Assets/QFrameworkData".CreateDirIfNotExists(); var path = Path.GetFullPath(Application.dataPath + Path.DirectorySeparatorChar + "QFrameworkData/QAssets.cs"); var writer = new StreamWriter(File.Open(path, FileMode.Create)); QBundleInfoGenerator.WriteClass(writer, "QAssetBundle", AssetBundleInfos); writer.Close(); AssetDatabase.Refresh(); } }
public static void WriteClass(TextWriter writer, string ns, List <AssetBundleInfo> 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++) { AssetBundleInfo assetBundleInfo = assetBundleInfos [i]; string className = assetBundleInfo.name; string bundleName = className.Substring(0, 1).ToUpper() + className.Substring(1); int firstNumber; if (int.TryParse(bundleName[0].ToString(), out firstNumber)) { continue; } 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(bundleName.ToUpperInvariant()); Dictionary <string, string> checkRepeatDict = new Dictionary <string, string> (); 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.Replace("@", "_").Replace("!", "_"); assetField.Type = new CodeTypeReference(typeof(System.String)); if (!assetField.Name.StartsWith("[") && !assetField.Name.StartsWith(" [") && !checkRepeatDict.ContainsKey(assetField.Name)) { checkRepeatDict.Add(assetField.Name, asset); codeType.Members.Add(assetField); } assetField.InitExpression = new CodePrimitiveExpression(content); } checkRepeatDict.Clear(); } var provider = new CSharpCodeProvider(); var options = new CodeGeneratorOptions(); options.BlankLinesBetweenMembers = false; options.BracingStyle = "C"; provider.GenerateCodeFromCompileUnit(compileUnit, writer, options); }
private static void GenerateVersionConfig(string outputPath, List <string> finalZips, List <string> finalFiles) { string abManifestFile; if (projectTag != "") { abManifestFile = Path.Combine(outputPath, projectTag); } else { abManifestFile = Path.Combine(outputPath, GetPlatformName()); } AssetBundle ab = AssetBundle.LoadFromFile(abManifestFile); AssetBundleManifest abMainfest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest"); string[] allABNames = abMainfest.GetAllAssetBundles(); XmlDocument xmlDoc = new XmlDocument(); XmlElement xmlRoot = xmlDoc.CreateElement("config"); xmlRoot.SetAttribute("res_version", QResSystemBuilder.resVersion); xmlDoc.AppendChild(xmlRoot); assetBundleInfos.Clear(); for (int i = 0; i < allABNames.Length; 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])); AssetBundleInfo abInfo = new AssetBundleInfo(allABNames[i]); abInfo.assets = assetBundle.GetAllAssetNames(); assetBundleInfos.Add(abInfo); assetBundle.Unload(true); } // 这里要加上平台相关的xml string platformBundleName = 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); } XmlElement filTiem = CreateConfigItem(xmlDoc, Application.streamingAssetsPath + QFrameworkConfigData.RELATIVE_AB_ROOT_FOLDER + QFrameworkConfigData.EXPORT_ASSETBUNDLE_CONFIG_FILENAME, QFrameworkConfigData.EXPORT_ASSETBUNDLE_CONFIG_FILENAME, QFrameworkConfigData.EXPORT_ASSETBUNDLE_CONFIG_FILENAME); xmlRoot.AppendChild(filTiem); ab.Unload(true); xmlDoc.Save(outputPath + "/resconfig.xml"); AssetDatabase.Refresh(); if (QResSystemBuilder.isEnableGenerateClass) { if (!Directory.Exists("QFrameworkData")) { Directory.CreateDirectory("QFrameworkData"); } var path = Path.GetFullPath(Application.dataPath + Path.DirectorySeparatorChar + "QFrameworkData/QAssets.cs"); StreamWriter writer = new StreamWriter(File.Open(path, FileMode.Create)); QBundleInfoGenerator.WriteClass(writer, "QAssetBundle", assetBundleInfos, projectTag); writer.Close(); AssetDatabase.Refresh(); } }
private static void GenerateVersionConfig(string outputPath, List <string> finalZips, List <string> finalFiles) { string abManifestFile; if (projectTag != "") { abManifestFile = Path.Combine(outputPath, projectTag); } else { abManifestFile = Path.Combine(outputPath, GetPlatformName()); } AssetBundle ab = AssetBundle.LoadFromFile(abManifestFile); AssetBundleManifest abMainfest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest"); string[] allABNames = abMainfest.GetAllAssetBundles(); XmlDocument xmlDoc = new XmlDocument(); XmlElement xmlRoot = xmlDoc.CreateElement("config"); xmlRoot.SetAttribute("res_version", QAssetBundleBuilder.resVersion); xmlDoc.AppendChild(xmlRoot); assetBundleInfos.Clear(); for (int i = 0; i < allABNames.Length; 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])); AssetBundleInfo abInfo = new AssetBundleInfo(allABNames[i]); abInfo.assets = assetBundle.GetAllAssetNames(); assetBundleInfos.Add(abInfo); assetBundle.Unload(true); } // 这里要加上平台相关的xml string platformBundleName = 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); xmlDoc.Save(outputPath + "/resconfig.xml"); AssetDatabase.Refresh(); if (QAssetBundleBuilder.isEnableGenerateClass) { IOUtils.CreateDirIfNotExists("QFrameworkData"); QABCodeGenerator.Generate("QAssetBundle", assetBundleInfos, projectTag); } }