Skip to content

Commit

Permalink
save file annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Cattlesquat committed Dec 23, 2020
1 parent 3ed7701 commit 99cd1a3
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 24 deletions.
2 changes: 0 additions & 2 deletions DeathRun/DeathRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,6 @@ public static void Patch()
{
foreach (KeyValuePair<TechType,TechData> tech in techChanges)
{
CattleLogger.Message("Change to: " + tech.Key);

CraftDataHandler.SetTechData(tech.Key, tech.Value);
}
}
Expand Down
84 changes: 72 additions & 12 deletions DeathRun/DeathRunUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ public static string sayBriefTime(TimeSpan time, bool noDays)
"Survive longer? Higher score. Win fastest? Highest Score.",
"Hold a fish in your hand: many enemies will bite it instead of you!",
"Famous Last Words: I'll just leave my pump here for a minute.",
"You swim faster when you aren't holding something."
"You swim faster when you aren't holding something.",
"Low on air? Top off at a friendly brain coral!"
};


Expand Down Expand Up @@ -374,6 +375,24 @@ public static void HideHighScores(bool should)
highScoreScores[row].Hide();
}
}


static Dictionary<string, DeathRunSaveData> saveList = new Dictionary<string, DeathRunSaveData>();

public static void RegisterSave(string slotName, DeathRunSaveData saveData)
{
saveList.Add(slotName, saveData);
}

public static DeathRunSaveData FindSave(string slotName)
{
DeathRunSaveData saveData;
if (saveList.TryGetValue(slotName, out saveData))
{
return saveData;
}
return null;
}
}

public class RunData
Expand Down Expand Up @@ -457,11 +476,19 @@ public void countSettings()
}


public void updateFromSave(DeathRunSaveData saveData)
{
RunTime = saveData.playerSave.allLives;
Deaths = saveData.playerSave.numDeaths + 1;
calcScore();
}


public void updateVitals (bool victory)
{
Cause = DeathRun.cause;
Cause = DeathRun.cause;
RunTime = DeathRun.saveData.playerSave.allLives;
Deaths = DeathRun.saveData.playerSave.numDeaths;
Deaths = DeathRun.saveData.playerSave.numDeaths;
Victory = victory;

countSettings();
Expand All @@ -485,7 +512,7 @@ public int calcScore()
{
float timeVal = 0;
float timeLeft = RunTime;
do
while (timeLeft > 0)
{
if (timeLeft <= 3600)
{
Expand All @@ -497,7 +524,7 @@ public int calcScore()
timeVal += 3600;
timeLeft = (timeLeft - 3600) / 2;
}
} while (timeLeft > 0);
};

float vehicleVal = 0;
if ((VehicleFlags & FLAG_SEAGLIDE) != 0)
Expand Down Expand Up @@ -546,9 +573,15 @@ public int calcScore()
totalVal = totalVal / Deaths;
}

int numerator = DeathRunSettingCount + DeathRunSettingBonus / 2;

Score = (int)(totalVal * numerator / MAX_DEATHRUN_SETTING_COUNT);
if (DeathRunSettingCount >= 0)
{
Score = (int)(totalVal * (DeathRunSettingCount + DeathRunSettingBonus / 2) / MAX_DEATHRUN_SETTING_COUNT);
}
else
{
Score = (int)totalVal;
}

if (Score > 99999) Score = 99999;

Expand Down Expand Up @@ -607,8 +640,6 @@ public DeathRunSaveData()
countSave = new CountdownSaveData();
runData = new RunData();

CattleLogger.Message("SaveData Constructor");

setDefaults();
}

Expand Down Expand Up @@ -697,6 +728,38 @@ public void Load()
setDefaults();
}
}


/**
* This deserializes a byte stream we've retrieved from other sources (used on the main menu)
*/
public static bool LoadFromBytes(byte[] bytes, out DeathRunSaveData target)
{
try
{
string @jsonData = Encoding.UTF8.GetString(bytes);

var jsonSerializerSettings = new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
};


// This deserializes the whole saveData object all at once.
target = JsonConvert.DeserializeObject<DeathRunSaveData>(jsonData, jsonSerializerSettings);
}
catch (Exception e)
{
CattleLogger.GenericError(e);
CattleLogger.Message("Death Run thumbnail data not found - using defaults");
CattleLogger.Message(e.StackTrace);
target = null;
return false;
}

