public async Task <Htx> WriteToFile(string file) { List <byte> data = new List <byte>(); Htx htx = new Htx(); foreach (var command in Commands) { if (command.GetType() == typeof(DialogueCommand)) { ((DialogueCommand)command).DialogueIndex = htx.AddLine(((DialogueCommand)command).Dialogue); } data.AddRange(command.ToByteCode(htx)); } await File.WriteAllBytesAsync(file, data.ToArray()); return(htx); }
private void LoadHtx_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "SCR file|*.scr" }; if (openFileDialog.ShowDialog() == true) { string scrFile = openFileDialog.FileName; string htxFile = scrFile.Replace(".scr", ".htx"); Htx = Htx.ParseFromFile(htxFile); Scr = Scr.ParseFromFile(scrFile, Htx); scrBox.ItemsSource = Scr.Commands; } }
public static Scr ParseFromFile(string file, Htx htx) { var commands = new ObservableCollection <IScrCommand>(); byte[] data = File.ReadAllBytes(file); for (int i = 0; i < data.Length;) { if (data[i] != 0x23) { throw new FileFormatException($"First byte of segment not 0x23 -- {data[i]:X2}"); } else { i++; } switch (data[i]) { case 0x00: commands.Add(new DialogueCommand { Dialogue = htx.Lines[data[i + 1] + (data[i + 2] << 8)] }); i += 3; break; case 0x0F: commands.Add(new WaitCommand { TimeInHundredthsSeconds = (ushort)(data[i + 1] + (data[i + 2] << 8)), }); i += 3; break; case 0x13: string sound = SoundCommand.SoundToByteMap .FirstOrDefault(l => l.Value == data[i + 1] + (data[i + 2] << 8)).Key; if (string.IsNullOrEmpty(sound)) { throw new FileFormatException($"Encountered unknown sound 0x{data[i + 1] + (data[i + 2] << 8):X4}"); } commands.Add(new SoundCommand { Sound = sound }); i += 3; break; case 0x15: commands.Add(new FadeInCommand { Bytes = new byte[] { data[i + 1], data[i + 2], data[i + 3], data[i + 4] } }); i += 5; break; case 0x16: string transition = TransitionCommand.TransitionToByteMap .FirstOrDefault(l => l.Value == data[i + 2]).Key; if (string.IsNullOrEmpty(transition)) { throw new FileFormatException($"Encountered unknown transition 0x{data[i + 2]:X2}"); } commands.Add(new TransitionCommand { UnknownByte = data[i + 1], Transition = transition, Speed = (short)(data[i + 3] + (data[i + 4] << 8)) }); i += 5; break; case 0x1B: string background = BackgroundCommand.FileToByteMap .FirstOrDefault(l => l.Value == data[i + 2]).Key; if (string.IsNullOrEmpty(background)) { throw new FileFormatException($"Encountered unknown background 0x{data[i + 2]:X4}"); } commands.Add(new BackgroundCommand { Background = background, UnknownByte = data[i + 1] }); i += 3; break; case 0x1C: string character = CharacterCommand.NameToByteMap .FirstOrDefault(l => l.Value == data[i + 3] + (data[i + 4] << 8)).Key; if (string.IsNullOrEmpty(character)) { throw new FileFormatException($"Encountered unknown character 0x{data[i + 3] + (data[i + 4] << 8):X4}"); } CharacterCommand.Position position = (CharacterCommand.Position)(data[i + 1] + (data[i + 2] << 8)); if (position > CharacterCommand.Position.RIGHT_FACING_LEFT && position < CharacterCommand.Position.MIDDLE_FACING_LEFT || position > CharacterCommand.Position.RIGHT_FACING_RIGHT) { throw new FileFormatException($"Encountered unknown position 0x{(short)position:X4}"); } commands.Add(new CharacterCommand { CharacterPosition = position, CharacterName = character }); i += 5; break; case 0xFE: // end code commands.Add(new EndCommand()); i += 1; break; default: throw new FileFormatException($"Encountered unknown opcode 0x{data[i]:X2}"); } } return(new Scr { Commands = commands }); }