Skip to content

Instantly share code, notes, and snippets.

@ggtylerr
Last active May 8, 2022 03:21
Show Gist options
  • Save ggtylerr/9f4b38e5409ac391ac5fa4e4725add2b to your computer and use it in GitHub Desktop.
Save ggtylerr/9f4b38e5409ac391ac5fa4e4725add2b to your computer and use it in GitHub Desktop.
Converts save data from The Stanley Parable: Ultra Deluxe into JSON (and vice versa)
using System;
using System.Diagnostics;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
class Program {
public static String EncSec = "saRpmZ6mMgZpmcojUkvkyGEQGez9YkWoXZfJdRc9ZmmJrCzfM8JksVxQfQK8uBBs";
public static void Main (string[] args) {
Console.WriteLine("ALWAYS MAKE BACKUPS!!!");
bool InputLoop = true;
while (InputLoop) {
Console.WriteLine(
"What do you want to do?" + "\n" +
"(1) - Convert to JSON" + "\n" +
"(2) - Convert to save data"
);
string input = Console.ReadLine();
if (input == "1") {
InputLoop = false;
ToJSON();
}
else if (input == "2") {
InputLoop = false;
ToSave();
}
else Console.WriteLine("Incorrect input!");
}
}
public static void ToJSON () {
Stopwatch msw = new Stopwatch();
Stopwatch sw = new Stopwatch();
Console.WriteLine("Reading File...");
msw.Start();
sw.Start();
string data = System.IO.File.ReadAllText("tspud-savedata.txt");
sw.Stop();
Console.WriteLine("Took {0}. Decrypting...", sw.Elapsed);
sw.Reset();
sw.Start();
StringBuilder _sb = new StringBuilder();
// From: https://github.com/richardelms/FileBasedPlayerPrefs/blob/master/Runtime/FBPP.cs#L277
for (int i = 0; i < data.Length; i++) {
_sb.Append((char)(data[i] ^ EncSec[i % EncSec.Length]));
}
string json = _sb.ToString();
sw.Stop();
Console.WriteLine("Took {0}. Prettying up...", sw.Elapsed);
sw.Reset();
sw.Start();
// Parse
JObject obj = JObject.Parse(json);
// Get the StringData and convert it to an object
// (This is to pretty it up since it's initially in a string)
String StrData = (String) obj["StringData"][0]["Value"];
obj["StringData"][0]["Value"] = JObject.Parse(StrData);
// Convert + Format
string pjson = JsonConvert.SerializeObject(
obj,
Formatting.Indented
);
sw.Stop();
Console.WriteLine("Took {0}. Writing...", sw.Elapsed);
sw.Reset();
sw.Start();
System.IO.File.WriteAllText("tspud-savedata.json", pjson);
sw.Stop();
msw.Stop();
Console.WriteLine("Took {0}. Total operation took {1}.", sw.Elapsed, msw.Elapsed);
Console.WriteLine("Your file is in tspud-savedata.json!");
}
public static void ToSave() {
Stopwatch msw = new Stopwatch();
Stopwatch sw = new Stopwatch();
Console.WriteLine("Reading File...");
msw.Start();
sw.Start();
string json = System.IO.File.ReadAllText("tspud-savedata.json");
sw.Stop();
Console.WriteLine("Took {0}. De-prettying...", sw.Elapsed);
sw.Reset();
sw.Start();
JObject obj = JObject.Parse(json);
// Assuming that the save data came from this tool,
// and thus converting the object back into a string.
JObject StrData = (JObject) obj["StringData"][0]["Value"];
obj["StringData"][0]["Value"] = JsonConvert.SerializeObject(StrData);
string raw = JsonConvert.SerializeObject(obj);
sw.Stop();
Console.WriteLine("Took {0}. Encrypting...", sw.Elapsed);
sw.Reset();
sw.Start();
StringBuilder _sb = new StringBuilder();
// From: https://github.com/richardelms/FileBasedPlayerPrefs/blob/master/Runtime/FBPP.cs#L277
for (int i = 0; i < raw.Length; i++) {
_sb.Append((char)(raw[i] ^ EncSec[i % EncSec.Length]));
}
string data = _sb.ToString();
sw.Stop();
Console.WriteLine("Took {0}. Writing...", sw.Elapsed);
sw.Reset();
sw.Start();
System.IO.File.WriteAllText("tspud-savedata.txt", data);
sw.Stop();
msw.Stop();
Console.WriteLine("Took {0}. Total operation took {1}.", sw.Elapsed, msw.Elapsed);
Console.WriteLine("Your file is in tspud-savedata.txt!");
}
}
@ggtylerr
Copy link
Author

ggtylerr commented May 8, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment