Beispiel #1
0
        internal static string ToFlagsName(MegaloScriptDatabase db, MegaloScriptValueType flagsValueType, uint flags)
        {
            if (flags == 0)
            {
                return("0");
            }

            var e = db.Enums[flagsValueType.EnumIndex];

            ToFlagsNameValidateFlags(e, flags);

            var  sb         = new System.Text.StringBuilder();
            bool first_flag = true;

            for (int x = 0; x < e.Members.Count; x++)
            {
                if (!Bitwise.Flags.Test(flags, 1U << x))
                {
                    continue;
                }

                if (!first_flag)
                {
                    sb.Append(kFlagsSeperator);
                }

                sb.Append(e.Members[x]);
                first_flag = false;
            }

            return(sb.ToString());
        }
Beispiel #2
0
        internal static uint FromFlagsName(MegaloScriptDatabase db, MegaloScriptValueType flagsValueType, string names)
        {
            if (string.IsNullOrEmpty(names) || names == "0")
            {
                return(0);
            }

            var  e     = db.Enums[flagsValueType.EnumIndex];
            uint flags = 0;

            string[] parts = names.Split(kFlagsSeperator, StringSplitOptions.RemoveEmptyEntries);
            for (int x = 0; x < parts.Length; x++)
            {
                int bit;
                if (e.NameToIndex.TryGetValue(parts[x], out bit))
                {
                    flags |= 1U << bit;
                }
                else
                {
                    throw new KeyNotFoundException(string.Format("'{0}' is not a valid bit name in {1}",
                                                                 parts[x], e.Name));
                }
            }

            return(flags);
        }
Beispiel #3
0
        internal static string ToMemberName(MegaloScriptDatabase db, MegaloScriptValueType enumValueType, int index)
        {
            var e = db.Enums[enumValueType.EnumIndex];

            if (enumValueType.EnumTraits == Proto.MegaloScriptValueEnumTraits.HasNoneMember)
            {
                index += 1;
            }
            return(e.Members[index]);
        }
Beispiel #4
0
            public void Initialize(MegaloScriptDatabase db)
            {
                var player_ref_type = db.VariableRefTypes[MegaloScriptVariableReferenceType.Player];

                PlayerIndexObject = player_ref_type.NameToMember[kObject_PlayerVar_Name].ValueType;
                PlayerIndexPlayer = player_ref_type.NameToMember[kPlayer_PlayerVar_Name].ValueType;

                ObjectReferenceType = db.VariableRefTypes[MegaloScriptVariableReferenceType.Object];
                // #NOTE_BLAM: assumes Player.SlaveObject is the first reference type and everything after it are player variable SlaveObject references
                FirstSlaveObjectTypeIndex =
                    ObjectReferenceType.NameToMember[kPlayer_SlaveObject_Name].TypeIndex;
                HighestSlaveObjectTypeIndex = ObjectReferenceType.Members.Count - 1;
            }
Beispiel #5
0
        internal static int FromMemberName(MegaloScriptDatabase db, MegaloScriptValueType enumValueType, string name)
        {
            var e     = db.Enums[enumValueType.EnumIndex];
            int index = 0;

            if (e.NameToIndex.TryGetValue(name, out index))
            {
                return(enumValueType.EnumTraits != Proto.MegaloScriptValueEnumTraits.HasNoneMember ? index : index - 1);
            }
            else
            {
                throw new KeyNotFoundException(string.Format("'{0}' is not a valid member name in {1}",
                                                             name, e.Name));
            }
        }
Beispiel #6
0
 public FlagsNameResolvingContext(Proto.MegaloScriptDatabase db, MegaloScriptValueType valueType)
 {
     Db = db; ValueType = valueType;
 }
        internal bool SerializeValueTypeReference <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s,
                                                                  string attributeName, ref MegaloScriptValueType type, bool isOptional = false)
            where TDoc : class
            where TCursor : class
        {
            bool streamed = true;
            bool reading  = s.IsReading;

            string type_name = reading ? null : ValueTypeNames[type.NameIndex];

            if (isOptional)
            {
                streamed = s.StreamAttributeOpt(attributeName, ref type_name, ValueTypeNameIsNotNone);

                if (!streamed && reading)
                {
                    type_name = kNoValueTypeName;
                }
            }
            else
            {
                s.StreamAttribute(attributeName, ref type_name);
            }

            if (reading)
            {
                type = GetValueType(type_name);
            }

            return(streamed);
        }
        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;
            }
        }