public static SavegameFile Read( string path, IReadSavegameProgress progress = null) { if (progress != null) { progress.Report(true, true, 0, 0); } using (var compressedInputStream = File.OpenRead(path)) using (var inputStream = ChunkedLz4File.Decompress(compressedInputStream)) using (var reader = new BinaryReader(inputStream, Encoding.ASCII, true)) { var savegameFile = new SavegameFile(); savegameFile.ReadHeader(reader); savegameFile.ReadFooter(reader); savegameFile.ReadStringTable(reader); savegameFile.ReadVariableTable(reader); if (progress != null) { progress.Report(true, false, 0, savegameFile.VariableTableEntries.Length); } savegameFile.ReadVariables(reader, progress); savegameFile.ReferenceVariable(reader, progress); if (progress != null) { progress.Report(false, false, 0, 0); } return(savegameFile); } }
private void ReferenceVariable(BinaryReader reader, IReadSavegameProgress progress) { List <Variable> referencedVariables = new List <Variable>(); for (int i = 0; i < Variables.Length; i++) { Variable currentVariable = Variables[i]; VariableSet currentVariableSet = currentVariable as VariableSet; if (currentVariableSet != null && currentVariable.Size > currentVariable.TokenSize) { int size = currentVariable.Size - currentVariableSet.TokenSize; List <Variable> childrenVariables = new List <Variable>(); while (size > 0) { Variable nextVariable = Variables[++i]; childrenVariables.Add(nextVariable); size -= nextVariable.TokenSize; } currentVariableSet.Variables = childrenVariables.ToArray(); } referencedVariables.Add(currentVariable); } Variables = referencedVariables.ToArray(); }
public static Task <SavegameFile> ReadAsync( string path, IReadSavegameProgress progress = null) { return(Task.Run(() => Read(path, progress))); }
private void ReadVariables(BinaryReader reader, IReadSavegameProgress progress) { var parser = new VariableParser(VariableNames); // TODO: Use while loops in each of the recursive parsers var parsers = new List <VariableParserBase> { new ManuVariableParser(), new OpVariableParser(), new VlVariableParser(), new AvalVariableParser(), new PorpVariableParser(), new SxapVariableParser(), new SsVariableParser(parser), new BsVariableParser(parser), new BlckVariableParser(parser) }; parser.RegisterParsers(parsers); Variable[] variables = new Variable[VariableTableEntries.Length]; for (int i = 0; i < VariableTableEntries.Length; i++) { // Calculate the size of the next token var size = VariableTableEntries[i].Size; int tokenSize; // There is a hidden variable before the last one if (i < VariableTableEntries.Length - 2) { tokenSize = VariableTableEntries[i + 1].Offset - VariableTableEntries[i].Offset; } else { tokenSize = VariableTableEntries[i].Size; } reader.BaseStream.Position = VariableTableEntries[i].Offset; try { // Tokenizing var readTokenSize = tokenSize; var variable = parser.Parse(reader, ref readTokenSize); variable.Size = size; variable.TokenSize = tokenSize; variables[i] = variable; } catch (ParseVariableException e) { Debug.WriteLine(e.Message); } catch (Exception ex) { Debug.WriteLine(ex); } if (i % 250 == 0 && progress != null) { progress.Report(true, false, i, VariableTableEntries.Length); } } // Parsing var valueParser = new VariableValueParser(); var stack = new Stack <Variable>(variables.Reverse()); var root = valueParser.Parse <SavegameRoot>(stack); Variables = variables; }
private void ReferenceVariable(BinaryReader reader, IReadSavegameProgress progress) { List<Variable> referencedVariables = new List<Variable>(); for (int i = 0; i < Variables.Length; i++) { Variable currentVariable = Variables[i]; VariableSet currentVariableSet = currentVariable as VariableSet; if (currentVariableSet!= null && currentVariable.Size > currentVariable.TokenSize) { int size = currentVariable.Size - currentVariableSet.TokenSize; List<Variable> childrenVariables = new List<Variable>(); while (size > 0) { Variable nextVariable = Variables[++i]; childrenVariables.Add(nextVariable); size -= nextVariable.TokenSize; } currentVariableSet.Variables = childrenVariables.ToArray(); } referencedVariables.Add(currentVariable); } Variables = referencedVariables.ToArray(); }
private void ReadVariables(BinaryReader reader, IReadSavegameProgress progress) { var parser = new VariableParser(VariableNames); // TODO: Use while loops in each of the recursive parsers var parsers = new List<VariableParserBase> { new ManuVariableParser(), new OpVariableParser(), new VlVariableParser(), new AvalVariableParser(), new PorpVariableParser(), new SxapVariableParser(), new SsVariableParser(parser), new BsVariableParser(parser), new BlckVariableParser(parser) }; parser.RegisterParsers(parsers); Variable[] variables = new Variable[VariableTableEntries.Length]; for (int i = 0; i < VariableTableEntries.Length; i++) { // Calculate the size of the next token var size = VariableTableEntries[i].Size; int tokenSize; // There is a hidden variable before the last one if (i < VariableTableEntries.Length - 2) { tokenSize = VariableTableEntries[i + 1].Offset - VariableTableEntries[i].Offset; } else { tokenSize = VariableTableEntries[i].Size; } reader.BaseStream.Position = VariableTableEntries[i].Offset; try { // Tokenizing var readTokenSize = tokenSize; var variable = parser.Parse(reader, ref readTokenSize); variable.Size = size; variable.TokenSize = tokenSize; variables[i] = variable; } catch (ParseVariableException e) { Debug.WriteLine(e.Message); } catch (Exception ex) { Debug.WriteLine(ex); } if (i % 250 == 0 && progress != null) progress.Report(true, false, i, VariableTableEntries.Length); } // Parsing var valueParser = new VariableValueParser(); var stack = new Stack<Variable>(variables.Reverse()); var root = valueParser.Parse<SavegameRoot>(stack); Variables = variables; }
public static Task<SavegameFile> ReadAsync( string path, IReadSavegameProgress progress = null) { return Task.Run(() => Read(path, progress)); }
public static SavegameFile Read( string path, IReadSavegameProgress progress = null) { if (progress != null) progress.Report(true, true, 0, 0); using (var compressedInputStream = File.OpenRead(path)) using (var inputStream = ChunkedLz4File.Decompress(compressedInputStream)) using (var reader = new BinaryReader(inputStream, Encoding.ASCII, true)) { var savegameFile = new SavegameFile(); savegameFile.ReadHeader(reader); savegameFile.ReadFooter(reader); savegameFile.ReadStringTable(reader); savegameFile.ReadVariableTable(reader); if (progress != null) progress.Report(true, false, 0, savegameFile.VariableTableEntries.Length); savegameFile.ReadVariables(reader, progress); savegameFile.ReferenceVariable(reader, progress); if (progress != null) progress.Report(false, false, 0, 0); return savegameFile; } }