This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.c
142 lines (118 loc) · 4.04 KB
/
dag.c
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
#include "libra/dat_container.h"
#include "libra/dependency_dag.h"
static void list(const char* input_file);
static void deps(const char* input_file);
static void lookup(const char* input_file, const char* hash_str);
static void rebuild(const char* input_file, const char* output_file);
static void print_help();
int main(int argc, char** argv) {
if(argc == 3 && strcmp(argv[1], "list") == 0) {
list(argv[2]);
} else if(argc == 3 && strcmp(argv[1], "deps") == 0) {
deps(argv[2]);
} else if(argc == 4 && strcmp(argv[1], "lookup") == 0) {
lookup(argv[2], argv[3]);
} else if(argc == 4 && strcmp(argv[1], "rebuild") == 0) {
rebuild(argv[2], argv[3]);
} else {
print_help();
return 1;
}
}
static void list(const char* input_file) {
RA_Result result;
u8* data;
s64 size;
if((result = RA_file_read(input_file, &data, &size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to read input file '%s'.\n", input_file);
exit(1);
}
RA_DependencyDag dag;
if((result = RA_dag_parse(&dag, data, (u32) size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to parse DAG file '%s' (%s).\n", input_file, result->message);
exit(1);
}
for(u32 i = 0; i < dag.asset_count; i++) {
RA_DependencyDagAsset* asset = &dag.assets[i];
printf("%s\n", asset->name);
}
RA_dag_free(&dag, true);
}
static void deps(const char* input_file) {
RA_Result result;
u8* data;
s64 size;
if((result = RA_file_read(input_file, &data, &size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to read input file '%s'.\n", input_file);
exit(1);
}
RA_DependencyDag dag;
if((result = RA_dag_parse(&dag, data, (u32) size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to parse DAG file '%s' (%s).\n", input_file, result->message);
exit(1);
}
for(u32 i = 0; i < dag.asset_count; i++) {
RA_DependencyDagAsset* asset = &dag.assets[i];
printf("%s\n", asset->name);
for(u32 j = 0; j < asset->dependency_count; j++) {
RA_DependencyDagAsset* dependency = &dag.assets[asset->dependencies[j]];
printf("\t%s\n", dependency->name);
}
}
RA_dag_free(&dag, true);
}
static void lookup(const char* input_file, const char* hash_str) {
RA_Result result;
u8* data;
s64 size;
if((result = RA_file_read(input_file, &data, &size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to read input file '%s'.\n", input_file);
exit(1);
}
RA_DependencyDag dag;
if((result = RA_dag_parse(&dag, data, (u32) size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to parse DAG file '%s' (%s).\n", input_file, result->message);
exit(1);
}
u64 hash = strtoull(hash_str, NULL, 16);
RA_DependencyDagAsset* asset = RA_dag_lookup_asset(&dag, hash);
if(asset == NULL) {
fprintf(stderr, "No asset with that hash.\n");
exit(1);
}
printf("%s\n", asset->name);
RA_dag_free(&dag, true);
}
static void rebuild(const char* input_file, const char* output_file) {
RA_Result result;
u8* in_data;
s64 in_size;
if((result = RA_file_read(input_file, &in_data, &in_size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to read input file '%s' (%s).\n", input_file, result->message);
exit(1);
}
RA_DependencyDag dag;
if((result = RA_dag_parse(&dag, in_data, (u32) in_size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to parse DAG file '%s' (%s).\n", input_file, result->message);
exit(1);
}
u8* out_data;
s64 out_size;
if((result = RA_dag_build(&dag, &out_data, &out_size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to rebuild DAG file '%s' (%s).\n", output_file, result->message);
exit(1);
}
if((result = RA_file_write(output_file, out_data, out_size)) != RA_SUCCESS) {
fprintf(stderr, "Failed to write output file '%s' (%s).\n", input_file, result->message);
exit(1);
}
RA_dag_free(&dag, true);
}
static void print_help() {
puts("A utility for working with Insomniac Games Dependency DAG files, such as those used by the PC version of Rift Apart.");
puts("");
puts("Commands:");
puts(" list <input file> -- List all asset file paths, one per line.");
puts(" deps <input file> -- List all asset file paths, and their dependencies.");
puts(" lookup <input file> <hash> -- Lookup an asset by its hash.");
}