From c22e8c50afb6e0605f8c2b48d14a15e5b132a0fc Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <c.oelckers@zdoom.fake>
Date: Thu, 22 Sep 2016 19:36:23 +0200
Subject: [PATCH] - fixed some errors with parsing globals.json. It looks like
 this file is being processed correctly now.

---
 src/g_game.cpp     | 17 ++++++++--------
 src/m_random.cpp   |  2 +-
 src/p_acs.cpp      | 51 ++++++++++++++++++++++++----------------------
 src/serializer.cpp |  2 +-
 4 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/src/g_game.cpp b/src/g_game.cpp
index ee826aa68..ef104d67b 100644
--- a/src/g_game.cpp
+++ b/src/g_game.cpp
@@ -1926,11 +1926,11 @@ void G_DoLoadGame ()
 	// we are done with info.json.
 	arc.Close();
 
-	info = resfile->FindLump("global.json");
+	info = resfile->FindLump("globals.json");
 	if (info == nullptr)
 	{
 		delete resfile;
-		Printf("'%s' is not a valid savegame: Missing 'global.json'.\n", savename.GetChars());
+		Printf("'%s' is not a valid savegame: Missing 'globals.json'.\n", savename.GetChars());
 		return;
 	}
 
@@ -1948,14 +1948,15 @@ void G_DoLoadGame ()
 
 	bglobal.RemoveAllBots (true);
 
-	arc("importantcvars", map);
-	if (!map.IsEmpty())
+	FString cvar;
+	arc("importantcvars",cvar);
+	if (!cvar.IsEmpty())
 	{
-		BYTE *vars_p = (BYTE *)map.GetChars();
+		BYTE *vars_p = (BYTE *)cvar.GetChars();
 		C_ReadCVars (&vars_p);
 	}
 
-	DWORD time[2] = { 0,1 };
+	DWORD time[2] = { 1,0 };
 
 	arc("ticrate", time[0])
 		("leveltime", time[1]);
@@ -1970,7 +1971,6 @@ void G_DoLoadGame ()
 	bool demoplaybacksave = demoplayback;
 	G_InitNew (map, false);
 	demoplayback = demoplaybacksave;
-	delete[] map;
 	savegamerestore = false;
 
 	STAT_Serialize(arc);
@@ -1981,7 +1981,8 @@ void G_DoLoadGame ()
 	NextSkill = -1;
 	arc("nextskill", NextSkill);
 
-	level.info->Snapshot.Clean();
+	if (level.info != nullptr)
+		level.info->Snapshot.Clean();
 
 	BackupSaveName = savename;
 
diff --git a/src/m_random.cpp b/src/m_random.cpp
index 11075916e..e0a55d095 100644
--- a/src/m_random.cpp
+++ b/src/m_random.cpp
@@ -353,8 +353,8 @@ void FRandom::StaticReadRNGState(FSerializer &arc)
 				}
 				arc.EndObject();
 			}
-			arc.EndArray();
 		}
+		arc.EndArray();
 	}
 }
 
diff --git a/src/p_acs.cpp b/src/p_acs.cpp
index aea40945f..d7b042698 100644
--- a/src/p_acs.cpp
+++ b/src/p_acs.cpp
@@ -725,36 +725,39 @@ void ACSStringPool::ReadStrings(FSerializer &file, const char *key)
 {
 	Clear();
 
-	int poolsize;
+	if (file.BeginObject(key))
+	{
+		int poolsize = 0;
 
-	file("poolsize", poolsize);
-	Pool.Resize(poolsize);
-	for (auto &p : Pool)
-	{
-		p.Next = FREE_ENTRY;
-		p.LockCount = 0;
-	}
-	if (file.BeginArray("pool"))
-	{
-		int j = file.ArraySize();
-		for (int i = 0; i < j; i++)
+		file("poolsize", poolsize);
+		Pool.Resize(poolsize);
+		for (auto &p : Pool)
 		{
-			if (file.BeginObject(nullptr))
+			p.Next = FREE_ENTRY;
+			p.LockCount = 0;
+		}
+		if (file.BeginArray("pool"))
+		{
+			int j = file.ArraySize();
+			for (int i = 0; i < j; i++)
 			{
-				unsigned ii = UINT_MAX;
-				file("index", ii);
-				if (ii < Pool.Size())
+				if (file.BeginObject(nullptr))
 				{
-					file("string", Pool[ii].Str)
-						("lockcount", Pool[ii].LockCount);
+					unsigned ii = UINT_MAX;
+					file("index", ii);
+					if (ii < Pool.Size())
+					{
+						file("string", Pool[ii].Str)
+							("lockcount", Pool[ii].LockCount);
 
-					unsigned h = SuperFastHash(Pool[ii].Str, Pool[ii].Str.Len());
-					unsigned bucketnum = h % NUM_BUCKETS;
-					Pool[ii].Hash = h;
-					Pool[ii].Next = PoolBuckets[bucketnum];
-					PoolBuckets[bucketnum] = i;
+						unsigned h = SuperFastHash(Pool[ii].Str, Pool[ii].Str.Len());
+						unsigned bucketnum = h % NUM_BUCKETS;
+						Pool[ii].Hash = h;
+						Pool[ii].Next = PoolBuckets[bucketnum];
+						PoolBuckets[bucketnum] = i;
+					}
+					file.EndObject();
 				}
-				file.EndObject();
 			}
 		}
 	}
diff --git a/src/serializer.cpp b/src/serializer.cpp
index 301e284a9..251c9a0f4 100644
--- a/src/serializer.cpp
+++ b/src/serializer.cpp
@@ -51,7 +51,6 @@ struct FJSONObject
 		else if (v->IsArray())
 		{
 			mIndex = 0;
-			mIterator = v->MemberEnd();
 		}
 	}
 };
@@ -717,6 +716,7 @@ unsigned FSerializer::GetSize(const char *group)
 const char *FSerializer::GetKey()
 {
 	if (isWriting()) return nullptr;	// we do not know this when writing.
+	if (!r->mObjects.Last().mObject->IsObject()) return nullptr;	// non-objects do not have keys.
 	auto &it = r->mObjects.Last().mIterator;
 	if (it == r->mObjects.Last().mObject->MemberEnd()) return nullptr;
 	return it->name.GetString();