Skip to content

Instantly share code, notes, and snippets.

@LambdaSix
Last active January 30, 2020 13:05
Show Gist options
  • Select an option

  • Save LambdaSix/97fde8aee7719a5be8992057c3a52e72 to your computer and use it in GitHub Desktop.

Select an option

Save LambdaSix/97fde8aee7719a5be8992057c3a52e72 to your computer and use it in GitHub Desktop.
Space engineers recipe extraction.
void Main()
{
var modBasePath = @"C:\source\repos\Personal\SpaceEngineers\";
var filePaths = Directory.EnumerateFiles($"{modBasePath}\\Data\\", "Blueprints_Components_*.sbc");
var overMap = new List<Dictionary<string,BlueprintInfo>>();
foreach (var filePath in filePaths) {
var document = XDocument.Load(filePath);
var map = new Dictionary<string,BlueprintInfo>();
foreach (var blueprint in document.Root.Element("Blueprints").Elements("Blueprint")){
var blueprintName = blueprint.Element("Id").Element("SubtypeId").Value;
var outputs = new List<ComponentInfo>();
if (blueprint.Element("Result") != null) {
var element = blueprint.Element("Result");
outputs = new []{new ComponentInfo(element.Attribute("Amount").Value, $"{element.Attribute("TypeId").Value}/{element.Attribute("SubtypeId").Value}")}.ToList();
} else if (blueprint.Element("Results") != null) {
outputs = blueprint.Dump().Element("Results").Elements("Item").Dump().AsEnumerable().Select(s =>
new ComponentInfo(s.Attribute("Amount").Value, $"{s.Attribute("TypeId").Value}/{s.Attribute("SubtypeId").Value}")
).ToList();
}
var blueprintInfo = new BlueprintInfo(blueprintName, outputs);
foreach (var requisite in blueprint.Element("Prerequisites").Elements("Item")){
var info = new ComponentInfo(requisite.Attribute("Amount").Value, $"{requisite.Attribute("TypeId").Value}/{requisite.Attribute("SubtypeId").Value}");
blueprintInfo.Components.Add(info);
}
map.Add(blueprintName, blueprintInfo);
}
overMap.Add(map);
}
overMap.Dump();
//JsonConvert.SerializeObject(overMap).Dump();
var sb = new StringBuilder();
sb.AppendLine("new Dictionary<string,ComponentInfo>() {");
foreach (var map in overMap) {
foreach (var subMap in map) {
var value = subMap.Value;
var output = subMap.Value.Outputs[0];
sb.AppendLine($"[\"{output.Amount}:{output.TypeId}\"] = new ComponentList {{");
foreach (var component in value.Components) {
sb.AppendLine($"\t\"{component.Amount}:{component.TypeId}\",");
}
sb.AppendLine("},");
}
}
sb.AppendLine("};");
Console.Write(sb.ToString());
}
// Define other methods and classes here
class ComponentInfo {
public float Amount {get;set;}
public string TypeId {get;set;}
public ComponentInfo(string amount, string typeId) {
Amount = Single.Parse(amount);
TypeId = typeId;
}
}
class BlueprintInfo {
public string BlueprintName {get;set;}
public List<ComponentInfo> Outputs {get;set;}
public List<ComponentInfo> Components {get;set;}
public BlueprintInfo(string name, List<ComponentInfo> outputs,List<ComponentInfo> components = null) {
BlueprintName = name;
Outputs = outputs;
Components = components ?? new List<ComponentInfo>();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace IngameScript
{
public class RecipeMap
{
public class ComponentList : IEnumerable<ComponentInfo>
{
private ICollection<ComponentInfo> _componentList;
public ComponentList()
{
_componentList = new List<ComponentInfo>();
}
public void Add(string info)
{
_componentList.Add(new ComponentInfo(info));
}
public IEnumerator<ComponentInfo> GetEnumerator()
{
return _componentList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) _componentList).GetEnumerator();
}
}
public class ComponentInfo
{
public float Amount { get; set; }
public string TypeId { get; set; }
public ComponentInfo(string amount, string typeId)
{
Amount = Single.Parse(amount);
TypeId = typeId;
}
public ComponentInfo(string info)
{
var split = info.Split(':');
Amount = Single.Parse(split[0]);
TypeId = split[1];
}
}
// AUTOGENERATED CODE FOLLOWS:
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment