aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/print_cmd.cpp
blob: 469dddf557a86b0f234df08c934dae522e3cb2cc (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright Epic Games, Inc. All Rights Reserved.

#include "print_cmd.h"

#include <zencore/compactbinarypackage.h>
#include <zencore/compactbinaryvalidation.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/string.h>
#include <zenhttp/packageformat.h>

using namespace std::literals;

namespace zen {

static void
PrintCbObject(CbObject Object)
{
	ExtendableStringBuilder<1024> ObjStr;
	CompactBinaryToJson(Object, ObjStr);
	ZEN_CONSOLE("{}", ObjStr);
}

static void
PrintCompactBinary(IoBuffer Data)
{
	ExtendableStringBuilder<1024> StreamString;
	CompactBinaryToJson(Data.GetView(), StreamString);
	ZEN_CONSOLE("{}", StreamString);
}

PrintCommand::PrintCommand()
{
	m_Options.add_options()("h,help", "Print help");
	m_Options.add_option("", "s", "source", "Object payload file (use '-' to read from STDIN)", cxxopts::value(m_Filename), "<file name>");
	m_Options.parse_positional({"source"});
}

PrintCommand::~PrintCommand() = default;

int
PrintCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	ZEN_UNUSED(GlobalOptions);

	if (!ParseOptions(argc, argv))
	{
		return 0;
	}

	// Validate arguments

	if (m_Filename.empty())
		throw std::runtime_error("No file specified");

	FileContents Fc;

	if (m_Filename == "-")
	{
		Fc = ReadStdIn();
	}
	else
	{
		Fc = ReadFile(m_Filename);
	}

	if (Fc.ErrorCode)
	{
		ZEN_ERROR("Failed to read file '{}': {}", m_Filename, Fc.ErrorCode.message());

		return 1;
	}

	IoBuffer Data = Fc.Flatten();

	IoHash	 RawHash;
	uint64_t RawSize;
	if (CompressedBuffer::ValidateCompressedHeader(Data, RawHash, RawSize))
	{
		ZEN_CONSOLE("Compressed binary: size {}, raw size {}, hash: {}", Data.GetSize(), RawSize, RawHash);
	}
	else if (IsPackageMessage(Data))
	{
		CbPackage Package = ParsePackageMessage(Data);

		CbObject					  Object	  = Package.GetObject();
		std::span<const CbAttachment> Attachments = Package.GetAttachments();

		ZEN_CONSOLE("Package - {} attachments, object hash {}", Package.GetAttachments().size(), Package.GetObjectHash());
		ZEN_CONSOLE("");

		int AttachmentIndex = 1;

		for (const CbAttachment& Attachment : Attachments)
		{
			std::string AttachmentSize = "n/a";
			const char* AttachmentType = "unknown";

			if (Attachment.IsCompressedBinary())
			{
				AttachmentType = "Compressed";
				AttachmentSize = fmt::format("{} ({} uncompressed)",
											 Attachment.AsCompressedBinary().GetCompressedSize(),
											 Attachment.AsCompressedBinary().DecodeRawSize());
			}
			else if (Attachment.IsBinary())
			{
				AttachmentType = "Binary";
				AttachmentSize = fmt::format("{}", Attachment.AsBinary().GetSize());
			}
			else if (Attachment.IsObject())
			{
				AttachmentType = "Object";
				AttachmentSize = fmt::format("{}", Attachment.AsObject().GetSize());
			}
			else if (Attachment.IsNull())
			{
				AttachmentType = "null";
			}

			ZEN_CONSOLE("Attachment #{} : {}, {}, size {}", AttachmentIndex, Attachment.GetHash(), AttachmentType, AttachmentSize);

			++AttachmentIndex;
		}

		ZEN_CONSOLE("---8<---");

		PrintCbObject(Object);
	}
	else if (CbValidateError Result = ValidateCompactBinary(Data,
															CbValidateMode::Default | CbValidateMode::Names | CbValidateMode::Format |
																CbValidateMode::Package | CbValidateMode::PackageHash);
			 Result == CbValidateError::None)
	{
		PrintCompactBinary(Data);
	}
	else
	{
		ZEN_ERROR("Data in file '{}' does not appear to be compact binary (validation error {:#x})", m_Filename, uint32_t(Result));

		return 1;
	}

	return 0;
}

//////////////////////////////////////////////////////////////////////////

PrintPackageCommand::PrintPackageCommand()
{
	m_Options.add_options()("h,help", "Print help");
	m_Options.add_option("", "s", "source", "Package payload file", cxxopts::value(m_Filename), "<file name>");
	m_Options.parse_positional({"source"});
}

PrintPackageCommand::~PrintPackageCommand()
{
}

int
PrintPackageCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	ZEN_UNUSED(GlobalOptions);

	if (!ParseOptions(argc, argv))
	{
		return 0;
	}

	// Validate arguments

	if (m_Filename.empty())
		throw std::runtime_error("No file specified");

	FileContents Fc	  = ReadFile(m_Filename);
	IoBuffer	 Data = Fc.Flatten();
	CbPackage	 Package;

	bool Ok = Package.TryLoad(Data) || legacy::TryLoadCbPackage(Package, Data, &UniqueBuffer::Alloc);

	if (Ok)
	{
		ExtendableStringBuilder<1024> ObjStr;
		CompactBinaryToJson(Package.GetObject(), ObjStr);
		ZEN_CONSOLE("{}", ObjStr);
	}
	else
	{
		ZEN_ERROR("error: malformed package?");
	}

	return 0;
}

}  // namespace zen