From 460ea2d0c2edd74e16d2dca1fae303a7dcfd2199 Mon Sep 17 00:00:00 2001
From: Nic0Nic0Nii <sigonasr2@yahoo.com>
Date: Wed, 26 Jul 2023 18:08:06 +0000
Subject: [PATCH] Reorganize data file specs and implement custom operator
 syntax

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
---
 Crawler/Crawler.cpp                     | 63 ++++++++++++++++++++++---
 Crawler/Crawler.h                       | 24 +++++++++-
 Crawler/assets/config/configuration.txt |  5 ++
 Crawler/olcUTIL_DataFile.h              | 41 ++++++++++++++++
 4 files changed, 124 insertions(+), 9 deletions(-)

diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp
index 93cc88ce..735c948b 100644
--- a/Crawler/Crawler.cpp
+++ b/Crawler/Crawler.cpp
@@ -21,12 +21,13 @@ INCLUDE_EMITTER_LIST
 //#define DEBUG_POS //Shows player position.
 
 //192x192
-const vi2d WINDOW_SIZE={24*15,24*10};
+vi2d WINDOW_SIZE={24*15,24*10};
 std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
 std::vector<Monster>MONSTER_LIST;
 std::vector<MonsterSpawner>SPAWNER_LIST;
 std::vector<std::shared_ptr<DamageNumber>>DAMAGENUMBER_LIST;
 std::vector<std::unique_ptr<Bullet>>BULLET_LIST;
+utils::datafile DATA;
 Crawler*game;
 
 Key Crawler::KEY_ABILITY1=Q;
@@ -38,18 +39,17 @@ Crawler::Crawler()
 {
 	sAppName = "Crawler Concept";
 	game=this;
-}
-
-bool Crawler::OnUserCreate(){
 
 	utils::datafile::Read(DATA,"assets/config/configuration.txt");
 
-	std::string CONFIG_PATH = GetString("config_path");
-
-	std::string GFX_CONFIG = CONFIG_PATH + GetString("gfx_config");
+	std::string CONFIG_PATH = "config_path"S;
 
+	std::string GFX_CONFIG = CONFIG_PATH + "gfx_config"S;
 	utils::datafile::Read(DATA,GFX_CONFIG);
+	WINDOW_SIZE={"WINDOW_SIZE"i[0],"WINDOW_SIZE"i[1]};
+}
 
