예제 #1
0
        private bool ReadScripts()
        {
            LevelScriptCatalog.LoadCatalog();

            _languageFiles  = new List <string>();
            _levels         = new List <LevelScript>();
            _strings        = new List <LanguageScript>();
            _levelFileNames = new List <string>();

            try
            {
                using (var stream = File.OpenRead(_srcPath + "\\Script.txt"))
                    using (var reader = new StreamReader(stream))
                    {
                        var         lastBlock = Enumerations.None;
                        LevelScript lastLevel = null;

                        while (reader.EndOfStream == false)
                        {
                            string line = reader.ReadLine().Trim();

                            // Comment or empty line?
                            if (line.StartsWith(";") || line == "")
                            {
                                continue;
                            }

                            // Inline comment?
                            var tokensLine = line.Split(';');
                            line = tokensLine[0];

                            // Block header?
                            if (line.StartsWith("["))
                            {
                                if (line == "[PSXExtensions]")
                                {
                                    lastBlock = Enumerations.PsxExtensions;
                                    continue;
                                }

                                if (line == "[PCExtensions]")
                                {
                                    lastBlock = Enumerations.PcExtensions;
                                    continue;
                                }

                                if (line == "[Language]")
                                {
                                    lastBlock = Enumerations.Language;
                                    continue;
                                }

                                if (line == "[Options]")
                                {
                                    lastBlock = Enumerations.Options;
                                    continue;
                                }

                                if (line == "[Title]")
                                {
                                    lastBlock = Enumerations.Title;
                                    lastLevel = new LevelScript(true);
                                    _levels.Add(lastLevel);
                                    continue;
                                }

                                if (line == "[Level]")
                                {
                                    lastBlock = Enumerations.Level;
                                    lastLevel = new LevelScript(false);
                                    _levels.Add(lastLevel);
                                    continue;
                                }
                            }

                            // Block content?
                            var tokens = line.Split('=');
                            if (tokens.Length < 2)
                            {
                                continue;
                            }

                            string command = tokens[0].Trim();
                            string value   = tokens[1].Trim();

                            if (lastBlock == Enumerations.PsxExtensions)
                            {
                                if (command == "Level")
                                {
                                    _psxLevel = value;
                                }
                                else if (command == "Cut")
                                {
                                    _psxCut = value;
                                }
                                else if (command == "FMV")
                                {
                                    _psxFmv = value;
                                }
                            }

                            if (lastBlock == Enumerations.PcExtensions)
                            {
                                if (command == "Level")
                                {
                                    _pcLevel = value;
                                }
                                else if (command == "Cut")
                                {
                                    _pcCut = value;
                                }
                                else if (command == "FMV")
                                {
                                    _pcFmv = value;
                                }
                            }

                            if (lastBlock == Enumerations.Language)
                            {
                                if (command == "File")
                                {
                                    var tokensFile = value.Split(',');
                                    if (tokensFile.Length < 2 || !File.Exists(_srcPath + "\\" + tokensFile[1].Trim()))
                                    {
                                        continue;
                                    }
                                    _languageFiles.Add(tokensFile[1].Trim());
                                }
                            }

                            if (lastBlock == Enumerations.Options)
                            {
                                if (command == "LoadSave")
                                {
                                    _loadSave = (value == "ENABLED");
                                }
                                else if (command == "Title")
                                {
                                    _title = (value == "ENABLED");
                                }
                                else if (command == "PlayAnyLevel")
                                {
                                    _playAnyLevel = (value == "ENABLED");
                                }
                                else if (command == "FlyCheat")
                                {
                                    _flyCheat = (value == "ENABLED");
                                }
                                else if (command == "DemoDisc")
                                {
                                    _demoDisc = (value == "ENABLED");
                                }
                                else if (command == "InputTimeout")
                                {
                                    _inputTimeout = int.Parse(value);
                                }
                                else if (command == "Security")
                                {
                                    _security = Convert.ToByte(value.Replace("$", ""), 16);
                                }
                            }

                            if (lastBlock == Enumerations.Title || lastBlock == Enumerations.Level)
                            {
                                if (LevelScriptCatalog.Commands.ContainsKey(command))
                                {
                                    var cmd          = LevelScriptCatalog.Commands[command];
                                    var tokensParams = value.Split(',');
                                    if (tokensParams.Length != cmd.Parameters.Count)
                                    {
                                        continue;
                                    }

                                    var entry = new LevelScriptEntry(cmd);

                                    for (int j = 0; j < tokensParams.Length; j++)
                                    {
                                        string paramValue = tokensParams[j].Trim();

                                        switch (cmd.Parameters[j])
                                        {
                                        case LevelScriptCatalogParameterType.Boolean:
                                            entry.Parameters.Add(paramValue == "ENABLED");
                                            break;

                                        case LevelScriptCatalogParameterType.Hex:
                                            short val     = 0;
                                            bool  success = short.TryParse(paramValue.Replace("$", ""), NumberStyles.HexNumber, null, out val);
                                            entry.Parameters.Add(val);
                                            break;

                                        case LevelScriptCatalogParameterType.Int8:
                                            entry.Parameters.Add(byte.Parse(paramValue));
                                            break;

                                        case LevelScriptCatalogParameterType.SInt8:
                                            entry.Parameters.Add(sbyte.Parse(paramValue));
                                            break;

                                        case LevelScriptCatalogParameterType.Int16:
                                            entry.Parameters.Add(short.Parse(paramValue));
                                            break;

                                        case LevelScriptCatalogParameterType.Int32:
                                            entry.Parameters.Add(int.Parse(paramValue));
                                            break;

                                        case LevelScriptCatalogParameterType.String:
                                            entry.Parameters.Add(paramValue);
                                            break;
                                        }
                                    }

                                    if (entry.Command.Name == "Name")
                                    {
                                        lastLevel.Name = (string)entry.Parameters[0];
                                    }
                                    else if (entry.Command.Name == "Level")
                                    {
                                        lastLevel.FileName   = (string)entry.Parameters[0];
                                        lastLevel.AudioTrack = (byte)entry.Parameters[1];
                                    }

                                    lastLevel.Entries.Add(entry);
                                }
                            }
                        }
                    }

                foreach (var languageFile in _languageFiles)
                {
                    string languageFilePath = _srcPath + "\\" + languageFile;
                    if (!File.Exists(languageFilePath))
                    {
                        continue;
                    }
                    using (var reader = new StreamReader(File.OpenRead(languageFilePath)))
                    {
                        var    strings   = new LanguageScript();
                        string lastBlock = "";
                        while (reader.EndOfStream == false)
                        {
                            var line = reader.ReadLine().Trim();

                            if (line.StartsWith(";") || line == "")
                            {
                                continue;
                            }

                            if (line.StartsWith("["))
                            {
                                lastBlock = line;
                                continue;
                            }

                            if (lastBlock == "[Strings]")
                            {
                                strings.GeneralStrings.Add(line);
                            }
                            else if (lastBlock == "[PSXStrings]")
                            {
                                strings.PsxStrings.Add(line);
                            }
                            else if (lastBlock == "[PCStrings]")
                            {
                                strings.PcStrings.Add(line);
                            }
                        }
                        _strings.Add(strings);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #2
0
        private byte[] BuildRawScriptData(LevelScript level, TRVersion.Game version)
        {
            using (var ms = new MemoryStream())
            {
                using (var writer = new BinaryWriter(ms))
                {
                    short flags = 0;

                    foreach (var entry in level.Entries)
                    {
                        if (entry.Command.Name == "Puzzle")
                        {
                            writer.Write((byte)(0x9F + (byte)entry.Parameters[0] - 1));
                            writer.Write((short)GetStringIndex((string)entry.Parameters[1]));
                            for (int i = 2; i < 8; i++)
                            {
                                writer.Write((short)entry.Parameters[i]);
                            }
                        }
                        else if (entry.Command.Name == "Key")
                        {
                            writer.Write((byte)(0x93 + (byte)entry.Parameters[0] - 1));
                            writer.Write((short)GetStringIndex((string)entry.Parameters[1]));
                            for (int i = 2; i < 8; i++)
                            {
                                writer.Write((short)entry.Parameters[i]);
                            }
                        }
                        else if (entry.Command.Name == "Pickup")
                        {
                            writer.Write((byte)(0xAB + (byte)entry.Parameters[0] - 1));
                            writer.Write((short)GetStringIndex((string)entry.Parameters[1]));
                            for (int i = 2; i < 8; i++)
                            {
                                writer.Write((short)entry.Parameters[i]);
                            }
                        }
                        else if (entry.Command.Name == "Examine")
                        {
                            writer.Write((byte)(0xAF + (byte)entry.Parameters[0]) - 1);
                            writer.Write((short)GetStringIndex((string)entry.Parameters[1]));
                            for (int i = 2; i < 8; i++)
                            {
                                writer.Write((short)entry.Parameters[i]);
                            }
                        }
                        if (entry.Command.Name == "PuzzleCombo")
                        {
                            writer.Write((byte)(0xC2 + (byte)(2 * (byte)entry.Parameters[0] - 2) + (byte)((byte)entry.Parameters[1] - 1)));
                            writer.Write((short)GetStringIndex((string)entry.Parameters[2]));
                            for (int i = 3; i < 9; i++)
                            {
                                writer.Write((short)entry.Parameters[i]);
                            }
                        }
                        else if (entry.Command.Name == "KeyCombo")
                        {
                            writer.Write((byte)(0xB2 + (byte)(2 * (byte)entry.Parameters[0] - 2) + (byte)((byte)entry.Parameters[1] - 1)));
                            writer.Write((short)GetStringIndex((string)entry.Parameters[2]));
                            for (int i = 3; i < 9; i++)
                            {
                                writer.Write((short)entry.Parameters[i]);
                            }
                        }
                        else if (entry.Command.Name == "PickupCombo")
                        {
                            writer.Write((byte)(0xD2 + (byte)(2 * (byte)entry.Parameters[0] - 2) + (byte)((byte)entry.Parameters[1] - 1)));
                            writer.Write((short)GetStringIndex((string)entry.Parameters[2]));
                            for (int i = 3; i < 9; i++)
                            {
                                writer.Write((short)entry.Parameters[i]);
                            }
                        }
                        else if (entry.Command.Name == "FMV")
                        {
                            writer.Write((byte)0x80);
                            writer.Write((byte)entry.Parameters[0]);
                        }
                        else if (entry.Command.Name == "UVrotate")
                        {
                            writer.Write((byte)0x8B);
                            writer.Write((byte)entry.Parameters[0]);
                        }
                        else if (entry.Command.Name == "Cut")
                        {
                            writer.Write((byte)0x84);
                            writer.Write((byte)entry.Parameters[0]);
                        }
                        else if (version == TRVersion.Game.TR5 && entry.Command.Name == "GiveItem")
                        {
                            writer.Write((byte)0xD9);
                            writer.Write((byte)entry.Parameters[0]);
                        }
                        else if (version == TRVersion.Game.TR5 && entry.Command.Name == "LoseItem")
                        {
                            writer.Write((byte)0xDA);
                            writer.Write((byte)entry.Parameters[0]);
                        }
                        else if (entry.Command.Name == "Legend")
                        {
                            writer.Write((byte)0x8C);
                            writer.Write((byte)GetStringIndex((string)entry.Parameters[0]));
                        }
                        else if (entry.Command.Name == "Layer1")
                        {
                            writer.Write((byte)0x89);
                            writer.Write((byte)entry.Parameters[0]);
                            writer.Write((byte)entry.Parameters[1]);
                            writer.Write((byte)entry.Parameters[2]);
                            writer.Write((sbyte)entry.Parameters[3]);
                            flags |= 0x08;
                        }
                        else if (entry.Command.Name == "Layer2")
                        {
                            writer.Write((byte)0x8A);
                            writer.Write((byte)entry.Parameters[0]);
                            writer.Write((byte)entry.Parameters[1]);
                            writer.Write((byte)entry.Parameters[2]);
                            writer.Write((sbyte)entry.Parameters[3]);
                            flags |= 0x10;
                        }
                        else if (entry.Command.Name == "ResidentCut")
                        {
                            writer.Write((byte)0x85 + (byte)((byte)entry.Parameters[0] - 1));
                            writer.Write((byte)entry.Parameters[1]);
                        }
                        else if (entry.Command.Name == "Fog")
                        {
                            writer.Write((byte)0x8F);
                            writer.Write((byte)entry.Parameters[0]);
                            writer.Write((byte)entry.Parameters[1]);
                            writer.Write((byte)entry.Parameters[2]);
                        }
                        else if (entry.Command.Name == "Horizon")
                        {
                            flags |= 0x04;
                        }
                        else if (entry.Command.Name == "ColAddHorizon")
                        {
                            flags |= 0x200;
                        }
                        else if (entry.Command.Name == "Lightning")
                        {
                            flags |= 0x40;
                        }
                        else if (entry.Command.Name == "Pulse")
                        {
                            flags |= 0x100;
                        }
                        else if (entry.Command.Name == "StarField")
                        {
                            flags |= 0x20;
                        }
                        else if (entry.Command.Name == "Weather")
                        {
                            flags |= 0x02;
                        }
                        else if (entry.Command.Name == "Timer")
                        {
                            flags |= 0x1000;
                        }
                        else if (entry.Command.Name == "Train")
                        {
                            flags |= 0x80;
                        }
                        else if (entry.Command.Name == "YoungLara")
                        {
                            flags |= 0x01;
                        }
                        else if (entry.Command.Name == "RemoveAmulet")
                        {
                            flags |= 0x4000;
                        }

                        /*else if (entry.Command.Name == "NoLevel")
                         *  flags |= (short)0x8000;*/
                        else if (entry.Command.Name == "LensFlare")
                        {
                            writer.Write((byte)0x8D);
                            writer.Write((byte)((int)entry.Parameters[0] / 256));
                            writer.Write((byte)((int)entry.Parameters[1] / 256));
                            writer.Write((byte)((int)entry.Parameters[2] / 256));
                            writer.Write((byte)entry.Parameters[3]);
                            writer.Write((byte)entry.Parameters[4]);
                            writer.Write((byte)entry.Parameters[5]);
                            flags |= 0x800;
                        }
                        else if (entry.Command.Name == "Mirror")
                        {
                            writer.Write((byte)0x8E);
                            writer.Write((byte)entry.Parameters[0]);
                            writer.Write((int)((short)entry.Parameters[1]));
                        }
                        else if (entry.Command.Name == "LoadCamera")
                        {
                            writer.Write((byte)0x91);
                            writer.Write((int)entry.Parameters[0]);
                            writer.Write((int)entry.Parameters[1]);
                            writer.Write((int)entry.Parameters[2]);
                            writer.Write((int)entry.Parameters[3]);
                            writer.Write((int)entry.Parameters[4]);
                            writer.Write((int)entry.Parameters[5]);
                            writer.Write((byte)entry.Parameters[6]);
                        }
                        else if (entry.Command.Name == "ResetHUB")
                        {
                            writer.Write((byte)0x92);
                            writer.Write((byte)entry.Parameters[0]);
                        }
                    }

                    if (level.IsTitle)
                    {
                        writer.Write((byte)0x82);
                        writer.Write((short)flags);
                        writer.Write((byte)0);
                        writer.Write((byte)level.AudioTrack);
                    }
                    else
                    {
                        writer.Write((byte)0x81);
                        writer.Write((byte)GetStringIndex(level.Name));
                        writer.Write((short)flags);
                        writer.Write((byte)GetFileNameIndex(level.FileName));
                        writer.Write((byte)level.AudioTrack);
                    }

                    // End of level script
                    writer.Write((byte)0x83);
                }

                return(ms.ToArray());
            }
        }
예제 #3
0
        private bool ReadScripts()
        {
            LevelScriptCatalog.LoadCatalog();

            _languageStrings = new List <LanguageStrings>();
            _levels          = new List <LevelScript>();
            _levelFileNames  = new List <string>();

            try
            {
                using (var stream = File.OpenRead(_srcPath + "\\Script.txt"))
                    using (var reader = new StreamReader(stream))
                    {
                        var         lastBlock = Enumerations.None;
                        LevelScript lastLevel = null;

                        while (reader.EndOfStream == false)
                        {
                            string line = reader.ReadLine().Trim();

                            // Comment or empty line?
                            if (line.StartsWith(";") || line == "")
                            {
                                continue;
                            }

                            // Inline comment?
                            var tokensLine = line.Split(';');
                            line = tokensLine[0];

                            // Block header?
                            if (line.StartsWith("["))
                            {
                                if (line == "[Language]")
                                {
                                    lastBlock = Enumerations.Language;
                                    continue;
                                }

                                if (line == "[Options]")
                                {
                                    lastBlock = Enumerations.Options;
                                    continue;
                                }

                                if (line == "[Title]")
                                {
                                    lastBlock = Enumerations.Title;
                                    lastLevel = new LevelScript(true);
                                    _levels.Add(lastLevel);
                                    continue;
                                }

                                if (line == "[Level]")
                                {
                                    lastBlock = Enumerations.Level;
                                    lastLevel = new LevelScript(false);
                                    _levels.Add(lastLevel);
                                    continue;
                                }
                            }

                            // Block content?
                            var tokens = line.Split('=');
                            if (tokens.Length < 2)
                            {
                                continue;
                            }

                            string command = tokens[0].Trim();
                            string value   = tokens[1].Trim();

                            if (lastBlock == Enumerations.Language)
                            {
                                if (command == "File")
                                {
                                    var tokensFile = value.Split(',');
                                    if (tokensFile.Length < 2)
                                    {
                                        continue;
                                    }
                                    _languageStrings.Add(new LanguageStrings(tokensFile[2], tokensFile[1]));
                                }
                            }

                            if (lastBlock == Enumerations.Options)
                            {
                                if (command == "LoadSave")
                                {
                                    _loadSave = (value == "ENABLED");
                                }
                                else if (command == "Title")
                                {
                                    _title = (value == "ENABLED");
                                }
                                else if (command == "PlayAnyLevel")
                                {
                                    _playAnyLevel = (value == "ENABLED");
                                }
                                else if (command == "FlyCheat")
                                {
                                    _flyCheat = (value == "ENABLED");
                                }
                                else if (command == "Diagnostics")
                                {
                                    _diagnostics = (value == "ENABLED");
                                }
                                else if (command == "LevelFarView")
                                {
                                    _levelFarView = int.Parse(value);
                                }
                                else if (command == "Intro")
                                {
                                    _intro = value;
                                }
                            }

                            if (lastBlock == Enumerations.Title || lastBlock == Enumerations.Level)
                            {
                                if (LevelScriptCatalog.Commands.ContainsKey(command))
                                {
                                    var cmd          = LevelScriptCatalog.Commands[command];
                                    var tokensParams = value.Split(',');
                                    if (tokensParams.Length != cmd.Parameters.Count)
                                    {
                                        continue;
                                    }

                                    var entry = new LevelScriptEntry(cmd);

                                    for (int j = 0; j < tokensParams.Length; j++)
                                    {
                                        string paramValue = tokensParams[j].Trim();

                                        switch (cmd.Parameters[j])
                                        {
                                        case LevelScriptCatalogParameterType.Boolean:
                                            entry.Parameters.Add(paramValue == "ENABLED");
                                            break;

                                        case LevelScriptCatalogParameterType.Hex:
                                            short val     = 0;
                                            bool  success = short.TryParse(paramValue.Replace("$", ""), NumberStyles.HexNumber, null, out val);
                                            entry.Parameters.Add(val);
                                            break;

                                        case LevelScriptCatalogParameterType.Int8:
                                            entry.Parameters.Add(byte.Parse(paramValue));
                                            break;

                                        case LevelScriptCatalogParameterType.SInt8:
                                            entry.Parameters.Add(sbyte.Parse(paramValue));
                                            break;

                                        case LevelScriptCatalogParameterType.Int16:
                                            entry.Parameters.Add(short.Parse(paramValue));
                                            break;

                                        case LevelScriptCatalogParameterType.Int32:
                                            entry.Parameters.Add(int.Parse(paramValue));
                                            break;

                                        case LevelScriptCatalogParameterType.String:
                                            entry.Parameters.Add(paramValue);
                                            break;
                                        }
                                    }

                                    if (entry.Command.Name == "Name")
                                    {
                                        lastLevel.Name = (string)entry.Parameters[0];
                                    }
                                    if (entry.Command.Name == "Background")
                                    {
                                        lastLevel.Background = (string)entry.Parameters[0];
                                    }
                                    else if (entry.Command.Name == "AmbientTrack")
                                    {
                                        lastLevel.AudioTrack = byte.Parse(entry.Parameters[0].ToString());
                                    }
                                    else if (entry.Command.Name == "LaraType")
                                    {
                                        lastLevel.LaraType = GetLaraType((string)entry.Parameters[0]);
                                    }
                                    else if (entry.Command.Name == "Horizon")
                                    {
                                        lastLevel.Horizon = (entry.Parameters[0].ToString() == "True");
                                    }
                                    else if (entry.Command.Name == "ResetInventory")
                                    {
                                        lastLevel.ResetInventory = (entry.Parameters[0].ToString() == "True");
                                    }
                                    else if (entry.Command.Name == "Sky")
                                    {
                                        lastLevel.Sky = (entry.Parameters[0].ToString() == "True");
                                    }
                                    else if (entry.Command.Name == "ColAddHorizon")
                                    {
                                        lastLevel.ColAddHorizon = (entry.Parameters[0].ToString() == "True");
                                    }
                                    else if (entry.Command.Name == "Lightning")
                                    {
                                        lastLevel.Lightning = (entry.Parameters[0].ToString() == "True");
                                    }
                                    else if (entry.Command.Name == "UVRotate")
                                    {
                                        lastLevel.UVRotate = (byte)entry.Parameters[0];
                                    }
                                    else if (entry.Command.Name == "LevelFarView")
                                    {
                                        lastLevel.LevelFarView = (int)entry.Parameters[0];
                                    }
                                    else if (entry.Command.Name == "LoadScreen")
                                    {
                                        lastLevel.LoadScreen = (string)entry.Parameters[0];
                                    }
                                    else if (entry.Command.Name == "LevelFile")
                                    {
                                        lastLevel.FileName = (string)entry.Parameters[0];
                                    }
                                    else if (entry.Command.Name == "Rumble")
                                    {
                                        lastLevel.Rumble = (entry.Parameters[0].ToString() == "True");
                                    }
                                    else if (entry.Command.Name == "UnlimitedAir")
                                    {
                                        lastLevel.UnlimitedAir = (entry.Parameters[0].ToString() == "True");
                                    }
                                    else if (entry.Command.Name == "Weather")
                                    {
                                        if (entry.Parameters[0].ToString() == "RAIN")
                                        {
                                            lastLevel.Weather = Weather.Rain;
                                        }
                                        else if (entry.Parameters[0].ToString() == "SNOW")
                                        {
                                            lastLevel.Weather = Weather.Snow;
                                        }
                                    }
                                    else if (entry.Command.Name == "Level")
                                    {
                                        lastLevel.FileName   = (string)entry.Parameters[0];
                                        lastLevel.AudioTrack = (byte)entry.Parameters[1];
                                    }

                                    lastLevel.Entries.Add(entry);
                                }
                            }
                        }
                    }

                for (int i = 0; i < _languageStrings.Count; i++)
                {
                    string languageFilePath = _srcPath + "\\" + _languageStrings[i].FileName;
                    if (!File.Exists(languageFilePath))
                    {
                        continue;
                    }

                    using (var reader = new StreamReader(File.OpenRead(languageFilePath)))
                    {
                        var    strings   = new LanguageScript();
                        string lastBlock = "";
                        while (reader.EndOfStream == false)
                        {
                            var line = reader.ReadLine().Trim();

                            if (line.StartsWith(";") || line == "")
                            {
                                continue;
                            }

                            if (line.StartsWith("["))
                            {
                                lastBlock = line;
                                continue;
                            }

                            _languageStrings[i].Strings.Add(line);
                        }
                    }
                }

                _tracks = new List <string>();
                using (var reader = new StreamReader(File.OpenRead(_srcPath + "\\" + "Tracks.txt")))
                {
                    while (reader.EndOfStream == false)
                    {
                        _tracks.Add(reader.ReadLine());
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }