summaryrefslogtreecommitdiff
path: root/ZAPD/WarningHandler.h
blob: f99330042b8d0cc048ea90696e482f100767272d (plain)
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
#pragma once

#include <array>
#include <string>
#include <string_view>
#include <unordered_map>

#include "Utils/vt.h"
#include "ZFile.h"

#ifdef _MSC_VER
#define __PRETTY_FUNCTION__ __FUNCSIG__
#elif not defined(__GNUC__)
#define __PRETTY_FUNCTION__ __func__
#endif

// =======================================
/* Formatting macros */

// TODO: move this somewhere else so it can be used by other help
#define HELP_DT_INDT "  "

/* Macros for formatting warnings/errors */
#define VT_HILITE VT_BOLD_FGCOL(WHITE)
#define VT_WARN VT_BOLD_FGCOL(PURPLE)
#define VT_ERR VT_BOLD_FGCOL(RED)

#define HILITE(string) (VT_HILITE string VT_RST)
#define WARN_FMT(string) (VT_WARN string VT_RST)
#define ERR_FMT(string) (VT_ERR string VT_RST)

// Maybe make WARN_LF instead
// Currently 8 spaces
#define WARN_INDT "        "
// Currently 16 spaces
#define HANG_INDT "                "

// =======================================
/* Warning and error macros */
// TODO: better names

// General-purpose, plain style (only prints function,file,line in the preamble)
#define HANDLE_ERROR(warningType, header, body)                                                    \
	WarningHandler::Error_Plain(__FILE__, __LINE__, __PRETTY_FUNCTION__, warningType, header, body)
#define HANDLE_WARNING(warningType, header, body)                                                  \
	WarningHandler::Warning_Plain(__FILE__, __LINE__, __PRETTY_FUNCTION__, warningType, header,    \
	                              body)

// For processing XMLs or textures/blobs (preamble contains function,file,line; processed file)
#define HANDLE_ERROR_PROCESS(warningType, header, body)                                            \
	WarningHandler::Error_Process(__FILE__, __LINE__, __PRETTY_FUNCTION__, warningType, header,    \
	                              body)
#define HANDLE_WARNING_PROCESS(warningType, header, body)                                          \
	WarningHandler::Warning_Process(__FILE__, __LINE__, __PRETTY_FUNCTION__, warningType, header,  \
	                                body)

// For ZResource-related stuff (preamble contains function,file,line; processed file; extracted file
// and offset)
#define HANDLE_ERROR_RESOURCE(warningType, parent, resource, offset, header, body)                 \
	WarningHandler::Error_Resource(__FILE__, __LINE__, __PRETTY_FUNCTION__, warningType, parent,   \
	                               resource, offset, header, body)
#define HANDLE_WARNING_RESOURCE(warningType, parent, resource, offset, header, body)               \
	WarningHandler::Warning_Resource(__FILE__, __LINE__, __PRETTY_FUNCTION__, warningType, parent, \
	                                 resource, offset, header, body)

// =======================================

enum class WarningType
{
	Always,  // Warnings of this type are always printed, cannot be disabled.
	Deprecated,
	Unaccounted,
	MissingOffsets,
	Intersection,
	MissingAttribute,
	InvalidAttributeValue,
	UnknownAttribute,
	InvalidXML,
	InvalidJPEG,
	InvalidPNG,
	InvalidExtractedData,
	MissingSegment,
	HardcodedPointer,
	HardcodedGenericPointer,
	NotImplemented,
	Max,
};

enum class WarningLevel
{
	Off,
	Warn,
	Err,
};

class WarningHandler
{
public:
	static void ConstructTypeToInfoMap();

	static void Init(int argc, char* argv[]);

	static bool IsWarningEnabled(WarningType warnType);
	static bool WasElevatedToError(WarningType warnType);

	static void FunctionPreamble(const char* filename, int32_t line, const char* function);
	static void ProcessedFilePreamble();
	static void ExtractedFilePreamble(const ZFile* parent, const ZResource* res,
	                                  const uint32_t offset);
	static std::string ConstructMessage(std::string message, const std::string& header,
	                                    const std::string& body);

	[[noreturn]] static void PrintErrorAndThrow(const std::string& header, const std::string& body);
	static void PrintWarningBody(const std::string& header, const std::string& body);

	[[noreturn]] static void ErrorType(WarningType warnType, const std::string& header,
	                                   const std::string& body);
	[[noreturn]] static void Error_Plain(const char* filename, int32_t line, const char* function,
	                                     WarningType warnType, const std::string& header,
	                                     const std::string& body);
	[[noreturn]] static void Error_Process(const char* filename, int32_t line, const char* function,
	                                       WarningType warnType, const std::string& header,
	                                       const std::string& body);
	[[noreturn]] static void Error_Resource(const char* filename, int32_t line,
	                                        const char* function, WarningType warnType,
	                                        const ZFile* parent, const ZResource* res,
	                                        const uint32_t offset, const std::string& header,
	                                        const std::string& body);

	static void WarningTypeAndChooseEscalate(WarningType warnType, const std::string& header,
	                                         const std::string& body);

	static void Warning_Plain(const char* filename, int32_t line, const char* function,
	                          WarningType warnType, const std::string& header,
	                          const std::string& body);
	static void Warning_Process(const char* filename, int32_t line, const char* function,
	                            WarningType warnType, const std::string& header,
	                            const std::string& body);
	static void Warning_Resource(const char* filename, int32_t line, const char* function,
	                             WarningType warnType, const ZFile* parent, const ZResource* res,
	                             const uint32_t offset, const std::string& header,
	                             const std::string& body);

	static void PrintHelp();
	static void PrintWarningsDebugInfo();
};