Atdump

From ActiveWiki
Jump to navigation Jump to search

An atdump is a file that contains the attributes of the world. It can be used to backup and restore a worlds objects. Together with the propdump (world objects) and elevdump (world terrain) it is a complete backup of a world.

Atdump structure

Every line in a atdump file represents and attribute. That line contains a mapped attribute# and a string representing the according attribute string or value.

Example file content:

atdump version 4
0 Y
1 N
2 Y
3 Y
4 N
5 Y
6 Y
7 N
8 N
9 Y
10 192
11 192
12 192
13 2
14 
15 1 21 607 565
17 1 21 607 660 565
18 1.000000
19 4400
20 cloud_strat1m
21 155
22 0.000000
23 0.500000
24 cloud_strat1
25 2.000000
26 cloud_strat1m
27 204
28 0.500000
29 0.000000
...

Code Sniplets

Write Attribute Format

static void codeNL(char* string)
{
    while (*string)
    {
        if (*string == '\n')
            *string = (char)127;
        string++;
    }
}

static void atdump(void)
{
    int           rc;
    int           i;
    int           read_only;
    FILE* fp;
    char          stringA[2048];
#if defined(UNICODE)
    wchar_t       stringW[2048];
#endif

    fp = fopen("atdump.txt", "w");
    if (!fp)
        return;
    fprintf(fp, "atdump version 4\n");

    for (i = 0; i < AW_MAX_ATTRIBUTE; i++)
    {
        read_only = 1;
#if !defined(UNICODE)
        if (rc = aw_world_attribute_get(i, &read_only, stringA) || read_only)
            continue;
#else
        if (rc = aw_world_attribute_getW(i, &read_only, stringW) || read_only)
            continue;
        strncpy(stringA, aw_string_from_unicode(stringW), sizeof(stringA));
#endif
        codeNL(stringA);
        fprintf(fp, "%d %s\n", i, stringA);
    }
    fclose(fp);
}

Read Attribute Format

static void decodeNL(char* string)
{
   while (*string)
    {
        if (*string == 127)
            *string = '\n';
        string++;
    }
}

int atload(void)
{
    int   rc      = 0;
    FILE* fp      = NULL;
    int   id      = 0;
    int   version = 0;
    char  string[2048];
    char  buf[2048];

    if (!(fp = fopen("", "r")))
    {
        // can not open file
        return -1;
    }
    if (fscanf(fp, "atdump version %d", &version) != 1 || version != 4)
    {
        // unknown file format
        return -2;
    }

    aw_world_attributes_reset();

    for (;;)
    {
        memset(buf, 0, sizeof(buf));
        if (!fgets(buf, sizeof(buf) - 1, fp))
            break;
        if (buf[0] < '0')
            // skip blank lines
            continue;

        memset(string, 0, sizeof(string));
        rc = sscanf(buf, "%d %[^\n]", &id, string);
        if (rc == 0)
        {
            // invalid format
            fclose(fp);
            return -3;
        }

        decodeNL(string);
#if !defined(UNICODE)
        if ((rc = aw_world_attribute_set(id, string)) && rc != RC_READ_ONLY)
#else
        if ((rc = aw_world_attribute_setW(id, aw_string_to_unicode(string))) && rc != RC_READ_ONLY)
#endif
        {
            // TODO: check the rc...
        }
    }
    fclose(fp);

    rc = aw_world_attributes_change();

    // TODO: tell user if an error occurred
    return rc;
}

See Also

SDK: aw_world_attribute_set SDK: aw_world_attribute_get