Files
RAGECOOP-V/Tools/DataDumper/Program.cs

118 lines
4.0 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System.Text;
using System.Xml;
2022-10-19 19:07:46 +08:00
using Newtonsoft.Json;
2022-10-23 19:02:39 +08:00
using RageCoop.Core;
2022-10-21 19:41:38 +08:00
using Formatting = Newtonsoft.Json.Formatting;
2022-10-19 19:07:46 +08:00
namespace DataDumper;
2022-10-23 19:02:39 +08:00
2022-11-16 17:37:31 +08:00
[Flags]
public enum GenFlags
{
WeaponInfo = 1,
WeaponHash = 2,
VehicleWeaponInfo = 4,
Animations = 8,
All = ~0
}
2022-11-16 17:40:07 +08:00
2022-11-16 17:37:31 +08:00
public static class Program
2022-10-19 19:07:46 +08:00
{
2022-11-16 17:37:31 +08:00
public static GenFlags ToGenerate = GenFlags.All;
2022-11-16 17:40:07 +08:00
2022-11-16 17:37:31 +08:00
public static void Main(string[] args)
2022-10-21 19:41:38 +08:00
{
2022-11-16 17:40:07 +08:00
if (args.Length > 0 && Enum.TryParse<GenFlags>(args[0], true, out var flags)) ToGenerate = flags;
2022-11-16 17:37:31 +08:00
Directory.CreateDirectory("out");
#region META
2022-11-16 17:40:07 +08:00
2022-11-16 17:37:31 +08:00
// Dumps from the game's xml documents, needs to have all *.meta file extracted to "meta" directory. OpenIV is recommended
if (ToGenerate.HasFlag(GenFlags.WeaponInfo))
{
Dictionary<uint, WeaponInfo> weapons = new();
foreach (var f in Directory.GetFiles("meta", "*.meta")) Parse(f, weapons);
File.WriteAllText("out/Weapons.json", JsonConvert.SerializeObject(weapons, Formatting.Indented));
2022-11-16 17:40:07 +08:00
if (ToGenerate.HasFlag(GenFlags.WeaponHash)) DumpWeaponHash(weapons, true);
2022-11-16 17:37:31 +08:00
}
2022-10-23 19:02:39 +08:00
2022-11-16 17:37:31 +08:00
#endregion
2022-10-23 19:02:39 +08:00
2022-11-16 17:37:31 +08:00
#region EXTERNAL
2022-11-16 17:40:07 +08:00
2022-11-16 17:37:31 +08:00
// External data from DurtyFree's data dumps: https://github.com/DurtyFree/gta-v-data-dumps
Directory.CreateDirectory("ext");
if (ToGenerate.HasFlag(GenFlags.VehicleWeaponInfo))
VehicleWeaponInfo.Dump("ext/vehicles.json", "out/VehicleWeapons.json");
2022-11-16 17:40:07 +08:00
if (ToGenerate.HasFlag(GenFlags.Animations)) AnimDic.Dump("ext/animDictsCompact.json", "out/Animations.json");
2022-11-16 17:37:31 +08:00
#endregion
2022-10-23 19:02:39 +08:00
}
private static void Parse(string filename, Dictionary<uint, WeaponInfo> weap)
{
Console.WriteLine("Parsing " + filename);
var doc = new XmlDocument();
try
2022-10-21 19:41:38 +08:00
{
2022-10-23 19:02:39 +08:00
doc.Load(filename);
2022-10-21 19:41:38 +08:00
}
2022-10-23 19:02:39 +08:00
catch (Exception ex)
2022-10-21 19:41:38 +08:00
{
2022-10-23 19:02:39 +08:00
Console.WriteLine(ex);
return;
2022-10-21 19:41:38 +08:00
}
2022-10-23 19:02:39 +08:00
var nodes = doc.ChildNodes.ToList();
if (nodes.Any(x => x.Name == "CWeaponInfoBlob"))
2022-10-21 19:41:38 +08:00
{
2022-10-23 19:02:39 +08:00
var infosNode = doc.GetElementsByTagName("Item");
foreach (XmlNode n in infosNode)
if (n.Attributes["type"]?.Value == "CWeaponInfo")
2022-10-21 19:41:38 +08:00
{
2022-10-23 19:02:39 +08:00
var info = new WeaponInfo(n);
if (!info.Name.StartsWith("VEHICLE_WEAPON") && !info.Name.StartsWith("WEAPON")) continue;
2022-11-16 17:37:31 +08:00
var hash = CoreUtils.JoaatHash(info.Name);
2022-10-23 19:02:39 +08:00
if (weap.ContainsKey(hash))
weap[hash] = info;
else
weap.Add(hash, info);
2022-10-21 19:41:38 +08:00
}
}
2022-10-23 19:02:39 +08:00
}
2022-10-21 19:41:38 +08:00
2022-10-23 19:02:39 +08:00
private static void DumpWeaponHash(Dictionary<uint, WeaponInfo> weapons, bool sort = false,
2022-11-16 17:37:31 +08:00
string path = @"out/WeaponHash.cs")
2022-10-23 19:02:39 +08:00
{
StringBuilder output = new();
List<string> lines = new();
var weps = weapons.Where(x => x.Value.Name.StartsWith("WEAPON"));
var vehWeaps = weapons.Where(x => x.Value.Name.StartsWith("VEHICLE_WEAPON"));
output.Append("public enum WeaponHash : uint\r\n{");
foreach (var info in weps)
lines.Add($"{CoreUtils.FormatToSharpStyle(info.Value.Name, 7)} = {info.Key.ToHex()}");
if (sort) lines.Sort();
foreach (var l in lines) output.Append($"\r\n\t{l},");
output.AppendLine("\r\n}");
output.AppendLine();
output.Append("public enum VehicleWeaponHash : uint\r\n{\r\n\tInvalid = 0xFFFFFFFF,");
lines = new List<string>();
foreach (var info in vehWeaps)
2022-11-16 17:37:31 +08:00
lines.Add($"{CoreUtils.FormatToSharpStyle(info.Value.Name, 14)} = {info.Key.ToHex()}");
2022-10-23 19:02:39 +08:00
if (sort) lines.Sort();
foreach (var l in lines) output.Append($"\r\n\t{l},");
output.Append("\r\n}");
File.WriteAllText(path, output.ToString());
}
private static List<XmlNode> ToList(this XmlNodeList l)
{
var nodes = new List<XmlNode>();
foreach (XmlNode n in l) nodes.Add(n);
return nodes;
}
2022-10-19 19:07:46 +08:00
}