forked from GreyHak/dsp-csv-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSPSpreadsheetGenMod.cs
299 lines (271 loc) · 14.2 KB
/
DSPSpreadsheetGenMod.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// Copyright (c) 2021, Aaron Shumate
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE.txt file in the root directory of this source tree.
//
// Dyson Sphere Program is developed by Youthcat Studio and published by Gamera Game.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using System.IO;
using BepInEx.Logging;
using System.Security;
namespace StarSectorResourceSpreadsheetGenerator
{
[BepInPlugin(pluginGuid, pluginName, pluginVersion)]
[BepInProcess("DSPGAME.exe")]
public class SpreadsheetGenMod : BaseUnityPlugin // Plugin config: "C:\Program Files (x86)\Steam\steamapps\common\Dyson Sphere Program\BepInEx\config\BepInEx.cfg"
{
public const string pluginGuid = "greyhak.dysonsphereprogram.resourcespreadsheetgen";
public const string pluginName = "DSP Star Sector Resource Spreadsheet Generator";
public const string pluginVersion = "1.1.0.0";
public static bool spreadsheetGenRequestFlag = false;
public static string spreadsheetFileName = "default.csv";
new internal static ManualLogSource Logger;
new internal static BepInEx.Configuration.ConfigFile Config;
public void Awake()
{
SpreadsheetGenMod.Logger = base.Logger; // "C:\Program Files (x86)\Steam\steamapps\common\Dyson Sphere Program\BepInEx\LogOutput.log"
SpreadsheetGenMod.Config = base.Config;
// Determine the default spreadsheet path and configured spreadsheet path.
spreadsheetFileName = "DSP_Star_Sector_Resources.csv";
if (Environment.GetEnvironmentVariable("USERPROFILE") != null)
{
spreadsheetFileName = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Documents\" + spreadsheetFileName);
}
spreadsheetFileName = Config.Bind<string>("Output", "SpreadsheetFileName", spreadsheetFileName, "Path to the output spreadsheet.").Value;
Logger.LogInfo("Will use spreadsheet path \"" + spreadsheetFileName + "\"");
Harmony harmony = new Harmony(pluginGuid);
System.Reflection.MethodInfo originalBegin = AccessTools.Method(typeof(GameMain), "Begin");
System.Reflection.MethodInfo originalPause = AccessTools.Method(typeof(GameMain), "Pause");
System.Reflection.MethodInfo originalPlanetLoadPrim = AccessTools.Method(typeof(PlanetData), "NotifyLoaded"); // This one works for each planet with a delay. This acts more like a reoccuring interval even onces the requested loads are complete.
System.Reflection.MethodInfo originalPlanetLoadAlt = AccessTools.Method(typeof(PlanetAlgorithm), "GenerateVeins"); // This one works for each planet immediately.
System.Reflection.MethodInfo myQueueLoading = AccessTools.Method(typeof(SpreadsheetGenMod), "QueuePlanetLoading");
System.Reflection.MethodInfo myNotifyLoaded = AccessTools.Method(typeof(SpreadsheetGenMod), "OnPlanetFactoryLoaded");
harmony.Patch(originalBegin, new HarmonyMethod(myQueueLoading)); // Run mine before
harmony.Patch(originalPause, new HarmonyMethod(myQueueLoading)); // Run mine before
harmony.Patch(originalPlanetLoadPrim, null, new HarmonyMethod(myNotifyLoaded)); // Run mine after
Logger.LogInfo("Initialization complete.");
}
// Called on save load and game pause. Queues planet loading which will trigger OnFactoryLoaded().
public static void QueuePlanetLoading()
{
if (GameMain.gameName == "0")
{
SpreadsheetGenMod.Logger.LogInfo("Ignoring load screen.");
return;
}
SpreadsheetGenMod.Logger.LogInfo("Checking for planets to load...");
uint loadRequests = 0;
foreach (StarData star in GameMain.universeSimulator.galaxyData.stars)
{
foreach (PlanetData planet in star.planets)
{
if ((planet.type != EPlanetType.Gas) && (planet.veinGroups.Length == 0))
{
// PlanetModelingManager.PlanetComputeThreadMain (static, but private) -> PlanetAlgorithm.GenerateVeins
//PlanetModelingManager.Algorithm(planet).GenerateVeins(false); // Fails when called directly because something is null.
planet.Load();
loadRequests++;
}
}
}
if (loadRequests == 0)
{
SpreadsheetGenMod.Logger.LogInfo("Planets already loaded. Proceeding with resource spreadsheet generation.");
GenerateResourceSpreadsheet();
}
else
{
var sb = new StringBuilder();
sb.AppendFormat("Requested {0} planets be loaded. Waiting for planets to load.", loadRequests);
SpreadsheetGenMod.Logger.LogInfo(sb.ToString());
SpreadsheetGenMod.spreadsheetGenRequestFlag = true;
}
}
// Called when each planet loads. When all planets are loaded, will call GenerateResourceSpreadsheet().
public static void OnPlanetFactoryLoaded()
{
//SpreadsheetGenMod.Logger.LogInfo("Planet loaded.");
if (SpreadsheetGenMod.spreadsheetGenRequestFlag)
{
SpreadsheetGenMod.Logger.LogInfo("Checking if there are still unloaded planets...");
uint unloadedPlanetCount = 0;
foreach (StarData star in GameMain.universeSimulator.galaxyData.stars)
{
foreach (PlanetData planet in star.planets)
{
if ((planet.type != EPlanetType.Gas) && (planet.veinGroups.Length == 0))
{
unloadedPlanetCount++;
}
}
}
if (unloadedPlanetCount == 0)
{
SpreadsheetGenMod.spreadsheetGenRequestFlag = false;
SpreadsheetGenMod.Logger.LogInfo("Planet loading completed. Proceeding with resource spreadsheet generation.");
GenerateResourceSpreadsheet();
}
}
}
// Called when all planets are loaded. Saves resource spreadsheet.
public static void GenerateResourceSpreadsheet()
{
try
{
SpreadsheetGenMod.Logger.LogInfo("Begin resource spreadsheet generation...");
var sb = new StringBuilder();
sb.Append("Planet Name,Star Name,Star Luminosity,Star Type,Star Mass,Star Position X,Star Position Y,Star Position Z,Wind Strength,Luminosity,Planet Type,Land Percent,Singularity,Planet/Moon,Orbit Inclination,Ocean,");
//sb.Append("Ocean,Iron Ore,Copper Ore,Silicon Ore,Titanium Ore,Stone Ore,Coal Ore,Crude Oil,Fire Ice,Kimberlite Ore,Fractal Silicon,Spiniform Stalagmite Crystal,Optical Grating Crystal,Bamboo,Unipolar Magnet,");
foreach (VeinProto item in LDB.veins.dataArray)
{
sb.AppendFormat("{0},", item.name);
}
int[] gases = { 1120, 1121, 1011 };
foreach (int item in gases)
{
sb.AppendFormat("{0},", LDB.items.Select(item).name);
}
sb.Append("\n");
foreach (StarData star in GameMain.universeSimulator.galaxyData.stars)
{
foreach (PlanetData planet in star.planets)
{
sb.AppendFormat("{0},", planet.displayName);
sb.AppendFormat("{0},", star.displayName);
sb.AppendFormat("{0},", star.luminosity);
sb.AppendFormat("{0},", star.typeString);
sb.AppendFormat("{0},", star.mass);
sb.AppendFormat("{0},", star.position.x);
sb.AppendFormat("{0},", star.position.y);
sb.AppendFormat("{0},", star.position.z);
sb.AppendFormat("{0},", planet.windStrength);
sb.AppendFormat("{0},", planet.luminosity);
sb.AppendFormat("{0},", planet.typeString);
sb.AppendFormat("{0},", planet.landPercent);
sb.AppendFormat("\"{0}\",", planet.singularity);
sb.AppendFormat("{0},", planet.orbitAround); // Mostly 0, but also 1-4
sb.AppendFormat("{0},", planet.orbitInclination);
if (planet.type == EPlanetType.Gas)
{
sb.Append("None,"); // Ocean
foreach (VeinProto item in LDB.veins.dataArray)
{
sb.Append("0,");
}
foreach (int item in gases)
{
int index = Array.IndexOf(planet.gasItems, item);
if (index == -1)
{
sb.Append("0,");
}
else
{
sb.AppendFormat("{0},", planet.gasSpeeds[index]);
}
}
sb.Append("\n");
}
else
{
if (planet.waterItemId == 0)
{
sb.Append("None,");
}
else if (planet.waterItemId == -1)
{
sb.Append("Lava,");
}
else
{
ItemProto waterItem = LDB.items.Select(planet.waterItemId);
sb.AppendFormat("{0},", waterItem.name);
}
if (planet.veinGroups.Length == 0)
{ // Theoretically this shouldn't happen.
planet.Load();
foreach (VeinProto item in LDB.veins.dataArray)
{
sb.Append("Unloaded,");
}
}
else
{
EVeinType type = (EVeinType)1;
foreach (VeinProto item in LDB.veins.dataArray)
{
long amount = planet.veinAmounts[(int)type];
if (type == EVeinType.Oil)
{
sb.AppendFormat("{0},", (double)amount * VeinData.oilSpeedMultiplier);
}
else
{
sb.AppendFormat("{0},", amount);
}
type++;
}
}
foreach (int item in gases)
{
sb.Append("0,");
}
sb.Append("\n");
}
}
}
// Make sure the folder we're trying to write in exists.
// {username}/Documents doesn't always exist on Wine platform.
System.IO.FileInfo file = new System.IO.FileInfo(spreadsheetFileName);
file.Directory.Create(); // If the directory already exists, this method does nothing.
File.WriteAllText(spreadsheetFileName, sb.ToString());
SpreadsheetGenMod.Logger.LogInfo("Completed saving resource spreadsheet.");
}
catch (ArgumentNullException e)
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: ArgumentNullException while generating and saving resource spreadsheet: " + e.Message);
}
catch (ArgumentException e)
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: ArgumentException while generating and saving resource spreadsheet: " + e.Message);
}
catch (PathTooLongException e)
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: PathTooLongException while generating and saving resource spreadsheet: " + e.Message);
}
catch (DirectoryNotFoundException e)
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: DirectoryNotFoundException while generating and saving resource spreadsheet: " + e.Message);
}
catch (IOException e)
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: IOException while generating and saving resource spreadsheet: " + e.Message);
}
catch (UnauthorizedAccessException e)
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: UnauthorizedAccessException while generating and saving resource spreadsheet: " + e.Message);
}
catch (NotSupportedException e)
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: NotSupportedException while generating and saving resource spreadsheet: " + e.Message);
}
catch (SecurityException e)
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: SecurityException while generating and saving resource spreadsheet: " + e.Message);
}
catch
{
SpreadsheetGenMod.Logger.LogInfo("ERROR: Exception (catch-all) while generating and saving resource spreadsheet.");
}
}
}
}