Changeset - r26934:c3c7be10b12c
[Not reviewed]
master
0 4 0
glx22 - 22 months ago 2023-02-17 23:52:22
glx@openttd.org
Change: [Script] Extract params info from GS strings
4 files changed with 77 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/game/game_text.cpp
Show inline comments
 
@@ -16,12 +16,13 @@
 
#include "../strings_func.h"
 
#include "game_text.hpp"
 
#include "game.hpp"
 
#include "game_info.hpp"
 

	
 
#include "table/strings.h"
 
#include "table/strgen_tables.h"
 

	
 
#include <stdarg.h>
 
#include <memory>
 

	
 
#include "../safeguards.h"
 

	
 
@@ -270,22 +271,49 @@ GameStrings *LoadTranslations()
 
	} catch (...) {
 
		delete gs;
 
		return nullptr;
 
	}
 
}
 

	
 
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 &params)
 
{
 
	for (size_t i = 0; i < data.max_strings; i++) {
 
		const LangString *ls = data.strings[i];
 

	
 
		if (ls != nullptr) {
 
			StringParams &param = 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. */
 
void GameStrings::Compile()
 
{
 
	StringData data(32);
 
	StringListReader master_reader(data, this->raw_strings[0], true, false);
 
	master_reader.ParseFile();
 
	if (_errors != 0) throw std::exception();
 

	
 
	this->version = data.Version();
 

	
 
	ExtractStringParams(data, this->string_params);
 

	
 
	StringNameWriter id_writer(this->string_names);
 
	id_writer.WriteHeader(data);
 

	
 
	for (const auto &p : this->raw_strings) {
 
		data.FreeTranslation();
 
		StringListReader translation_reader(data, p, false, p.language != "english");
 
@@ -310,12 +338,26 @@ const char *GetGameStringPtr(uint id)
 
{
 
	if (id >= _current_data->cur_language->lines.size()) return GetStringPtr(STR_UNDEFINED);
 
	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.
 
 * @param engine The engine to update/
 
 */
 
void RegisterGameTranslation(Squirrel *engine)
 
{
 
	delete _current_data;
src/game/game_text.hpp
Show inline comments
 
@@ -9,13 +9,29 @@
 

	
 
#ifndef GAME_TEXT_HPP
 
#define GAME_TEXT_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 StringParams &GetGameStringParams(uint id);
 
void RegisterGameTranslation(class Squirrel *engine);
 
void ReconsiderGameScriptLanguage();
 

	
 
/** Container for the raw (unencoded) language strings of a language. */
 
struct LanguageStrings {
 
	std::string language; ///< Name of the language (base filename). Empty string if invalid.
 
@@ -34,12 +50,13 @@ struct GameStrings {
 
	uint version;                  ///< The version of the language strings.
 
	LanguageStrings *cur_language; ///< The current (compiled) 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!.
 
	StringList string_names;                       ///< The names of the compiled strings.
 
	StringParamsList string_params;                ///< The parameters for the strings.
 

	
 
	void Compile();
 

	
 
	GameStrings() = default;
 

	
 
	GameStrings(const GameStrings &) = delete;
src/strgen/strgen.h
Show inline comments
 
@@ -133,12 +133,28 @@ struct LanguageWriter {
 
	virtual ~LanguageWriter() {}
 

	
 
	virtual void WriteLength(uint length);
 
	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_error(const char *s, ...) WARN_FORMAT(1, 2);
 
void NORETURN CDECL strgen_fatal(const char *s, ...) WARN_FORMAT(1, 2);
 
char *ParseWord(char **buf);
 

	
 
extern const char *_file;
src/strgen/strgen_base.cpp
Show inline comments
 
@@ -214,23 +214,12 @@ uint StringData::CountInUse(uint tab) co
 
	for (i = TAB_SIZE; --i >= 0;) if (this->strings[(tab * TAB_SIZE) + i] != nullptr) break;
 
	return i + 1;
 
}
 

	
 
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. */
 
static ParsedCommandStruct _cur_pcs;
 
static int _cur_argidx;
 

	
 
/** The buffer for writing a single string. */
 
struct Buffer : std::vector<byte> {
 
@@ -591,13 +580,13 @@ StringReader::StringReader(StringData &d
 
/** Make sure the right reader gets freed. */
 
StringReader::~StringReader()
 
{
 
	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];
 
	int argno;
 
	int argidx = 0;
 
	int casei;
 

	
 
@@ -625,13 +614,13 @@ static void ExtractCommandString(ParsedC
 
			p->np++;
 
		}
 
	}
 
}
 

	
 

	
 
static const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
 
const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
 
{
 
	if (a == nullptr) return nullptr;
 

	
 
	if (strcmp(a->cmd, "STRING1") == 0 ||
 
			strcmp(a->cmd, "STRING2") == 0 ||
 
			strcmp(a->cmd, "STRING3") == 0 ||
0 comments (0 inline, 0 general)