-
Notifications
You must be signed in to change notification settings - Fork 1
/
PackageManager.cpp
105 lines (88 loc) · 1.9 KB
/
PackageManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include "PackageManager.h"
bool
PackageManager::ReadPKGJson( std::string filename )
{
std::cout << "Loading pkg.json..." << std::endl;
std::ifstream file( filename.c_str(), std::ios::binary );
if( !file.is_open() )
{
return false;
}
try
{
nlohmann::json reader = nlohmann::json::parse( file );
_kxrList.clear();
_kxrFileList.clear();
_currentPath.clear();
_packageReady = false;
BeginParseJson( reader );
}
catch( std::exception e )
{
std::cout << e.what() << std::endl;
return false;
}
return true;
}
void
PackageManager::BeginParseJson( nlohmann::json root )
{
if( root.end() == root.find( "kxrlist" ) )
{
throw std::exception( "pkg.json missing 'kxrlist' entry!" );
}
uint32_t kxrId = 0;
for( auto kxr : root[ "kxrlist" ] )
{
_kxrList[ kxr.at( "kxrname" ) ] = kxrId++;
}
RecursiveParseJson( root[ "entries" ] );
_packageReady = true;
}
void
PackageManager::RecursiveParseJson( nlohmann::json list )
{
for( auto entry : list )
{
std::string name = entry.at( "name" );
if( entry.find( "list" ) != entry.end() )
{
// Is a folder
_currentPath.append( name );
RecursiveParseJson( entry[ "list" ] );
_currentPath = _currentPath.parent_path();
}
else
{
// Is a file
uint32_t kxr = entry.at( "kxr" );
uint32_t eid = entry.at( "eid" );
_kxrFileList[ kxr ].push_back( { kxr, eid, ( _currentPath / name ) } );
}
}
}
std::filesystem::path
PackageManager::FindFileName( std::string kxrName, uint32_t eid )
{
if( _packageReady )
{
uint32_t kxr = _kxrList[ kxrName ];
if( eid < _kxrFileList[ kxr ].size() )
{
std::vector< KXRJsonEntry >::const_iterator iter = ( _kxrFileList[ kxr ].begin() + eid );
if( ( *iter ).eid == eid )
{
return ( *iter ).name;
}
}
for( auto i : _kxrFileList[ kxr ] )
{
if( eid == i.eid )
{
return i.name;
}
}
}
return kxrName + "/" + std::to_string( eid ) + ".bin";
}