cm3d2tool/source/cm3d2/menu.d

150 lines
4.1 KiB
D
Raw Normal View History

2019-02-18 23:59:29 +01:00
module cm3d2.menu;
2019-02-19 02:50:08 +01:00
import std.conv;
import std.math;
2019-02-19 05:04:53 +01:00
import std.array;
import std.range;
import sdlang;
2019-02-19 02:50:08 +01:00
2019-02-23 08:29:45 +01:00
import cm3d2;
2019-02-23 06:04:31 +01:00
2019-02-18 23:59:29 +01:00
class Menu
{
2019-02-19 05:04:53 +01:00
private static string[string] _translations;
static this()
{
_translations["メニューフォルダ"] = "menufolder";
_translations["setumei"] = "description";
_translations["アタッチポイントの設定"] = "attachpoint";
_translations["属性追加"] = "addattribute";
}
2019-02-19 02:50:08 +01:00
uint fileVersion;
string path;
string name;
string category;
string description;
string[][] lines;
2019-02-18 23:59:29 +01:00
public static Menu fromSDL(string sdl)
{
throw new Exception("Not implemented yet");
}
2019-02-19 02:50:08 +01:00
public static Menu fromMenu(ubyte[] data)
2019-02-18 23:59:29 +01:00
{
2019-02-19 02:50:08 +01:00
auto menu = new Menu();
2019-02-23 06:04:31 +01:00
assert(data.readString() == "CM3D2_MENU", "Not a valid .menu file");
2019-02-19 02:50:08 +01:00
2019-02-23 06:04:31 +01:00
menu.fileVersion = data.readInt();
menu.path = data.readString();
menu.name = data.readString();
menu.category = data.readString();
menu.description = data.readString();
2019-02-19 02:50:08 +01:00
2019-02-23 06:04:31 +01:00
assert(data.readInt() == data.length, "Unexpected data at end of file");
2019-02-19 02:50:08 +01:00
2019-02-23 08:29:45 +01:00
while (data.length > 0)
2019-02-19 02:50:08 +01:00
{
string[] line;
2019-02-23 06:04:31 +01:00
foreach (_; 0 .. data.readByte())
2019-02-19 02:50:08 +01:00
{
2019-02-23 06:04:31 +01:00
line ~= data.readString();
2019-02-19 02:50:08 +01:00
}
2019-02-19 05:04:53 +01:00
if (line.length > 0)
{
menu.lines ~= line;
}
2019-02-19 02:50:08 +01:00
}
return menu;
2019-02-18 23:59:29 +01:00
}
public string toMenu()
{
throw new Exception("Not implemented yet");
}
public string toSDL()
{
2019-02-19 05:04:53 +01:00
auto versionTag = new Tag(null, "version", [Value(cast(int) fileVersion)]);
auto pathTag = new Tag(null, "path", [Value(path)]);
auto nameTag = new Tag(null, "name", [Value(name)]);
auto categoryTag = new Tag(null, "category", [Value(category)]);
auto descriptionTag = new Tag(null, "description", [Value(description)]);
2019-02-23 08:29:45 +01:00
auto root = new Tag(null, null, null, null, [versionTag, pathTag,
nameTag, categoryTag, descriptionTag]);
2019-02-19 05:04:53 +01:00
Tag[] dataTags;
foreach (line; lines)
{
if (line[0] in _translations)
{
line[0] = _translations[line[0]];
}
auto lineTag = new Tag(null, null, line[0], null, null, null);
if (line[0] == "blendset" || line[0] == "paramset")
{
lineTag.values = [Value(line[1])];
foreach (setting; line[2 .. $].chunks(2))
{
auto settingTag = new Tag(null, null, setting[0], null, null, null);
if (line[0] == "blendset")
{
settingTag.values = [Value(setting[1].to!int)];
}
else
{
settingTag.values = [Value(setting[1])];
}
lineTag.add(settingTag);
}
}
else if (line[0] == "attachpoint")
{
lineTag.values = [Value(line[1])];
auto xTag = new Tag(null, null, "x", [Value(line[2])]);
auto yTag = new Tag(null, null, "y", [Value(line[3])]);
auto zTag = new Tag(null, null, "z", [Value(line[4])]);
auto rxTag = new Tag(null, null, "rx", [Value(line[5])]);
auto ryTag = new Tag(null, null, "ry", [Value(line[6])]);
auto rzTag = new Tag(null, null, "rz", [Value(line[7])]);
lineTag.add([xTag, yTag, zTag, rxTag, ryTag, rzTag]);
}
else if (line.length > 2)
{
foreach (item; line[1 .. $])
{
lineTag.add(Value(item));
}
}
else if (line.length == 2)
{
line[1] = line[1].replace("《改行》", "\n");
lineTag.values = [Value(line[1])];
}
dataTags ~= lineTag;
}
auto dataTag = new Tag(null, "data", null, null, dataTags);
root.add(dataTag);
return root.toSDLDocument();
2019-02-18 23:59:29 +01:00
}
}