cm3d2tool/source/cm3d2/menu.d

106 lines
2.0 KiB
D

module cm3d2.menu;
import std.bitmanip;
import std.conv;
import std.math;
class Menu
{
uint fileVersion;
string path;
string name;
string category;
string description;
string[][] lines;
public static Menu fromSDL(string sdl)
{
throw new Exception("Not implemented yet");
}
public static Menu fromMenu(ubyte[] data)
{
auto menu = new Menu();
ubyte readByte()
{
auto value = data[0];
data = data[1 .. $];
return value;
}
string readString()
{
auto length = 0;
ubyte[] chars;
while (true)
{
auto lengthByte = readByte();
if (length != 0 && lengthByte < 128)
{
break;
}
length = length << 8 | lengthByte;
if (lengthByte < 128)
{
break;
}
}
auto value = cast(string) data[0 .. length];
data = data[length .. $];
return value;
}
uint readInt()
{
auto value = littleEndianToNative!uint(data[0 .. 4]);
data = data[4 .. $];
return value;
}
assert(readString() == "CM3D2_MENU", "Not a valid .menu file");
menu.fileVersion = readInt();
menu.path = readString();
menu.name = readString();
menu.category = readString();
menu.description = readString();
assert(readInt() == data.length, "Unexpected data at end of file");
while(data.length > 0)
{
string[] line;
foreach (_; 0 .. readByte())
{
line ~= readString();
}
menu.lines ~= line;
}
std.stdio.writeln(menu.lines);
return menu;
}
public string toMenu()
{
throw new Exception("Not implemented yet");
}
public string toSDL()
{
throw new Exception("Not implemented yet");
}
}