// writes the game javascript file based on xml contents private static void WriteJavascriptFile() { string picturesXmlFile = Path.Combine(dstPath, "pictures.xml"); string viewsXmlFile = Path.Combine(dstPath, "views.xml"); if (!File.Exists(picturesXmlFile)) { Console.WriteLine(String.Format("Could not write javascript file. Source xml file {0} was not found. Use -p and -v operator to generate the xml files.", picturesXmlFile)); return; } if (!File.Exists(viewsXmlFile)) { Console.WriteLine(String.Format("Could not write javascript file. Source xml file {0} was not found. Use -p and -v operator to generate the xml files.", viewsXmlFile)); return; } XmlDocument picturesXml = new XmlDocument(); XmlDocument viewsXml = new XmlDocument(); picturesXml.Load(picturesXmlFile); viewsXml.Load(viewsXmlFile); XmlGame game = new XmlGame(viewsXml, picturesXml); string js = "window.PICTURES={\n"; foreach (XmlPicture pic in game.Pictures) { js += pic.JsVisual; } js = js.TrimEnd(',', '\n') + "};"; // add controls to logic files foreach (XmlPicture pic in game.Pictures) { string logicFile = Path.Combine(dstPath, "logic" + pic.Id + ".js"); if (File.Exists(logicFile)) { File.AppendAllText(logicFile, String.Format("\nCONTROLS[{0}]=\"{1}\";", pic.Id, pic.ControlMap)); } else Console.WriteLine("Logic file {0} missing!", logicFile); //int pic.Id; } /* js += "\nwindow.CONTROLS={\n"; foreach (XmlPicture pic in game.Pictures) { js += pic.JsPriority; } js = js.TrimEnd(',', '\n') + "};"; */ js += "\nwindow.VIEWS." + gameId + "={\n"; foreach (XmlView view in game.Views) { js += view.Js; } js = js.TrimEnd(',', '\n') + "};"; js += "\nwindow.WORDS=[\n"; foreach (string word in Logic.words) { string wordToUse = word; if (hasWordReplacements) { XmlElement wordEl = (XmlElement)words.SelectSingleNode("/words/word[@from=\"" + word + "\"]"); if (wordEl != null) wordToUse = wordEl.GetAttribute("to"); } js += "\"" + wordToUse + "\","; } js = js.TrimEnd(',', '\n') + "];"; // inventory objects string objectsFile = Path.Combine(srcPath, "objects.txt"); if (File.Exists(objectsFile)) { js += "\nwindow.INVENTORY=[\n"; string[] lines = File.ReadAllLines(objectsFile); foreach (string line in lines) { string[] parts = line.Split(':'); string objectId = parts[0]; string objectName = parts[1]; string quote = "\""; if (objectName.Contains(quote)) quote = "'"; js += quote + objectName + quote + ",\n"; } js = js.TrimEnd(',', '\n') + "];\n"; } // word tokens string tokFile = Path.Combine(srcPath, "WORDS.agw"); if (File.Exists(tokFile)) { js += "\nwindow.TOKENS={\n"; string[] lines = File.ReadAllLines(tokFile); foreach (string line in lines) { if (!Regex.Match(line, @"^\d+").Success) continue; string[] parts = line.Split(Convert.ToChar(9)); string tokId = parts[0]; js += tokId + ":["; for (int i=0; i<parts.Length; i++) { if (i == 0) continue; js += "\"" + parts[i] + "\","; } js = js.TrimEnd(',', '\n') + "],\n"; } js = js.TrimEnd(',', '\n') + "};\n"; } // finalize the js file if (OptimizeJs) js = js.Replace("\n", ""); File.WriteAllText(Path.Combine(dstPath, "game.js"), js); }
// writes the game javascript file based on xml contents private static void WriteJavascriptFile() { string picturesXmlFile = Path.Combine(dstPath, "pictures.xml"); string viewsXmlFile = Path.Combine(dstPath, "views.xml"); if (!File.Exists(picturesXmlFile)) { Console.WriteLine(String.Format("Could not write javascript file. Source xml file {0} was not found. Use -p and -v operator to generate the xml files.", picturesXmlFile)); return; } if (!File.Exists(viewsXmlFile)) { Console.WriteLine(String.Format("Could not write javascript file. Source xml file {0} was not found. Use -p and -v operator to generate the xml files.", viewsXmlFile)); return; } XmlDocument picturesXml = new XmlDocument(); XmlDocument viewsXml = new XmlDocument(); picturesXml.Load(picturesXmlFile); viewsXml.Load(viewsXmlFile); XmlGame game = new XmlGame(viewsXml, picturesXml); string js = "window.PICTURES={\n"; foreach (XmlPicture pic in game.Pictures) { js += pic.JsVisual; } js = js.TrimEnd(',', '\n') + "};"; // add controls to logic files foreach (XmlPicture pic in game.Pictures) { string logicFile = Path.Combine(dstPath, "logic" + pic.Id + ".js"); if (File.Exists(logicFile)) { File.AppendAllText(logicFile, String.Format("\nCONTROLS[{0}]=\"{1}\";", pic.Id, pic.ControlMap)); } else { Console.WriteLine("Logic file {0} missing!", logicFile); } //int pic.Id; } /* * js += "\nwindow.CONTROLS={\n"; * * foreach (XmlPicture pic in game.Pictures) * { * js += pic.JsPriority; * } * js = js.TrimEnd(',', '\n') + "};"; */ js += "\nwindow.VIEWS." + gameId + "={\n"; foreach (XmlView view in game.Views) { js += view.Js; } js = js.TrimEnd(',', '\n') + "};"; js += "\nwindow.WORDS=[\n"; foreach (string word in Logic.words) { string wordToUse = word; if (hasWordReplacements) { XmlElement wordEl = (XmlElement)words.SelectSingleNode("/words/word[@from=\"" + word + "\"]"); if (wordEl != null) { wordToUse = wordEl.GetAttribute("to"); } } js += "\"" + wordToUse + "\","; } js = js.TrimEnd(',', '\n') + "];"; // inventory objects string objectsFile = Path.Combine(srcPath, "objects.txt"); if (File.Exists(objectsFile)) { js += "\nwindow.INVENTORY=[\n"; string[] lines = File.ReadAllLines(objectsFile); foreach (string line in lines) { string[] parts = line.Split(':'); string objectId = parts[0]; string objectName = parts[1]; string quote = "\""; if (objectName.Contains(quote)) { quote = "'"; } js += quote + objectName + quote + ",\n"; } js = js.TrimEnd(',', '\n') + "];\n"; } // word tokens string tokFile = Path.Combine(srcPath, "WORDS.agw"); if (File.Exists(tokFile)) { js += "\nwindow.TOKENS={\n"; string[] lines = File.ReadAllLines(tokFile); foreach (string line in lines) { if (!Regex.Match(line, @"^\d+").Success) { continue; } string[] parts = line.Split(Convert.ToChar(9)); string tokId = parts[0]; js += tokId + ":["; for (int i = 0; i < parts.Length; i++) { if (i == 0) { continue; } js += "\"" + parts[i] + "\","; } js = js.TrimEnd(',', '\n') + "],\n"; } js = js.TrimEnd(',', '\n') + "};\n"; } // finalize the js file if (OptimizeJs) { js = js.Replace("\n", ""); } File.WriteAllText(Path.Combine(dstPath, "game.js"), js); }