예제 #1
0
        static int StaticCompare(GameLanguageHandle lhs, GameLanguageHandle rhs)
        {
            Contract.Assert(GameLanguageHandle.BitCount < Bits.kInt32BitCount,
                            "Handle bits needs to be <= 31 (ie, sans sign bit) in order for this implementation of CompareTo to reasonably work");

            int lhs_data = (int)lhs.mHandle;
            int rhs_data = (int)rhs.mHandle;
            int result   = lhs_data - rhs_data;

            return(result);
        }
        void SerializeStringType(IO.EndianStream s, AKBKHashHeader hdr, AkSoundBank bank)
        {
            Contract.Assert(hdr.Type == AkSoundBankStringMappingBase.StringType.Bank);

            uint   bank_id = uint.MaxValue;
            string str     = null;

            s.Stream(ref bank_id);
            s.Stream(ref str, kStringEncoding);

            bank.MapIdToName(bank_id, str);
        }
예제 #3
0
        uint GetDataSize(XmbVariantMemoryPool pool)
        {
            uint size = Size;

            if (SizeIsIndirect)
            {
                size = pool.GetUInt32(Offset - sizeof(uint));
                Contract.Assert(size > Constants.kSizeBitField.Bitmask32);
            }

            return(size);
        }
        public static void SetSerializerInterface <TDoc, TCursor>(this IO.TagElementStream <TDoc, TCursor, string> s,
                                                                  XML.BXmlSerializerInterface xsi)
            where TDoc : class
            where TCursor : class
        {
            if (xsi != null)
            {
                Contract.Assert(s.Owner == null || !(s.Owner is XML.BXmlSerializerInterface));
            }

            s.Owner = xsi;
        }
예제 #5
0
        internal bool StreamID <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s, string xmlName, ref int dbid,
                                               TacticDataObjectKind kind,
                                               bool isOptional = true, IO.TagElementNodeType xmlSource = XML.XmlUtil.kSourceElement)
            where TDoc : class
            where TCursor : class
        {
            Contract.Requires(xmlSource.RequiresName() == (xmlName != XML.XmlUtil.kNoXmlName));
            Contract.Requires(kind != TacticDataObjectKind.None);

            string id_name      = null;
            bool   was_streamed = true;
            bool   to_lower     = false;

            if (s.IsReading)
            {
                if (isOptional)
                {
                    was_streamed = s.StreamStringOpt(xmlName, ref id_name, to_lower, xmlSource, intern: true);
                }
                else
                {
                    s.StreamString(xmlName, ref id_name, to_lower, xmlSource, intern: true);
                }

                if (was_streamed)
                {
                    IProtoDataObjectDatabaseProvider provider = this;
                    dbid = provider.GetId((int)kind, id_name);
                    Contract.Assert(dbid.IsNotNone());
                }
                else
                {
                    dbid = TypeExtensions.kNone;
                }
            }
            else if (s.IsWriting && dbid.IsNotNone())
            {
                IProtoDataObjectDatabaseProvider provider = this;
                id_name = provider.GetName((int)kind, dbid);
                Contract.Assert(!string.IsNullOrEmpty(id_name));

                if (isOptional)
                {
                    s.StreamStringOpt(xmlName, ref id_name, to_lower, xmlSource, intern: true);
                }
                else
                {
                    s.StreamString(xmlName, ref id_name, to_lower, xmlSource, intern: true);
                }
            }

            return(was_streamed);
        }
예제 #6
0
        /// <summary>Create a new instance of the system. Only call me if you are <see cref="BlamEngine"/>!</summary>
        /// <param name="prototype"></param>
        /// <returns></returns>
        internal EngineSystemBase NewInstance(BlamEngineSystem prototype)
        {
            Contract.Requires(prototype != null);
            Contract.Assert(mFactoryMethod != null, "Rerun engine unit tests");

            var system = mFactoryMethod();

            Contract.Assert(system != null);

            system.InitializeForNewInstance(prototype);
            return(system);
        }
