aboutsummaryrefslogtreecommitdiff
path: root/src/unittest/test_map_settings_manager.cpp
blob: 4f5ac80f2a08cd15d24b5180b6b90e1f280bf416 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 /*
Minetest
Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "test.h"

#include "noise.h"
#include "settings.h"
#include "mapgen_v5.h"
#include "util/sha1.h"
#include "map_settings_manager.h"

class TestMapSettingsManager : public TestBase {
public:
	TestMapSettingsManager() { TestManager::registerTestModule(this); }
	const char *getName() { return "TestMapSettingsManager"; }

	void makeUserConfig(Settings *conf);
	std::string makeMetaFile(bool make_corrupt);

	void runTests(IGameDef *gamedef);

	void testMapSettingsManager();
	void testMapMetaSaveLoad();
	void testMapMetaFailures();
};

static TestMapSettingsManager g_test_instance;

void TestMapSettingsManager::runTests(IGameDef *gamedef)
{
	TEST(testMapSettingsManager);
	TEST(testMapMetaSaveLoad);
	TEST(testMapMetaFailures);
}

////////////////////////////////////////////////////////////////////////////////


void check_noise_params(const NoiseParams *np1, const NoiseParams *np2)
{
	UASSERTEQ(float, np1->offset, np2->offset);
	UASSERTEQ(float, np1->scale, np2->scale);
	UASSERT(np1->spread == np2->spread);
	UASSERTEQ(s32, np1->seed, np2->seed);
	UASSERTEQ(u16, np1->octaves, np2->octaves);
	UASSERTEQ(float, np1->persist, np2->persist);
	UASSERTEQ(float, np1->lacunarity, np2->lacunarity);
	UASSERTEQ(u32, np1->flags, np2->flags);
}


std::string read_file_to_string(const std::string &filepath)
{
	std::string buf;
	FILE *f = fopen(filepath.c_str(), "rb");
	if (!f)
		return "";

	fseek(f, 0, SEEK_END);

	long filesize = ftell(f);
	if (filesize == -1) {
		fclose(f);
		return "";
	}
	rewind(f);

	buf.resize(filesize);

	UASSERTEQ(size_t, fread(&buf[0], 1, filesize, f), 1);

	fclose(f);
	return buf;
}


void TestMapSettingsManager::makeUserConfig(Settings *conf)
{
	conf->set("mg_name", "v7");
	conf->set("seed", "5678");
	conf->set("water_level", "20");
	conf->set("mgv5_np_factor", "0, 12,  (500, 250, 500), 920382, 5, 0.45, 3.0");
	conf->set("mgv5_np_height", "0, 15, (500, 250, 500), 841746,  5, 0.5,  3.0");
	conf->set("mgv5_np_filler_depth", "20, 1, (150, 150, 150), 261, 4, 0.7,  1.0");
	conf->set("mgv5_np_ground", "-43, 40, (80,  80,  80),  983240, 4, 0.55, 2.0");
}


std::string TestMapSettingsManager::makeMetaFile(bool make_corrupt)
{
	std::string metafile = getTestTempFile();

	const char *metafile_contents =
		"mg_name = v5\n"
		"seed = 1234\n"
		"mg_flags = light\n"
		"mgv5_np_filler_depth = 20, 1, (150, 150, 150), 261, 4, 0.7,  1.0\n"
		"mgv5_np_height = 20, 10, (250, 250, 250), 84174,  4, 0.5,  1.0\n";

	FILE *f = fopen(metafile.c_str(), "wb");
	UASSERT(f != NULL);

	fputs(metafile_contents, f);
	if (!make_corrupt)
		fputs("[end_of_params]\n", f);

	fclose(f);

	return metafile;
}


void TestMapSettingsManager::testMapSettingsManager()
{
	Settings user_settings;
	makeUserConfig(&user_settings);

	std::string test_mapmeta_path = makeMetaFile(false);

	MapSettingsManager mgr(&user_settings, test_mapmeta_path);
	std::string value;

	UASSERT(mgr.getMapSetting("mg_name", &value));
	UASSERT(value == "v7");

	// Pretend we're initializing the ServerMap
	UASSERT(mgr.loadMapMeta());

	// Pretend some scripts are requesting mapgen params
	UASSERT(mgr.getMapSetting("mg_name", &value));
	UASSERT(value == "v5");
	UASSERT(mgr.getMapSetting("seed", &value));
	UASSERT(value == "1234");
	UASSERT(mgr.getMapSetting("water_level", &value));
	UASSERT(value == "20");

    // Pretend we have some mapgen settings configured from the scripting
	UASSERT(mgr.setMapSetting("water_level", "15"));
	UASSERT(mgr.setMapSetting("seed", "02468"));
	UASSERT(mgr.setMapSetting("mg_flags", "nolight", true));

	NoiseParams script_np_filler_depth(0, 100, v3f(200, 100, 200), 261, 4, 0.7, 2.0);
	NoiseParams script_np_factor(0, 100, v3f(50, 50, 50), 920381, 3, 0.45, 2.0);
	NoiseParams script_np_height(0, 100, v3f(450, 450, 450), 84174, 4, 0.5, 2.0);
	NoiseParams meta_np_height(20, 10, v3f(250, 250, 250), 84174,  4, 0.5,  1.0);
	NoiseParams user_np_ground(-43, 40, v3f(80,  80,  80),  983240, 4, 0.55, 2.0, NOISE_FLAG_EASED);

	mgr.setMapSettingNoiseParams("mgv5_np_filler_depth", &script_np_filler_depth, true);
	mgr.setMapSettingNoiseParams("mgv5_np_height", &script_np_height);
	mgr.setMapSettingNoiseParams("mgv5_np_factor", &script_np_factor);

	// Now make our Params and see if the values are correctly sourced
	MapgenParams *params = mgr.makeMapgenParams();
	UASSERT(params->mgtype == MAPGEN_V5);
	UASSERT(params->chunksize == 5);
	UASSERT(params->water_level == 15);
	UASSERT(params->seed == 1234);
	UASSERT((params->flags & MG_LIGHT) == 0);

	MapgenV5Params *v5params = (MapgenV5Params *)params;

	check_noise_params(&v5params->np_filler_depth, &script_np_filler_depth);
	check_noise_params(&v5params->np_factor, &script_np_factor);
	check_noise_params(&v5params->np_height, &meta_np_height);
	check_noise_params(&v5params->np_ground, &user_np_ground);

	UASSERT(mgr.setMapSetting("foobar", "25") == false);

	// Pretend the ServerMap is shutting down
	UASSERT(mgr.saveMapMeta());

	// Make sure our interface expectations are met
	UASSERT(mgr.mapgen_params == params);
	UASSERT(mgr.makeMapgenParams() == params);

#if 0
	// TODO(paramat or hmmmm): change this to compare the result against a static file

	// Load the resulting map_meta.txt and make sure it contains what we expect
	unsigned char expected_contents_hash[20] = {
		0x48, 0x3f, 0x88, 0x5a, 0xc0, 0x7a, 0x14, 0x48, 0xa4, 0x71,
		0x78, 0x56, 0x95, 0x2d, 0xdc, 0x6a, 0xf7, 0x61, 0x36, 0x5f
	};

	SHA1 ctx;
	std::string metafile_contents = read_file_to_string(test_mapmeta_path);
	ctx.addBytes(&metafile_contents[0], metafile_contents.size());
	unsigned char *sha1_result = ctx.getDigest();
	int resultdiff = memcmp(sha1_result, expected_contents_hash, 20);
	free(sha1_result);

	UASSERT(!resultdiff);
#endif
}


void TestMapSettingsManager::testMapMetaSaveLoad()
{
	Settings conf;
	std::string path = getTestTempDirectory()
		+ DIR_DELIM + "foobar" + DIR_DELIM + "map_meta.txt";

	// Create a set of mapgen params and save them to map meta
	conf.set("seed", "12345");
	conf.set("water_level", "5");
	MapSettingsManager mgr1(&conf, path);
	MapgenParams *params1 = mgr1.makeMapgenParams();
	UASSERT(params1);
	UASSERT(mgr1.saveMapMeta());

	// Now try loading the map meta to mapgen params
	conf.set("seed", "67890");
	conf.set("water_level", "32");
	MapSettingsManager mgr2(&conf, path);
	UASSERT(mgr2.loadMapMeta());
	MapgenParams *params2 = mgr2.makeMapgenParams();
	UASSERT(params2);

	// Check that both results are correct
	UASSERTEQ(u64, params1->seed, 12345);
	UASSERTEQ(s16, params1->water_level, 5);
	UASSERTEQ(u64, params2->seed, 12345);
	UASSERTEQ(s16, params2->water_level, 5);
}


void TestMapSettingsManager::testMapMetaFailures()
{
	std::string test_mapmeta_path;
	Settings conf;

	// Check to see if it'll fail on a non-existent map meta file
	test_mapmeta_path = "woobawooba/fgdfg/map_meta.txt";
	UASSERT(!fs::PathExists(test_mapmeta_path));

	MapSettingsManager mgr1(&conf, test_mapmeta_path);
	UASSERT(!mgr1.loadMapMeta());

	// Check to see if it'll fail on a corrupt map meta file
	test_mapmeta_path = makeMetaFile(true);
	UASSERT(fs::PathExists(test_mapmeta_path));

	MapSettingsManager mgr2(&conf, test_mapmeta_path);
	UASSERT(!mgr2.loadMapMeta());
}
checker(L); #endif // retrieve metatable of the object if (lua_getmetatable(L, idx) != 1) return false; // use our global table to map it to the registry name lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_METATABLE_MAP); assert(lua_istable(L, -1)); lua_pushvalue(L, -2); lua_rawget(L, -2); if (lua_isnil(L, -1)) { lua_pop(L, 3); return false; } // load the associated data bool found = find_packer(lua_tostring(L, -1), out); FATAL_ERROR_IF(!found, "Inconsistent internal state"); lua_pop(L, 3); return true; } // // Packing implementation // static VectorRef<PackedInstr> record_object(lua_State *L, int idx, PackedValue &pv, std::unordered_map<const void *, s32> &seen) { const void *ptr = lua_topointer(L, idx); assert(ptr); auto found = seen.find(ptr); if (found == seen.end()) { seen[ptr] = pv.i.size(); return VectorRef<PackedInstr>(); } s32 ref = found->second; assert(ref < (s32)pv.i.size()); // reuse the value from first time auto r = emplace(pv, INSTR_PUSHREF); r->ref = ref; pv.i[ref].keep_ref = true; return r; } static VectorRef<PackedInstr> pack_inner(lua_State *L, int idx, int vidx, PackedValue &pv, std::unordered_map<const void *, s32> &seen) { #ifndef NDEBUG StackChecker checker(L); assert(idx > 0); assert(vidx > 0); #endif switch (lua_type(L, idx)) { case LUA_TNONE: case LUA_TNIL: return emplace(pv, LUA_TNIL); case LUA_TBOOLEAN: { auto r = emplace(pv, LUA_TBOOLEAN); r->bdata = lua_toboolean(L, idx); return r; } case LUA_TNUMBER: { auto r = emplace(pv, LUA_TNUMBER); r->ndata = lua_tonumber(L, idx); return r; } case LUA_TSTRING: { auto r = emplace(pv, LUA_TSTRING); size_t len; const char *str = lua_tolstring(L, idx, &len); assert(str); r->sdata.assign(str, len); return r; } case LUA_TTABLE: { auto r = record_object(L, idx, pv, seen); if (r) return r; break; // execution continues } case LUA_TFUNCTION: { auto r = record_object(L, idx, pv, seen); if (r) return r; r = emplace(pv, LUA_TFUNCTION); call_string_dump(L, idx); size_t len; const char *str = lua_tolstring(L, -1, &len); assert(str); r->sdata.assign(str, len); lua_pop(L, 1); return r; } case LUA_TUSERDATA: { auto r = record_object(L, idx, pv, seen); if (r) return r; PackerTuple ser; if (!find_packer(L, idx, ser)) throw LuaError("Cannot serialize unsupported userdata"); pv.contains_userdata = true; r = emplace(pv, LUA_TUSERDATA); r->sdata = ser.first; r->ptrdata = ser.second.fin(L, idx); return r; } default: { std::string err = "Cannot serialize type "; err += lua_typename(L, lua_type(L, idx)); throw LuaError(err); } } // LUA_TTABLE lua_checkstack(L, 5); auto rtable = emplace(pv, LUA_TTABLE); const int vi_table = vidx++; lua_pushnil(L); while (lua_next(L, idx) != 0) { // key at -2, value at -1 const int ktype = lua_type(L, -2), vtype = lua_type(L, -1); if (ktype == LUA_TNUMBER) rtable->uidata1++; // narr else rtable->uidata2++; // nrec // check if we can use a shortcut if (can_set_into(ktype, vtype) && suitable_key(L, -2)) { // push only the value auto rval = pack_inner(L, absidx(L, -1), vidx, pv, seen); rval->pop = rval->type != LUA_TTABLE; // and where to put it: rval->set_into = vi_table; if (ktype == LUA_TSTRING) rval->sdata = lua_tostring(L, -2); else rval->sidata1 = lua_tointeger(L, -2); // pop tables after the fact if (!rval->pop) { auto ri1 = emplace(pv, INSTR_POP); ri1->sidata1 = vidx; } } else { // push the key and value pack_inner(L, absidx(L, -2), vidx, pv, seen); vidx++; pack_inner(L, absidx(L, -1), vidx, pv, seen); vidx++; // push an instruction to set them auto ri1 = emplace(pv, INSTR_SETTABLE); ri1->set_into = vi_table; ri1->sidata1 = vidx - 2; ri1->sidata2 = vidx - 1; ri1->pop = true; vidx -= 2; } lua_pop(L, 1); } assert(vidx == vi_table + 1); return rtable; } PackedValue *script_pack(lua_State *L, int idx) { if (idx < 0) idx = absidx(L, idx); PackedValue pv; std::unordered_map<const void *, s32> seen; pack_inner(L, idx, 1, pv, seen); return new PackedValue(std::move(pv)); } // // Unpacking implementation // void script_unpack(lua_State *L, PackedValue *pv) { lua_newtable(L); // table at index top to track ref indices -> objects const int top = lua_gettop(L); int ctr = 0; for (size_t packed_idx = 0; packed_idx < pv->i.size(); packed_idx++) { auto &i = pv->i[packed_idx]; // If leaving values on stack make sure there's space (every 5th iteration) if (!i.pop && (ctr++) >= 5) { lua_checkstack(L, 5); ctr = 0; } switch (i.type) { /* Instructions */ case INSTR_SETTABLE: lua_pushvalue(L, top + i.sidata1); // key lua_pushvalue(L, top + i.sidata2); // value lua_rawset(L, top + i.set_into); if (i.pop) { if (i.sidata1 != i.sidata2) { // removing moves indices so pop higher index first lua_remove(L, top + std::max(i.sidata1, i.sidata2)); lua_remove(L, top + std::min(i.sidata1, i.sidata2)); } else { lua_remove(L, top + i.sidata1); } } continue; case INSTR_POP: lua_remove(L, top + i.sidata1); if (i.sidata2 > 0) lua_remove(L, top + i.sidata2); continue; case INSTR_PUSHREF: lua_pushinteger(L, i.ref); lua_rawget(L, top); break; /* Lua types */ case LUA_TNIL: lua_pushnil(L); break; case LUA_TBOOLEAN: lua_pushboolean(L, i.bdata); break; case LUA_TNUMBER: lua_pushnumber(L, i.ndata); break; case LUA_TSTRING: lua_pushlstring(L, i.sdata.data(), i.sdata.size()); break; case LUA_TTABLE: lua_createtable(L, i.uidata1, i.uidata2); break; case LUA_TFUNCTION: luaL_loadbuffer(L, i.sdata.data(), i.sdata.size(), nullptr); break; case LUA_TUSERDATA: { PackerTuple ser; sanity_check(find_packer(i.sdata.c_str(), ser)); ser.second.fout(L, i.ptrdata); i.ptrdata = nullptr; // ownership taken by callback break; } default: assert(0); break; } if (i.keep_ref) { lua_pushinteger(L, packed_idx); lua_pushvalue(L, -2); lua_rawset(L, top); } if (i.set_into) { if (!i.pop) lua_pushvalue(L, -1); if (uses_sdata(i.type)) lua_rawseti(L, top + i.set_into, i.sidata1); else lua_setfield(L, top + i.set_into, i.sdata.c_str()); } else { if (i.pop) lua_pop(L, 1); } } // as part of the unpacking process we take ownership of all userdata pv->contains_userdata = false; // leave exactly one value on the stack lua_settop(L, top+1); lua_remove(L, top); } // // PackedValue // PackedValue::~PackedValue() { if (!contains_userdata) return; for (auto &i : this->i) { if (i.type == LUA_TUSERDATA && i.ptrdata) { PackerTuple ser; if (find_packer(i.sdata.c_str(), ser)) { // tell it to deallocate object ser.second.fout(nullptr, i.ptrdata); } else { assert(false); } } } } // // script_dump_packed // #ifndef NDEBUG void script_dump_packed(const PackedValue *val) { printf("instruction stream: [\n"); for (const auto &i : val->i) { printf("\t("); switch (i.type) { case INSTR_SETTABLE: printf("SETTABLE(%d, %d)", i.sidata1, i.sidata2); break; case INSTR_POP: printf(i.sidata2 ? "POP(%d, %d)" : "POP(%d)", i.sidata1, i.sidata2); break; case INSTR_PUSHREF: printf("PUSHREF(%d)", i.ref); break; case LUA_TNIL: printf("nil"); break; case LUA_TBOOLEAN: printf(i.bdata ? "true" : "false"); break; case LUA_TNUMBER: printf("%f", i.ndata); break; case LUA_TSTRING: printf("\"%s\"", i.sdata.c_str()); break; case LUA_TTABLE: printf("table(%d, %d)", i.uidata1, i.uidata2); break; case LUA_TFUNCTION: printf("function(%d byte)", i.sdata.size()); break; case LUA_TUSERDATA: printf("userdata %s %p", i.sdata.c_str(), i.ptrdata); break; default: printf("!!UNKNOWN!!"); break; } if (i.set_into) { if (i.type >= 0 && uses_sdata(i.type)) printf(", k=%d, into=%d", i.sidata1, i.set_into); else if (i.type >= 0) printf(", k=\"%s\", into=%d", i.sdata.c_str(), i.set_into); else printf(", into=%d", i.set_into); } if (i.keep_ref) printf(", keep_ref"); if (i.pop) printf(", pop"); printf(")\n"); } printf("]\n"); } #endif