public void Load(Stream source, TextureSet textureSet, TextureDatabase textureDatabase, bool leaveOpen = false) { Load(source, leaveOpen); if (textureSet == null || TextureIds.Count != textureSet.Textures.Count) { return; } TextureSet = textureSet; for (int i = 0; i < TextureIds.Count; i++) { TextureSet.Textures[i].Id = TextureIds[i]; var textureInfo = textureDatabase?.GetTextureInfo(TextureIds[i]); if (textureInfo != null) { TextureSet.Textures[i].Name = textureInfo.Name; } } }
public void Save(Stream destination, ObjectDatabase objectDatabase, TextureDatabase textureDatabase, BoneDatabase boneDatabase, bool leaveOpen = false) { if (objectDatabase != null) { foreach (var obj in Objects) { obj.Id = objectDatabase.GetObjectInfo(obj.Name)?.Id ?? obj.Id; } } if (boneDatabase != null) { string fileName = destination is FileStream fileStream ? Path.GetFileName(fileStream.Name) : string.Empty; var skeleton = boneDatabase.Skeletons.FirstOrDefault(x => fileName.StartsWith(x.Name, StringComparison.OrdinalIgnoreCase)) ?? boneDatabase.Skeletons.FirstOrDefault(x => x.Name.Equals("CMN", StringComparison.OrdinalIgnoreCase)) ?? null; if (skeleton != null) { foreach (var skin in Objects.Where(x => x.Skin != null).Select(x => x.Skin)) { foreach (var boneInfo in skin.Bones) { int index = skin.ExData?.BoneNames?.FindIndex(x => x.Equals(boneInfo.Name, StringComparison.OrdinalIgnoreCase)) ?? -1; if (index == -1) { index = skeleton.ObjectBoneNames.FindIndex(x => x.Equals(boneInfo.Name, StringComparison.OrdinalIgnoreCase)); } else { index = 0x8000 | index; } if (index == -1) { continue; } foreach (var childBone in skin.Bones.Where(x => x.ParentId == boneInfo.Id)) { childBone.ParentId = index; } boneInfo.Id = index; } } } } if (textureDatabase != null && TextureSet != null) { int id = textureDatabase.Textures.Max(x => x.Id) + 1; var idDictionary = new Dictionary <int, int>(TextureSet.Textures.Count); foreach (var texture in TextureSet.Textures) { var textureInfo = !string.IsNullOrEmpty(texture.Name) ? textureDatabase.GetTextureInfo(texture.Name) : textureDatabase.GetTextureInfo(texture.Id); if (textureInfo == null) { textureDatabase.Textures.Add( textureInfo = new TextureInfo { Name = texture.Name, Id = id++ }); if (string.IsNullOrEmpty(textureInfo.Name)) { textureInfo.Name = $"Unnamed {textureInfo.Id}"; } } idDictionary[texture.Id] = textureInfo.Id; texture.Name = textureInfo.Name; texture.Id = textureInfo.Id; } foreach (var materialTexture in Objects.SelectMany(x => x.Materials) .SelectMany(x => x.MaterialTextures)) { if (idDictionary.TryGetValue(materialTexture.TextureId, out id)) { materialTexture.TextureId = id; } } TextureIds.Clear(); TextureIds.AddRange(TextureSet.Textures.Select(x => x.Id)); } Save(destination, leaveOpen); }