public byte[] GetBytes(uint imageBase, LandTableFormat format, Dictionary <string, uint> labels, out uint address) { List <byte> result = new List <byte>(); byte[] tmpbyte; uint[] colmdladdrs = new uint[COL.Count]; uint tmpaddr; List <COL> cnk = new List <COL>(); List <COL> bas = new List <COL>(); foreach (COL item in COL) { if (item.Model.Attach is BasicAttach) { bas.Add(item); } else { cnk.Add(item); } } COL.Clear(); COL.AddRange(cnk); COL.AddRange(bas); for (int i = 0; i < COL.Count; i++) { if (labels.ContainsKey(COL[i].Model.Name)) { colmdladdrs[i] = labels[COL[i].Model.Name]; } else { result.Align(4); tmpbyte = COL[i].Model.GetBytes(imageBase + (uint)result.Count, format == LandTableFormat.SADX, labels, out tmpaddr); colmdladdrs[i] = tmpaddr + (uint)result.Count + imageBase; result.AddRange(tmpbyte); } } uint[] animmdladdrs = new uint[Anim.Count]; uint[] animaniaddrs = new uint[Anim.Count]; for (int i = 0; i < Anim.Count; i++) { if (labels.ContainsKey(Anim[i].Model.Name)) { animmdladdrs[i] = labels[Anim[i].Model.Name]; } else { result.Align(4); tmpbyte = Anim[i].Model.GetBytes(imageBase + (uint)result.Count, format == LandTableFormat.SADX, labels, out tmpaddr); animmdladdrs[i] = tmpaddr + (uint)result.Count + imageBase; result.AddRange(tmpbyte); } if (labels.ContainsKey(Anim[i].Animation.Name)) { animaniaddrs[i] = labels[Anim[i].Animation.Name]; } else { result.Align(4); tmpbyte = Anim[i].Animation.WriteHeader(imageBase + (uint)result.Count, animmdladdrs[i], labels, out tmpaddr); animaniaddrs[i] = tmpaddr + (uint)result.Count + imageBase; result.AddRange(tmpbyte); } } uint coladdr; if (COL.Count > 0) { if (labels.ContainsKey(COLName)) { coladdr = labels[COLName]; } else { coladdr = imageBase + (uint)result.Count; labels.Add(COLName, coladdr); for (int i = 0; i < COL.Count; i++) { result.Align(4); result.AddRange(COL[i].GetBytes(imageBase + (uint)result.Count, colmdladdrs[i], format)); } } } else { coladdr = 0; } uint animaddr; if (Anim.Count > 0) { if (labels.ContainsKey(AnimName)) { animaddr = labels[AnimName]; } else { animaddr = imageBase + (uint)result.Count; labels.Add(AnimName, animaddr); for (int i = 0; i < Anim.Count; i++) { result.Align(4); result.AddRange(Anim[i].GetBytes(imageBase + (uint)result.Count, animmdladdrs[i], animaniaddrs[i])); } } } else { animaddr = 0; } result.Align(4); uint texnameaddr = 0; if (TextureFileName != null) { texnameaddr = imageBase + (uint)result.Count; result.AddRange(Encoding.ASCII.GetBytes(TextureFileName)); result.Add(0); } result.Align(4); address = (uint)result.Count; result.AddRange(ByteConverter.GetBytes((ushort)COL.Count)); switch (format) { case LandTableFormat.SA1: case LandTableFormat.SADX: result.AddRange(ByteConverter.GetBytes((ushort)Anim.Count)); result.AddRange(ByteConverter.GetBytes(Flags)); result.AddRange(ByteConverter.GetBytes(Unknown1)); result.AddRange(ByteConverter.GetBytes(coladdr)); result.AddRange(ByteConverter.GetBytes(animaddr)); result.AddRange(ByteConverter.GetBytes(texnameaddr)); result.AddRange(ByteConverter.GetBytes(TextureList)); result.AddRange(ByteConverter.GetBytes(Unknown2)); result.AddRange(ByteConverter.GetBytes(Unknown3)); break; case LandTableFormat.SA2: result.AddRange(ByteConverter.GetBytes((ushort)cnk.Count)); result.AddRange(new byte[8]); // TODO: figure out what these do result.AddRange(ByteConverter.GetBytes(Unknown1)); result.AddRange(ByteConverter.GetBytes(coladdr)); result.AddRange(ByteConverter.GetBytes(animaddr)); result.AddRange(ByteConverter.GetBytes(texnameaddr)); result.AddRange(ByteConverter.GetBytes(TextureList)); break; } labels.Add(Name, address + imageBase); return(result.ToArray()); }
public void ToStructVariables(TextWriter writer, LandTableFormat format, List <string> labels, string[] textures = null) { List <COL> cnk = new List <COL>(); List <COL> bas = new List <COL>(); foreach (COL item in COL) { if (item.Model.Attach is BasicAttach) { bas.Add(item); } else { cnk.Add(item); } } COL.Clear(); COL.AddRange(cnk); COL.AddRange(bas); for (int i = 0; i < COL.Count; i++) { if (!labels.Contains(COL[i].Model.Name)) { labels.Add(COL[i].Model.Name); COL[i].Model.ToStructVariables(writer, format == LandTableFormat.SADX, labels, textures); } } for (int i = 0; i < Anim.Count; i++) { string aniid = Anim[i].Animation.Name.MakeIdentifier(); if (!labels.Contains(Anim[i].Model.Name)) { labels.Add(Anim[i].Model.Name); Anim[i].Model.ToStructVariables(writer, format == LandTableFormat.SADX, labels, textures); } if (!labels.Contains(aniid)) { labels.Add(aniid); Anim[i].Animation.ToStructVariables(writer); writer.Write("NJS_ACTION action_"); writer.Write(aniid); writer.Write(" = { &"); writer.Write(Anim[i].Model.Name); writer.Write(", &"); writer.Write(aniid); writer.WriteLine(" };"); writer.WriteLine(); } } if (!labels.Contains(COLName)) { labels.Add(COLName); writer.Write("COL "); writer.Write(COLName); writer.WriteLine("[] = {"); List <string> lines = new List <string>(COL.Count); foreach (COL item in COL) { lines.Add(item.ToStruct(format)); } writer.WriteLine("\t" + string.Join("," + Environment.NewLine + "\t", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); } if (Anim.Count > 0 && !labels.Contains(AnimName)) { labels.Add(AnimName); writer.Write("GeoAnimData "); writer.Write(AnimName); writer.WriteLine("[] = {"); List <string> lines = new List <string>(Anim.Count); foreach (GeoAnimData item in Anim) { lines.Add(item.ToStruct()); } writer.WriteLine("\t" + string.Join("," + Environment.NewLine + "\t", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); } writer.Write("LandTable "); writer.Write(Name); writer.Write(" = { LengthOfArray<int16_t>("); writer.Write(COLName); writer.Write("), "); switch (format) { case LandTableFormat.SA1: case LandTableFormat.SADX: writer.Write(Anim.Count > 0 ? "LengthOfArray<int16_t>(" + AnimName + ")" : "0"); writer.Write(", "); writer.Write(Flags.ToCHex()); writer.Write(", "); writer.Write(Unknown1.ToC()); writer.Write(", "); writer.Write(COLName); writer.Write(", "); writer.Write(Anim.Count > 0 ? AnimName : "NULL"); writer.Write(", "); writer.Write(TextureFileName.ToC()); writer.Write(", (NJS_TEXLIST *)"); writer.Write(TextureList.ToCHex()); writer.Write(", "); writer.Write(Unknown2.ToCHex()); writer.Write(", "); writer.Write(Unknown3.ToCHex()); break; case LandTableFormat.SA2: writer.Write(cnk.Count); writer.Write(", 0, 0, 0, 0, "); writer.Write(Unknown1.ToC()); writer.Write(", "); writer.Write(COLName); writer.Write(", "); writer.Write(Anim.Count > 0 ? AnimName : "NULL"); writer.Write(", "); writer.Write(TextureFileName.ToC()); writer.Write(", (NJS_TEXLIST *)"); writer.Write(TextureList.ToCHex()); break; } writer.WriteLine(" };"); }
static void Main(string[] args) { string filename; if (args.Length > 0) { filename = args[0]; Console.WriteLine("File: {0}", filename); } else { Console.Write("File: "); filename = Console.ReadLine(); } LandTable level = LandTable.LoadFromFile(filename); switch (level.Format) { case LandTableFormat.SA1: { List<COL> newcollist = new List<COL>(); foreach (COL col in level.COL.Where((col) => col.Model != null && col.Model.Attach != null)) { if ((col.SurfaceFlags & SurfaceFlags.Visible) == SurfaceFlags.Visible) { COL newcol = new COL() { Bounds = col.Bounds }; newcol.SurfaceFlags = SurfaceFlags.Visible; newcol.Model = new SonicRetro.SAModel.NJS_OBJECT() { Name = col.Model.Name + "_cnk" }; newcol.Model.Position = col.Model.Position; newcol.Model.Rotation = col.Model.Rotation; newcol.Model.Scale = col.Model.Scale; BasicAttach basatt = (BasicAttach)col.Model.Attach; ChunkAttach cnkatt = new ChunkAttach(true, true) { Name = basatt.Name + "_cnk", Bounds = basatt.Bounds }; newcol.Model.Attach = cnkatt; VertexChunk vcnk; if (basatt.Normal != null && basatt.Normal.Length > 0) vcnk = new VertexChunk(ChunkType.Vertex_VertexNormal); else vcnk = new VertexChunk(ChunkType.Vertex_Vertex); vcnk.Vertices = new List<Vertex>(basatt.Vertex); if (basatt.Normal != null) vcnk.Normals = new List<Vertex>(basatt.Normal); vcnk.VertexCount = (ushort)basatt.Vertex.Length; vcnk.Size = (ushort)((vcnk.Type == ChunkType.Vertex_VertexNormal ? vcnk.VertexCount * 6 : vcnk.VertexCount * 3) + 1); cnkatt.Vertex.Add(vcnk); foreach (NJS_MESHSET mesh in basatt.Mesh) { if (mesh.PolyType != Basic_PolyType.Strips) { Console.WriteLine("Warning: Skipping non-strip mesh in {0} ({1}).", basatt.MeshName, mesh.PolyType); continue; } NJS_MATERIAL mat = null; if (basatt.Material != null && mesh.MaterialID < basatt.Material.Count) { mat = basatt.Material[mesh.MaterialID]; cnkatt.Poly.Add(new PolyChunkBitsBlendAlpha() { SourceAlpha = mat.SourceAlpha, DestinationAlpha = mat.DestinationAlpha }); cnkatt.Poly.Add(new PolyChunkTinyTextureID() { ClampU = mat.ClampU, ClampV = mat.ClampV, FilterMode = mat.FilterMode, FlipU = mat.FlipU, FlipV = mat.FlipV, SuperSample = mat.SuperSample, TextureID = (ushort)mat.TextureID }); cnkatt.Poly.Add(new PolyChunkMaterial() { Diffuse = mat.DiffuseColor, Specular = mat.SpecularColor, SpecularExponent = (byte)mat.Exponent }); } PolyChunkStrip strip; if (mesh.UV != null & mesh.VColor != null) strip = new PolyChunkStrip(ChunkType.Strip_StripUVNColor); else if (mesh.UV != null) strip = new PolyChunkStrip(ChunkType.Strip_StripUVN); else if (mesh.VColor != null) strip = new PolyChunkStrip(ChunkType.Strip_StripColor); else strip = new PolyChunkStrip(ChunkType.Strip_Strip); if (mat != null) { strip.IgnoreLight = mat.IgnoreLighting; strip.IgnoreSpecular = mat.IgnoreSpecular; strip.UseAlpha = mat.UseAlpha; strip.DoubleSide = mat.DoubleSided; strip.FlatShading = mat.FlatShading; strip.EnvironmentMapping = mat.EnvironmentMap; } int striptotal = 0; foreach (Strip item in mesh.Poly.Cast<Strip>()) { UV[] uvs = null; if (mesh.UV != null) { uvs = new UV[item.Indexes.Length]; Array.Copy(mesh.UV, striptotal, uvs, 0, item.Indexes.Length); } Color[] vcolors = null; if (mesh.VColor != null) { vcolors = new Color[item.Indexes.Length]; Array.Copy(mesh.VColor, striptotal, vcolors, 0, item.Indexes.Length); } strip.Strips.Add(new PolyChunkStrip.Strip(item.Reversed, item.Indexes, uvs, vcolors)); striptotal += item.Indexes.Length; } cnkatt.Poly.Add(strip); } newcollist.Add(newcol); } if ((col.SurfaceFlags & ~SurfaceFlags.Visible) != 0) { col.SurfaceFlags &= ~SurfaceFlags.Visible; newcollist.Add(col); } } level.COL = newcollist; } level.Anim = new List<GeoAnimData>(); level.Tool = "SA Tools Level Converter"; level.SaveToFile(System.IO.Path.ChangeExtension(filename, "sa2lvl"), LandTableFormat.SA2); break; case LandTableFormat.SA2: Vertex[] VertexBuffer = new Vertex[0]; Vertex[] NormalBuffer = new Vertex[0]; foreach (COL col in level.COL.Where((col) => col.Model != null && col.Model.Attach is ChunkAttach)) { ChunkAttach cnkatt = (ChunkAttach)col.Model.Attach; BasicAttach basatt = new BasicAttach() { Name = cnkatt.Name, Bounds = cnkatt.Bounds }; if (cnkatt.Vertex != null) foreach (VertexChunk chunk in cnkatt.Vertex) { if (VertexBuffer.Length < chunk.IndexOffset + chunk.VertexCount) { Array.Resize(ref VertexBuffer, chunk.IndexOffset + chunk.VertexCount); Array.Resize(ref NormalBuffer, chunk.IndexOffset + chunk.VertexCount); } Array.Copy(chunk.Vertices.ToArray(), 0, VertexBuffer, chunk.IndexOffset, chunk.Vertices.Count); Array.Copy(chunk.Normals.ToArray(), 0, NormalBuffer, chunk.IndexOffset, chunk.Normals.Count); } NJS_MATERIAL material = new NJS_MATERIAL() { UseTexture = true }; int minVtx = int.MaxValue; int maxVtx = int.MinValue; foreach (PolyChunk chunk in cnkatt.Poly) switch (chunk.Type) { case ChunkType.Bits_BlendAlpha: { PolyChunkBitsBlendAlpha c2 = (PolyChunkBitsBlendAlpha)chunk; material.SourceAlpha = c2.SourceAlpha; material.DestinationAlpha = c2.DestinationAlpha; } break; case ChunkType.Bits_MipmapDAdjust: break; case ChunkType.Bits_SpecularExponent: material.Exponent = ((PolyChunkBitsSpecularExponent)chunk).SpecularExponent; break; case ChunkType.Tiny_TextureID: case ChunkType.Tiny_TextureID2: { PolyChunkTinyTextureID c2 = (PolyChunkTinyTextureID)chunk; material.ClampU = c2.ClampU; material.ClampV = c2.ClampV; material.FilterMode = c2.FilterMode; material.FlipU = c2.FlipU; material.FlipV = c2.FlipV; material.SuperSample = c2.SuperSample; material.TextureID = c2.TextureID; } break; case ChunkType.Material_Diffuse: case ChunkType.Material_Ambient: case ChunkType.Material_DiffuseAmbient: case ChunkType.Material_Specular: case ChunkType.Material_DiffuseSpecular: case ChunkType.Material_AmbientSpecular: case ChunkType.Material_DiffuseAmbientSpecular: case ChunkType.Material_Diffuse2: case ChunkType.Material_Ambient2: case ChunkType.Material_DiffuseAmbient2: case ChunkType.Material_Specular2: case ChunkType.Material_DiffuseSpecular2: case ChunkType.Material_AmbientSpecular2: case ChunkType.Material_DiffuseAmbientSpecular2: { PolyChunkMaterial c2 = (PolyChunkMaterial)chunk; if (c2.Diffuse.HasValue) material.DiffuseColor = c2.Diffuse.Value; if (c2.Specular.HasValue) { material.SpecularColor = c2.Specular.Value; material.Exponent = c2.SpecularExponent; } } break; case ChunkType.Strip_Strip: case ChunkType.Strip_StripUVN: case ChunkType.Strip_StripUVH: case ChunkType.Strip_StripNormal: case ChunkType.Strip_StripUVNNormal: case ChunkType.Strip_StripUVHNormal: case ChunkType.Strip_StripColor: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVHColor: case ChunkType.Strip_Strip2: case ChunkType.Strip_StripUVN2: case ChunkType.Strip_StripUVH2: { PolyChunkStrip c2 = (PolyChunkStrip)chunk; material.DoubleSided = c2.DoubleSide; material.EnvironmentMap = c2.EnvironmentMapping; material.FlatShading = c2.FlatShading; material.IgnoreLighting = c2.IgnoreLight; material.IgnoreSpecular = c2.IgnoreSpecular; material.UseAlpha = c2.UseAlpha; bool hasVColor = false; switch (chunk.Type) { case ChunkType.Strip_StripColor: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVHColor: hasVColor = true; break; } bool hasUV = false; switch (chunk.Type) { case ChunkType.Strip_StripUVN: case ChunkType.Strip_StripUVH: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVHColor: case ChunkType.Strip_StripUVN2: case ChunkType.Strip_StripUVH2: hasUV = true; break; } List<Strip> strips = new List<Strip>(c2.StripCount); List<UV> uvs = hasUV ? new List<UV>() : null; List<Color> vcolors = hasVColor ? new List<Color>() : null; foreach (PolyChunkStrip.Strip strip in c2.Strips) { minVtx = Math.Min(minVtx, strip.Indexes.Min()); maxVtx = Math.Max(maxVtx, strip.Indexes.Max()); strips.Add(new Strip(strip.Indexes, strip.Reversed)); if (hasUV) uvs.AddRange(strip.UVs); if (hasVColor) vcolors.AddRange(strip.VColors); } NJS_MESHSET mesh = new NJS_MESHSET(strips.ToArray(), false, hasUV, hasVColor); if (hasUV) uvs.CopyTo(mesh.UV); if (hasVColor) vcolors.CopyTo(mesh.VColor); mesh.MaterialID = (ushort)basatt.Material.Count; basatt.Mesh.Add(mesh); basatt.Material.Add(material); material = new NJS_MATERIAL(material.GetBytes(), 0); } break; } int numVtx = maxVtx - minVtx + 1; basatt.ResizeVertexes(numVtx); Array.Copy(VertexBuffer, minVtx, basatt.Vertex, 0, numVtx); Array.Copy(NormalBuffer, minVtx, basatt.Normal, 0, numVtx); foreach (NJS_MESHSET mesh in basatt.Mesh) foreach (Poly poly in mesh.Poly) for (int i = 0; i < poly.Indexes.Length; i++) poly.Indexes[i] = (ushort)(poly.Indexes[i] - minVtx); col.Model.Attach = basatt; } level.Anim = new List<GeoAnimData>(); level.Tool = "SA Tools Level Converter"; level.SaveToFile(System.IO.Path.ChangeExtension(filename, "sa1lvl"), LandTableFormat.SA1); break; } }