Exemple #1
0
 static void StreamType <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s,
                                        object ctxt, ref MegaloScriptVariableType type)
     where TDoc : class
     where TCursor : class
 {
     s.StreamCursorEnum(ref type);
 }
Exemple #2
0
        int FromVariableIndexName(MegaloScriptVariableType type, string name)
        {
            int id = TypeExtensionsBlam.IndexOfByPropertyNotFoundResult;

            switch (type)
            {
            case MegaloScriptVariableType.Numeric:  id = MegaloScriptModel.FindNameIndex(Numerics, name); break;

            case MegaloScriptVariableType.Timer:    id = MegaloScriptModel.FindNameIndex(Timers, name); break;

            case MegaloScriptVariableType.Team:             id = MegaloScriptModel.FindNameIndex(Teams, name); break;

            case MegaloScriptVariableType.Player:   id = MegaloScriptModel.FindNameIndex(Players, name); break;

            case MegaloScriptVariableType.Object:   id = MegaloScriptModel.FindNameIndex(Objects, name); break;

            default: throw new KSoft.Debug.UnreachableException(type.ToString());
            }

            if (id == TypeExtensionsBlam.IndexOfByPropertyNotFoundResult)
            {
                throw new KeyNotFoundException(string.Format("Couldn't find {0} {1} variable named {2}",
                                                             SetType, type, name));
            }

            return(id);
        }
        public MegaloScriptValueType(int nameIndex, int bitLength,
                                     MegaloScriptVariableType varType, MegaloScriptVariableSet varSet) : this()
        {
            Contract.Requires(ValidateNameIndex(nameIndex));
            Contract.Requires(ValidateBitLength(bitLength));

            InitializeHandle(out mHandle, nameIndex, MegaloScriptValueBaseType.Var, bitLength, (uint)varType, (uint)varSet);
        }
Exemple #4
0
        internal bool VarIndexIsValid(MegaloScriptVariableType type, int index)
        {
            switch (type)
            {
            case MegaloScriptVariableType.Numeric: return(index < Numerics.Count);

            case MegaloScriptVariableType.Timer: return(index < Timers.Count);

            case MegaloScriptVariableType.Team: return(index < Teams.Count);

            case MegaloScriptVariableType.Player: return(index < Players.Count);

            case MegaloScriptVariableType.Object: return(index < Objects.Count);

            default: throw new KSoft.Debug.UnreachableException(type.ToString());
            }
        }
Exemple #5
0
        string ToVariableIndexName(MegaloScriptVariableType type, int index)
        {
            if (index < 0 || !VarIndexIsValid(type, index))
            {
                throw new System.IO.InvalidDataException(string.Format("#{0} is not a valid {1}.{2} variable index",
                                                                       index.ToString(), SetType.ToString(), type.ToString()));
            }

            switch (type)
            {
            case MegaloScriptVariableType.Numeric: return(Numerics[index].CodeName);

            case MegaloScriptVariableType.Timer: return(Timers[index].CodeName);

            case MegaloScriptVariableType.Team: return(Teams[index].CodeName);

            case MegaloScriptVariableType.Player: return(Players[index].CodeName);

            case MegaloScriptVariableType.Object: return(Objects[index].CodeName);

            default: throw new KSoft.Debug.UnreachableException(type.ToString());
            }
        }
Exemple #6
0
 /// <summary>Validates that the variable index in a given set is valid based on its type</summary>
 /// <param name="type">Type of variable</param>
 /// <param name="set">Set the variable is in</param>
 /// <param name="index">Index of the variable in the set</param>
 /// <returns></returns>
 public bool VarIndexIsValid(MegaloScriptVariableType type, MegaloScriptVariableSet set, int index)
 {
     return(index >= 0 && this[set].VarIndexIsValid(type, index));
 }
Exemple #7
0
 public IndexNameResolvingContext(MegaloScriptModel model, MegaloScriptVariableSet varSet, MegaloScriptVariableType varType, bool supportNone = false)
 {
     Model = model; VarSet = varSet; VarType = varType; SupportNone = supportNone;
 }
Exemple #8
0
 public IndexNameResolvingContext(MegaloScriptModel model, Proto.MegaloScriptValueType valueType, bool supportNone = false)
 {
     Model = model; VarSet = valueType.VarSet; VarType = valueType.VarType; SupportNone = supportNone;
 }
Exemple #9
0
        static string ToVariableIndexName(MegaloScriptModel model, MegaloScriptVariableSet varSet, MegaloScriptVariableType varType, int index, bool supportNone)
        {
            if (index.IsNone())
            {
                if (supportNone)
                {
                    return(MegaloScriptModel.kIndexNameNone);
                }
                else
                {
                    throw new System.IO.InvalidDataException(string.Format("Encountered a {0}.{1} variable reference that was NONE, where NONE isn't supported",
                                                                           varSet.ToString(), varType.ToString()));
                }
            }

            return(model[varSet].ToVariableIndexName(varType, index));
        }
Exemple #10
0
        static int FromVariableIndexName(MegaloScriptModel model, MegaloScriptVariableSet varSet, MegaloScriptVariableType varType, string name, bool supportNone)
        {
            if (supportNone && name == MegaloScriptModel.kIndexNameNone)
            {
                return(TypeExtensions.kNone);
            }

            return(model[varSet].FromVariableIndexName(varType, name));
        }
Exemple #11
0
 internal int GetVariableIndexBitLength(MegaloScriptVariableSet set, MegaloScriptVariableType type)
 {
     return(VariableSets[set].Traits[type].IndexBitLength);
 }
Exemple #12
0
 internal int GetVariableMaxCount(MegaloScriptVariableSet set, MegaloScriptVariableType type)
 {
     return(VariableSets[set].Traits[type].MaxCount);
 }
        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;
            }
        }