How do i get the gameengine material indices?

Ashsid wrote in Gameblender forum:

Materials are sorted in the same way as in method getMesh() in GameLogic module. Unfortunately it seems there is no relation to order of materials in Blender and when you change some material settings in Blender (add new material to mesh, reassign faces to different material), ordering of materials would change.

Any hints how to script that, as me and quite a few other people have to poke with the indices involving a few ugly crashes if index is not found. Any help would make the videotex plugin useable.