using System; using System.Collections.Generic; using System.Linq; using System.Text; using OpenRa.FileFormats; using System.IO; using IjwFramework.Types; namespace RulesConverter { using PL = Dictionary; class Program { static void Main(string[] args) { FileSystem.Mount(new Folder("./")); var ruleStreams = args .Where(a => a.EndsWith(".ini")) .Select(a => FileSystem.Open(a)).ToArray(); var rules = new IniFile(ruleStreams); var outputFile = args.Single(a => a.EndsWith(".rul")); var categoryMap = new Dictionary> { { "VehicleTypes", Pair.New( "DefaultVehicle", "Vehicle" ) }, { "ShipTypes", Pair.New( "DefaultShip", "Ship" ) }, { "PlaneTypes", Pair.New( "DefaultPlane", "Plane" ) }, { "DefenseTypes", Pair.New( "DefaultDefense", "Defense" ) }, { "BuildingTypes", Pair.New( "DefaultBuilding", "Building" ) }, { "InfantryTypes", Pair.New( "DefaultInfantry", "Infantry" ) }, }; var traitMap = new Dictionary { { "Unit", new PL { { "HP", "Strength" }, { "Armor", "Armor" }, { "Crewed", "Crewed" } } }, { "Selectable", new PL { { "Priority", "SelectionPriority" }, { "Voice", "Voice" }, { "@Bounds", "SelectionSize" } } }, { "Mobile", new PL { { "Sight", "Sight" }, { "ROT", "ROT" }, { "Speed", "Speed" } } //{ "MovementType", ... }, }, { "Plane", new PL { { "ROT", "ROT" }, { "Speed", "Speed" } } }, { "Helicopter", new PL { { "ROT", "ROT" }, { "Speed", "Speed" } } }, { "RenderBuilding", new PL { { "Image", "Image" } } }, { "RenderUnit", new PL { { "Image", "Image" } } }, { "RenderBuildingCharge", new PL { { "Image", "Image" } } }, { "RenderBuildingOre", new PL { { "Image", "Image" } } }, { "RenderBuildingTurreted", new PL { { "Image", "Image" } } }, { "RenderInfantry", new PL { { "Image", "Image" } } }, { "RenderUnitMuzzleFlash", new PL { { "Image", "Image" } } }, { "RenderUnitReload", new PL { { "Image", "Image" } } }, { "RenderUnitRotor", new PL { { "Image", "Image" } } }, { "RenderUnitSpinner", new PL { { "Image", "Image" } } }, { "RenderUnitTurreted", new PL { { "Image", "Image" } } }, { "Buildable", new PL { { "TechLevel", "TechLevel" }, { "Tab", "$Tab" }, { "@Prerequisites", "Prerequisite" }, { "Owner", "Owner" }, { "Cost", "Cost" }, { "Icon", "Icon" }, { "$Description", "Description" }, { "$LongDesc", "LongDesc" } } } }; using (var writer = File.CreateText(outputFile)) { foreach (var cat in categoryMap) foreach (var item in rules.GetSection(cat.Key).Select(a => a.Key)) { var iniSection = rules.GetSection(item); writer.WriteLine("{0}:", item); writer.WriteLine("\tInherits: {0}", cat.Value.First); var traits = iniSection.GetValue("Traits", "") .Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (iniSection.GetValue("Selectable", "yes") == "yes") traits.Insert(0, "Selectable"); if (iniSection.GetValue("TechLevel", "-1") != "-1") traits.Insert(0, "Buildable"); foreach (var t in traits) { writer.WriteLine("\t{0}:", t); if (traitMap.ContainsKey(t)) foreach (var kv in traitMap[t]) { var v = kv.Value == "$Tab" ? cat.Value.Second : iniSection.GetValue(kv.Value, ""); var fmt = "\t\t{0}: {1}"; var k = kv.Key; if (k.StartsWith("@")) { k = k.Substring(1); fmt = "\t\t{0}: [{1}]"; } if (k.StartsWith("$")) { k = k.Substring(1); fmt = "\t\t{0}: \"{1}\""; } if (!string.IsNullOrEmpty(v)) writer.WriteLine(fmt, k, v); } } writer.WriteLine(); } } } } }