+bool Crawler::OnUserCreate(){
 	InitializeLevel("assets/Campaigns/1_1.tmx",CAMPAIGN_1_1);
 
 	player=std::make_unique<Warrior>();
@@ -1100,18 +1100,35 @@ std::string Crawler::GetString(std::string key){
 	return DATA.GetProperty(key).GetString();
 }
 
+datafilestringdata Crawler::GetStringList(std::string key){
+	return {DATA,key};
+}
+
 int Crawler::GetInt(std::string key){
 	return DATA.GetProperty(key).GetInt();
 }
 
+datafileintdata Crawler::GetIntList(std::string key){
+	return {DATA,key};
+}
+
 float Crawler::GetFloat(std::string key){
 	return DATA.GetProperty(key).GetReal();
 }
 
+datafilefloatdata Crawler::GetFloatList(std::string key){
+	return {DATA,key};
+}
+
 double Crawler::GetDouble(std::string key){
 	return DATA.GetProperty(key).GetReal();
 }
 
+datafiledoubledata Crawler::GetDoubleList(std::string key){
+	return {DATA,key};
+}
+
+
 int main()
 {
 	Crawler demo;
@@ -1119,4 +1136,36 @@ int main()
 		demo.Start();
 
 	return 0;
+}
+
+datafilestringdata operator ""s(const char*key,std::size_t len){
+	return {DATA,std::string(key,len)};
+}
+
+datafileintdata operator ""i(const char*key,std::size_t len){
+	return {DATA,std::string(key,len)};
+}
+
+datafilefloatdata operator ""f(const char*key,std::size_t len){
+	return {DATA,std::string(key,len)};
+}
+
+datafiledoubledata operator ""d(const char*key,std::size_t len){
+	return {DATA,std::string(key,len)};
+}
+
+std::string operator ""S(const char*key,std::size_t len){
+	return DATA.GetProperty(std::string(key,len)).GetString();
+}
+
+int operator ""I(const char*key,std::size_t len){
+	return DATA.GetProperty(std::string(key,len)).GetInt();
+}
+
+float operator ""F(const char*key,std::size_t len){
+	return DATA.GetProperty(std::string(key,len)).GetReal();
+}
+
+double operator ""D(const char*key,std::size_t len){
+	return DATA.GetProperty(std::string(key,len)).GetReal();
 }
\ No newline at end of file
diff --git a/Crawler/Crawler.h b/Crawler/Crawler.h
index 727a8c1e..9adef51f 100644
--- a/Crawler/Crawler.h
+++ b/Crawler/Crawler.h
@@ -52,7 +52,6 @@ private:
 	int bridgeLayerIndex=-1;
 	float bridgeFadeFactor=0.f;
 	void InitializeClassAbilities();
-	utils::datafile DATA;
 public:
 	Crawler();
 	bool OnUserCreate() override;
@@ -101,7 +100,28 @@ public:
 	void PopulateRenderLists(std::vector<Monster*>&monstersBeforeLower,std::vector<Monster*>&monstersBeforeUpper,std::vector<Monster*>&monstersAfterLower,std::vector<Monster*>&monstersAfterUpper,std::vector<Bullet*>&bulletsLower,std::vector<Bullet*>&bulletsUpper,std::vector<Effect*>&backgroundEffectsLower,std::vector<Effect*>&backgroundEffectsUpper,std::vector<Effect*>&foregroundEffectsLower,std::vector<Effect*>&foregroundEffectsUpper);
 	void ChangePlayerClass(Class cl);
 	std::string GetString(std::string key);
+	datafilestringdata GetStringList(std::string key);
 	int GetInt(std::string key);
+	datafileintdata GetIntList(std::string key);
 	float GetFloat(std::string key);
+	datafilefloatdata GetFloatList(std::string key);
 	double GetDouble(std::string key);
-};
\ No newline at end of file
+	datafiledoubledata GetDoubleList(std::string key);
+};
+
+//Read a string array from the config.
+datafilestringdata operator ""s(const char*key,std::size_t len);
+//Read an int array from the config.
+datafileintdata operator ""i(const char*key,std::size_t len);
+//Read a float array from the config.
+datafilefloatdata operator ""f(const char*key,std::size_t len);
+//Read a double array from the config.
+datafiledoubledata operator ""d(const char*key,std::size_t len);
+//Read a string key from the config.
+std::string operator ""S(const char*key,std::size_t len);
+//Read an integer key from the config.
+int operator ""I(const char*key,std::size_t len);
+//Read a float key from the config.
+float operator ""F(const char*key,std::size_t len);
+//Read a double key from the config.
+double operator ""D(const char*key,std::size_t len);
\ No newline at end of file
diff --git a/Crawler/assets/config/configuration.txt b/Crawler/assets/config/configuration.txt
index 7a0aad51..a9ae8ef1 100644
--- a/Crawler/assets/config/configuration.txt
+++ b/Crawler/assets/config/configuration.txt
@@ -1,2 +1,7 @@
 config_path = assets/config/
+
+# 360x240 is 15x10 tiles of visibility.
+WINDOW_SIZE = 360,240
+
+# Graphics Loading Config
 gfx_config = gfx/gfx.txt
\ No newline at end of file
diff --git a/Crawler/olcUTIL_DataFile.h b/Crawler/olcUTIL_DataFile.h
index 826cf15c..0d172b94 100644
--- a/Crawler/olcUTIL_DataFile.h
+++ b/Crawler/olcUTIL_DataFile.h
@@ -64,6 +64,42 @@ David Barr, aka javidx9, �OneLoneCoder 2019, 2020, 2021, 2022
 
 namespace olc::utils
 {
+	class datafilestringdata
+	{
+		std::reference_wrapper<datafile>data;
+		std::string key;
+		public:
+		inline datafilestringdata(datafile&dat,std::string key)
+		:data(dat),key(key){};
+		std::string operator[](int index){data.get().GetIndexedProperty(key,index).GetString();};
+	};
+	class datafileintdata
+	{
+		std::reference_wrapper<datafile>data;
+		std::string key;
+		public:
+		inline datafileintdata(datafile&dat,std::string key)
+		:data(dat),key(key){};
+		int operator[](int index){data.get().GetIndexedProperty(key,index).GetInt();};
+	};
+	class datafilefloatdata
+	{
+		std::reference_wrapper<datafile>data;
+		std::string key;
+		public:
+		inline datafilefloatdata(datafile&dat,std::string key)
+		:data(dat),key(key){};
+		float operator[](int index){data.get().GetIndexedProperty(key,index).GetReal();};
+	};
+	class datafiledoubledata
+	{
+		std::reference_wrapper<datafile>data;
+		std::string key;
+		public:
+		inline datafiledoubledata(datafile&dat,std::string key)
+		:data(dat),key(key){};
+		double operator[](int index){data.get().GetIndexedProperty(key,index).GetReal();};
+	};
 	class datafile
 	{
 	public:
@@ -118,6 +154,11 @@ namespace olc::utils
 			return m_vContent.size();
 		}
 
+		inline std::vector<std::string> GetValues() const
+		{
+			return m_vContent;
+		}
+
 		// Checks if a property exists - useful to avoid creating properties
 		// via reading them, though non-essential
 		inline bool HasProperty(const std::string& sName) const