예제 #7
0
        public virtual void BuildBuffer(IO.EndianStream blockStream, Stream sourceFile
                                        , Security.Cryptography.TigerHashBase hasher = null)
        {
            blockStream.AlignToBoundry(DataAlignmentBit);

            sourceFile.Seek(0, SeekOrigin.Begin);
            if (hasher != null)
            {
                UpdateDecompressedDataTigerHash(sourceFile, hasher);
            }

            Contract.Assert(blockStream.BaseStream.Position == blockStream.BaseStream.Length);

            DataOffset = blockStream.PositionPtr;

            // #TODO determine if compressing the sourceFile data has any savings (eg, 7% smaller)

            var assumed_compression_type = CompressionType;

            // #NOTE CompressionType can be Stored but IsDeflateStream can be true (seen it in XMB).
            // So just handle the flag as we do EcfCompressionType.DeflateStream
            if (IsDeflateStream)
            {
                assumed_compression_type = EcfCompressionType.DeflateStream;
            }

            switch (assumed_compression_type)
            {
            case EcfCompressionType.Stored:
            {
                // Update this ECF's size
                DataSize = (int)sourceFile.Length;
                // Also update this ECF's checksum
                Adler32 = Security.Cryptography.Adler32.Compute(sourceFile, DataSize, restorePosition: true);
                // Copy the source file's bytes to the block stream
                sourceFile.CopyTo(blockStream.BaseStream);
                break;
            }

            case EcfCompressionType.DeflateRaw:
                CompressSourceToStream(blockStream.Writer, sourceFile);
                break;

            case EcfCompressionType.DeflateStream:
                CompressSourceToCompressionStream(blockStream.Writer, sourceFile);
                break;

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

            Contract.Assert(blockStream.BaseStream.Position == ((long)DataOffset + DataSize));
        }
예제 #8
0
        protected override void Read <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s, BXmlSerializerInterface xs, int iteration)
        {
            int index = ReadExplicitIndex(s, xs);

            Contract.Assert(index.IsNotNone());

            mList.InitializeItem(index);
            T data = new T();

            data.Serialize(s);
            mList[index] = data;
        }
예제 #9
0
 public void Serialize(MegaloScriptModel model, IO.BitStream s)
 {
     foreach (var value_id in mValueIds)
     {
         if (s.IsWriting)
         {
             Contract.Assert(value_id.IsNotNone());
         }
         var value = model.Values[value_id];
         value.Serialize(model, s);
     }
 }
예제 #10
0
        public void InitializeValues(params MegaloScriptValueBase[] values)
        {
            Contract.Requires(values.Length == Count);

            for (int x = 0; x < mValueIds.Length; x++)
            {
                Contract.Assert(values[x] != null);
                Contract.Assert(ProtoData.ParameterList[x].Type.Equals(values[x].ValueType));
                mValueIds[x] = values[x].Id;
            }

            NotifyItemsInitialized();
        }
예제 #11
0
        static void InitializeHandle(out uint handle,
                                     int engineIndex, int branchIndex, int revisionIndex)
        {
            var encoder = new Bitwise.HandleBitEncoder();

            EngineBuildRevision.BitEncodeIndex(ref encoder, revisionIndex);
            EngineBuildBranch.BitEncodeIndex(ref encoder, branchIndex);
            BlamEngine.BitEncodeIndex(ref encoder, engineIndex);

            Contract.Assert(encoder.UsedBitCount == EngineBuildHandle.BitCount);

            handle = encoder.GetHandle32();
        }
예제 #12
0
        /// <summary>Bit encode an enumeration value into an unsigned integer</summary>
        /// <param name="value">Enumeration value to encode</param>
        /// <param name="bits">Bit data as an unsigned integer</param>
        /// <param name="bitIndex">Index in <paramref name="bits"/> to start encoding at</param>
        /// <param name="bitMask">Masking value for <paramref name="value"/></param>
        /// <remarks>
        /// On return <paramref name="bits"/> has <paramref name="value"/> encoded into it and <paramref name="bitIndex"/>
        /// is incremented by the bit count (determined from <paramref name="bitMask"/>)
        /// </remarks>
        public static void BitEncodeEnum(ulong value, ref ulong bits, ref int bitIndex, ulong bitMask)
        {
            Contract.Requires/*<ArgumentOutOfRangeException>*/ (bitIndex >= 0);
            Contract.Requires/*<ArgumentOutOfRangeException>*/ (bitIndex < kInt64BitCount);
            Contract.Requires/*<ArgumentException>*/ (bitMask != 0);

            int bit_count = BitCount(bitMask);

            Contract.Assert((bitIndex + bit_count) <= Bits.kInt64BitCount);

            bits      = BitEncodeEnum(value, bits, bitIndex, bitMask);
            bitIndex += bit_count;
        }
        internal void Swap(MegaloScriptModelObjectHandle lhs, MegaloScriptModelObjectHandle rhs)
        {
            int lhs_index = mElements.IndexOf(lhs);

            Contract.Assert(lhs_index >= 0);
            int rhs_index = mElements.IndexOf(rhs);

            Contract.Assert(rhs_index >= 0);

            mElements[rhs_index] = lhs;
            mElements[lhs_index] = rhs;
            NotifyItemsSwapped(lhs_index, lhs, rhs_index, rhs);
        }
