Example #1
0
        public int[] ReadScriptTable(int offset)
        {
            if (offset == 0)
            {
                return(null);
            }
            int position = offset;
            int value    = FileTools.ByteArrayToInt32(_scriptBuffer, position);

            while (value != 0)
            {
                if (ScriptOpCodeSizes.Case01.Contains(value))
                {
                    position += (3 * sizeof(int));
                }
                else if (ScriptOpCodeSizes.Case02.Contains(value))
                {
                    position += (2 * sizeof(int));
                }
                else if (ScriptOpCodeSizes.Case03.Contains(value))
                {
                    position += (1 * sizeof(int));
                }
                else if (ScriptOpCodeSizes.BitField.Contains(value))
                {
                    position += (2 * sizeof(int));
                }
                else
                {
                    return(null);
                }

                value = FileTools.ByteArrayToInt32(_scriptBuffer, position);
            }

            int length = (position + sizeof(int) - offset) / sizeof(int);

            return(FileTools.ByteArrayToInt32Array(_scriptBuffer, ref offset, length));
        }
Example #2
0
        /// <summary>
        /// Parses a level rules file bytes.
        /// </summary>
        /// <param name="fileBytes">The bytes of the level rules to parse.</param>
        public override void ParseFileBytes(byte[] fileBytes)
        {
            // sanity check
            if (fileBytes == null)
            {
                throw new ArgumentNullException("fileBytes", "File bytes cannot be null!");
            }
            RoomDefinition = new RoomDefinitionStruct();

            // file header checks
            int    offset        = 0;
            UInt32 fileMagicWord = FileTools.ByteArrayToUInt32(fileBytes, ref offset);

            if (fileMagicWord != FileMagicWord)
            {
                throw new Exceptions.UnexpectedMagicWordException();
            }

            UInt32 fileVersion = FileTools.ByteArrayToUInt32(fileBytes, ref offset);

            if (fileVersion != RequiredVersion)
            {
                throw new Exceptions.NotSupportedFileVersionException();
            }

            // main file header
            RoomDefinition.FileHeader = FileTools.ByteArrayToStructure <RoomDefinitionHeader>(fileBytes, ref offset);
            RoomDefinitionHeader header = RoomDefinition.FileHeader;

            // read UnknownStruct1 array
            offset = (int)header.Offset110;
            int count10C = header.Count10C;

            if (offset > 0 && count10C > 0)
            {
                RoomDefinition.UnknownStruct1Array = _ReadUnknownStruct1Array(fileBytes, ref offset, count10C); // FileTools.ByteArrayToArray<UnknownStruct1>(fileBytes, ref offset, count10C);
            }

            // read UnknownStruct2 array
            offset = (int)header.Offset120;
            int count118 = header.Count118;

            if (offset > 0 && count118 > 0)
            {
                RoomDefinition.UnknownStruct2Array = _ReadUnknownStruct2Array(fileBytes, ref offset, count118); // FileTools.ByteArrayToArray<UnknownStruct2>(fileBytes, ref offset, count118);
            }

            // read UnknownStruct3 arrays
            offset = (int)header.Offset288;
            int count29C = header.Count29C;
            int count298 = header.Count298;

            if (offset > 0 && count29C > 0 && count298 > 0)
            {
                RoomDefinition.UnknownStruct3Arrays = new UnknownStruct3[count29C][];
                for (int i = 0; i < count29C; i++)
                {
                    RoomDefinition.UnknownStruct3Arrays[i] = _ReadUnknownStruct3Array(fileBytes, ref offset, count298); // FileTools.ByteArrayToArray<UnknownStruct3>(fileBytes, ref offset, count298);
                }
            }
            // read UnknownStruct3 Int32 array
            offset = (int)header.Offset2A0;
            int count2A8 = header.Count2A8;

            if (offset > 0 && count2A8 > 0)
            {
                RoomDefinition.UnknownStruct3Int32Array = FileTools.ByteArrayToInt32Array(fileBytes, ref offset, count2A8);
            }

            // read room vertices array
            offset = (int)header.VerticesOffset;
            int vertexCount = header.VertexCount;

            if (offset > 0 && vertexCount > 0)
            {
                RoomDefinition.Vertices = _ReadVector3Array(fileBytes, ref offset, vertexCount); // FileTools.ByteArrayToArray<Vector3>(fileBytes, ref offset, vertexCount);
            }

            // read UnknownStruct5 array
            offset = (int)header.Offset278;
            int count280 = header.Count280;

            if (offset > 0 && count280 > 0)
            {
                RoomDefinition.UnknownStruct5Array = FileTools.ByteArrayToArray <UnknownStruct5>(fileBytes, ref offset, count280);
            }

            // read UnknownStruct6 array
            offset = (int)header.Offset130;
            int count12C = header.Count12C;

            if (offset > 0 && count12C > 0)
            {
                RoomDefinition.UnknownStruct6Array = FileTools.ByteArrayToArray <UnknownStruct6>(fileBytes, ref offset, count12C);
            }

            // read UnknownStruct7 array (not seen read like this - but it works) - has same structure (3xfloat) as UnknownStruct4
            offset = (int)header.Offset178;
            int count174 = header.Count174;

            if (offset > 0 && count174 > 0)
            {
                RoomDefinition.UnknownStruct7Array = _ReadUnknownStruct7Vector3Array(fileBytes, ref offset, count174); // FileTools.ByteArrayToArray<Vector3>(fileBytes, ref offset, count174);
            }

            // read UnknownStruct8 array (not seen read like this - but it works)
            offset = (int)header.Offset188;
            int count184 = header.Count184;

            if (offset > 0 && count184 > 0)
            {
                RoomDefinition.UnknownStruct8Array = _ReadUnknownStruct7Array(fileBytes, ref offset, count184); // FileTools.ByteArrayToArray<UnknownStruct7>(fileBytes, ref offset, count184);
            }

            // read UnknownStruct9 array
            offset = (int)header.Offset168;
            int countUnknown7 = header.Count160;

            if (offset > 0 && countUnknown7 > 0)
            {
                RoomDefinition.UnknownStructFooter = FileTools.ByteArrayToArray <UnknownStruct5>(fileBytes, ref offset, countUnknown7);
            }

            // final debug check
            Debug.Assert(offset == fileBytes.Length);
        }
