1 /*
2  * Copyright (C) 2015-2017, by Laszlo Szeremi under the Boost license.
3  *
4  * Pixel Perfect Engine, config module
5  */
6 
7 module PixelPerfectEngine.system.config;
8 
9 import std.xml;
10 import std.file;
11 import std.stdio;
12 import std..string;
13 import std.conv;
14 
15 import PixelPerfectEngine.system.inputHandler;
16 import PixelPerfectEngine.system.exc;
17 
18 public class ConfigurationProfile{
19 	public int sfxVol, musicVol;
20 	public string screenMode, resolution, scalingQuality, driver;
21 	public KeyBinding[] keyBindingList, inputDevList;
22 	private string lastFile;
23 	public AuxillaryElements[] auxillaryParameters;
24 	//public AuxillaryElements auxillaryElements[];
25 
26 	public this(string configFile){
27 		lastFile = configFile;
28 		//restore(lastFile);
29 	}
30 
31 	public this(){
32 
33 	}
34 
35 	public void restore(string configFile){
36 		lastFile = configFile;
37 		string s = cast(string)std.file.read(configFile);
38 		try{
39 			check(s);
40 		}
41 		catch(CheckException e){
42 			writeln(e.toString);
43 		}
44 		finally{
45 			auto doc = new Document(s);
46 
47 			foreach(Element e1; doc.elements){
48 
49 				if(e1.tag.name == "Audio"){
50 					foreach(Element e2; e1.elements){
51 						if(e2.tag.name == "sfxVol")
52 							sfxVol = to!int(e2.text());
53 						else if(e2.tag.name == "musicvol")
54 							musicVol = to!int(e2.text());
55 					}
56 				}
57 				else if(e1.tag.name == "Video"){
58 					foreach(Element e2; e1.elements){
59 						if(e2.tag.name == "screenMode")
60 							screenMode = e2.text();
61 						else if(e2.tag.name == "resolution")
62 							resolution = e2.text();
63 						else if(e2.tag.name == "scalingQuality")
64 							scalingQuality = e2.text();
65 						else if(e2.tag.name == "driver")
66 							driver = e2.text();
67 					}
68 				}
69 				else if(e1.tag.name == "Input"){
70 					int dn = to!uint(e1.tag.attr["devNum"]);
71 					int dt;
72 					if(e1.tag.attr["devType"] == "Joystick")
73 						dt = Devicetype.JOYSTICK;
74 					else if(e1.tag.attr["devType"] == "Mouse")
75 						dt = Devicetype.MOUSE;
76 					inputDevList ~= KeyBinding(0, 0, dn, "", dt);
77 					foreach(Element e2; e1.elements){
78 						keyBindingList ~= KeyBinding(to!ushort(e2.tag.attr["keyMod"]), to!uint(e2.tag.attr["keyCode"]), dn, e2.tag.attr["ID"], dt);
79 					}
80 						
81 
82 
83 				}
84 				else if(e1.tag.name == "Aux"){
85 					foreach(Element e2; e1.elements){
86 						auxillaryParameters ~= AuxillaryElements(e2.tag.name, e2.text());
87 					}
88 				}
89 			}
90 		}
91 	}
92 	public void store(string configFile){
93 
94 		auto doc = new Document(new Tag("Configuration"));
95 
96 		auto e1 = new Element("Audio");
97 		e1 ~= new Element("sfxVol", to!string(sfxVol));
98 		e1 ~= new Element("musicVol", to!string(musicVol));
99 		doc ~= e1;
100 
101 		auto e2 = new Element("Video");
102 		e2 ~= new Element("screenMode", screenMode);
103 		e2 ~= new Element("resolution", resolution);
104 		e2 ~= new Element("scalingQuality", scalingQuality);
105 		e2 ~= new Element("driver", driver);
106 		doc ~= e2;
107 
108 
109 		foreach(KeyBinding id; inputDevList){
110 			auto e3 = new Element("Input");
111 			e3.tag.attr["devType"] = to!string(id.devicetype);
112 			e3.tag.attr["devNum"] = to!string(id.devicenumber);
113 			for(int k; k < keyBindingList.length; k++){
114 				if(keyBindingList[k].devicetype == id.devicetype && keyBindingList[k].devicenumber == id.devicenumber){
115 					auto e4 = new Element("KeyBinding");
116 					e4.tag.attr["keyMod"] = to!string(keyBindingList[k].keymod);
117 					e4.tag.attr["keyCode"] = to!string(keyBindingList[k].keymod);
118 					e4.tag.attr["ID"] = keyBindingList[k].ID;
119 					e3 ~= e4;
120 				}
121 			}
122 			doc ~= e3;
123 		}
124 
125 		auto e4 = new Element("Aux");
126 		foreach(AuxillaryElements aux; auxillaryParameters){
127 			e4 ~= new Element(aux.name, aux.value);
128 		}
129 		doc ~= e4;
130 
131 		std.file.write(configFile, doc.toString());
132 	}
133 }
134 
135 public struct AuxillaryElements{
136 	public string value, name;
137 	public this(string name, string value){
138 		this.name = name;
139 		this.value = value;
140 	}
141 }