Skip to content

Commit

Permalink
Add cost exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanKell committed Sep 28, 2023
1 parent be9c727 commit b53cc37
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Source/CostExporter/CostExporter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>C:\Users\nderaney\Documents\GitHub\RP-0\Source\Tech Tree\Parts Browser\</OutputPath>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<PropertyGroup>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

</Project>
168 changes: 168 additions & 0 deletions Source/CostExporter/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace CostExporter
{
class Program
{
class PBData
{
public string name { get; set; }
public string title { get; set; }
public string description { get; set; }
public string mod { get; set; }
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public int cost { get; set; }
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public int entry_cost { get; set; }
public string category { get; set; }
public string info { get; set; }
public string year { get; set; }
public string technology { get; set; }
public string era { get; set; }
public bool ro { get; set; }
public bool rp0 { get; set; }
public bool orphan { get; set; }
public bool rp0_conf { get; set; }
public string spacecraft { get; set; }
public string engine_config { get; set; }
public bool upgrade { get; set; }
public string entry_cost_mods { get; set; }
public string identical_part_name { get; set; }
public List<string> module_tags { get; set; }

public void Fix()
{
if (era == null)
era = string.Empty;
}
}

public class CostData
{
public string name { get; set; }
public string config { get; set; }
public int cost { get; set; }
}

private static string FixJSON(string json)
{
var ret = json.Replace("\r\n \"rp0\": false,", string.Empty);
ret = ret.Replace("\r\n ", "\r\nxxxx").Replace("\r\nxxxx ", "\r\nxxxxxxxx").Replace("\r\nxxxxxxxx ", "\r\n ");
ret = ret.Replace("\r\nxxxxxxxx", "\r\n ").Replace("\r\nxxxx", "\r\n ");
return ret;
}

static void Main(string[] args)
{
var dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data"));
List<PBData> parts = new List<PBData>();
List<PBData> configs = new List<PBData>();
foreach (var fi in dir.GetFiles())
{
var dest = fi.Name == "Engine_Config.json" ? configs : parts;
string fileData = File.ReadAllText(fi.FullName);
var data = JsonSerializer.Deserialize<List<PBData>>(fileData);
dest.AddRange(data);
}
List<CostData> newParts = JsonSerializer.Deserialize<List<CostData>>(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "costdataParts.json")));
Dictionary<string, int> newConfigs = JsonSerializer.Deserialize<Dictionary<string, int>>(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "costdataConfigs.json")));
List<string> missedParts = new List<string>();
List<string> missedConfigs = new List<string>();
string output = "Configs changed:";
foreach (var kvp in newConfigs)
{
string item = kvp.Key;
int cost = kvp.Value;

bool missing = true;
foreach (var sub in configs)
{
if (string.Equals(sub.name, item, StringComparison.InvariantCultureIgnoreCase))
{
missing = false;
if (sub.cost != cost)
{
output += "\n" + item + " : was " + sub.cost + ", now " + cost;
sub.cost = cost;
}
}
}
if (missing)
missedConfigs.Add(item);
}

foreach (var c in newParts)
{
bool missing = true;
foreach (var sub in parts)
{
if (string.Equals(sub.engine_config, c.config, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(sub.title, c.name, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(sub.entry_cost_mods, c.config, StringComparison.InvariantCultureIgnoreCase))
{

missing = false;
break;
}
}
if (missing)
missedParts.Add(c.config + " : " + c.name);
}

foreach (var pb in configs)
pb.Fix();
var opts = new JsonSerializerOptions(JsonSerializerDefaults.General);
opts.WriteIndented = true;
var files = dir.GetFiles();
output += "\n\nParts changed:";
foreach (var fi in files)
{
bool isConfigs = fi.Name == "Engine_Config.json";
if (isConfigs)
{
string cfgText = JsonSerializer.Serialize(configs, opts);
cfgText = FixJSON(cfgText);
File.WriteAllText(fi.FullName, cfgText);
continue;
}

var data = JsonSerializer.Deserialize<List<PBData>>(File.ReadAllText(fi.FullName));
foreach (var pb in data)
pb.Fix();
foreach (var c in newParts)
{
foreach (var sub in data)
{
if (string.Equals(sub.engine_config, c.config, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(sub.title, c.name, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(sub.entry_cost_mods, c.config, StringComparison.InvariantCultureIgnoreCase))
{
if (sub.cost != c.cost)
{
output += "\n" + sub.name + "(" + sub.title + ") : was " + sub.cost + ", now " + c.cost;
sub.cost = c.cost;
}
}
}
}
string text = JsonSerializer.Serialize(data, opts);
text = FixJSON(text);
File.WriteAllText(fi.FullName, text);
}

string logStr = "\nMissing parts:";
foreach (var s in missedParts)
logStr += "\n" + s;
logStr += "\n\nMissing Configs:";
foreach (var s in missedConfigs)
logStr += "\n" + s;

Console.WriteLine(logStr + "\n\nChanges:\n" + output);
}
}
}
10 changes: 10 additions & 0 deletions Source/RP0.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RP0InstallChecker", "Instal
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameData", "GameData.csproj", "{1388557C-2FD6-4A0F-A276-BEC4A94D5808}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CostExporter", "CostExporter\CostExporter.csproj", "{46642BB3-C918-4638-A6B3-64D0F57975A4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -55,6 +57,14 @@ Global
{1388557C-2FD6-4A0F-A276-BEC4A94D5808}.Debug|x64.ActiveCfg = Debug|Any CPU
{1388557C-2FD6-4A0F-A276-BEC4A94D5808}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1388557C-2FD6-4A0F-A276-BEC4A94D5808}.Release|x64.ActiveCfg = Release|Any CPU
{46642BB3-C918-4638-A6B3-64D0F57975A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{46642BB3-C918-4638-A6B3-64D0F57975A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46642BB3-C918-4638-A6B3-64D0F57975A4}.Debug|x64.ActiveCfg = Debug|Any CPU
{46642BB3-C918-4638-A6B3-64D0F57975A4}.Debug|x64.Build.0 = Debug|Any CPU
{46642BB3-C918-4638-A6B3-64D0F57975A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46642BB3-C918-4638-A6B3-64D0F57975A4}.Release|Any CPU.Build.0 = Release|Any CPU
{46642BB3-C918-4638-A6B3-64D0F57975A4}.Release|x64.ActiveCfg = Release|Any CPU
{46642BB3-C918-4638-A6B3-64D0F57975A4}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
23 changes: 23 additions & 0 deletions Source/Tech Tree/Parts Browser/CostExporter.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v5.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v5.0": {
"CostExporter/1.0.0": {
"runtime": {
"CostExporter.dll": {}
}
}
}
},
"libraries": {
"CostExporter/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
Binary file added Source/Tech Tree/Parts Browser/CostExporter.exe
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\nderaney\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\nderaney\\.nuget\\packages"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}

0 comments on commit b53cc37

Please sign in to comment.