… | |
… | |
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 (auto (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 | |
368 | if (!head->next) |
367 | // sever chain of existing head object |
|
|
368 | for (archetype *more, *at = head; at; at = more) |
369 | { |
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 |
|
|
382 | if (!head->linked) |
|
|
383 | { |
|
|
384 | head->linked = true; |
370 | head->next = first_archetype; |
385 | head->next = first_archetype; |
371 | first_archetype = head; |
386 | first_archetype = head; |
372 | } |
387 | } |
373 | |
388 | |
|
|
389 | // reassemble new chain |
|
|
390 | archetype *prev = head; |
|
|
391 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
|
|
392 | { |
|
|
393 | archetype *at = p->first; |
|
|
394 | overwrite (at, p->second); |
|
|
395 | |
|
|
396 | if (at->clone.x > head->tail_x) head->tail_x = at->clone.x; |
|
|
397 | if (at->clone.y > head->tail_y) head->tail_y = at->clone.y; |
|
|
398 | |
|
|
399 | at->head = head; |
|
|
400 | at->clone.head = &head->clone; |
|
|
401 | prev->more = at; |
|
|
402 | prev->clone.more = &at->clone; |
|
|
403 | |
|
|
404 | prev = at; |
|
|
405 | } |
|
|
406 | } |
|
|
407 | |
374 | return head; |
408 | return true; |
|
|
409 | |
|
|
410 | fail: |
|
|
411 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
|
|
412 | p->second->destroy (true); |
|
|
413 | |
|
|
414 | return false; |
375 | } |
415 | } |
376 | |
416 | |
377 | /* |
417 | /* |
378 | * Reads/parses the archetype-file, and copies into a linked list |
418 | * Reads/parses the archetype-file, and copies into a linked list |
379 | * of archetype-structures. |
419 | * of archetype-structures. |
… | |
… | |
385 | { |
425 | { |
386 | switch (f.kw) |
426 | switch (f.kw) |
387 | { |
427 | { |
388 | case KW_object: |
428 | case KW_object: |
389 | loading_arch = true; |
429 | loading_arch = true; |
390 | if (!archetype::read (f)) |
430 | if (!archetype::load (f)) |
391 | { |
431 | { |
392 | loading_arch = false; |
432 | loading_arch = false; |
393 | return false; |
433 | return false; |
394 | } |
434 | } |
395 | |
435 | |
… | |
… | |
491 | |
531 | |
492 | return arch_to_object (at); |
532 | return arch_to_object (at); |
493 | } |
533 | } |
494 | |
534 | |
495 | /* |
535 | /* |
496 | * Hash-function used by the arch-hashtable. |
|
|
497 | */ |
|
|
498 | |
|
|
499 | unsigned long |
|
|
500 | hasharch (const char *str, int tablesize) |
|
|
501 | { |
|
|
502 | unsigned long hash = 0; |
|
|
503 | unsigned int i = 0; |
|
|
504 | const char *p; |
|
|
505 | |
|
|
506 | /* use the one-at-a-time hash function, which supposedly is |
|
|
507 | * better than the djb2-like one used by perl5.005, but |
|
|
508 | * certainly is better then the bug used here before. |
|
|
509 | * see http://burtleburtle.net/bob/hash/doobs.html |
|
|
510 | */ |
|
|
511 | for (p = str; i < MAXSTRING && *p; p++, i++) |
|
|
512 | { |
|
|
513 | hash += *p; |
|
|
514 | hash += hash << 10; |
|
|
515 | hash ^= hash >> 6; |
|
|
516 | } |
|
|
517 | |
|
|
518 | hash += hash << 3; |
|
|
519 | hash ^= hash >> 11; |
|
|
520 | hash += hash << 15; |
|
|
521 | |
|
|
522 | return hash % tablesize; |
|
|
523 | } |
|
|
524 | |
|
|
525 | /* |
|
|
526 | * Finds, using the hashtable, which archetype matches the given name. |
536 | * Finds, using the hashtable, which archetype matches the given name. |
527 | * returns a pointer to the found archetype, otherwise NULL. |
537 | * returns a pointer to the found archetype, otherwise NULL. |
528 | */ |
538 | */ |
529 | archetype * |
539 | archetype * |
530 | archetype::find (const char *name) |
540 | archetype::find (const char *name) |
531 | { |
541 | { |
532 | if (!name) |
542 | if (!name) |
533 | return 0; |
543 | return 0; |
534 | |
544 | |
535 | AUTODECL (i, ht.find (name)); |
545 | auto (i, ht.find (name)); |
536 | |
546 | |
537 | if (i == ht.end ()) |
547 | if (i == ht.end ()) |
538 | return 0; |
548 | return 0; |
539 | else |
549 | else |
540 | return i->second; |
550 | return i->second; |