Skip to content

Commit

Permalink
Improved PSO to XML, dropped XML to PSO support
Browse files Browse the repository at this point in the history
  • Loading branch information
Neodymium146 committed Jun 17, 2017
1 parent 471a11b commit 7f2dde4
Show file tree
Hide file tree
Showing 45 changed files with 2,501 additions and 1,314 deletions.
57 changes: 39 additions & 18 deletions RageLib.GTA5/PSO/PsoDefinitionSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class PsoDefinitionSection
public List<PsoElementInfo> Entries;

public void Read(DataReader reader)
{
{
Ident = reader.ReadInt32();
var Length = reader.ReadInt32();
this.Count = reader.ReadUInt32();
Expand Down Expand Up @@ -80,17 +80,17 @@ public void Write(DataWriter writer)
var entriesWriter = new DataWriter(entriesStream, Endianess.BigEndian);
for (int i = 0; i < Entries.Count; i++)
{
EntriesIdx[i].Offset = 12 + 8* Entries.Count + (int)entriesWriter.Position;
EntriesIdx[i].Offset = 12 + 8 * Entries.Count + (int)entriesWriter.Position;
Entries[i].Write(entriesWriter);
}



var indexStream = new MemoryStream();
var indexWriter = new DataWriter(indexStream, Endianess.BigEndian);
foreach (var entry in EntriesIdx)
entry.Write(indexWriter);
entry.Write(indexWriter);




Expand All @@ -110,7 +110,7 @@ public void Write(DataWriter writer)
entriesStream.Read(buf2, 0, buf2.Length);
writer.Write(buf2);


}
}

Expand Down Expand Up @@ -146,7 +146,7 @@ public class PsoStructureInfo : PsoElementInfo
public byte Unk { get; set; }
public int StructureLength { get; set; }
public uint Unk_Ch { get; set; } = 0x00000000;
public List<PsoStructureEntryInfo> Entries { get; set; }
public List<PsoStructureEntryInfo> Entries { get; set; } = new List<PsoStructureEntryInfo>();

