Skip to content

Optimization in Load_Map, Calc_CRC #1034

Open
@xezon

Description

@xezon
bool Load_Map(Utf8String name)
{
    Utf8String name_copy2;
    int len = 0;
    char name_copy[PATH_MAX];
    strcpy(name_copy, name.Str());
    len = strlen(name_copy);
    char name_without_ext[PATH_MAX];

    if (len >= 4) {
        memset(name_without_ext, 0, PATH_MAX);
        strncpy(name_without_ext, name_copy, len - 4);
    }

    CachedFileInputStream stream;
    name_copy2 = name;

    if (stream.Open(name_copy2)) {
        DataChunkInput input(&stream);
        s_waypoints = new WaypointMap();
        input.Register_Parser("HeightMapData", Utf8String::s_emptyString, Parse_Size_Only_In_Chunk, nullptr);
        input.Register_Parser("WorldInfo", Utf8String::s_emptyString, Parse_World_Dict_Data_Chunk, nullptr);
        input.Register_Parser("ObjectsList", Utf8String::s_emptyString, Parse_Objects_Data_Chunk, nullptr);

        if (!input.Parse(nullptr)) {
            throw CODE_05;
        }

        s_mapDX = s_width - 2 * s_borderSize;
        s_mapDY = s_height - 2 * s_borderSize;
        return true;
    } else {
        return false;
    }
}

unsigned int Calc_CRC(Utf8String dir, Utf8String name)
{
    CRC crc;
    crc.Clear();

    Utf8String str;
    char name_copy[PATH_MAX];
    strcpy(name_copy, name.Str());
    int len = strlen(name_copy);
    char name_without_ext[PATH_MAX];

    if (len >= 4) {
        memset(name_without_ext, 0, PATH_MAX);
        strncpy(name_without_ext, name_copy, len - 4);
    }

    str = name;
    File *file = g_theFileSystem->Open_File(str.Str(), File::READ);

    if (file != nullptr) {
        for (;;) {
            unsigned char buf3[4096];
            int val = file->Read(buf3, 4096);

            if (val <= 0) {
                break;
            }

            crc.Compute_CRC(buf3, val);
        }

        file->Close();
        return crc.Get_CRC();
    } else {
        captainslog_dbgassert(false, "Couldn't open '%s'", name.Str());
        return 0;
    }
}

Both these functions create name_copy, name_without_ext, name_copy2 just to never use them. Can be removed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions