Changeset - r27594:fd5f658fe30f
[Not reviewed]
master
0 5 0
Rubidium - 15 months ago 2023-06-15 15:09:34
rubidium@openttd.org
Codechange: move StringParameters to strings_internal.h
5 files changed with 125 insertions and 125 deletions:
0 comments (0 inline, 0 general)
src/industry_cmd.cpp
Show inline comments
 
@@ -4,49 +4,49 @@
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file industry_cmd.cpp Handling of industry tiles. */
 

	
 
#include "stdafx.h"
 
#include "clear_map.h"
 
#include "industry.h"
 
#include "station_base.h"
 
#include "landscape.h"
 
#include "viewport_func.h"
 
#include "command_func.h"
 
#include "town.h"
 
#include "news_func.h"
 
#include "cheat_type.h"
 
#include "company_base.h"
 
#include "genworld.h"
 
#include "tree_map.h"
 
#include "newgrf_cargo.h"
 
#include "newgrf_debug.h"
 
#include "newgrf_industrytiles.h"
 
#include "autoslope.h"
 
#include "water.h"
 
#include "strings_func.h"
 
#include "strings_internal.h"
 
#include "window_func.h"
 
#include "vehicle_func.h"
 
#include "sound_func.h"
 
#include "animated_tile_func.h"
 
#include "effectvehicle_func.h"
 
#include "effectvehicle_base.h"
 
#include "ai/ai.hpp"
 
#include "core/pool_func.hpp"
 
#include "subsidy_func.h"
 
#include "core/backup_type.hpp"
 
#include "object_base.h"
 
#include "game/game.hpp"
 
#include "error.h"
 
#include "string_func.h"
 
#include "industry_cmd.h"
 
#include "landscape_cmd.h"
 
#include "terraform_cmd.h"
 
#include "timer/timer.h"
 
#include "timer/timer_game_calendar.h"
 
#include "timer/timer_game_tick.h"
 

	
 
#include "table/strings.h"
 
#include "table/industry_land.h"
 
#include "table/build_industry.h"
src/newgrf_text.cpp
Show inline comments
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/**
 
 * @file newgrf_text.cpp
 
 * Implementation of  Action 04 "universal holder" structure and functions.
 
 * This file implements a linked-lists of strings,
 
 * holding everything that the newgrf action 04 will send over to OpenTTD.
 
 * One of the biggest problems is that Dynamic lang Array uses ISO codes
 
 * as way to identifying current user lang, while newgrf uses bit shift codes
 
 * not related to ISO.  So equivalence functionality had to be set.
 
 */
 

	
 
#include "stdafx.h"
 

	
 
#include "newgrf.h"
 
#include "strings_func.h"
 
#include "strings_internal.h"
 
#include "newgrf_storage.h"
 
#include "newgrf_text.h"
 
#include "newgrf_cargo.h"
 
#include "string_func.h"
 
#include "date_type.h"
 
#include "debug.h"
 
#include "core/alloc_type.hpp"
 
#include "language.h"
 
#include <sstream>
 

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

	
 
#include "safeguards.h"
 

	
 
/**
 
 * Explains the newgrf shift bit positioning.
 
 * the grf base will not be used in order to find the string, but rather for
 
 * jumping from standard langID scheme to the new one.
 
 */
 
enum GRFBaseLanguages {
 
	GRFLB_AMERICAN    = 0x01,
 
	GRFLB_ENGLISH     = 0x02,
 
	GRFLB_GERMAN      = 0x04,
src/station_cmd.cpp
Show inline comments
 
@@ -6,49 +6,49 @@
 