예제 #14
0
 internal void Insert(MegaloScriptCondition cond, MegaloScriptModelObjectHandle evaluateBefore)
 {
     cond.AssociateWith(this);
     if (evaluateBefore.IsNone)
     {
         mConditions.Add(cond.Handle);
     }
     else
     {
         int insert_index = mConditions.IndexOf(evaluateBefore);
         Contract.Assert(insert_index >= 0);
         mConditions.Insert(insert_index, cond.Handle);
     }
 }
예제 #15
0
        public GameLanguageTable GetLanguageTable(Engine.EngineBuildHandle forBuild)
        {
#if LANGUAGE_SYSTEM_USE_ONLY_ONE_TABLE
            Contract.Requires <ArgumentNullException>(!forBuild.IsNone);
            Contract.Assert(forBuild.EngineIndex == mEngineTable.BuildHandle.EngineIndex);

            return(mEngineTable);
#else
            GameLanguageTable engine_table = null;
            forBuild.TryGetValue(mEngineTables, ref engine_table);

            return(engine_table);
#endif
        }
예제 #16
0
        void InitializeEngineLanguageTableWithBuildHandle()
        {
            mEngineLanguageTable = new GameLanguageHandle[LanguageRegistry.NumberOfLanguages];

            for (int langIndex = 0; langIndex < mEngineLanguageTable.Length; langIndex++)
            {
                mEngineLanguageTable[langIndex] =
                    new GameLanguageHandle(mBuildHandle, langIndex, TypeExtensions.kNone);
            }

            Contract.Assert(LanguageRegistry.NumberOfLanguages <= Bits.kInt32BitCount,
                            nameof(mOptionalEngineLanguageFlags) + " and " + nameof(mOptionalGameLanguageFlags) +
                            " are too small to actually be a language bitvector");
        }
예제 #17
0
        protected override void SerializeActivity <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s,
                                                                  string attrName, ref sbyte value)
        {
            var activity = s.IsReading ? GameActivity.None : (GameActivity)value;

            s.StreamAttributeEnum(attrName, ref activity);

            Contract.Assert(activity < GameActivity.kNumberOf);

            if (s.IsReading)
            {
                value = (sbyte)activity;
            }
        }
예제 #18
0
 protected void SerializeIdOpt <TDoc, TCursor>(MegaloScriptModel model, IO.TagElementStream <TDoc, TCursor, string> s)
     where TDoc : class
     where TCursor : class
 {
     if (!model.TagElementStreamSerializeFlags.EmbedObjectsWriteSansIds())
     {
         s.StreamAttribute(kIdAttributeName, ref mId);
     }
     else if (s.IsReading)
     {
         Contract.Assert(Id.IsNotNone(),                 // ID should have been set prior to serialize (eg, in the object's Create method in the Model)
                         "Tried to read an embedded object (sans ID) which wasn't given an ID already");
     }
 }
        public bool StreamTactic <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s
                                                 , string xmlName
                                                 , ref int dbid
                                                 , IO.TagElementNodeType xmlSource = XmlUtil.kSourceElement)
            where TDoc : class
            where TCursor : class
        {
            const Phx.DatabaseObjectKind kDbKind = Phx.DatabaseObjectKind.Tactic;

            Contract.Requires(xmlSource.RequiresName() == (xmlName != XML.XmlUtil.kNoXmlName));

            string id_name      = null;
            bool   was_streamed = true;
            bool   to_lower     = false;

            if (s.IsReading)
            {
                was_streamed = s.StreamStringOpt(xmlName, ref id_name, to_lower, xmlSource, intern: true);

                if (was_streamed)
                {
                    id_name = System.IO.Path.GetFileNameWithoutExtension(id_name);

                    dbid = Database.GetId(kDbKind, id_name);
                    Contract.Assert(dbid.IsNotNone(), id_name);

                    if (PhxUtil.IsUndefinedReferenceHandle(dbid))
                    {
                        TraceUndefinedHandle(s, id_name, xmlName, dbid, kDbKind.ToString());
                    }
                }
            }
            else if (s.IsWriting)
            {
                if (dbid.IsNone())
                {
                    was_streamed = false;
                    return(was_streamed);
                }

                id_name = Database.GetName(kDbKind, dbid);
                Contract.Assert(!string.IsNullOrEmpty(id_name));

                id_name += Phx.BTacticData.kFileExt;
                s.StreamStringOpt(xmlName, ref id_name, to_lower, xmlSource, intern: true);
            }

            return(was_streamed);
        }
예제 #20
0
        /// <summary>Streams an integer which is >= -1, but when streamed out the value is added by 1 (so it will be >= 0)</summary>
        /// <param name="s"></param>
        /// <param name="value"></param>
        /// <param name="bitCount"></param>
        public static void StreamNoneable(this IO.BitStream s, ref short value, int bitCount = Bits.kInt16BitCount)
        {
            Contract.Requires(bitCount <= Bits.kInt16BitCount);

            if (s.IsReading)
            {
                s.Read(out value, bitCount);
                value--;
            }
            else if (s.IsWriting)
            {
                Contract.Assert(value >= TypeExtensions.kNone);
                s.Write(value + 1, bitCount);
            }
        }
예제 #21
0
 public void Serialize(IO.EndianStream s)
 {
     s.StreamV(ref Vector);
     BSaveGame.StreamArray16(s, ref SquadList);
     Contract.Assert(SquadList.Length <= kMaxEntitiesPerList);
     BSaveGame.StreamArray16(s, ref UnitList);
     Contract.Assert(SquadList.Length <= kMaxEntitiesPerList);
     s.Stream(EntityFilterSet);
     s.Stream(ref Float);
     s.Stream(ref ObjectType);
     s.Stream(ref LocStringID);
     s.Stream(ref HasVector); s.Stream(ref HasSquadList); s.Stream(ref HasUnitList);
     s.Stream(ref HasEntityFilterSet); s.Stream(ref HasFloat); s.Stream(ref HasObjectType);
     s.Stream(ref HasLocStringID);
 }
예제 #22
0
        //internal uint Handle { get { return mHandle; } }

        static void InitializeHandle(out uint handle, int nameIndex, MegaloScriptValueBaseType baseType, int bitLength,
                                     uint typeParam = 0, uint typeTraits = 0)
        {
            var encoder = new Bitwise.HandleBitEncoder();

            encoder.Encode32((uint)nameIndex, Constants.kNameIndexBitField);
            encoder.Encode32(baseType, BitEncoders.MegaloScriptValueBaseType);
            encoder.Encode32((uint)bitLength, Constants.kBitLengthBitField);
            encoder.Encode32(typeParam, Constants.kTypeParamBitField);
            encoder.Encode32(typeTraits, Constants.kTypeTraitsBitField);

            Contract.Assert(encoder.UsedBitCount == MegaloScriptValueType.BitCount);

            handle = encoder.GetHandle32();
        }
예제 #23
0
        public TEnum BitDecode(ulong bits, int bitIndex)
        {
            Contract.Requires(bitIndex >= 0);
            Contract.Requires(bitIndex < Bits.kInt64BitCount);

            ulong v = Bits.BitDecode(bits, bitIndex, kBitmask);

            if (kHasNone)
            {
                v--;
            }

            Contract.Assert(v <= kMaxValue || (kHasNone && v == ulong.MaxValue));
            return(Reflection.EnumValue <TEnum> .FromUInt64(v));
        }
