public void WriteOffsetU24(FileWriter writer, uint Offset, ParamEntry entry) { using (writer.TemporarySeek(OffsetPosition, SeekOrigin.Begin)) { uint ValuePacked = 0; writer.Write((uint)((ValuePacked << 24) | ((Offset - BasePosition) >> 2))); writer.Seek(OffsetPosition + 3, SeekOrigin.Begin); writer.Write((byte)entry.ParamType); } }
private static string WriteParamData(ParamEntry entry) { string value = ""; switch (entry.ParamType) { case ParamType.Boolean: value = $"{(bool)entry.Value}"; break; case ParamType.BufferBinary: value = $"!BufferBinary [ {WriteBytes((byte[])entry.Value)} ]"; break; case ParamType.BufferFloat: value = $"!BufferFloat [ {WriteFloats((float[])entry.Value)} ]"; break; case ParamType.BufferInt: value = $"!BufferInt [ {WriteInts((int[])entry.Value)} ]"; break; case ParamType.BufferUint: value = $"!BufferUint [ {WriteUints((uint[])entry.Value)} ]"; break; case ParamType.Quat: value = $"!BufferUint [ {WriteFloats((float[])entry.Value)} ]"; break; case ParamType.Color4F: value = $"{WriteColor4F((Vector4F)entry.Value)}"; break; case ParamType.Vector2F: value = $"{WriteVec2F((Vector2F)entry.Value)}"; break; case ParamType.Vector3F: value = $"{WriteVec3F((Vector3F)entry.Value)}"; break; case ParamType.Vector4F: value = $"{WriteVec4F((Vector4F)entry.Value)}"; break; case ParamType.Uint: value = $"{(uint)entry.Value}"; break; case ParamType.Int: value = $"{(int)entry.Value}"; break; case ParamType.Float: value = string.Format("{0:0.0######}", (float)entry.Value); break; case ParamType.String256: value = $"!str256 {((AampLibraryCSharp.StringEntry)entry.Value).ToString()}"; break; case ParamType.String32: value = $"!str32 {((AampLibraryCSharp.StringEntry)entry.Value).ToString()}"; break; case ParamType.String64: value = $"!str64 {((AampLibraryCSharp.StringEntry)entry.Value).ToString()}"; break; case ParamType.StringRef: value = $"!strRef {((AampLibraryCSharp.StringEntry)entry.Value).ToString()}"; break; case ParamType.Curve1: value = $"{WriteCurve((Curve[])entry.Value, 1)}"; break; case ParamType.Curve2: value = $"{WriteCurve((Curve[])entry.Value, 2)}"; break; case ParamType.Curve3: value = $"{WriteCurve((Curve[])entry.Value, 3)}"; break; case ParamType.Curve4: value = $"{WriteCurve((Curve[])entry.Value, 4)}"; break; default: throw new Exception("Unsupported type! " + entry.ParamType); } return($"{YamlHashStr(entry.HashString)}: {value}"); }
public void SetEntryValue(string hashName, object value) { ParamEntry entry = paramEntries.FirstOrDefault(x => x.HashString == hashName); if (entry != null) { entry.Value = value; } else { entry = new ParamEntry(); entry.HashString = hashName; entry.Value = value; } }
private static ParamEntry ParseParamEntry(string key, YamlNode valueNode) { ParamEntry entry = new ParamEntry(); entry.Hash = ParseHash(key); if (valueNode is YamlSequenceNode) { var values = ((YamlSequenceNode)valueNode); switch (valueNode.Tag) { case "!BufferBinary": entry.Value = ToByteArray(values); entry.ParamType = ParamType.BufferBinary; break; case "!BufferFloat": entry.Value = ToFloatArray(values); entry.ParamType = ParamType.BufferFloat; break; case "!BufferUint": entry.Value = ToUIntArray(values); entry.ParamType = ParamType.BufferUint; break; case "!BufferInt": entry.Value = ToIntArray(values); entry.ParamType = ParamType.BufferInt; break; case "!vec2": { float[] singles = ToFloatArray(values, 2); entry.Value = new Vector2F(singles[0], singles[1]); entry.ParamType = ParamType.Vector2F; } break; case "!vec3": { float[] singles = ToFloatArray(values, 3); entry.Value = new Vector3F(singles[0], singles[1], singles[2]); entry.ParamType = ParamType.Vector3F; } break; case "!color": { float[] singles = ToFloatArray(values, 4); entry.Value = new Vector4F( singles[0], singles[1], singles[2], singles[3]); entry.ParamType = ParamType.Color4F; } break; case "!vec4": { float[] singles = ToFloatArray(values, 4); entry.Value = new Vector4F( singles[0], singles[1], singles[2], singles[3]); entry.ParamType = ParamType.Vector4F; } break; case "!curve1": entry.Value = ParseCurves(values, 1); entry.ParamType = ParamType.Curve1; break; case "!curve2": entry.Value = ParseCurves(values, 2); entry.ParamType = ParamType.Curve2; break; case "!curve3": entry.Value = ParseCurves(values, 3); entry.ParamType = ParamType.Curve3; break; case "!curve4": entry.Value = ParseCurves(values, 4); entry.ParamType = ParamType.Curve4; break; default: throw new Exception($"Unknown tag type using a sequence! {valueNode.Tag}"); } } else if (valueNode is YamlScalarNode) { var value = ((YamlScalarNode)valueNode).Value; switch (valueNode.Tag) { case "!str256": entry.Value = new StringEntry(Encoding.UTF8.GetBytes(value)); entry.ParamType = ParamType.String256; break; case "!str64": entry.Value = new StringEntry(Encoding.UTF8.GetBytes(value)); entry.ParamType = ParamType.String64; break; case "!str32": entry.Value = new StringEntry(Encoding.UTF8.GetBytes(value)); entry.ParamType = ParamType.String32; break; case "!strRef": entry.Value = new StringEntry(Encoding.UTF8.GetBytes(value)); entry.ParamType = ParamType.StringRef; break; default: bool booleanValue; uint uintValue; float floatValue; int intValue; bool isBoolean = bool.TryParse(value, out booleanValue); bool isUint = uint.TryParse(value, out uintValue); bool isFloat = float.TryParse(value, out floatValue); bool isInt = int.TryParse(value, out intValue); bool HasDecimal = value.Contains("."); if (isBoolean) { entry.Value = booleanValue; entry.ParamType = ParamType.Boolean; } else if (isUint && !HasDecimal) { entry.Value = uintValue; entry.ParamType = ParamType.Uint; } else if (isInt && !HasDecimal) { entry.Value = intValue; entry.ParamType = ParamType.Int; } else if (isFloat) { entry.Value = floatValue; entry.ParamType = ParamType.Float; } else { throw new Exception($"Failed to parse value for param {key} value {value}!"); } break; } } return(entry); }
private byte[] GetParamData(ParamEntry entry) { MemoryStream mem = new MemoryStream(); var writer = new FileWriter(mem); writer.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian; switch (entry.ParamType) { case ParamType.Boolean: writer.Write((bool)entry.Value == false ? (uint)0 : (uint)1); break; case ParamType.Float: writer.Write((float)entry.Value); break; case ParamType.Int: writer.Write((int)entry.Value); break; case ParamType.Vector2F: writer.WriteVector2F((Vector2F)entry.Value); break; case ParamType.Vector3F: writer.WriteVector3F((Vector3F)entry.Value); break; case ParamType.Vector4F: writer.WriteVector4F((Vector4F)entry.Value); break; case ParamType.Color4F: writer.WriteVector4F((Vector4F)entry.Value); break; case ParamType.Quat: writer.Write((float[])entry.Value); break; case ParamType.Uint: writer.Write((uint)entry.Value); break; case ParamType.BufferUint: writer.Write(((uint[])entry.Value).Length); writer.Write((uint[])entry.Value); break; case ParamType.BufferInt: writer.Write(((int[])entry.Value).Length); writer.Write((int[])entry.Value); break; case ParamType.BufferFloat: writer.Write(((float[])entry.Value).Length); writer.Write((float[])entry.Value); break; case ParamType.BufferBinary: writer.Write(((byte[])entry.Value).Length); writer.Write((byte[])entry.Value); break; case ParamType.String64: case ParamType.String32: case ParamType.String256: case ParamType.StringRef: writer.Write(((AampLibraryCSharp.StringEntry)entry.Value).Data); break; case ParamType.Curve1: case ParamType.Curve2: case ParamType.Curve3: case ParamType.Curve4: int curveAmount = entry.ParamType - ParamType.Curve1 + 1; var curves = (Curve[])entry.Value; for (int i = 0; i < curveAmount; i++) { writer.Write(curves[i].valueUints); writer.Write(curves[i].valueFloats); } break; default: throw new Exception("Unsupported param type! " + entry.ParamType); } return(mem.ToArray()); }
internal static ParamEntry Read(FileReader reader) { ParamEntry entry = new ParamEntry(); long CurrentPosition = reader.Position; entry.Hash = reader.ReadUInt32(); int field4 = reader.ReadInt32(); int DataOffset = (field4 & 0xffffff); var type = (field4 >> 24); entry.ParamType = (ParamType)type; int DataSize = 12; if (DataOffset != 0) { using (reader.TemporarySeek(DataOffset * 4 + CurrentPosition, SeekOrigin.Begin)) { switch (entry.ParamType) { case ParamType.Boolean: entry.Value = reader.ReadIntBoolean(); break; case ParamType.Float: entry.Value = reader.ReadSingle(); break; case ParamType.Quat: entry.Value = reader.ReadSingles(4); break; case ParamType.Int: entry.Value = reader.ReadInt32(); break; case ParamType.Vector2F: entry.Value = reader.ReadVector2F(); break; case ParamType.Vector3F: entry.Value = reader.ReadVector3F(); break; case ParamType.Vector4F: entry.Value = reader.ReadVector4F(); break; case ParamType.Color4F: entry.Value = reader.ReadVector4F(); break; case ParamType.Uint: entry.Value = reader.ReadUInt32(); break; case ParamType.BufferUint: reader.Seek(-4, SeekOrigin.Current); uint countUInt = reader.ReadUInt32(); Console.WriteLine($"countUInt {countUInt}"); entry.Value = reader.ReadUInt32s((int)countUInt); break; case ParamType.BufferInt: reader.Seek(-4, SeekOrigin.Current); uint countInt = reader.ReadUInt32(); Console.WriteLine($"countInt {countInt}"); entry.Value = reader.ReadInt32s((int)countInt); break; case ParamType.BufferFloat: reader.Seek(-4, SeekOrigin.Current); uint countF = reader.ReadUInt32(); Console.WriteLine($"countF {countF}"); entry.Value = reader.ReadSingles((int)countF); break; case ParamType.BufferBinary: reader.Seek(-4, SeekOrigin.Current); uint countBin = reader.ReadUInt32(); entry.Value = reader.ReadBytes((int)countBin); break; case ParamType.String64: entry.Value = CreateStringEntry(reader, 64); break; case ParamType.String32: entry.Value = CreateStringEntry(reader, 32); break; case ParamType.String256: entry.Value = CreateStringEntry(reader, 256); break; case ParamType.StringRef: entry.Value = CreateStringEntry(reader, -1); break; case ParamType.Curve1: case ParamType.Curve2: case ParamType.Curve3: case ParamType.Curve4: int curveAmount = entry.ParamType - ParamType.Curve1 + 1; var curves = new Curve[curveAmount]; entry.Value = curves; for (int i = 0; i < curveAmount; i++) { curves[i] = new Curve(); curves[i].valueUints = reader.ReadUInt32s(2); curves[i].valueFloats = reader.ReadSingles(30); } break; default: throw new Exception("Unsupported param type! " + entry.ParamType); } } } else { switch (entry.ParamType) { case ParamType.Boolean: entry.Value = false; break; case ParamType.String256: case ParamType.String32: case ParamType.String64: case ParamType.StringRef: entry.Value = ""; break; case ParamType.Float: case ParamType.Uint: case ParamType.Int: entry.Value = 0; break; } } return(entry); }
internal static ParamEntry Read(FileReader reader) { ParamEntry entry = new ParamEntry(); long CurrentPosition = reader.Position; uint Size = reader.ReadUInt32(); entry.ParamType = (ParamType)reader.ReadUInt32(); entry.Hash = reader.ReadUInt32(); int DataSize = (int)Size - 12; switch (entry.ParamType) { case ParamType.Boolean: entry.Value = reader.ReadBoolean(); break; case ParamType.Float: entry.Value = reader.ReadSingle(); break; case ParamType.Int: entry.Value = reader.ReadInt32(); break; case ParamType.Vector2F: entry.Value = reader.ReadVector2F(); break; case ParamType.Vector3F: entry.Value = reader.ReadVector3F(); break; case ParamType.Vector4F: entry.Value = reader.ReadVector4F(); break; case ParamType.Quat: entry.Value = reader.ReadSingles(4); break; case ParamType.Color4F: entry.Value = reader.ReadVector4F(); break; case ParamType.Uint: entry.Value = reader.ReadUInt32(); break; case ParamType.BufferUint: entry.Value = reader.ReadUInt32s(DataSize / sizeof(uint)); break; case ParamType.BufferInt: entry.Value = reader.ReadInt32s(DataSize / sizeof(int)); break; case ParamType.BufferFloat: entry.Value = reader.ReadSingles(DataSize / sizeof(float)); break; case ParamType.BufferBinary: entry.Value = reader.ReadBytes(DataSize); break; case ParamType.String64: entry.Value = CreateStringEntry(reader, 64); break; case ParamType.String32: entry.Value = CreateStringEntry(reader, 32); break; case ParamType.String256: entry.Value = CreateStringEntry(reader, 256); break; case ParamType.StringRef: entry.Value = CreateStringEntry(reader, -1); break; case ParamType.Curve1: case ParamType.Curve2: case ParamType.Curve3: case ParamType.Curve4: int curveAmount = entry.ParamType - ParamType.Curve1 + 1; var curves = new Curve[curveAmount]; entry.Value = curves; for (int i = 0; i < curveAmount; i++) { curves[i] = new Curve(); curves[i].valueUints = reader.ReadUInt32s(2); curves[i].valueFloats = reader.ReadSingles(30); } break; default: entry.Value = reader.ReadBytes(DataSize); break; } reader.Seek(CurrentPosition + Size, SeekOrigin.Begin); return(entry); }
internal static void Write(ParamEntry entry, FileWriter writer) { long startPosition = writer.Position; writer.Write(uint.MaxValue); //Write the size after writer.Write((uint)entry.ParamType); writer.Write(entry.Hash); switch (entry.ParamType) { case ParamType.Boolean: writer.Write((bool)entry.Value == false ? (byte)0 : (byte)1); break; case ParamType.Float: writer.Write((float)entry.Value); break; case ParamType.Int: writer.Write((int)entry.Value); break; case ParamType.Vector2F: writer.WriteVector2F((Vector2F)entry.Value); break; case ParamType.Vector3F: writer.WriteVector3F((Vector3F)entry.Value); break; case ParamType.Vector4F: writer.WriteVector4F((Vector4F)entry.Value); break; case ParamType.Color4F: writer.WriteVector4F((Vector4F)entry.Value); break; case ParamType.Quat: writer.Write((float[])entry.Value); break; case ParamType.Uint: writer.Write((uint)entry.Value); break; case ParamType.BufferUint: writer.Write((uint[])entry.Value); break; case ParamType.BufferInt: writer.Write((int[])entry.Value); break; case ParamType.BufferFloat: writer.Write((float[])entry.Value); break; case ParamType.BufferBinary: writer.Write((byte[])entry.Value); break; case ParamType.String64: case ParamType.String32: case ParamType.String256: case ParamType.StringRef: writer.Write(((AampLibraryCSharp.StringEntry)entry.Value).Data); // writer.Align(4); break; case ParamType.Curve1: case ParamType.Curve2: case ParamType.Curve3: case ParamType.Curve4: int curveAmount = entry.ParamType - ParamType.Curve1 + 1; var curves = (Curve[])entry.Value; for (int i = 0; i < curveAmount; i++) { writer.Write(curves[i].valueUints); writer.Write(curves[i].valueFloats); } break; default: writer.Write((byte[])entry.Value); break; } writer.WriteSize(writer.Position, startPosition); }