return true;
}
}


Expand Down Expand Up @@ -812,11 +875,8 @@ public int addRun(RunData run)
bool added = false;
for (place = 0; place < HighScores.Count; place++)
{
CattleLogger.Message("Place: " + place + " Existing: " + HighScores[place].Score + " Comparing: " + run.Score);
if (!run.betterThan(HighScores[place])) continue;

CattleLogger.Message("BETTER THAN!");

// Add our score to the list
HighScores.Insert(place, run);
added = true;
Expand Down
99 changes: 99 additions & 0 deletions DeathRun/Patchers/MainMenuPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace DeathRun.Patchers
using Items;
using Common;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Collections.Generic;

[HarmonyPatch(typeof(uGUI_MainMenu))]
[HarmonyPatch("OnButtonSurvival")]
Expand Down Expand Up @@ -111,4 +114,100 @@ public static void Postfix()
DeathRunUtils.ShowHighScores(true);
}
}


/**
* UpdateLoadButtonState -- this lets us annotate the load buttons for individual games.
*/
[HarmonyPatch(typeof(MainMenuLoadPanel))]
[HarmonyPatch("UpdateLoadButtonState")]
internal class MainMenuLoadPanel_UpdateLoadButtonState_Patch
{
[HarmonyPostfix]
public static void Postfix(MainMenuLoadPanel __instance, MainMenuLoadButton lb)
{
SaveLoadManager.GameInfo gameInfo = SaveLoadManager.main.GetGameInfo(lb.saveGame);

if ((gameInfo == null) || !gameInfo.IsValid() || gameInfo.isFallback)
{
return;
}

DeathRunSaveData slotData = DeathRunUtils.FindSave(lb.saveGame);
if (slotData == null)
{
return;
}

if (!"".Equals(slotData.startSave.message))
{
lb.load.FindChild("SaveGameMode").GetComponent<Text>().text = slotData.startSave.message;

slotData.runData.updateFromSave(slotData);

string duration = Utils.PrettifyTime((int)slotData.playerSave.allLives);

if (slotData.playerSave.allLives >= 60 * 60 * 24)
{
int total = (int)slotData.playerSave.allLives;
int days = total / (60 * 60 * 24);
total -= (days * 60 * 60 * 24);

int hours = total / (60 * 60);
total -= hours * 60 * 60;

int minutes = total / 60;
total -= minutes;

duration = "" + days + " " + ((days == 1) ? "day" : "days") + ", " + hours + ":" + ((minutes < 10) ? "0" : "") + minutes;
}

duration += ". Score: " + slotData.runData.Score;

lb.load.FindChild("SaveGameLength").GetComponent<Text>().text = duration;
}
}
}

/**
* LoadSlotsAsync - in Unity's insane user files system, we add the name of our JSON file to the list of files for it to retrieve from the
* "save slot". This is entirely so we can have access to it when the list of saved games is being created on the main menu.
*/
[HarmonyPatch(typeof(UserStoragePC))]
[HarmonyPatch("LoadSlotsAsync")]
internal class LoadSlots_Patch
{
[HarmonyPrefix]
private static bool Prefix(ref List<string> fileNames)
{
fileNames.Add(DeathRun.SaveFile);
return true;
}
}