 */
 

	
 
/** @file station_cmd.cpp Handling of station tiles. */
 

	
 
#include "stdafx.h"
 
#include "aircraft.h"
 
#include "bridge_map.h"
 
#include "viewport_func.h"
 
#include "viewport_kdtree.h"
 
#include "command_func.h"
 
#include "town.h"
 
#include "news_func.h"
 
#include "train.h"
 
#include "ship.h"
 
#include "roadveh.h"
 
#include "industry.h"
 
#include "newgrf_cargo.h"
 
#include "newgrf_debug.h"
 
#include "newgrf_station.h"
 
#include "newgrf_canal.h" /* For the buoy */
 
#include "pathfinder/yapf/yapf_cache.h"
 
#include "road_internal.h" /* For drawing catenary/checking road removal */
 
#include "autoslope.h"
 
#include "water.h"
 
#include "strings_func.h"
 
#include "strings_internal.h"
 
#include "clear_func.h"
 
#include "timer/timer_game_calendar.h"
 
#include "vehicle_func.h"
 
#include "string_func.h"
 
#include "animated_tile_func.h"
 
#include "elrail_func.h"
 
#include "station_base.h"
 
#include "station_func.h"
 
#include "station_kdtree.h"
 
#include "roadstop_base.h"
 
#include "newgrf_railtype.h"
 
#include "newgrf_roadtype.h"
 
#include "waypoint_base.h"
 
#include "waypoint_func.h"
 
#include "pbs.h"
 
#include "debug.h"
 
#include "core/random_func.hpp"
 
#include "core/container_func.hpp"
 
#include "company_base.h"
 
#include "table/airporttile_ids.h"
 
#include "newgrf_airporttiles.h"
 
#include "order_backup.h"
 
#include "newgrf_house.h"
 
#include "company_gui.h"
src/strings_func.h
Show inline comments
 
@@ -37,171 +37,49 @@ static inline StringTab GetStringTab(Str
 
static inline uint GetStringIndex(StringID str)
 
{
 
	return str - (GetStringTab(str) << TAB_SIZE_BITS);
 
}
 

	
 
/**
 
 * Create a StringID
 
 * @param tab StringTab
 
 * @param index StringIndex
 
 * @return StringID composed from \a tab and \a index
 
 */
 
static inline StringID MakeStringID(StringTab tab, uint index)
 
{
 
	if (tab == TEXT_TAB_NEWGRF_START) {
 
		assert(index < TAB_SIZE_NEWGRF);
 
	} else if (tab == TEXT_TAB_GAMESCRIPT_START) {
 
		assert(index < TAB_SIZE_GAMESCRIPT);
 
	} else {
 
		assert(tab < TEXT_TAB_END);
 
		assert(index < TAB_SIZE);
 
	}
 
	return (tab << TAB_SIZE_BITS) + index;
 
}
 

	
 
class StringParameters {
 
	StringParameters *parent; ///< If not nullptr, this instance references data from this parent instance.
 
	uint64 *data;             ///< Array with the actual data.
 
	WChar *type;              ///< Array with type information about the data. Can be nullptr when no type information is needed. See #StringControlCode.
 

	
 
public:
 
	uint offset;              ///< Current offset in the data/type arrays.
 
	uint num_param;           ///< Length of the data array.
 
	WChar next_type = 0; ///< The type of the next data that is retrieved.
 

	
 
	/** Create a new StringParameters instance. */
 
	StringParameters(uint64 *data, uint num_param, WChar *type) :
 
		parent(nullptr),
 
		data(data),
 
		type(type),
 
		offset(0),
 
		num_param(num_param)
 
	{ }
 

	
 
	/** Create a new StringParameters instance. */
 
	template <size_t Tnum_param>
 
	StringParameters(int64 (&data)[Tnum_param]) :
 
		parent(nullptr),
 
		data((uint64 *)data),
 
		type(nullptr),
 
		offset(0),
 
		num_param(Tnum_param)
 
	{
 
		static_assert(sizeof(data[0]) == sizeof(uint64));
 
	}
 

	
 
	/**
 
	 * Create a new StringParameters instance that can reference part of the data of
 
	 * the given partent instance.
 
	 */
 
	StringParameters(StringParameters &parent, uint size) :
 
		parent(&parent),
 
		data(parent.data + parent.offset),
 
		offset(0),
 
		num_param(size)
 
	{
 
		assert(size <= parent.GetDataLeft());
 
		if (parent.type == nullptr) {
 
			this->type = nullptr;
 
		} else {
 
			this->type = parent.type + parent.offset;
 
		}
 
	}
 

	
 
	~StringParameters()
 
	{
 
		if (this->parent != nullptr) {
 
			this->parent->offset += this->num_param;
 
		}
 
	}
 

	
 
	void ClearTypeInformation();
 

	
 
	int64 GetInt64();
 

	
 
	/** Read an int32 from the argument array. @see GetInt64. */
 
	int32 GetInt32()
 
	{
 
		return (int32)this->GetInt64();
 
	}
 

	
 
	/**
 
	 * Get a new instance of StringParameters that is a "range" into the
 
	 * parameters existing parameters. Upon destruction the offset in the parent
 
	 * is not updated. However, calls to SetDParam do update the parameters.
 
	 *
 
	 * The returned StringParameters must not outlive this StringParameters.
 
	 * @return A "range" of the string parameters.
 
	 */
 
	StringParameters GetRemainingParameters()
 
	{
 
		return StringParameters(&this->data[this->offset], GetDataLeft(),
 
			this->type == nullptr ? nullptr : &this->type[this->offset]);
 
	}
 

	
 
	/** Return the amount of elements which can still be read. */
 
	uint GetDataLeft() const
 
	{
 
		return this->num_param - this->offset;
 
	}
 

	
 
	/** Get a pointer to a specific element in the data array. */
 
	uint64 *GetPointerToOffset(uint offset) const
 
	{
 
		assert(offset < this->num_param);
 
		return &this->data[offset];
 
	}
 

	
 
	/** Does this instance store information about the type of the parameters. */
 
	bool HasTypeInformation() const
 
	{
 
		return this->type != nullptr;
 
	}
 

	
 
	/** Get the type of a specific element. */
 
	WChar GetTypeAtOffset(uint offset) const
 
	{
 
		assert(offset < this->num_param);
 
		assert(this->HasTypeInformation());
 
		return this->type[offset];
 
	}
 

	
 
	void SetParam(uint n, uint64 v)
 
	{
 
		assert(n < this->num_param);
 
		this->data[n] = v;
 
	}
 

	
 
	uint64 GetParam(uint n) const
 
	{
 
		assert(n < this->num_param);
 
		return this->data[n];
 
	}
 
};
 
extern StringParameters _global_string_params;
 

	
 
std::string GetString(StringID string);
 
std::string GetStringWithArgs(StringID string, StringParameters &args);
 
const char *GetStringPtr(StringID string);
 

	
 
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type);
 
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type);
 

	
 
