mirror of https://github.com/OpenTTD/OpenTTD
Change: [Script] Extract params info from GS strings
parent
6e52ceab96
commit
af15dca316
|
@ -19,6 +19,7 @@
|
||||||
#include "game_info.hpp"
|
#include "game_info.hpp"
|
||||||
|
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
#include "table/strgen_tables.h"
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
@ -273,6 +274,31 @@ GameStrings *LoadTranslations()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static StringParam::ParamType GetParamType(const CmdStruct *cs)
|
||||||
|
{
|
||||||
|
if (cs->value == SCC_RAW_STRING_POINTER) return StringParam::RAW_STRING;
|
||||||
|
if (cs->value == SCC_STRING || cs != TranslateCmdForCompare(cs)) return StringParam::STRING;
|
||||||
|
return StringParam::OTHER;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ExtractStringParams(const StringData &data, StringParamsList ¶ms)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < data.max_strings; i++) {
|
||||||
|
const LangString *ls = data.strings[i];
|
||||||
|
|
||||||
|
if (ls != nullptr) {
|
||||||
|
StringParams ¶m = params.emplace_back();
|
||||||
|
ParsedCommandStruct pcs;
|
||||||
|
ExtractCommandString(&pcs, ls->english, false);
|
||||||
|
|
||||||
|
for (const CmdStruct *cs : pcs.cmd) {
|
||||||
|
if (cs == nullptr) break;
|
||||||
|
param.emplace_back(GetParamType(cs), cs->consumes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Compile the language. */
|
/** Compile the language. */
|
||||||
void GameStrings::Compile()
|
void GameStrings::Compile()
|
||||||
{
|
{
|
||||||
|
@ -283,6 +309,8 @@ void GameStrings::Compile()
|
||||||
|
|
||||||
this->version = data.Version();
|
this->version = data.Version();
|
||||||
|
|
||||||
|
ExtractStringParams(data, this->string_params);
|
||||||
|
|
||||||
StringNameWriter id_writer(this->string_names);
|
StringNameWriter id_writer(this->string_names);
|
||||||
id_writer.WriteHeader(data);
|
id_writer.WriteHeader(data);
|
||||||
|
|
||||||
|
@ -312,6 +340,20 @@ const char *GetGameStringPtr(uint id)
|
||||||
return _current_data->cur_language->lines[id].c_str();
|
return _current_data->cur_language->lines[id].c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the string parameters of a particular game string.
|
||||||
|
* @param id The ID of the game string.
|
||||||
|
* @return The string parameters.
|
||||||
|
*/
|
||||||
|
const StringParams &GetGameStringParams(uint id)
|
||||||
|
{
|
||||||
|
/* An empty result for STR_UNDEFINED. */
|
||||||
|
static StringParams empty;
|
||||||
|
|
||||||
|
if (id >= _current_data->string_params.size()) return empty;
|
||||||
|
return _current_data->string_params[id];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the current translation to the Squirrel engine.
|
* Register the current translation to the Squirrel engine.
|
||||||
* @param engine The engine to update/
|
* @param engine The engine to update/
|
||||||
|
|
|
@ -12,7 +12,23 @@
|
||||||
|
|
||||||
#include "../core/smallvec_type.hpp"
|
#include "../core/smallvec_type.hpp"
|
||||||
|
|
||||||
|
struct StringParam {
|
||||||
|
enum ParamType {
|
||||||
|
RAW_STRING,
|
||||||
|
STRING,
|
||||||
|
OTHER
|
||||||
|
};
|
||||||
|
|
||||||
|
ParamType type;
|
||||||
|
uint8 consumes;
|
||||||
|
|
||||||
|
StringParam(ParamType type, uint8 consumes) : type(type), consumes(consumes) {}
|
||||||
|
};
|
||||||
|
using StringParams = std::vector<StringParam>;
|
||||||
|
using StringParamsList = std::vector<StringParams>;
|
||||||
|
|
||||||
const char *GetGameStringPtr(uint id);
|
const char *GetGameStringPtr(uint id);
|
||||||
|
const StringParams &GetGameStringParams(uint id);
|
||||||
void RegisterGameTranslation(class Squirrel *engine);
|
void RegisterGameTranslation(class Squirrel *engine);
|
||||||
void ReconsiderGameScriptLanguage();
|
void ReconsiderGameScriptLanguage();
|
||||||
|
|
||||||
|
@ -37,6 +53,7 @@ struct GameStrings {
|
||||||
std::vector<LanguageStrings> raw_strings; ///< The raw strings per language, first must be English/the master language!.
|
std::vector<LanguageStrings> raw_strings; ///< The raw strings per language, first must be English/the master language!.
|
||||||
std::vector<LanguageStrings> compiled_strings; ///< The compiled strings per language, first must be English/the master language!.
|
std::vector<LanguageStrings> compiled_strings; ///< The compiled strings per language, first must be English/the master language!.
|
||||||
StringList string_names; ///< The names of the compiled strings.
|
StringList string_names; ///< The names of the compiled strings.
|
||||||
|
StringParamsList string_params; ///< The parameters for the strings.
|
||||||
|
|
||||||
void Compile();
|
void Compile();
|
||||||
|
|
||||||
|
|
|
@ -136,6 +136,22 @@ struct LanguageWriter {
|
||||||
virtual void WriteLang(const StringData &data);
|
virtual void WriteLang(const StringData &data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct CmdStruct;
|
||||||
|
|
||||||
|
struct CmdPair {
|
||||||
|
const CmdStruct *a;
|
||||||
|
const char *v;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ParsedCommandStruct {
|
||||||
|
uint np;
|
||||||
|
CmdPair pairs[32];
|
||||||
|
const CmdStruct *cmd[32]; // ordered by param #
|
||||||
|
};
|
||||||
|
|
||||||
|
const CmdStruct *TranslateCmdForCompare(const CmdStruct *a);
|
||||||
|
void ExtractCommandString(ParsedCommandStruct *p, const char *s, bool warnings);
|
||||||
|
|
||||||
void CDECL strgen_warning(const char *s, ...) WARN_FORMAT(1, 2);
|
void CDECL strgen_warning(const char *s, ...) WARN_FORMAT(1, 2);
|
||||||
void CDECL strgen_error(const char *s, ...) WARN_FORMAT(1, 2);
|
void CDECL strgen_error(const char *s, ...) WARN_FORMAT(1, 2);
|
||||||
void NORETURN CDECL strgen_fatal(const char *s, ...) WARN_FORMAT(1, 2);
|
void NORETURN CDECL strgen_fatal(const char *s, ...) WARN_FORMAT(1, 2);
|
||||||
|
|
|
@ -217,17 +217,6 @@ uint StringData::CountInUse(uint tab) const
|
||||||
|
|
||||||
static const char *_cur_ident;
|
static const char *_cur_ident;
|
||||||
|
|
||||||
struct CmdPair {
|
|
||||||
const CmdStruct *a;
|
|
||||||
const char *v;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ParsedCommandStruct {
|
|
||||||
uint np;
|
|
||||||
CmdPair pairs[32];
|
|
||||||
const CmdStruct *cmd[32]; // ordered by param #
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Used when generating some advanced commands. */
|
/* Used when generating some advanced commands. */
|
||||||
static ParsedCommandStruct _cur_pcs;
|
static ParsedCommandStruct _cur_pcs;
|
||||||
static int _cur_argidx;
|
static int _cur_argidx;
|
||||||
|
@ -594,7 +583,7 @@ StringReader::~StringReader()
|
||||||
free(file);
|
free(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ExtractCommandString(ParsedCommandStruct *p, const char *s, bool warnings)
|
void ExtractCommandString(ParsedCommandStruct *p, const char *s, bool warnings)
|
||||||
{
|
{
|
||||||
char param[MAX_COMMAND_PARAM_SIZE];
|
char param[MAX_COMMAND_PARAM_SIZE];
|
||||||
int argno;
|
int argno;
|
||||||
|
@ -628,7 +617,7 @@ static void ExtractCommandString(ParsedCommandStruct *p, const char *s, bool war
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
|
const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
|
||||||
{
|
{
|
||||||
if (a == nullptr) return nullptr;
|
if (a == nullptr) return nullptr;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue