/// <summary> /// 创建翻译文件 /// </summary> /// <param name="language">目标语言</param> public void CreateTranslationFile(string language) { if (!Application.isEditor) { throw new NotSupportedException($"Cannot create translation file for {id}: static file compiler can only run in editor mode"); } var target = $"Assets/{CompileConfiguration.Content.TranslationFolder}/{language}"; if (!Directory.Exists(target)) { Directory.CreateDirectory(target); } target = CreateLanguageAssetPathFromId(id, language); if (File.Exists(target)) { var translation = new ScriptTranslation(File.ReadAllText(target, Encoding.UTF8)); if (translation.MergeWith(ScriptHeader.LoadSync(id).Header.LoadDefaultTranslation())) { translation.SaveToAsset(target); } } else { ScriptHeader.LoadSync(id).Header.LoadDefaultTranslation().SaveToAsset(target); } }
/// <summary> /// 编译文件 /// </summary> /// <param name="path">脚本ID</param> /// <param name="forceCompile">是否强制重新编译</param> /// <returns>发生变化的文件列表</returns> public static IEnumerable <string> CompileAsset(string path, bool forceCompile = false) { if (!Application.isEditor) { throw new NotSupportedException($"Cannot compile {path}: static file compiler can only run in editor mode"); } if (!path.EndsWith(".vns")) { throw new NotSupportedException($"Cannot compile {path}: file name extension must be .vns"); } var option = ScriptInformation.CreateInformationFromAsset(path); if (option == null) { throw new NullReferenceException($"Cannot compile {path}: target outside of source folder or target is not acceptable script/binary"); } var changedFiles = new List <string>(); var source = File.ReadAllText(path, Encoding.UTF8).UnifyLineBreak(); if (!option.hash.HasValue) { option.hash = Hasher.Crc32(Encoding.UTF8.GetBytes(source)); } var identifier = new CodeIdentifier { Id = option.id, Hash = option.hash.Value }; // 编译文件 if (!forceCompile) { if (option.recordedHash.HasValue && option.recordedHash.Value == identifier.Hash) { return(new string[] { }); // 如果源代码内容没有变化则直接跳过编译 } } var(content, defaultTranslation) = CompileCode(source, identifier); var binaryFile = option.BinaryAssetPath(); File.WriteAllBytes(binaryFile, content); option.recordedHash = option.hash = identifier.Hash; changedFiles.Add(binaryFile); // 处理其他翻译 foreach (var(language, _) in option.Translations) { var languageFile = option.LanguageAssetPath(language); if (File.Exists(languageFile)) { var existedTranslation = new ScriptTranslation(File.ReadAllText(languageFile)); if (!existedTranslation.MergeWith(defaultTranslation)) { continue; } existedTranslation.SaveToAsset(languageFile); changedFiles.Add(languageFile); } else { // 如果翻译不存在,以默认翻译为蓝本新建翻译文件 defaultTranslation.SaveToAsset(languageFile); changedFiles.Add(languageFile); } } CompileConfiguration.Save(); return(changedFiles); }