1- #include " pkg_repack_main .h"
1+ #include " pkg_repack .h"
22
33#include < array>
44#include < cstdint>
55#include < cstdio>
66#include < cstring>
77#include < filesystem>
8+ #include < format>
89#include < memory>
910#include < optional>
1011#include < string>
1415#include " cpp-optparse/OptionParser.h"
1516
1617#include " rapidjson/document.h"
18+ #include " rapidjson/error/en.h"
1719
20+ #include " pkg_repack_main.h"
1821#include " sen/pkg.h"
1922#include " sen/pkg_compress.h"
2023#include " util/endian.h"
@@ -100,30 +103,44 @@ int PKG_Repack_Function(int argc, char** argv) {
100103 std::string_view source (args[0 ]);
101104 std::string_view target (output_option->first_string ());
102105
106+ auto result = RepackPkg (source, target);
107+ if (result.IsError ()) {
108+ printf (" %s\n " , result.GetErrorValue ().c_str ());
109+ return -1 ;
110+ }
103111
112+ return 0 ;
113+ }
114+
115+ HyoutaUtils::Result<RepackPkgResult, std::string> RepackPkg (std::string_view source,
116+ std::string_view target) {
104117 std::filesystem::path sourcepath = HyoutaUtils::IO::FilesystemPathFromUtf8 (source);
105118 HyoutaUtils::IO::File f (sourcepath, HyoutaUtils::IO::OpenMode::Read);
106119 if (!f.IsOpen ()) {
107- return - 1 ;
120+ return std::format ( " Failed to open {} " , source) ;
108121 }
109122 auto length = f.GetLength ();
110123 if (!length) {
111- return - 1 ;
124+ return std::format ( " Failed to get length of {} " , source) ;
112125 }
113126 auto buffer = std::make_unique_for_overwrite<char []>(*length);
114127 if (!buffer) {
115- return - 1 ;
128+ return std::format ( " Failed to allocate memory for {} " , source) ;
116129 }
117130 if (f.Read (buffer.get (), *length) != *length) {
118- return - 1 ;
131+ return std::format ( " Failed to read {} " , source) ;
119132 }
120133
121134 rapidjson::Document json;
122135 json.Parse <rapidjson::kParseFullPrecisionFlag | rapidjson::kParseNanAndInfFlag
123136 | rapidjson::kParseCommentsFlag ,
124137 rapidjson::UTF8<char >>(buffer.get (), *length);
125- if (json.HasParseError () || !json.IsObject ()) {
126- return -1 ;
138+ if (json.HasParseError ()) {
139+ return std::format (" Failed to parse JSON: {}" ,
140+ rapidjson::GetParseError_En (json.GetParseError ()));
141+ }
142+ if (!json.IsObject ()) {
143+ return std::string (" JSON root is not an object" );
127144 }
128145
129146 const auto root = json.GetObject ();
@@ -138,15 +155,21 @@ int PKG_Repack_Function(int argc, char** argv) {
138155 if (file.IsObject ()) {
139156 const auto fileobj = file.GetObject ();
140157 const auto nameInArchive = JsonReadString (file, " NameInArchive" );
158+ if (!nameInArchive) {
159+ return std::string (" JSON error: 'NameInArchive' missing or invalid" );
160+ }
141161 const auto pathOnDisk = JsonReadString (file, " PathOnDisk" );
162+ if (!pathOnDisk) {
163+ return std::string (" JSON error: 'PathOnDisk' missing or invalid" );
164+ }
142165 const auto flags = JsonReadUInt32 (file, " Flags" );
143- const auto isPkaRef = JsonReadBool (file, " IsPkaReference" );
144- if (!nameInArchive || !pathOnDisk || !flags) {
145- return -1 ;
166+ if (!flags) {
167+ return std::string (" JSON error: 'Flags' missing or invalid" );
146168 }
169+ const auto isPkaRef = JsonReadBool (file, " IsPkaReference" );
147170 std::array<char , 0x40 > fn{};
148171 if (nameInArchive->size () > fn.size ()) {
149- return - 1 ;
172+ return std::format ( " JSON error: 'NameInArchive' too long ({}) " , *nameInArchive) ;
150173 }
151174 std::memcpy (fn.data (), nameInArchive->data (), nameInArchive->size ());
152175
@@ -159,23 +182,19 @@ int PKG_Repack_Function(int argc, char** argv) {
159182 ((const char8_t *)pathOnDisk->data ()) + pathOnDisk->size ())),
160183 HyoutaUtils::IO::OpenMode::Read);
161184 if (!infile.IsOpen ()) {
162- printf (" Failed opening file.\n " );
163- return -1 ;
185+ return std::format (" Failed opening {}" , *pathOnDisk);
164186 }
165187 const auto uncompressedLength = infile.GetLength ();
166188 if (!uncompressedLength) {
167- printf (" Failed getting size of file.\n " );
168- return -1 ;
189+ return std::format (" Failed getting size of {}" , *pathOnDisk);
169190 }
170191 if (*uncompressedLength > UINT32_MAX) {
171- printf (" File too big to put into pkg.\n " );
172- return -1 ;
192+ return std::format (" {} too big to put into pkg" , *pathOnDisk);
173193 }
174194 auto uncompressedData = std::make_unique_for_overwrite<char []>(*uncompressedLength);
175195 if (infile.Read (uncompressedData.get (), *uncompressedLength)
176196 != *uncompressedLength) {
177- printf (" Failed to read file.\n " );
178- return -1 ;
197+ return std::format (" Failed to read {}" , *pathOnDisk);
179198 }
180199
181200 if (isPkaRef.has_value () && *isPkaRef) {
@@ -195,16 +214,15 @@ int PKG_Repack_Function(int argc, char** argv) {
195214 static_cast <uint32_t >(*uncompressedLength),
196215 *flags,
197216 HyoutaUtils::EndianUtils::Endianness::LittleEndian)) {
198- printf (" Failed adding file to pkg.\n " );
199- return -1 ;
217+ return std::string (" Failed adding file to pkg" );
200218 }
201219 }
202220 } else {
203- return - 1 ;
221+ return std::string ( " JSON error: File is not a JSON object " ) ;
204222 }
205223 }
206224 } else {
207- return - 1 ;
225+ return std::string ( " JSON error: 'Files' not found or not an array " ) ;
208226 }
209227
210228 std::unique_ptr<char []> ms;
@@ -215,21 +233,18 @@ int PKG_Repack_Function(int argc, char** argv) {
215233 static_cast <uint32_t >(fileinfos.size ()),
216234 unknownValue,
217235 HyoutaUtils::EndianUtils::Endianness::LittleEndian)) {
218- printf (" Failed to create pkg.\n " );
219- return -1 ;
236+ return std::string (" Failed to create pkg" );
220237 }
221238
222239 HyoutaUtils::IO::File outfile (target, HyoutaUtils::IO::OpenMode::Write);
223240 if (!outfile.IsOpen ()) {
224- printf (" Failed to open output file.\n " );
225- return -1 ;
241+ return std::string (" Failed to open output file" );
226242 }
227243
228244 if (outfile.Write (ms.get (), msSize) != msSize) {
229- printf (" Failed to write to output file.\n " );
230- return -1 ;
245+ return std::string (" Failed to write to output file" );
231246 }
232247
233- return 0 ;
248+ return RepackPkgResult::Success ;
234249}
235250} // namespace SenTools
0 commit comments