Skip to content

Commit

Permalink
Merge branch '1.5.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
dommrogers committed Dec 12, 2023
2 parents eabb749 + a2e91aa commit 8d2dcc9
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 151 deletions.
6 changes: 3 additions & 3 deletions ModData.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<!--This is an xml comment. Comments have no impact on compiling.-->

<PropertyGroup>
Expand Down Expand Up @@ -28,7 +28,7 @@
<!--This is for NuGet.-->
<PropertyGroup>
<Authors>STBlade</Authors>
<Version>1.5.1</Version>
<Version>1.5.2</Version>
<AssemblyVersion>1.5.1</AssemblyVersion>
<PackageId>stblade.Modding.TLD.ModData</PackageId>
<PackageTags>MelonLoader Modding TLD Custom Save Game Data Utility</PackageTags>
Expand All @@ -47,7 +47,7 @@
<!--This is the of packages that the mod references.-->
<ItemGroup>
<!--This package contains almost everything a person could possibly need to reference while modding.-->
<PackageReference Include="ds5678.Modding.TLD.Il2CppAssemblies.Windows" Version="2.6.0" />
<PackageReference Include="STBlade.Modding.TLD.Il2CppAssemblies.Windows" Version="2.25.0" />
<!--The package version here in this template may be outdated and need to be updated. Visual Studio can update package versions automatically.-->
<!--If the mod references any other mods (such as ModSettings), that NuGet package will also need to be listed here.-->
</ItemGroup>
Expand Down
119 changes: 65 additions & 54 deletions ModData/ModDataCore.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Diagnostics;

namespace ModData;

internal class ModDataCore
{
internal static string ModDataEntryName { get; } = "ModData.txt";
internal static string modDataFolderName { get; } = "ModData";
internal static string modDataFileExt { get; } = ".moddata";
internal static string modDataRoot { get; private set; } = "";
Expand All @@ -12,41 +13,46 @@ internal class ModDataCore

internal static Dictionary<string, string> dataCache = new();

internal static bool initComplete = false;
internal static bool cacheLoaded = false;
internal static bool cacheChanged = false;

internal static void InitModDataCore(string saveName)
{
if (initComplete && cacheLoaded)
{
return;
}
InitModDataRoot();
DebugMsg("Loading slot " + saveName);
DebugMsg($"Init ModData: {saveName}");
modDataSaveSlotName = saveName;
InitModDataSaveSlot();

initComplete = true;
LoadCache();
cacheLoaded = true;


}

internal static void InitModDataRoot()
{
modDataRoot = Path.Combine(MelonEnvironment.ModsDirectory, modDataFolderName);
if (!Directory.Exists(modDataRoot))
{
DebugMsg("Creating modDataRoot (" + modDataRoot + ")");
Directory.CreateDirectory(modDataRoot);
// DebugMsg($"InitModDataRoot: {modDataRoot}");
}
}

internal static void InitModDataSaveSlot()
{
if (modDataSaveSlotName == null)
{
DebugMsg("No SaveSlot loaded.");
//DebugMsg("No SaveSlot loaded.");
return;
}
modDataSaveSlotFile = Path.Combine(modDataRoot, modDataSaveSlotName + modDataFileExt);

// create and/or open the saveSlotFile
if (!File.Exists(modDataSaveSlotFile))
{
DebugMsg("Creating modDataSaveSlotFile (" + modDataSaveSlotFile + ")");
ZipUtils.CreateEmptyFile(modDataSaveSlotFile);
}
// DebugMsg($"InitModDataSaveSlot: {modDataSaveSlotFile}");
}

internal static void DeleteModDataSaveSlot(string slotName)
Expand All @@ -57,20 +63,22 @@ internal static void DeleteModDataSaveSlot(string slotName)

if (File.Exists(modDataSaveSlotFile))
{
DebugMsg("Deleting modDataSaveSlotFile (" + modDataSaveSlotFile + ")");
DebugMsg($"Delete: {modDataSaveSlotFile}");
File.Delete(modDataSaveSlotFile);
}

}

internal static void CloseModDataSaveSlot()
{
if (modDataSaveSlotName != null)
if (initComplete)
{
modDataSaveSlotName = null;
modDataSaveSlotFile = null;
dataCache.Clear();
DebugMsg("CloseModDataSaveSlot");
initComplete = false;
cacheLoaded = false;
// DebugMsg($"CloseModDataSaveSlot: {modDataSaveSlotFile}");
}
}

