public InjectOffset newInject(int off, int type) { InjectOffset t = new InjectOffset(); t.type = type; t.off = off; return(t); }
public InjectOffset newInject(int off, int type) { InjectOffset t = new InjectOffset(); t.type = type; t.off = off; return t; }
public byte[] InjectFromPSK(PSKFile.PSKObject PSK) { byte[] buff = new byte[0]; if (offFace == -1 || offIndex == -1 || offUV == -1 || offVert == -1 || offUnk == -1) { return(buff); } List <InjectOffset> l = new List <InjectOffset>(); MemoryStream m = new MemoryStream(); l.Add(newInject(offFace, 0)); l.Add(newInject(offIndex, 1)); l.Add(newInject(offUV, 2)); l.Add(newInject(offVert, 3)); l.Add(newInject(offUnk, 4)); bool run = true; while (run) { run = false; for (int i = 0; i < 4; i++) { if (l[i].off > l[i + 1].off) { InjectOffset t = l[i]; l[i] = l[i + 1]; l[i + 1] = t; run = true; } } } int nextoff = l[0].off; int pos = 0; int size; int count; for (int i = 0; i < 5; i++) { for (int j = 0; j < nextoff - pos; j++) { m.WriteByte(memory[pos + j]); } switch (l[i].type) { #region Faces case 0: count = PSK.Faces.Length; buff = BitConverter.GetBytes((Int32)8); m.Write(buff, 0, 4); buff = BitConverter.GetBytes((Int32)count); m.Write(buff, 0, 4); for (int j = 0; j < count; j++) { buff = BitConverter.GetBytes((UInt16)PSK.Faces[j].e0); m.Write(buff, 0, 2); buff = BitConverter.GetBytes((UInt16)PSK.Faces[j].e1); m.Write(buff, 0, 2); buff = BitConverter.GetBytes((UInt16)PSK.Faces[j].e2); m.Write(buff, 0, 2); buff = BitConverter.GetBytes((UInt16)0); m.Write(buff, 0, 2); } break; #endregion #region IndexBuffer case 1: ushort[] Indexes = new ushort[PSK.Faces.Length * 3]; int n = 0; for (int j = 0; j < PSK.Faces.Length; j++) { Indexes[n] = PSK.Faces[j].e0; n++; Indexes[n] = PSK.Faces[j].e1; n++; Indexes[n] = PSK.Faces[j].e2; n++; } count = Indexes.Length; buff = BitConverter.GetBytes((Int32)2); m.Write(buff, 0, 4); buff = BitConverter.GetBytes((Int32)count); m.Write(buff, 0, 4); for (int j = 0; j < count; j++) { buff = BitConverter.GetBytes((UInt16)Indexes[j]); m.Write(buff, 0, 2); } break; #endregion #region UV case 2: size = BitConverter.ToInt32(memory, l[i].off); count = BitConverter.ToInt32(memory, l[i].off); int subc = size / 4; count = PSK.Edges.Length; buff = BitConverter.GetBytes((Int32)size); m.Write(buff, 0, 4); buff = BitConverter.GetBytes((Int32)count); m.Write(buff, 0, 4); for (int j = 0; j < count; j++) { for (int k = 0; k < subc; k++) { buff = BitConverter.GetBytes(FloatToHalf(PSK.Edges[j].U)); m.Write(buff, 0, 2); buff = BitConverter.GetBytes(FloatToHalf(PSK.Edges[j].V)); m.Write(buff, 0, 2); } } break; #endregion #region Verts case 3: count = PSK.Edges.Length; buff = BitConverter.GetBytes((Int32)12); m.Write(buff, 0, 4); buff = BitConverter.GetBytes((Int32)count); m.Write(buff, 0, 4); for (int j = 0; j < count; j++) { buff = BitConverter.GetBytes(PSK.Points[PSK.Edges[j].index].x); m.Write(buff, 0, 4); buff = BitConverter.GetBytes(PSK.Points[PSK.Edges[j].index].y); m.Write(buff, 0, 4); buff = BitConverter.GetBytes(PSK.Points[PSK.Edges[j].index].z); m.Write(buff, 0, 4); } break; #endregion #region Unkown case 4: size = BitConverter.ToInt32(memory, nextoff); count = BitConverter.ToInt32(memory, nextoff + 4); if (size == 6) { buff = BitConverter.GetBytes((Int32)size); m.Write(buff, 0, 4); count = 1; while (count * 8 <= PSK.Faces.Length) { count *= 2; } buff = BitConverter.GetBytes((Int32)count); m.Write(buff, 0, 4); for (int j = 0; j < size * count; j++) { m.WriteByte(0); } } else { m.Write(Unknown1, 0, Unknown1.Length); } break; #endregion } if (i < 4) { pos = nextoff; nextoff = l[i + 1].off; //if (l[i + 1].type == 3) //{ // size = BitConverter.ToInt32(memory, nextoff - 12); // if (size == 12) // { // buff = BitConverter.GetBytes((Int32)PSK.Edges.Length); // for (int j = 0; j < 4; j++) // memory[nextoff - 8 + j] = buff[j]; // } //} //if (l[i + 1].type == 2) //{ // size = BitConverter.ToInt32(memory, nextoff - 16); // if (size == 12) // { // buff = BitConverter.GetBytes((Int32)PSK.Edges.Length); // for (int j = 0; j < 4; j++) // memory[nextoff - 12 + j] = buff[j]; // } //} //if (l[i + 1].type == 1) //{ // buff = BitConverter.GetBytes((Int32)PSK.Faces.Length * 3); // for (int j = 0; j < 4; j++) // memory[nextoff - 4 + j] = buff[j]; //} size = BitConverter.ToInt32(memory, pos); count = BitConverter.ToInt32(memory, pos + 4); pos += size * count + 8; } else { pos = l[i].off; size = BitConverter.ToInt32(memory, pos); count = BitConverter.ToInt32(memory, pos + 4); pos += size * count + 8; nextoff = memsize; for (int j = 0; j < nextoff - pos; j++) { m.WriteByte(memory[pos + j]); } } } //SaveFileDialog d = new SaveFileDialog(); //d.Filter = "*.bin|*.bin"; //buff = m.ToArray(); //if (d.ShowDialog() == DialogResult.OK) //{ // FileStream fs = new FileStream(d.FileName, FileMode.Create, FileAccess.Write); // fs.Write(buff,0,buff.Length); // fs.Close(); // MessageBox.Show("Done."); //} return(m.ToArray()); }