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

166 lines
5.4 KiB
C#
Raw Normal View History

2022-10-19 19:07:46 +08:00
using RageCoop.Core;
using Newtonsoft.Json;
2022-10-21 19:41:38 +08:00
using Formatting = Newtonsoft.Json.Formatting;
2022-10-19 19:07:46 +08:00
using System;
2022-10-21 19:41:38 +08:00
using System.Xml;
using System.Linq;
using System.Text;
2022-10-19 19:07:46 +08:00
namespace RageCoop.Client.DataDumper
{
2022-10-21 19:41:38 +08:00
class WeaponInfo
{
public WeaponInfo(XmlNode node)
{
if (node.Attributes["type"].Value != "CWeaponInfo")
{
throw new Exception("Not a CWeaponInfo node");
}
foreach (XmlNode info in node.ChildNodes)
{
switch (info.Name)
{
case "Name":
Name = info.InnerText; break;
case "Audio":
Audio = info.InnerText; break;
case "FireType":
FireType = info.InnerText; break;
case "DamageType":
DamageType = info.InnerText; break;
case "Damage":
Damage = info.GetFloat(); break;
case "Speed":
Speed = info.GetFloat(); break;
}
}
IsVehicleWeapon = Name.StartsWith("VEHICLE_WEAPON");
}
public string Name;
public string Audio;
public string FireType;
public string DamageType;
public float Damage;
public float Speed;
public bool IsVehicleWeapon;
}
2022-10-19 19:07:46 +08:00
public static class Program
{
2022-10-21 19:41:38 +08:00
public static float GetFloat(this XmlNode n)
{
return float.Parse(n.Attributes["value"].Value);
}
public static void Main()
{
Dictionary<uint, WeaponInfo> weapons = new();
foreach (var f in Directory.GetFiles("meta", "*.meta"))
{
Parse(f, weapons);
}
Directory.CreateDirectory("Weapons");
File.WriteAllText("Weapons\\Weapons.json", JsonConvert.SerializeObject(weapons, Formatting.Indented));
DumpWeaponHash(weapons, true);
}
static void Parse(string filename, Dictionary<uint, WeaponInfo> weap)
{
Console.WriteLine("Parsing " + filename);
var doc = new XmlDocument();
try
{
doc.Load(filename);
}
catch (Exception ex)
{
Console.WriteLine(ex); return;
}
var nodes = doc.ChildNodes.ToList();
if (nodes.Any(x => x.Name == "CWeaponInfoBlob"))
{
var infosNode = doc.GetElementsByTagName("Item");
foreach (XmlNode n in infosNode)
{
if (n.Attributes["type"]?.Value == "CWeaponInfo")
{
var info = new WeaponInfo(n);
if (!info.Name.StartsWith("VEHICLE_WEAPON") && !info.Name.StartsWith("WEAPON"))
{ continue; }
var hash = Hash(info.Name);
if (weap.ContainsKey(hash))
{
weap[hash] = info;
}
else
{
weap.Add(hash, info);
}
}
}
}
}
static void DumpWeaponHash(Dictionary<uint, WeaponInfo> weapons, bool sort = false, string path = @"Weapons\WeaponHash.cs")
{
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();
foreach (var info in vehWeaps)
{
lines.Add($"{CoreUtils.FormatToSharpStyle(info.Value.Name)} = {info.Key.ToHex()}");
}
if (sort)
{
lines.Sort();
}
foreach (var l in lines)
{
output.Append($"\r\n\t{l},");
}
output.Append("\r\n}");
File.WriteAllText(path, output.ToString());
}
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
static UInt32 Hash(string key)
{
2022-10-21 19:41:38 +08:00
key = key.ToLower();
2022-10-19 19:07:46 +08:00
int i = 0;
uint hash = 0;
while (i != key.Length)
{
hash += key[i++];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
}
}