-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
152 lines (123 loc) · 3.74 KB
/
Copy pathmain.cc
File metadata and controls
152 lines (123 loc) · 3.74 KB
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
#define THEORY_IMPL
#define THEORY_DUCKTAPE
#define THEORY_SLIM_BUILD
#include <theory/theory.hh>
using namespace UFG;
bool ConvertToBin(void* buffer, s64 bufferSize, const qString& filename)
{
qChunkFileBuilder chunk_builder;
if (!chunk_builder.CreateBuilder("PC64", filename)) {
return 0;
}
u32 screen_size = static_cast<u32>(sizeof(UIScreenChunk) + bufferSize);
auto screen = static_cast<UIScreenChunk*>(qMalloc(screen_size));
qMemSet(screen, 0, sizeof(UIScreenChunk));
auto screenName = filename.GetFilenameWithoutExtension();
screen->mNode.SetUID(screenName.GetStringHashUpper32());
screen->mTypeUID = RTypeUID_UIScreen;
screen->SetDebugName(screenName);
screen->mBufferSize = static_cast<u32>(bufferSize);
screen->mBuffer.Set(&screen[1]);
memcpy(screen->mBuffer.Get(), buffer, bufferSize);
chunk_builder.BeginChunk(ChunkUID_UIScreen, "UIScreenChunk");
chunk_builder.Write(screen, screen_size);
chunk_builder.EndChunk(ChunkUID_UIScreen);
qFree(screen);
bool dummy;
chunk_builder.CloseBuilder(&dummy, 0);
return 1;
}
bool ConvertToSWF(UIScreenChunk* screen, const qString& filename)
{
auto buffer = screen->mBuffer.Get();
if (!buffer || !screen->mBufferSize) {
return 0;
}
auto file = qOpen(filename, QACCESS_WRITE);
if (!file) {
return 0;
}
qWrite(file, buffer, screen->mBufferSize);
qClose(file);
return 1;
}
int DetermineFileType(void* data, s64 size)
{
if (size >= sizeof(UIScreenChunk))
{
auto chunk = static_cast<qChunk*>(data);
auto screen = static_cast<UIScreenChunk*>(chunk->GetData());
if (chunk->mUID == ChunkUID_UIScreen && screen->mTypeUID == RTypeUID_UIScreen) {
return 0;
}
}
if (size > 3)
{
auto f = static_cast<u8*>(data);
if ((f[0] == 'S' && f[1] == 'F' && f[2] == 'X') ||
(f[0] == 'C' && f[1] == 'F' && f[2] == 'X') ||
(f[0] == 'C' && f[1] == 'W' && f[2] == 'S') ||
(f[0] == 'G' && f[1] == 'F' && f[2] == 'X'))
{
return 1;
}
}
return -1;
}
int main(int argc, char** argv)
{
qInit(0);
if (1 >= argc)
{
auto filename = qString(argv[0]).GetFilename();
qPrintf("Missing argument!\nUsage: %s <filename> <outname:optional>\n", filename.mData);
return 1;
}
qString filename = argv[1];
qString outname = (argc > 2 ? argv[2] : argv[1]);
if (!qFileExists(filename))
{
qPrintf("Error: File doesn't exist!\n");
return 1;
}
char* ext = qStringFindLast(filename, ".");
if (!ext)
{
qPrintf("Error: Filename has no extension, can't decide what to do...\n");
return 1;
}
s64 fileSize;
auto fileData = StreamFileWrapper::ReadEntireFile(filename, &fileSize);
if (!fileData || !fileSize)
{
qPrintf("Error: Failed to read file or the file is empty!\n");
return 1;
}
int fileType = DetermineFileType(fileData, fileSize);
if (fileType == -1)
{
qPrintf("Error: Loaded file that is not valid SWF or UIScreen!\n");
return 1;
}
const bool isSWF = (fileType == 1);
outname = outname.ReplaceExtension(isSWF ? ".bin" : ".swf");
if (isSWF)
{
if (!ConvertToBin(fileData, fileSize, outname))
{
qPrintf("Error: Failed to convert SWF to UIScreen!\n");
return 1;
}
}
else
{
auto chunk = static_cast<qChunk*>(fileData);
auto screen = static_cast<UIScreenChunk*>(chunk->GetData());
if (!ConvertToSWF(screen, outname))
{
qPrintf("Error: Failed to convert UIScreen to SWF!\n");
return 1;
}
}
return 0;
}