private static void WriteNode(XmlWriter writer, IEnumerable <BinaryObject> parentChain, BinaryObject node) { var chain = parentChain.Concat(new[] { node }); writer.WriteStartElement("object"); // get object name from object hash via string list var objectHashInput = (int)node.NameHash; if (StringHasher.CanResolveHash(objectHashInput)) { var objectHashOutput = StringHasher.ResolveHash(objectHashInput); writer.WriteAttributeString("name", objectHashOutput); } else { writer.WriteAttributeString("hash", node.NameHash.ToString("X8")); } WriteNodeField(writer, node); foreach (var childNode in node.Children) { WriteNode(writer, chain, childNode); } writer.WriteEndElement(); }
static void SnoopFile(string filename) { XDocument xml = null; if (!Utils.TryLoadXmlFile(filename, out xml)) { Utils.Bastard.Fail("Snoopy only works with XML files."); } var strs = new HashSet <String>(); foreach (var name in EnumerateXMLNames(xml.Root)) { var id = new StringId(name.LocalName); if (!strs.Contains(id) && !StringHasher.CanResolveHash(id)) { strs.Add(id); } } if (strs.Count == 0) { Utils.Bastard.Fail("Couldn't find anything new, sadly."); } var outFile = Path.ChangeExtension(filename, ".snoop.log"); File.WriteAllText(outFile, String.Join("\n", strs.OrderBy((s) => s))); }
private static void WriteRootNode(XmlWriter writer, IEnumerable <BinaryObject> parentChain, BinaryObjectFile bof) { BinaryObject node = bof.Root; var chain = parentChain.Concat(new[] { node }); writer.WriteStartElement("object"); // get object name from object hash via string list var objectHashInput = (int)node.NameHash; if (StringHasher.CanResolveHash(objectHashInput)) { var objectHashOutput = StringHasher.ResolveHash(objectHashInput); writer.WriteAttributeString("name", objectHashOutput); } else { writer.WriteAttributeString("hash", node.NameHash.ToString("X8")); } // add version attribute to root object string bofVersion = bof.Version.ToString(); if (bofVersion != null) { writer.WriteAttributeString("version", bofVersion); } // add header attribute to root object string bofHeader = bof.Header; if (bofHeader.Length > 0) { writer.WriteAttributeString("header", bofHeader); } WriteNodeField(writer, node); foreach (var childNode in node.Children) { WriteNode(writer, chain, childNode); } writer.WriteEndElement(); }
private static FieldInfo ResolveField(uint key, byte[] value) { FieldInfo field = new FieldInfo(); // key string keyTypeString = "hash"; string keyNameString = $"{key:X8}"; if (StringHasher.CanResolveHash((int)key)) { keyTypeString = "name"; keyNameString = StringHasher.ResolveHash((int)key); } // value string valueTypeString = FieldHandling.GetTypeName(FieldType.BinHex); string valueValueString = BitConverter.ToString(value).Replace("-", ""); string r_valueTypeString = valueTypeString; string r_valueValueString = valueValueString; if (TypeGuesser.FieldHandler(value, out r_valueTypeString, out r_valueValueString) == true) { valueTypeString = r_valueTypeString; valueValueString = r_valueValueString; } // fill structure field.key.type = keyTypeString; field.key.name = keyNameString; field.value.type = valueTypeString; field.value.value = valueValueString; return(field); }
static void Bastard_Work() { var inputType = FileFactory.GetFileType(Config.Input); if (inputType == FileType.Xml) { using (var stream = File.Open(Config.Input, FileMode.Open, FileAccess.Read, FileShare.Read)) { Console.WriteLine("Loading XML..."); var xmlData = new NomadXmlSerializer(); var root = xmlData.Deserialize(stream); if (Config.HasArg("snoop")) { var strs = new Dictionary <int, string>(); foreach (var obj in root) { var id = obj.Id; if (!strs.ContainsKey(id) && !StringHasher.CanResolveHash(id)) { strs.Add(id.Hash, id.Name); } } var log = String.Join("\n", strs.ToArray().Select((kv) => kv.Value).OrderBy((s) => s)); File.WriteAllText(GetOutputFilePath(".snoop.log"), log); } var dataFilter = ResourceFactory.GetFilter(root); var ext = (dataFilter != null) ? dataFilter.FileExt : ".fcb"; var outFile = GetOutputFilePath(ext); using (var bs = new BinaryStream(1024)) { if (root.IsRml) { var rmlData = new NomadRmlSerializer(); Console.WriteLine("Saving RML..."); rmlData.Serialize(bs, root); } else { var resData = new NomadResourceSerializer() { Format = FormatType.Resource }; // HACK HACK HACK! Utils.Bastard.Warn("**** WARNING! Write cache is broken in this version -- this MAY cause game crashes! ****"); AttributeData.AllowMiniBuffers = false; WriteCache.Enabled = false; if ((dataFilter != null) && (dataFilter.Version >= 3)) { resData.Format = FormatType.Objects; AttributeData.AllowMiniBuffers = true; } Console.WriteLine("Saving data..."); resData.Serialize(bs, root); } Utils.WriteFile(bs, outFile, (int)bs.Length); } } } else { if (inputType == FileType.Any) { throw new InvalidOperationException(">>> NOT SUPPORTED YET! <<<"); } if (inputType == FileType.FCXMap) { using (var stream = new BinaryStream(Config.Input)) { var serializer = new FCXMapSerializer(); Console.WriteLine("Loading map data..."); var mapData = serializer.Deserialize(stream); var outFile = GetOutputFilePath(".fc5map.xml"); using (var fsXml = File.Open(outFile, FileMode.Create, FileAccess.Write, FileShare.Read)) { var xmlSerializer = new NomadXmlSerializer(); Console.WriteLine("Saving XML..."); xmlSerializer.Serialize(fsXml, mapData); } } } else { using (var stream = new BinaryStream(Config.Input)) { var serializer = NomadFileInfo.GetSerializer(stream); Console.WriteLine("Loading data..."); var root = serializer.Deserialize(stream); var outFile = GetOutputFilePath(".xml"); using (var fsXml = File.Open(outFile, FileMode.Create, FileAccess.Write, FileShare.Read)) { var xmlSerializer = new NomadXmlSerializer(); Console.WriteLine("Saving XML..."); xmlSerializer.Serialize(fsXml, root); } } } } // job well done var success = Config.GetSuccessMessage(); Utils.Bastard.Say(success); }
static void RunSuperHasher9000TM() { Console.Clear(); Utils.Bastard.Say("<<< SUPER HASHER 9000(tm) >>>"); var readLine = new Func <string>(() => { Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("> "); Console.ResetColor(); return(Console.ReadLine()); }); var respond = new Action <string>((s) => { Utils.Bastard.Say($"{s}"); }); var line = ""; while ((line = readLine()) != null) { if (line.Length == 0) { respond("Type something, dumbass!"); continue; } switch (line[0]) { case '?': { var sb = new StringBuilder(); var num = 0; foreach (var hash in StringHasher.GetMissingHashes().OrderBy((k) => $"{k:X8}")) { if ((num % 10) == 0.0) { if (num > 0) { sb.AppendLine(); } sb.Append($" - "); } sb.Append($"{hash:X8} "); num++; } sb.AppendLine(); if (num > 0) { respond($"{num} unknown hashes:"); respond(sb.ToString()); } else { respond("You should probably load something first, dumbass."); } } continue; case '@': { var file = line.Substring(1).Replace("\"", ""); try { respond("Loading..."); var resource = ResourceFactory.Open(file); if (resource != null) { respond("Done."); } else { respond("Fail."); } } catch (Exception) { respond("Nope."); } } continue; default: { var hash = 0; if (line[0] == '$') { try { hash = int.Parse(line.Substring(1), NumberStyles.HexNumber); } catch (Exception) { respond("You're an idiot."); continue; } } else { switch (line) { case "help": respond("I'm afraid I can't do that, Dave."); continue; case "exit": respond("You can check out anytime you like, but you can never leave."); continue; } hash = StringHasher.GetHash(line); } if (StringHasher.CanResolveHash(hash)) { respond($"{hash:X8} : '{line}'"); } else { if (StringHasher.IsMissingHash(hash)) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("You found a missing hash!"); Console.ResetColor(); } respond($"{hash:X8} : (unknown)"); } } continue; } } }