/**
* RegisterSaveGame - as saved games get "registered" to each slot on the load menu, we record a copy of the DeathRun JSON, so that we
* will have access to it when the individual load slots are being drawn. Did I mention that Unity's user file system is insane?
*/
[HarmonyPatch(typeof(SaveLoadManager))]
[HarmonyPatch("RegisterSaveGame")]
internal class RegisterSaveGame_Patch
{
[HarmonyPostfix]
private static void Postfix(string slotName, UserStorageUtils.LoadOperation loadOperation)
{
byte[] bytes;

if (loadOperation.GetSuccessful()) {
if (loadOperation.files.TryGetValue(DeathRun.SaveFile, out bytes))
{
DeathRunSaveData saveData = new DeathRunSaveData();
if (DeathRunSaveData.LoadFromBytes(bytes, out saveData))
{
DeathRunUtils.RegisterSave(slotName, saveData);
}
}
}
}
}
}
11 changes: 7 additions & 4 deletions DeathRun/Patchers/NitroLevPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,20 @@ public static bool Prefix (ref NitrogenLevel __instance)
{
if (DeathRun.murkinessDirty)
{
WaterBiomeManager.main.Rebuild();
DeathRun.murkinessDirty = false;
if (WaterBiomeManager.main != null)
{
WaterBiomeManager.main.Rebuild();
DeathRun.murkinessDirty = false;
}
}

// Nitrogen tracking doesn't start until player leaves the pod (for underwater starts)
if (!EscapePod.main.topHatchUsed && !EscapePod.main.bottomHatchUsed)
if ((EscapePod.main == null) || (!EscapePod.main.topHatchUsed && !EscapePod.main.bottomHatchUsed))
{
return false;
}

if (DeathRun.playerIsDead)
if (DeathRun.playerIsDead || (Player.main == null) || (Ocean.main == null) || (Inventory.main == null))
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions DeathRun/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.1.0")]
[assembly: AssemblyFileVersion("1.4.1.0")]
[assembly: AssemblyVersion("1.5.1.0")]
[assembly: AssemblyFileVersion("1.5.1.0")]
5 changes: 4 additions & 1 deletion DeathRun/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEATH RUN - 1.4.1 - by Cattlesquat "standing on the shoulders of giants"
DEATH RUN - 1.5.1 - by Cattlesquat "standing on the shoulders of giants"

Turns Subnautica into a "roguelike" where everything is harder and the only real question is how LONG you can survive.

Expand Down Expand Up @@ -31,6 +31,9 @@ Turns Subnautica into a "roguelike" where everything is harder and the only real
• 1.4 - Gradually adding to "Tips" section
• 1.4 - (Fixes) Various bugfixes
-------------------------------------------------------------------------------------------------------------
• 1.5 - Saved games now show current score & start spot
• 1.5 - (Fixes) Fixed an occasional null pointer in nitrogen code
-------------------------------------------------------------------------------------------------------------


:: GENERAL OTHER STUFF ::
Expand Down
2 changes: 1 addition & 1 deletion DeathRun/mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Id": "DeathRun",
"DisplayName": "DeathRun",
"Author": "Cattlesquat",
"Version": "1.4.1",
"Version": "1.5.1",
"Enable": true,
"AssemblyName": "DeathRun.dll",
"EntryMethod": "DeathRun.DeathRun.Patch",
Expand Down
5 changes: 4 additions & 1 deletion QMods/DeathRun/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEATH RUN - 1.4.1 - by Cattlesquat "standing on the shoulders of giants"
DEATH RUN - 1.5.1 - by Cattlesquat "standing on the shoulders of giants"

Turns Subnautica into a "roguelike" where everything is harder and the only real question is how LONG you can survive.

Expand Down Expand Up @@ -31,6 +31,9 @@ Turns Subnautica into a "roguelike" where everything is harder and the only real
• 1.4 - Gradually adding to "Tips" section
• 1.4 - (Fixes) Various bugfixes
-------------------------------------------------------------------------------------------------------------
• 1.5 - Saved games now show current score & start spot
• 1.5 - (Fixes) Fixed an occasional null pointer in nitrogen code
-------------------------------------------------------------------------------------------------------------


:: GENERAL OTHER STUFF ::
Expand Down
2 changes: 1 addition & 1 deletion QMods/DeathRun/mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Id": "DeathRun",
"DisplayName": "DeathRun",
"Author": "Cattlesquat",
"Version": "1.4.1",
"Version": "1.5.1",
"Enable": true,
"AssemblyName": "DeathRun.dll",
"EntryMethod": "DeathRun.DeathRun.Patch",
Expand Down

0 comments on commit 99cd1a3

Please sign in to comment.