… | |
… | |
311 | overwrite (archetype *at, object *op) |
311 | overwrite (archetype *at, object *op) |
312 | { |
312 | { |
313 | at->clone = *op; |
313 | at->clone = *op; |
314 | |
314 | |
315 | at->clone.arch = at; |
315 | at->clone.arch = at; |
316 | //TODO: inv et al. |
316 | at->clone.inv = op->inv; op->inv = 0; |
317 | } |
|
|
318 | |
317 | |
319 | archetype * |
318 | op->destroy (); |
|
|
319 | } |
|
|
320 | |
|
|
321 | bool |
320 | archetype::read (object_thawer &f) |
322 | archetype::load (object_thawer &f) |
321 | { |
323 | { |
322 | assert (f.kw == KW_object); |
324 | assert (f.kw == KW_object); |
323 | |
325 | |
|
|
326 | typedef std::pair<archetype *, object *> part; |
|
|
327 | std::vector<part> parts; |
|
|
328 | |
|
|
329 | for (;;) |
|
|
330 | { |
324 | archetype *head = get (f.get_str ()); |
331 | archetype *at = get (f.get_str ()); |
325 | unlink (head); |
|
|
326 | object *op = object::create (); |
332 | object *op = object::create (); |
327 | |
333 | |
328 | if (!op->parse_kv (f)) |
334 | if (!op->parse_kv (f)) |
329 | { |
335 | goto fail; |
330 | op->destroy (true); |
|
|
331 | // leak head |
|
|
332 | return 0; |
|
|
333 | } |
|
|
334 | |
336 | |
335 | overwrite (head, op); |
337 | parts.push_back (std::make_pair (at, op)); |
336 | op->destroy (); |
|
|
337 | |
338 | |
338 | for (archetype *prev = head; f.kw == KW_more; ) |
339 | if (f.kw != KW_more) |
339 | { |
340 | break; |
|
|
341 | |
340 | f.next (); |
342 | f.next (); |
341 | assert (f.kw == KW_object); |
343 | assert (f.kw == KW_object); |
|
|
344 | } |
342 | |
345 | |
343 | archetype *more = get (f.get_str ()); |
346 | { |
344 | unlink (more); |
347 | archetype *head = parts.front ().first; |
345 | object *op = object::create (); |
|
|
346 | |
348 | |
347 | if (!op->parse_kv (f)) |
349 | // check that all archetypes belong to the same object or are heads |
|
|
350 | for (AUTODECL (p, parts.begin ()); p != parts.end (); ++p) |
|
|
351 | { |
|
|
352 | archetype *at = p->first; |
|
|
353 | |
|
|
354 | if (at->head != head && at->head) |
348 | { |
355 | { |
349 | op->destroy (true); |
356 | LOG (llevError, "%s: unable to overwrite foreign non-head archetype with non-head archetype\n", &at->name); |
350 | // leak head more* |
357 | goto fail; |
351 | return 0; |
|
|
352 | } |
358 | } |
353 | |
359 | |
354 | overwrite (more, op); |
360 | if (at->next && at != head) |
355 | op->destroy (); |
361 | { |
356 | |
362 | LOG (llevError, "%s: unable to overwrite foreign head archetype with non-head archetype\n", &at->name); |
357 | if (more->clone.x > head->tail_x) head->tail_x = more->clone.x; |
363 | goto fail; |
358 | if (more->clone.y > head->tail_y) head->tail_y = more->clone.y; |
364 | } |
359 | |
|
|
360 | more->head = head; |
|
|
361 | more->clone.head = &head->clone; |
|
|
362 | prev->more = more; |
|
|
363 | prev->clone.more = &more->clone; |
|
|
364 | |
|
|
365 | prev = more; |
|
|
366 | } |
365 | } |
367 | |
366 | |
|
|
367 | // sever chain of existing head object |
|
|
368 | for (archetype *more, *at = head; at; at = more) |
|
|
369 | { |
|
|
370 | more = at->more; |
|
|
371 | |
|
|
372 | at->head = 0; |
|
|
373 | at->more = 0; |
|
|
374 | } |
|
|
375 | |
|
|
376 | // replace/update head |
|
|
377 | overwrite (head, parts.front ().second); |
|
|
378 | head->tail_x = 0; |
|
|
379 | head->tail_y = 0; |
|
|
380 | |
|
|
381 | // link into list of heads, if not already there |
368 | if (!head->next) |
382 | if (!head->next) |
369 | { |
383 | { |
370 | head->next = first_archetype; |
384 | head->next = first_archetype; |
371 | first_archetype = head; |
385 | first_archetype = head; |
372 | } |
386 | } |
373 | |
387 | |
|
|
388 | // reassemble new chain |
|
|
389 | archetype *prev = head; |
|
|
390 | for (AUTODECL (p, parts.begin () + 1); p != parts.end (); ++p) |
|
|
391 | { |
|
|
392 | archetype *at = p->first; |
|
|
393 | overwrite (at, p->second); |
|
|
394 | |
|
|
395 | if (at->clone.x > head->tail_x) head->tail_x = at->clone.x; |
|
|
396 | if (at->clone.y > head->tail_y) head->tail_y = at->clone.y; |
|
|
397 | |
|
|
398 | at->head = head; |
|
|
399 | at->clone.head = &head->clone; |
|
|
400 | prev->more = at; |
|
|
401 | prev->clone.more = &at->clone; |
|
|
402 | |
|
|
403 | prev = at; |
|
|
404 | } |
|
|
405 | } |
|
|
406 | |
374 | return head; |
407 | return true; |
|
|
408 | |
|
|
409 | fail: |
|
|
410 | for (AUTODECL (p, parts.begin ()); p != parts.end (); ++p) |
|
|
411 | p->second->destroy (true); |
|
|
412 | |
|
|
413 | return false; |
375 | } |
414 | } |
376 | |
415 | |
377 | /* |
416 | /* |
378 | * Reads/parses the archetype-file, and copies into a linked list |
417 | * Reads/parses the archetype-file, and copies into a linked list |
379 | * of archetype-structures. |
418 | * of archetype-structures. |
… | |
… | |
385 | { |
424 | { |
386 | switch (f.kw) |
425 | switch (f.kw) |
387 | { |
426 | { |
388 | case KW_object: |
427 | case KW_object: |
389 | loading_arch = true; |
428 | loading_arch = true; |
390 | if (!archetype::read (f)) |
429 | if (!archetype::load (f)) |
391 | { |
430 | { |
392 | loading_arch = false; |
431 | loading_arch = false; |
393 | return false; |
432 | return false; |
394 | } |
433 | } |
395 | |
434 | |