public override void Read(DataReader reader)
{
Expand Down Expand Up @@ -185,25 +185,25 @@ public override void Write(DataWriter writer)

public enum DataType : byte
{
BYTE_00h = 0x00,
Boolean = 0x00,
LONG_01h = 0x01,
BYTE_02h = 0x02,
Byte = 0x02,
SHORT_03h = 0x03,
SHORT_04h = 0x04,
INT_05h = 0x05,
INT_06h = 0x06,
Integer = 0x06,
Float = 0x07,
LONG_08h = 0x08,
Float2 = 0x08,
TYPE_09h = 0x09,
TYPE_0Ah = 0x0a,
INT_0Bh = 0x0b,
Float4 = 0x0a,
String = 0x0b,
Structure = 0x0c,
Array = 0x0d,
BYTE_ENUM_VALUE = 0x0e,
SHORT_0Fh = 0x0f,
TYPE_10h = 0x10,
Enum = 0x0e,
Flags = 0x0f,
Map = 0x10,
TYPE_14h = 0x14,
TYPE_15h = 0x15,
Float3 = 0x15,
SHORT_1Eh = 0x1e,
LONG_20h = 0x20
}
Expand All @@ -216,13 +216,25 @@ public class PsoStructureEntryInfo
public short DataOffset;
public int ReferenceKey; // when array -> entry index with type

public PsoStructureEntryInfo()
{ }

public PsoStructureEntryInfo(int nameHash, DataType type, byte unk5, short dataOffset, int referenceKey)
{
this.EntryNameHash = nameHash;
this.Type = type;
this.Unk_5h = unk5;
this.DataOffset = dataOffset;
this.ReferenceKey = referenceKey;
}

public void Read(DataReader reader)
{
this.EntryNameHash = reader.ReadInt32();
this.Type = (DataType)reader.ReadByte();
this.Unk_5h = reader.ReadByte();
this.DataOffset = reader.ReadInt16();
this.ReferenceKey = reader.ReadInt32();
this.ReferenceKey = reader.ReadInt32();
}

public void Write(DataWriter writer)
Expand Down Expand Up @@ -277,6 +289,15 @@ public class PsoEnumEntryInfo
public int EntryNameHash { get; set; }
public int EntryKey { get; set; }

public PsoEnumEntryInfo()
{ }

public PsoEnumEntryInfo(int nameHash, int key)
{
this.EntryNameHash = nameHash;
this.EntryKey = key;
}

public void Read(DataReader reader)
{
this.EntryNameHash = reader.ReadInt32();
Expand Down
6 changes: 6 additions & 0 deletions RageLib.GTA5/PSO/PsoFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public virtual void Save(Stream stream)



public static bool IsPSO(string fileName)
{
using (var stream = new FileStream(fileName, FileMode.Open))
return !IsRBF(stream);
}

public static bool IsPSO(Stream stream)
{
return !IsRBF(stream);
Expand Down
98 changes: 98 additions & 0 deletions RageLib.GTA5/PSOWrappers/Data/PsoDataReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright(c) 2016 Neodymium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using RageLib.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using RageLib.GTA5.PSO;

namespace RageLib.GTA5.PSOWrappers.Data
{
public class PsoDataReader : DataReader
{
private readonly PsoFile psoFile;

public override long Length
{
get
{
return psoFile.DataMappingSection.Entries[CurrentSectionIndex].Length;
}
}

public int CurrentSectionIndex
{
get;
private set;
}

public int CurrentSectionHash
{
get;
private set;
}

public override long Position
{
get;
set;
}



public PsoDataReader(PsoFile psoFile) : base(null, Endianess.BigEndian)
{
this.psoFile = psoFile;
}

protected override byte[] ReadFromStream(int count, bool ignoreEndianess = false)
{
// TODO very bad performance, improve this!
var str = new MemoryStream(psoFile.DataSection.Data);
str.Position = psoFile.DataMappingSection.Entries[CurrentSectionIndex].Offset;
str.Position += Position;

var buffer = new byte[count];
str.Read(buffer, 0, count);

Position += count;

// handle endianess
if (!ignoreEndianess && (Endianess == Endianess.BigEndian))
{
Array.Reverse(buffer);
}

return buffer;
}

public void SetSectionIndex(int index)
{
CurrentSectionIndex = index;
CurrentSectionHash = psoFile.DataMappingSection.Entries[index].NameHash;
}
}
}
96 changes: 10 additions & 86 deletions RageLib.GTA5/PSOWrappers/PsoReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using RageLib.Data;
using RageLib.GTA5.PSO;
using RageLib.GTA5.PSOWrappers.Data;
using RageLib.GTA5.PSOWrappers.Types;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -50,100 +50,24 @@ public IPsoValue Parse(PsoFile meta)
var blockKeys = new List<int>();
var blocks = new List<List<IPsoValue>>();


var resultStructure = new PsoStructure();
resultStructure.pso = meta;

var t1 = (PsoStructureInfo)null;
var t2 = (PsoElementIndexInfo)null;
var rootHash = meta.DataMappingSection.Entries[meta.DataMappingSection.RootIndex - 1].NameHash;
for (int i = 0; i < meta.DefinitionSection.Count; i++)
{
if (meta.DefinitionSection.EntriesIdx[i].NameHash == rootHash)
{
resultStructure.psoSection = (PsoStructureInfo)meta.DefinitionSection.Entries[i];
resultStructure.psoEntryInfo = meta.DefinitionSection.EntriesIdx[i];
t1 = (PsoStructureInfo)meta.DefinitionSection.Entries[i];
t2 = meta.DefinitionSection.EntriesIdx[i];
}
}

var reader = new DataReader(new MemoryStream(meta.DataSection.Data), Endianess.BigEndian);
reader.Position = meta.DataMappingSection.Entries[meta.DataMappingSection.RootIndex - 1].Offset;
resultStructure.Read(reader);


var stack = new Stack<PsoStructure>();
stack.Push(resultStructure);
while (stack.Count > 0)
{
var x = stack.Pop();
foreach (var ee in x.psoSection.Entries)
{
if (ee.EntryNameHash == 0x100)
continue;

var value = x.Values[ee.EntryNameHash];

if (value is PsoStructure)
{
stack.Push((PsoStructure)value);
}
if (value is PsoArray)
{
var arrayValue = (PsoArray)value;
switch (arrayValue.psoSection.Type)
{
case DataType.Structure:
{
if (arrayValue.NumberOfEntries > 0)
{
arrayValue.Entries = new List<IPsoValue>();
reader.Position = meta.DataMappingSection.Entries[arrayValue.BlockIndex - 1].Offset + arrayValue.Offset;
for (int i = 0; i < arrayValue.NumberOfEntries; i++)
{
PsoStructure item = new PsoStructure();
item.pso = meta;
item.psoSection = null;
for (int y = 0; y < meta.DefinitionSection.Count; y++)
{
if (meta.DefinitionSection.EntriesIdx[y].NameHash == arrayValue.psoSection.ReferenceKey)
{
item.psoSection = (PsoStructureInfo)meta.DefinitionSection.Entries[y];
item.psoEntryInfo = meta.DefinitionSection.EntriesIdx[y];
}
}

item.Read(reader);
arrayValue.Entries.Add(item);
stack.Push(item);

}
}

break;
}
case DataType.INT_0Bh:
{
if (arrayValue.NumberOfEntries > 0)
{
arrayValue.Entries = new List<IPsoValue>();
reader.Position = meta.DataMappingSection.Entries[arrayValue.BlockIndex - 1].Offset + arrayValue.Offset;
for (int i = 0; i < arrayValue.NumberOfEntries; i++)
{
PsoType11 item = new PsoType11(0);
item.Read(reader);
arrayValue.Entries.Add(item);
}

}


break;
}
default:
throw new System.Exception("Unknown array type.");
}
}
}
}
var resultStructure = new PsoStructure(meta, t1, t2, null);

var reader = new PsoDataReader(meta);
reader.SetSectionIndex(meta.DataMappingSection.RootIndex - 1);
reader.Position = 0;
resultStructure.Read(reader);
return resultStructure;
}
}
Expand Down
Loading

0 comments on commit 7f2dde4

Please sign in to comment.