OpenBarnyard
 
Loading...
Searching...
No Matches
PPropertiesWriter.h
Go to the documentation of this file.
1#pragma once
2#include "PBProperties.h"
3#include "Plugins/PTRB.h"
4#include "File/TTRB.h"
5#include "Toshi/TPString8.h"
6#include "Toshi/T2Map.h"
7#include "Toshi/T2String.h"
8
10{
11private:
12 inline static Toshi::T2Map<const TCHAR*, PTRBSections::MemoryStream::Ptr<TCHAR>, Toshi::T2String8Comparator> s_aStringMap;
13
14private:
15 static PTRBSections::MemoryStream::Ptr<TCHAR> GetStringPtr( const TCHAR* a_szString )
16 {
17 return s_aStringMap.Find( a_szString )->GetSecond();
18 }
19
20 static void DumpValue( PTRBSections::MemoryStream::Ptr<PBPropertyValue> a_ptrValue, const PBPropertyValue& a_OrigValue, PTRBSections::MemoryStream* a_pStack, PTRBSections::MemoryStream::Ptr<PBProperties>* a_pParentProperties )
21 {
22 a_ptrValue->m_eType = a_OrigValue.GetType();
23
24 switch ( a_ptrValue->m_eType )
25 {
27 a_ptrValue->m_uValue.Bool = a_OrigValue.GetBoolean();
28 break;
30 a_ptrValue->m_uValue.Int = a_OrigValue.GetInteger();
31 break;
33 a_ptrValue->m_uValue.UInt32 = a_OrigValue.GetLocaleString().uiIndex;
34 break;
36 a_ptrValue->m_uValue.Float = a_OrigValue.GetFloat();
37 break;
39 a_pStack->WritePointer( &a_ptrValue->m_uValue.String, GetStringPtr( a_OrigValue.GetString() ) );
40 break;
42 {
43 auto properties = DumpProperties( *a_OrigValue.GetProperties(), a_pStack, a_pParentProperties );
44 a_pStack->WritePointer( &a_ptrValue->m_uValue.Properties, properties );
45 break;
46 }
48 {
49 auto arraySize = a_OrigValue.GetArray()->GetSize();
50 auto pArray = a_pStack->Alloc( &a_ptrValue->m_uValue.Array );
51 auto pValues = a_pStack->Alloc( &pArray->m_pValues, arraySize );
52 pArray->m_iSize = arraySize;
53
54 for ( size_t i = 0; i < arraySize; i++ )
55 {
56 DumpValue( pValues + i, *a_OrigValue.GetArray()->GetValue( i ), a_pStack, a_pParentProperties );
57 }
58
59 break;
60 }
61 }
62 }
63
64 static PTRBSections::MemoryStream::Ptr<PBProperties> DumpProperties( const PBProperties& a_oProperties, PTRBSections::MemoryStream* a_pStack, PTRBSections::MemoryStream::Ptr<PBProperties>* a_pParentProperties = TNULL )
65 {
66 auto properties = a_pStack->Alloc<PBProperties>();
67
68 properties->m_iCount = a_oProperties.GetPropertyCount();
69 auto props = a_pStack->Alloc<PBProperties::PBProperty>( &properties->m_pProperties, properties->m_iCount );
70
71 const TBOOL bIsRoot = a_oProperties.GetParentProperties() == TNULL;
72
73 if ( bIsRoot )
74 {
75 // Write string table
76 for ( auto it = s_aStringMap.Begin(); it != s_aStringMap.End(); it++ )
77 {
78 auto buffer = a_pStack->AllocBytes( Toshi::TStringManager::String8Length( it->GetFirst() ) + 1 );
79 Toshi::TStringManager::String8Copy( buffer.get(), it->GetFirst() );
80
81 it->GetSecond() = buffer;
82 }
83 }
84 else
85 {
86 TASSERT( a_pParentProperties != TNULL );
87 a_pStack->WritePointer( &properties->m_pParent, *a_pParentProperties );
88 }
89
90 for ( size_t i = 0; i < properties->m_iCount; i++ )
91 {
92 auto& origProp = a_oProperties.Begin()[ i ];
93
94 auto prop = props + i;
95 auto propName = a_pStack->Alloc<PBPropertyName>( &prop->m_pName );
96 auto propValue = a_pStack->Alloc<PBPropertyValue>( &prop->m_pValue );
97
98 a_pStack->WritePointer( &propName->m_szName, GetStringPtr( origProp.GetName().GetString() ) );
99 DumpValue( propValue, *origProp.GetValue(), a_pStack, &properties );
100 }
101
102 return properties;
103 }
104
105 static void AddStringToMap( const TCHAR* a_szString )
106 {
107 if ( s_aStringMap.Find( a_szString ) == s_aStringMap.End() )
108 {
109 s_aStringMap.Insert( a_szString, {} );
110 }
111 }
112
113 static void FillStringMap( const PBProperties& a_oProperties )
114 {
115 for ( auto it = a_oProperties.Begin(); it != a_oProperties.End(); it++ )
116 {
117 auto propName = it->GetName().GetString();
118 auto propValue = it->GetValue();
119 auto propType = propValue->GetType();
120
121 AddStringToMap( propName );
122
123 switch ( propType )
124 {
126 FillStringMap( *propValue->GetProperties() );
127 break;
129 AddStringToMap( propValue->GetString() );
130 break;
132 for ( size_t i = 0; i < propValue->GetArray()->GetSize(); i++ )
133 {
134 auto arrayValue = propValue->GetArray()->GetValue( i );
135
136 if ( arrayValue->GetType() == PBPropertyValue::Type::String )
137 {
138 AddStringToMap( arrayValue->GetString() );
139 }
140 }
141 break;
142 }
143 }
144 }
145
146public:
147 static TBOOL WriteTRB( const Toshi::TString8& a_sFilePath, const PBProperties& a_oProperties, TBOOL a_bCompress = TFALSE )
148 {
149 TASSERT( a_oProperties.GetParentProperties() == TNULL );
150
151 PTRB trb;
152 auto sect = trb.GetSections();
153 auto symb = trb.GetSymbols();
154
155 auto stack = sect->CreateStream();
156
157 FillStringMap( a_oProperties );
158
159 symb->Add( stack, "Main", DumpProperties( a_oProperties, stack ).get() );
160 trb.WriteToFile( a_sFilePath.GetString(), a_bCompress );
161
162 s_aStringMap.Clear();
163
164 return TTRUE;
165 }
166};
TRB (Toshi Relocatable Binary) resource system for the Toshi engine.
#define TASSERT(X,...)
Definition Defines.h:138
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
const class PBProperties * GetProperties() const
PBPropertyLocaleString GetLocaleString() const
const class PBPropertyValueArray * GetArray() const
TINT GetInteger() const
Type GetType() const
TBOOL GetBoolean() const
TFLOAT GetFloat() const
const TCHAR * GetString() const
PBPropertyValue * GetValue(TSIZE a_iIndex)
TUINT32 GetSize() const
const PBProperties * GetParentProperties() const
TUINT32 GetPropertyCount() const
PBProperty * Begin()
PBProperty * End()
static TBOOL WriteTRB(const Toshi::TString8 &a_sFilePath, const PBProperties &a_oProperties, TBOOL a_bCompress=false)
void WritePointer(T **outPtr, const T *ptr)
Definition PTRB.h:927
Ptr< TCHAR > AllocBytes(TUINT Size)
Definition PTRB.h:947
Definition PTRB.h:595
PTRBSymbols * GetSymbols()
Definition PTRB.h:719
PTRBSections * GetSections()
Definition PTRB.h:720
TBOOL WriteToFile(Toshi::T2StringView filepath, TBOOL compress=false)
Definition PTRB.h:673