6 static void add_dependencies(asset_resolver_t*assets, abc_asset_t*asset, TAG*tag)
8 int num = swf_GetNumUsedIDs(tag);
9 int*positions = malloc(sizeof(int)*num);
10 swf_GetUsedIDs(tag, positions);
13 U16 id = GET16(&tag->data[positions[t]]);
15 /* check whether we already processed this one */
18 if(GET16(&tag->data[positions[s]])==id) break; //seen before
22 /* count number of occurences */
25 if(GET16(&tag->data[positions[s]])==id)
29 /* create dependency */
30 NEW(asset_dependency_t,d);
31 d->asset = assets->id2asset[id];
32 d->patch = malloc(sizeof(int)*count);
33 d->patch_size = count;
36 if(GET16(&tag->data[positions[s]])==id)
37 d->patch[count++] = positions[s];
40 fprintf(stderr, "Error: ID %d referenced, but not defined\n", id);
42 list_append(asset->dependencies, d);
48 asset_resolver_t* swf_ParseAssets(SWF*swf)
50 NEW(asset_resolver_t,assets);
51 assets->name2asset = dict_new2(&charptr_type);
52 assets->id2asset = malloc(sizeof(abc_asset_t*)*65536);
54 TAG*tag = swf->firstTag;
56 if(swf_isDefiningTag(tag)) {
57 NEW(abc_asset_t, asset);
58 list_append(asset->tags, tag);
59 assets->id2asset[swf_GetDefineID(tag)] = asset;
66 if(swf_isDefiningTag(tag)) {
67 abc_asset_t*asset = assets->id2asset[swf_GetDefineID(tag)];
69 add_dependencies(assets, asset, tag);
70 } else if(swf_isPseudoDefiningTag(tag)) {
71 abc_asset_t*asset = assets->id2asset[swf_GetDefineID(tag)];
73 list_append(asset->tags, tag);
74 add_dependencies(assets, asset, tag);
76 } else if(tag->id == ST_SYMBOLCLASS) {
77 int t, num = swf_GetU16(tag);
79 U16 id = swf_GetU16(tag);
81 assets->mainclass_id = id;
83 abc_asset_t*asset = assets->id2asset[id];
85 fprintf(stderr, "Error: ID %d referenced, but not defined.\n", id);
87 char*name = swf_GetString(tag);
88 dict_put(assets->name2asset, name, asset);
98 void swf_ResolveAssets(asset_resolver_t*assets, abc_file_t*file)
100 int num = assets->name2asset->num;
103 for(t=0;t<file->classes->num;t++) {
104 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
105 char*fullname = abc_class_fullname(cls);
106 abc_asset_t*a = (abc_asset_t*)dict_lookup(assets->name2asset, fullname);
115 static void dump_asset(FILE*fo, abc_asset_t*a, const char*prefix)
117 TAG_list_t*t = a->tags;
120 fprintf(fo, "%s[tag] %s defines ID %d\n", prefix, swf_TagGetName(tag), swf_GetDefineID(tag));
123 char*prefix2 = allocprintf("%s ", prefix);
124 asset_dependency_list_t*l = a->dependencies;
126 dump_asset(fo, l->asset_dependency->asset, prefix2);
132 void swf_DumpAsset(FILE*fo, abc_asset_t*asset, const char*prefix)
134 dump_asset(fo, asset, prefix);
137 static TAG* write_tag(TAG*prev, TAG*tag)
147 static TAG*write_asset(TAG*tag, abc_asset_t*a, dict_t*written, U16*currentid)
149 if(!dict_contains(written, a)) {
150 dict_put(written, a, 0);
151 a->id = (*currentid)++;
152 TAG_list_t*tags = a->tags;
154 asset_dependency_list_t*deps = a->dependencies;
156 asset_dependency_t*dep = deps->asset_dependency;
157 tag = write_asset(tag, dep->asset, written, currentid);
159 for(t=0;t<dep->patch_size;t++) {
160 PUT16(&tag->data[dep->patch[t]], dep->asset->id);
166 swf_SetDefineID(tags->TAG, a->id);
167 tag = write_tag(tag, tags->TAG);
174 void swf_WriteABCSymbols(TAG*tag, abc_file_t*file)
178 for(t=0;t<file->classes->num;t++) {
179 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
180 abc_asset_t*a = cls->asset;
185 swf_SetU16(tag, num);
186 for(t=0;t<file->classes->num;t++) {
187 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
188 abc_asset_t*a = cls->asset;
190 U16 id = swf_GetDefineID(a->tags->TAG);
191 char*fullname = abc_class_fullname(cls);
193 swf_SetString(tag, fullname);
198 TAG*swf_AssetsToTags(TAG*itag, asset_bundle_list_t*assets)
201 asset_bundle_list_t*l = assets;
202 dict_t*written = dict_new2(&ptr_type);
204 if(l->asset_bundle->used) {
205 abc_file_t*file = l->asset_bundle->file;
208 for(t=0;t<file->classes->num;t++) {
209 abc_asset_t*a = ((abc_class_t*)array_getvalue(file->classes, t))->asset;
211 tag = write_asset(tag, a, written, ¤tid);
215 tag = swf_InsertTag(tag, ST_DOABC);
216 swf_WriteABC(tag, file);
217 tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
218 swf_WriteABCSymbols(tag, file);
221 while(first && first->prev)
234 dict_destroy(written);