Game Settings Deserializer
Brett208 21 Oct 2016 06:40
Hey everyone,
I propose we just modify the Xevious settings deserializer for use in Blockade. This assumes the player manually edits the text document. We can write a simple serializer to edit the data if we get far enough into the UI programming.
It is called something like this:
private ConfigFileDeserializer configDeserializer = new ConfigFileDeserializer();
protected override void LoadContent()
{
configDeserializer.ParseConfigFile();
PlayerMode playerMode = parsePlayerMode(configDeserializer.GetSetting("PlayerMode"));
GameMode gameMode = parseGameMode(configDeserializer.GetSetting("GameMode"));
}
And here is the file's code
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
class ConfigFileDeserializer
{
FileStream fileStream;
StreamReader fileReader;
Dictionary<string, string> ConfigParametersList = new Dictionary<string, string>();
public string thisIP = "";
// Open and parse the config file into a dictionary called ConfigParametersList. Method returns a bool indicating success or failure.
public bool ParseConfigFile(string configFilename = "Settings.txt")
{
fileStream = new FileStream(configFilename, FileMode.OpenOrCreate);
if (!File.Exists(configFilename))
{
buildDefaultConfigFile(configFilename);
}
fileReader = new StreamReader(fileStream);
string lineOfText = "";
while ((lineOfText = fileReader.ReadLine()) != null)
{
string[] sTemp = lineOfText.Split(':');
ConfigParametersList.Add(sTemp[0], sTemp[1]);
}
fileReader.Close();
if (ConfigParametersList.Count() > 0) return true; // We have configuration parameters, so it was a success
return false; // If we get here it means the ConfigParametersList is empty...
// return a false since something went wrong.
}
public string GetSetting(string key)
{
return ConfigParametersList[key];
}
private void buildDefaultConfigFile(string configFilename)
{
StreamWriter fileWriter = new StreamWriter(fileStream);
File.CreateText(configFilename);
fileWriter.Write("ServerIP:127.0.0.1");
fileWriter.Write("ServerPort:15150");
fileWriter.Write("UserName:Player1");
fileWriter.Write("GameMode:Co-op");
fileWriter.Close();
}
}