Example #3
0
        private static bool _ParsePropertiesScript(byte[] data, ref int offset, ref ExcelFunction excelScript)
        {
            ExcelFunction.Parameter parameter = new ExcelFunction.Parameter();

            // token and version checks
            if (!_CheckToken(data, ref offset, Token.mysh))
            {
                return(false);
            }
            UInt32 version = FileTools.ByteArrayToUInt32(data, ref offset);

            if (version != Token.MyshVersion)
            {
                return(false);
            }


            // general parameter values
            int charCount = FileTools.ByteArrayToInt32(data, ref offset);

            if (charCount >= 0x1000)
            {
                return(false);
            }
            parameter.Name    = FileTools.ByteArrayToStringASCII(data, ref offset, charCount);
            parameter.Unknown = FileTools.ByteArrayToUInt32(data, ref offset);


            // what kind of parameter is it
            parameter.TypeId = FileTools.ByteArrayToUInt32(data, ref offset);
            int paramLength;

            switch (parameter.TypeId)
            {
            case 0x38:     // 56 // "nPowerChange" from TCv4 Skills
                paramLength = 4;
                break;

            case 0x39:     // 57 // oldstats, x, sel, dmgtype, ?
                paramLength = 4;
                break;

            case 0x3C:     // 60 // dam, ?
                paramLength = 5;
                break;

            case 0x41:     // 65 // dmg_elec, dmg_fire, etc
                paramLength = 8;
                break;

            default:
                Debug.Assert(false, "Unknown MYSH TypeId = " + parameter.TypeId);
                return(false);
            }
            parameter.TypeValues = FileTools.ByteArrayToInt32Array(data, ref offset, paramLength);

            excelScript.Parameters.Add(parameter);
            if (parameter.TypeId != 0x41)
            {
                return(true);                          // only 0x41 has paramaters and a script values block following it
            }
            // get remaining parameters
            int paramCount = parameter.TypeValues[5];

            for (int i = 0; i < paramCount; i++)
            {
                if (!_ParsePropertiesScript(data, ref offset, ref excelScript))
                {
                    return(false);
                }
            }


            // the actual script values
            int valuesByteCount = FileTools.ByteArrayToInt32(data, ref offset);

            if (valuesByteCount <= 0)
            {
                return(false);
            }
            excelScript.ScriptByteCode = new byte[valuesByteCount];
            Buffer.BlockCopy(data, offset, excelScript.ScriptByteCode, 0, valuesByteCount);
            offset += valuesByteCount;

            return(true);
        }