/**
 
 * Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
 
 * @param speed Display speed for parameter.
 
 * @param type Type of vehicle for parameter.
 
 * @return Bit-packed velocity and vehicle type, for use with SetDParam().
 
 */
 
static inline int64 PackVelocity(uint speed, VehicleType type)
 
{
 
	/* Vehicle type is a byte, so packed into the top 8 bits of the 64-bit
 
	 * parameter, although only values from 0-3 are relevant. */
 
	return speed | (static_cast<uint64>(type) << 56);
 
}
 

	
 
void SetDParam(uint n, uint64_t v);
 
void SetDParamMaxValue(uint n, uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
 
void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
 

	
 
void SetDParamStr(uint n, const char *str);
 
void SetDParamStr(uint n, const std::string &str);
src/strings_internal.h
Show inline comments
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file strings_interal.h Types and functions related to the internal workings of formatting OpenTTD's strings. */
 

	
 
#ifndef STRINGS_INTERNAL_H
 
#define STRINGS_INTERNAL_H
 

	
 
#include "strings_func.h"
 
#include "string_func.h"
 

	
 
class StringParameters {
 
	StringParameters *parent; ///< If not nullptr, this instance references data from this parent instance.
 
	uint64 *data;             ///< Array with the actual data.
 
	WChar *type;              ///< Array with type information about the data. Can be nullptr when no type information is needed. See #StringControlCode.
 

	
 
public:
 
	uint offset;              ///< Current offset in the data/type arrays.
 
	uint num_param;           ///< Length of the data array.
 
	WChar next_type = 0; ///< The type of the next data that is retrieved.
 

	
 
	/** Create a new StringParameters instance. */
 
	StringParameters(uint64 *data, uint num_param, WChar *type) :
 
		parent(nullptr),
 
		data(data),
 
		type(type),
 
		offset(0),
 
		num_param(num_param)
 
	{ }
 

	
 
	/** Create a new StringParameters instance. */
 
	template <size_t Tnum_param>
 
	StringParameters(int64 (&data)[Tnum_param]) :
 
		parent(nullptr),
 
		data((uint64 *)data),
 
		type(nullptr),
 
		offset(0),
 
		num_param(Tnum_param)
 
	{
 
		static_assert(sizeof(data[0]) == sizeof(uint64));
 
	}
 

	
 
	/**
 
	 * Create a new StringParameters instance that can reference part of the data of
 
	 * the given partent instance.
 
	 */
 
	StringParameters(StringParameters &parent, uint size) :
 
		parent(&parent),
 
		data(parent.data + parent.offset),
 
		offset(0),
 
		num_param(size)
 
	{
 
		assert(size <= parent.GetDataLeft());
 
		if (parent.type == nullptr) {
 
			this->type = nullptr;
 
		} else {
 
			this->type = parent.type + parent.offset;
 
		}
 
	}
 

	
 
	~StringParameters()
 
	{
 
		if (this->parent != nullptr) {
 
			this->parent->offset += this->num_param;
 
		}
 
	}
 

	
 
	void ClearTypeInformation();
 

	
 
	int64 GetInt64();
 

	
 
	/** Read an int32 from the argument array. @see GetInt64. */
 
	int32 GetInt32()
 
	{
 
		return (int32)this->GetInt64();
 
	}
 

	
 
	/**
 
	 * Get a new instance of StringParameters that is a "range" into the
 
	 * parameters existing parameters. Upon destruction the offset in the parent
 
	 * is not updated. However, calls to SetDParam do update the parameters.
 
	 *
 
	 * The returned StringParameters must not outlive this StringParameters.
 
	 * @return A "range" of the string parameters.
 
	 */
 
	StringParameters GetRemainingParameters()
 
	{
 
		return StringParameters(&this->data[this->offset], GetDataLeft(),
 
			this->type == nullptr ? nullptr : &this->type[this->offset]);
 
	}
 

	
 
	/** Return the amount of elements which can still be read. */
 
	uint GetDataLeft() const
 
	{
 
		return this->num_param - this->offset;
 
	}
 

	
 
	/** Get a pointer to a specific element in the data array. */
 
	uint64 *GetPointerToOffset(uint offset) const
 
	{
 
		assert(offset < this->num_param);
 
		return &this->data[offset];
 
	}
 

	
 
	/** Does this instance store information about the type of the parameters. */
 
	bool HasTypeInformation() const
 
	{
 
		return this->type != nullptr;
 
	}
 

	
 
	/** Get the type of a specific element. */
 
	WChar GetTypeAtOffset(uint offset) const
 
	{
 
		assert(offset < this->num_param);
 
		assert(this->HasTypeInformation());
 
		return this->type[offset];
 
	}
 

	
 
	void SetParam(uint n, uint64 v)
 
	{
 
		assert(n < this->num_param);
 
		this->data[n] = v;
 
	}
 

	
 
	uint64 GetParam(uint n) const
 
	{
 
		assert(n < this->num_param);
 
		return this->data[n];
 
	}
 
};
 

	
 
/**
 
 * Equivalent to the std::back_insert_iterator in function, with some
 
 * convenience helpers for string concatenation.
 
 */
 
class StringBuilder {
 
	std::string *string;
 

	
 
public:
 
	/* Required type for this to be an output_iterator; mimics std::back_insert_iterator. */
 
	using value_type = void;
 
	using difference_type = void;
 
	using iterator_category = std::output_iterator_tag;
 
	using pointer = void;
 
	using reference = void;
 

	
 
	/**
 
	 * Create the builder of an external buffer.
 
	 * @param start The start location to write to.
 
	 * @param last  The last location to write to.
 
	 */
 
	StringBuilder(std::string &string) : string(&string) {}
 

	
 
	/* Required operators for this to be an output_iterator; mimics std::back_insert_iterator, which has no-ops. */
 
@@ -92,33 +213,34 @@ public:
 
	{
 
		this->string->erase(this->string->size() - std::min(amount, this->string->size()));
 
	}
 

	
 
	/**
 
	 * Get the current index in the string.
 
	 * @return The index.
 
	 */
 
	size_t CurrentIndex()
 
	{
 
		return this->string->size();
 
	}
 

	
 
	/**
 
	 * Get the reference to the character at the given index.
 
	 * @return The reference to the character.
 
	 */
 
	char &operator[](size_t index)
 
	{
 
		return (*this->string)[index];
 
	}
 
};
 

	
 
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index = 0, bool game_script = false);
 
std::string GetStringWithArgs(StringID string, StringParameters &args);
 

	
 
/* Do not leak the StringBuilder to everywhere. */
 
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed);
 
void GetTownName(StringBuilder &builder, const struct Town *t);
 
void GRFTownNameGenerate(StringBuilder &builder, uint32 grfid, uint16 gen, uint32 seed);
 

	
 
uint RemapNewGRFStringControlCode(uint scc, const char **str, StringParameters &parameters, bool modify_parameters);
 

	
 
#endif /* STRINGS_INTERNAL_H */
0 comments (0 inline, 0 general)