From d39cdb9468b79dc6cf698afa7c5b9be4297b99e0 Mon Sep 17 00:00:00 2001 From: oy Date: Mon, 17 Dec 2012 00:05:43 +0100 Subject: [PATCH] use json format within the language index file --- bam.lua | 3 +- data/languages | 2 +- src/game/client/components/chat.cpp | 2 +- src/game/client/components/menus.h | 2 +- src/game/client/components/menus_settings.cpp | 84 ++++++++----------- src/game/client/components/scoreboard.cpp | 2 +- src/game/client/gameclient.cpp | 2 +- src/game/{ => client}/localization.cpp | 0 src/game/{ => client}/localization.h | 0 src/game/editor/editor.cpp | 2 +- src/game/editor/layer_quads.cpp | 3 +- src/game/editor/layer_tiles.cpp | 2 +- 12 files changed, 44 insertions(+), 60 deletions(-) rename src/game/{ => client}/localization.cpp (100%) rename src/game/{ => client}/localization.h (100%) diff --git a/bam.lua b/bam.lua index 11ac7b9e5..2fbd6b42d 100644 --- a/bam.lua +++ b/bam.lua @@ -201,6 +201,7 @@ function build(settings) -- build the small libraries wavpack = Compile(settings, Collect("src/engine/external/wavpack/*.c")) pnglite = Compile(settings, Collect("src/engine/external/pnglite/*.c")) + jsonparser = Compile(settings, Collect("src/engine/external/json-parser/*.c")) -- build game components engine_settings = settings:Copy() @@ -261,7 +262,7 @@ function build(settings) -- build client, server, version server and master server client_exe = Link(client_settings, "teeworlds", game_shared, game_client, - engine, client, game_editor, zlib, pnglite, wavpack, + engine, client, game_editor, zlib, pnglite, wavpack, jsonparser, client_link_other, client_osxlaunch) server_exe = Link(server_settings, "teeworlds_srv", engine, server, diff --git a/data/languages b/data/languages index 1de429e6c..928f0f47c 160000 --- a/data/languages +++ b/data/languages @@ -1 +1 @@ -Subproject commit 1de429e6c3de5a27f19affe0f86f3857d8c1af20 +Subproject commit 928f0f47c0ffb1f6fea9d13254da42c250a825af diff --git a/src/game/client/components/chat.cpp b/src/game/client/components/chat.cpp index fe3ec8b71..fee573c6b 100644 --- a/src/game/client/components/chat.cpp +++ b/src/game/client/components/chat.cpp @@ -11,10 +11,10 @@ #include #include +#include #include #include -#include #include "menus.h" #include "chat.h" diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index 027489a26..1bce61c88 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -11,8 +11,8 @@ #include #include -#include #include +#include #include diff --git a/src/game/client/components/menus_settings.cpp b/src/game/client/components/menus_settings.cpp index 768f0daab..cf4b93d7a 100644 --- a/src/game/client/components/menus_settings.cpp +++ b/src/game/client/components/menus_settings.cpp @@ -7,8 +7,8 @@ #include #include #include +#include #include -#include #include #include @@ -477,62 +477,44 @@ public: void LoadLanguageIndexfile(IStorage *pStorage, IConsole *pConsole, sorted_array *pLanguages) { - IOHANDLE File = pStorage->OpenFile("languages/index.txt", IOFLAG_READ, IStorage::TYPE_ALL); + // read file data into buffer + IOHANDLE File = pStorage->OpenFile("languages/index.json", IOFLAG_READ, IStorage::TYPE_ALL); if(!File) { pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", "couldn't open index file"); return; } - - char aOrigin[128]; - char aReplacement[128]; - CLineReader LineReader; - LineReader.Init(File); - char *pLine; - while((pLine = LineReader.Get())) - { - if(!str_length(pLine) || pLine[0] == '#') // skip empty lines and comments - continue; - - str_copy(aOrigin, pLine, sizeof(aOrigin)); - - pLine = LineReader.Get(); - if(!pLine) - { - pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", "unexpected end of index file"); - break; - } - - if(pLine[0] != '=' || pLine[1] != '=' || pLine[2] != ' ') - { - char aBuf[128]; - str_format(aBuf, sizeof(aBuf), "malform replacement for index '%s'", aOrigin); - pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf); - (void)LineReader.Get(); - continue; - } - str_copy(aReplacement, pLine+3, sizeof(aReplacement)); - - pLine = LineReader.Get(); - if(!pLine) - { - pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", "unexpected end of index file"); - break; - } - - if(pLine[0] != '=' || pLine[1] != '=' || pLine[2] != ' ') - { - char aBuf[128]; - str_format(aBuf, sizeof(aBuf), "malform replacement for index '%s'", aOrigin); - pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localization", aBuf); - continue; - } - - char aFileName[128]; - str_format(aFileName, sizeof(aFileName), "languages/%s.txt", aOrigin); - pLanguages->add(CLanguage(aReplacement, aFileName, str_toint(pLine+3))); - } + int FileSize = (int)io_length(File); + char *pFileData = (char *)mem_alloc(FileSize+1, 1); + io_read(File, pFileData, FileSize); + pFileData[FileSize] = 0; io_close(File); + + // parse json data + json_settings JsonSettings; + mem_zero(&JsonSettings, sizeof(JsonSettings)); + char aError[256]; + json_value *pJsonData = json_parse_ex(&JsonSettings, pFileData, aError); + if(pJsonData == 0) + { + pConsole->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "localizations", aError); + return; + } + + // extract data + const json_value &rStart = (*pJsonData)["language indices"]; + if(rStart.type == json_array) + { + for(int i = 0; i < rStart.u.array.length; ++i) + { + char aFileName[128]; + str_format(aFileName, sizeof(aFileName), "languages/%s.txt", (const char *)rStart[i]["file"]); + pLanguages->add(CLanguage((const char *)rStart[i]["name"], aFileName, (long)rStart[i]["code"])); + } + } + + // clean up + json_value_free(pJsonData); } void CMenus::RenderLanguageSelection(CUIRect MainView, bool Header) diff --git a/src/game/client/components/scoreboard.cpp b/src/game/client/components/scoreboard.cpp index 98b52db4e..f13fd879d 100644 --- a/src/game/client/components/scoreboard.cpp +++ b/src/game/client/components/scoreboard.cpp @@ -8,9 +8,9 @@ #include #include -#include #include #include +#include #include #include #include diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index a7ca2d2dc..c088e16f0 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -16,8 +16,8 @@ #include #include -#include #include +#include "localization.h" #include "render.h" #include "gameclient.h" diff --git a/src/game/localization.cpp b/src/game/client/localization.cpp similarity index 100% rename from src/game/localization.cpp rename to src/game/client/localization.cpp diff --git a/src/game/localization.h b/src/game/client/localization.h similarity index 100% rename from src/game/localization.h rename to src/game/client/localization.h diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 9dd69aa9a..c1f964db4 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -14,8 +14,8 @@ #include #include -#include #include +#include #include #include #include diff --git a/src/game/editor/layer_quads.cpp b/src/game/editor/layer_quads.cpp index b033aa1d3..ec5e01573 100644 --- a/src/game/editor/layer_quads.cpp +++ b/src/game/editor/layer_quads.cpp @@ -7,8 +7,9 @@ #include "editor.h" #include +#include #include -#include + CLayerQuads::CLayerQuads() { diff --git a/src/game/editor/layer_tiles.cpp b/src/game/editor/layer_tiles.cpp index 4ce4e2b00..11e146fd3 100644 --- a/src/game/editor/layer_tiles.cpp +++ b/src/game/editor/layer_tiles.cpp @@ -7,10 +7,10 @@ #include #include +#include #include #include "editor.h" -#include CLayerTiles::CLayerTiles(int w, int h) {