예제 #24
0
		//internal uint Handle { get { return mHandle; } }

		static void InitializeHandle(out uint handle,
			Engine.EngineBuildHandle buildHandle, int langIndex, int gameIndex)
		{
			uint is_supported = gameIndex.IsNotNone() ? 1U : 0U;

			var encoder = new Bitwise.HandleBitEncoder();
			encoder.EncodeNoneable32(gameIndex, Constants.kGameIndexBitField);
			encoder.Encode32(is_supported, Constants.kIsSupportedBitField);
			LanguageRegistry.BitEncodeLanguageIndex(ref encoder, langIndex);
			encoder.Encode32(buildHandle.Handle, Constants.kBuildBitField);

			Contract.Assert(encoder.UsedBitCount == GameLanguageHandle.BitCount);

			handle = encoder.GetHandle32();
		}
예제 #25
0
        void SerializeItem(IO.EndianStream s, AKBKSubHircSection section)
        {
            Contract.Assert(s.IsReading);

            using (s.EnterVirtualBufferWithBookmark(section.SectionSize))
            {
                var obj = AkSoundBankHierarchyObjectBase.New(section.Type);
                if (obj != null)
                {
                    s.Stream(obj);

                    MapObject(section.Type, obj);
                }
            }
        }
 void SerializeFxParams(IO.EndianStream s)
 {
     s.Pad8();             // IsOverrideParentFX
     s.Stream(ref NumFx);
     Contract.Assert(NumFx <= 4);
     if (s.IsReading)
     {
         FX = new FXData[NumFx];
     }
     if (NumFx > 0)
     {
         s.Pad8();                        // bitsFXBypass
     }
     s.StreamArray(FX);
 }
예제 #27
0
        /// <summary>Streams an integer which is >= -1, but when streamed out the value is added by 1 (so it will be >= 0)</summary>
        /// <param name="s"></param>
        /// <param name="value"></param>
        /// <param name="bitCount"></param>
        public static void StreamNoneable(this IO.BitStream s, ref int value, int bitCount = Bits.kInt32BitCount)
        {
            Contract.Requires(bitCount <= Bits.kInt32BitCount);

            if (s.IsReading)
            {
                s.Read(out value, bitCount);
                value--;
            }
            else if (s.IsWriting)
            {
                Contract.Assert(value.IsNoneOrPositive());
                s.Write(value + 1, bitCount);
            }
        }
예제 #28
0
        public static uint BitDecode(uint bits, ref int bitIndex, uint bitMask)
        {
            Contract.Requires/*<ArgumentOutOfRangeException>*/ (bitIndex >= 0);
            Contract.Requires/*<ArgumentOutOfRangeException>*/ (bitIndex < kInt32BitCount);
            Contract.Requires/*<ArgumentException>*/ (bitMask != 0);

            int bit_count = BitCount(bitMask);

            Contract.Assert((bitIndex + bit_count) <= Bits.kInt32BitCount);

            var value = (bits >> bitIndex) & bitMask;

            bitIndex += bit_count;

            return(value);
        }
예제 #29
0
 public override void Serialize(IO.BitStream s)
 {
     s.StreamObject(Header);                                  // 0x4
     SerializeFlags(s, 2);                                    // 0x33E8
     s.StreamObject(OptionsMisc);                             // 0x2B4
     s.StreamObject(OptionsPrototype);                        // 0x2BC
     s.StreamObject(OptionsRespawning);                       // 0x2C4
     s.StreamObject(OptionsSocial);
     s.StreamObject(OptionsMapOverrides);                     // 0x3D4
     s.StreamObject(OptionsRequisitions);                     // 0x26D8
     s.Stream(ref InfinityMissionId);
     Contract.Assert(InfinityMissionId.IsNone());             // haven't see it equal anything but -1
     s.StreamObject(TeamOptions);                             // 0xD00
     s.StreamObject(LoadoutOptions);                          // 0x2084
     s.StreamObject(OrdnanceOptions);                         // 0x21B4
 }
예제 #30
0
        internal int AddHack <TDoc, TCursor>(IO.TagElementStream <TDoc, TCursor, string> s, string stringName)
            where TDoc : class
            where TCursor : class
        {
            Contract.Requires <InvalidOperationException>(Count < Capacity);
            Contract.Assert(s.IsReading);

            using (s.EnterOwnerBookmark(this))
                using (s.EnterCursorBookmark(stringName))
                {
                    var sref = new LocaleStringTableReference(mEngineLanguageTable);
                    sref.Serialize(s);

                    return(AddImpl(sref));
                }
        }