Expand All @@ -81,18 +89,28 @@ internal static bool WriteEntry(string entryName, string entryData, string? entr
{
entryName += "_" + entrySuffix;
}
if (modDataSaveSlotFile == null)
if (!cacheLoaded)
{
DebugMsg("No SaveSlot loaded.");
// DebugMsg($"WriteEntry: No Cache. {entryName}");
return false;
}


if (dataCache.ContainsKey(entryName) && dataCache[entryName] == entryData)
{
// DebugMsg($"WriteEntry: {entryName} {entryData.Length} NO CHANGE");
return true;
}

if (dataCache.ContainsKey(entryName))
{
dataCache.Remove(entryName);
}

dataCache.Add(entryName, entryData);
// DebugMsg($"WriteEntry: {entryName} {entryData.Length} {entryData.Trim().Length}");

cacheChanged = true;
return true;
}

Expand All @@ -102,9 +120,9 @@ internal static bool WriteEntry(string entryName, string entryData, string? entr
{
entryName += "_" + entrySuffix;
}
if (modDataSaveSlotFile == null)
if (!cacheLoaded)
{
DebugMsg("No SaveSlot loaded.");
// DebugMsg($"ReadEntry: No Cache. {entryName}");
return null;
}

Expand All @@ -118,59 +136,52 @@ internal static bool WriteEntry(string entryName, string entryData, string? entr

internal static void LoadCache()
{
if (modDataSaveSlotFile == null)
{
DebugMsg("No SaveSlot loaded.");
return;
}
List<string> entries = ZipUtils.GetEntries(modDataSaveSlotFile);
if (entries != null && entries.Count > 0)
Stopwatch sw = new Stopwatch();
sw.Start();

Dictionary<string, string> entries = ZipUtils.ReadEntries(modDataSaveSlotFile);

Check warning on line 142 in ModData/ModDataCore.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'filePath' in 'Dictionary<string, string> ZipUtils.ReadEntries(string filePath)'.
foreach (var entry in entries)
{
foreach (string entry in entries)
// DebugMsg("Cache Loaded (" + entry.Key + ")");
if (entry.Value != null)
{
DebugMsg("Cache Loaded ("+ entry + ")");
string? data = ZipUtils.ReadEntry(modDataSaveSlotFile, entry);
if (data != null)
{
WriteEntry(entry, data);
}
dataCache.Add(entry.Key, entry.Value);
}
}
DebugMsg("Cache Loaded");

cacheLoaded = true;
sw.Stop();
DebugMsg($"Cache Loaded ({entries.Count}) ({sw.ElapsedMilliseconds}ms)");
}

internal static void SaveCache()
{
if (modDataSaveSlotFile == null)
if (!cacheLoaded)
{
DebugMsg("No SaveSlot loaded.");
// DebugMsg("SaveCache: No Cache.");
return;
}
if (!cacheChanged)
{
return;
}
Stopwatch sw = new Stopwatch();
sw.Start();
if (dataCache != null && dataCache.Count > 0)
{
foreach (KeyValuePair<string,string> entry in dataCache)
{
DebugMsg("Cache Saved (" + entry.Key + ")");
if (entry.Key != null && entry.Value != null)
{
string entryString = entry.Value;
if (entry.Key == ModDataEntryName)
{
entryString = ZipUtils.GetModDataLine();
}
ZipUtils.WriteEntry(modDataSaveSlotFile, entry.Key, entryString);
}
}
ZipUtils.WriteEntries(modDataSaveSlotFile, dataCache);

Check warning on line 172 in ModData/ModDataCore.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'filePath' in 'void ZipUtils.WriteEntries(string filePath, Dictionary<string, string> entries)'.
}
DebugMsg("Cache Saved");
sw.Stop();
DebugMsg($"Cache Saved ({dataCache.Count}) ({sw.ElapsedMilliseconds}ms)");
cacheChanged = false;
}

internal static void DebugMsg(string msg)
{
if (msg != null && ModDataManager.debugMode == true)
{
MelonLoader.MelonLogger.Msg(ConsoleColor.Yellow, msg);
}
//if (msg != null && ModDataManager.debugMode == true)
//{
MelonLoader.MelonLogger.Msg(ConsoleColor.Yellow, msg);
//}
}

}
4 changes: 2 additions & 2 deletions ModData/ModDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
public class ModDataManager
{
private string? currentModName = null;
internal static bool debugMode { get; private set; } = false;
internal bool debugMode { get; private set; } = true;

public ModDataManager(string modName, bool debug = false)
public ModDataManager(string modName, bool debug = true)
{
currentModName = modName;
debugMode = debug;
Expand Down
Loading

0 comments on commit 8d2dcc9

Please sign in to comment.