1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "gfCCatalogueParserVarTable.h"

#include <fsCore/fsIFile.h>
#include <fsCore/src/fsCVariableTable.h>
#include <fsCore/src/debug/fsAssert.h>

#include "gfCCatalogueCatalogue.h"
#include "fsAppCore/src/fsCLocalisedStrings.h"

gfCCatalogueCatalogue* const gfCCatalogueParserVarTable::catalogueLoad(const fsStr& pFileName)
{
	auto catalogue = new gfCCatalogueCatalogue();
	fsCVariableTable* varTable = new fsCVariableTable();
	varTable->load(fsCResourceName(pFileName, fsCResourceName::eRES_LOCATION_ASSETS));

	SSettings settings;

	graphicFileBasesGet(varTable, settings);
	entriesCreate(catalogue, "", varTable, settings);

	delete varTable;
	return catalogue;
}

void gfCCatalogueParserVarTable::graphicFileBasesGet(fsCVariableTable* const pVarTable, SSettings& pSettings)
{
	pSettings.mIconFileBase = pVarTable->strGetWithDefault("iconFilesBase", "");
	pSettings.mImageFileBase = pVarTable->strGetWithDefault("largeImageFilesBase", "");
	pSettings.mDefaultAction = pVarTable->strGetWithDefault("defaultPurchaseAction", "");
}

void gfCCatalogueParserVarTable::entriesCreate(gfCCatalogueCatalogue* pCatalogue, const fsStr& pParentCategory,
                                               fsCVariableTable* pVarTable, const SSettings& pSettings)
{
	fsS32 index = 0;
	fsStr entryType;
	fsStr key;
	do
	{
		key = "entry" + fsStr(index) + "type";
		entryType = pVarTable->strGetWithDefault(key, "item");
		if (entryType == "category")
		{
			key = "entry" + fsStr(index) + "categoryFile";

			const fsStr categoryFile = pVarTable->strGet(key);
			categoryLoad(pCatalogue, pParentCategory, categoryFile);
		}
		else if (entryType == "item")
		{
			if (!itemCreate(pCatalogue, pParentCategory, pVarTable, pSettings, index))
			{
				entryType = "";
			}
		}
		else
		{
			fsAssert(entryType.emptyGet(), "Unknown entry type: ", entryType.utf8Get());
		}
		++index;
	}
	while (!entryType.emptyGet());
}

void gfCCatalogueParserVarTable::categoryLoad(gfCCatalogueCatalogue* pCatalogue, const fsStr& pParentCategory,
                                              const fsStr& pFileName)
{
	fsCVariableTable* varTable = new fsCVariableTable();
	varTable->load(fsCResourceName(pFileName, fsCResourceName::eRES_LOCATION_ASSETS));

	const fsStr categoryName = varTable->strGet("categoryName");
	SSettings settings;
	graphicFileBasesGet(varTable, settings);

	fsStr textKey("shopCat_");
	textKey += categoryName;
	fsStr iconFileName;
	iconFileName = extensionAdd(settings.mIconFileBase + categoryName);
	pCatalogue->categoryAdd(pParentCategory, categoryName, textKey, iconFileName);

	entriesCreate(pCatalogue, categoryName, varTable, settings);
	delete varTable;
}

fsStr gfCCatalogueParserVarTable::extensionAdd(const fsStr& pFilename)
{
	if (fsIFile::fileExistsGet(fsCResourceName(pFilename + ".png", fsCResourceName::eRES_LOCATION_ASSETS)))
	{
		return pFilename + ".png";
	}
	return pFilename + ".jpg";
}

fsBool gfCCatalogueParserVarTable::itemCreate(gfCCatalogueCatalogue* pCatalogue, const fsStr& pParentCategory,<--- Parameter 'pCatalogue' can be declared as pointer to const
                                              fsCVariableTable* pVarTable, const SSettings& pSettings, fsS32 pIndex)
{
	fsStr keyBase;
	keyBase = "entry" + fsStr(pIndex);
	const fsStr itemName = pVarTable->strGet(keyBase + "itemName");
	if (itemName.emptyGet()) {
		return false;
	}

	const fsStr itemCost = pVarTable->strGet(keyBase + "itemCost");
	const fsStr purchaseAction = pVarTable->strGetWithDefault(keyBase + "purchaseAction", pSettings.mDefaultAction);
	const fsStr purchaseParam1 = pVarTable->strGet(keyBase + "purchaseParam1");
	const fsStr purchaseParam2 = pVarTable->strGet(keyBase + "purchaseParam2");

	std::map<fsStr, fsStr> params;
	params["itemName"] = itemName;
	params["itemCost"] = itemCost;
	params["purchaseAction"] = purchaseAction;

	fsS32 paramIndex = 1;
	fsStr key = keyBase + "purchaseParam" + fsStr(paramIndex);
	fsStr param = pVarTable->strGet(key);
	while(param != "")
	{
		params["purchaseParam" + fsStr(paramIndex)] = pVarTable->strGetWithDefault(key, param);
		key = keyBase + "purchaseParam" + fsStr(++paramIndex);
		param = pVarTable->strGet(key);
	}

	const auto makeLocalisedKey =
		[&](const char* prefix, const char* legacyPrefix)
		{
			fsStr key = fsStr(pParentCategory + prefix + itemName);
			fsStr unused;

			if (!fsCLocalisedStrings::localisedStringGetIfExists(key, unused))
			{
				key = fsStr(pParentCategory + legacyPrefix + itemName);
			}

			return key;
		};

	params["itemNameKey"] = makeLocalisedKey("item_", "Item_");
	params["descKey"] = makeLocalisedKey("desc_", "Desc_");
	params["iconFileName"] = extensionAdd(pSettings.mIconFileBase + itemName);
	params["imageFileName"] = extensionAdd(pSettings.mImageFileBase + itemName);

	pCatalogue->itemAddToCategory(pParentCategory, params);
	return true;
}

gfCCatalogueParserVarTable::gfCCatalogueParserVarTable()
{
}

gfCCatalogueParserVarTable::~gfCCatalogueParserVarTable()
{
}