public MegaloScriptValueType(int nameIndex, int bitLength, int enumIndex, MegaloScriptValueEnumTraits enumTraits) : this() { Contract.Requires(ValidateNameIndex(nameIndex)); Contract.Requires(ValidateBitLength(bitLength)); InitializeHandle(out mHandle, nameIndex, MegaloScriptValueBaseType.Enum, bitLength, (uint)enumIndex, (uint)enumTraits); }
internal int ValidBitLengthForEnum(int bitLength, MegaloScriptValueEnumTraits traits) { uint max_value = (uint)Members.Count; if (traits == MegaloScriptValueEnumTraits.HasNoneMember) { max_value++; } int required_bit_count = Bits.GetMaxEnumBits(max_value); return(bitLength - required_bit_count); }
static void SerializeValueType <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s, MegaloScriptDatabase db, ref MegaloScriptValueType value) where TDoc : class where TCursor : class { bool reading = s.IsReading; var base_type = reading ? MegaloScriptValueBaseType.None : value.BaseType; string name = reading ? null : db.ValueTypeNames[value.NameIndex]; int bit_length = reading ? 0 : value.BitLength; s.StreamAttributeEnum("baseType", ref base_type); s.StreamAttribute("name", ref name); bool has_bit_length = s.StreamAttributeOpt("bitLength", ref bit_length, Predicates.IsNotZero); int name_index = reading ? db.ValueTypeNames.Count : -1; if (reading) { db.ValueTypeNames.Add(name); } switch (base_type) { #region MegaloScriptValueBaseType.None case MegaloScriptValueBaseType.None: if (reading) { Contract.Assert(name_index == 0, "There should only be one None value type and it should come first"); Contract.Assert(db.ValueTypeNames[name_index] == kNoValueTypeName, "None value type is using non-standard name"); value = new MegaloScriptValueType(name_index, base_type, 0); } break; #endregion #region MegaloScriptValueBaseType.Single case MegaloScriptValueBaseType.Single: { int encoding_index = reading ? 0 : value.EncodingIndex; s.StreamAttributeIdAsString("encoding", ref encoding_index, db, (_db, str) => _db.SingleEncodings.FindIndex((x) => str.Equals(x.Name)), (_db, id) => _db.SingleEncodings[id].Name); if (reading) // #NOTE_BLAM: requires -1 when reading the TypeParam as the encoding index { value = new MegaloScriptValueType(name_index, base_type, bit_length, (uint)(encoding_index + 1)); } } break; #endregion #region MegaloScriptValueBaseType.Point3d case MegaloScriptValueBaseType.Point3d: { if (!has_bit_length) { throw new System.IO.InvalidDataException(string.Format("Point3d {0} didn't define a bit-length", name)); } bool is_signed = reading ? false : value.PointIsSigned; s.StreamAttributeOpt("signed", ref is_signed, Predicates.IsTrue); if (reading) { value = new MegaloScriptValueType(name_index, base_type, bit_length, 0, is_signed ? 1U : 0U); } } break; #endregion #region MegaloScriptValueBaseType.Flags case MegaloScriptValueBaseType.Flags: { int enum_index = reading ? 0 : value.EnumIndex; MegaloScriptValueEnumTraits enum_traits = reading ? 0 : value.EnumTraits; db.SerializeEnumTypeReference(s, "enumType", ref enum_index); if (reading) { if (!has_bit_length) { bit_length = db.Enums[enum_index].Members.Count; } value = new MegaloScriptValueType(name_index, base_type, bit_length, (uint)enum_index); } } break; #endregion #region MegaloScriptValueBaseType.Enum case MegaloScriptValueBaseType.Enum: { int enum_index = reading ? 0 : value.EnumIndex; MegaloScriptValueEnumTraits enum_traits = reading ? 0 : value.EnumTraits; db.SerializeEnumTypeReference(s, "enumType", ref enum_index); s.StreamAttributeEnumOpt("enumTraits", ref enum_traits, e => e != MegaloScriptValueEnumTraits.None); if (reading) { if (!has_bit_length) { int max_value = db.Enums[enum_index].Members.Count; if (enum_traits == MegaloScriptValueEnumTraits.HasNoneMember) { max_value++; } bit_length = Bits.GetMaxEnumBits(max_value); } value = new MegaloScriptValueType(name_index, bit_length, enum_index, enum_traits); } } break; #endregion #region MegaloScriptValueBaseType.Index case MegaloScriptValueBaseType.Index: { MegaloScriptValueIndexTarget index = reading ? MegaloScriptValueIndexTarget.Undefined : value.IndexTarget; MegaloScriptValueIndexTraits index_traits = reading ? 0 : value.IndexTraits; s.StreamAttributeEnum("indexTarget", ref index); s.StreamAttributeEnum("indexTraits", ref index_traits); if (reading) { // #NOTE_BLAM: If we for whatever reason wrote the DB back out, types with implicit bit lengths // would have that length written where they didn't before if (!has_bit_length) { bit_length = db.Limits.GetIndexTargetBitLength(index, index_traits); } value = new MegaloScriptValueType(name_index, bit_length, index, index_traits); } } break; #endregion #region MegaloScriptValueBaseType.Var case MegaloScriptValueBaseType.Var: { MegaloScriptVariableType var_type = reading ? 0 : value.VarType; MegaloScriptVariableSet var_set = reading ? 0 : value.VarSet; s.StreamAttributeEnum("varType", ref var_type); s.StreamAttributeEnum("varSet", ref var_set); if (reading) { // #NOTE_BLAM: If we for whatever reason wrote the DB back out, types with implicit bit lengths // would have that length written where they didn't before if (!has_bit_length) { bit_length = db.GetVariableIndexBitLength(var_set, var_type); } value = new MegaloScriptValueType(name_index, bit_length, var_type, var_set); } } break; #endregion #region MegaloScriptValueBaseType.VarReference case MegaloScriptValueBaseType.VarReference: { MegaloScriptVarReferenceType var_ref = reading ? 0 : value.VarReference; s.StreamAttributeEnum("varReferenceType", ref var_ref); if (reading) { value = new MegaloScriptValueType(name_index, var_ref); } } break; #endregion #region MegaloScriptValueBaseType.Tokens case MegaloScriptValueBaseType.Tokens: { int max_tokens = reading ? 0 : value.MaxTokens; s.StreamAttribute("maxTokens", ref max_tokens); if (reading) { bit_length = Bits.GetMaxEnumBits(max_tokens + 1); value = new MegaloScriptValueType(name_index, base_type, bit_length, (uint)max_tokens); } } break; #endregion #region MegaloScriptValueBaseType.ObjectReferenceWithPlayerVarIndex case MegaloScriptValueBaseType.ObjectReferenceWithPlayerVarIndex: { if (reading) { value = new MegaloScriptValueType(name_index, MegaloScriptVarReferenceType.Object, MegaloScriptValueBaseType.ObjectReferenceWithPlayerVarIndex); } } break; #endregion default: if (reading) { value = new MegaloScriptValueType(name_index, base_type, bit_length); } break; } }