Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Xgames authored and Xgames committed Aug 1, 2022
1 parent ef3617f commit a2ce5f6
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 5 deletions.
3 changes: 2 additions & 1 deletion DemoMod/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public override void OnUpdate()

if (Input.GetKeyUp(KeyCode.Space))
{
HierarchyXmlDumper.DumpSceneToFile();
CustomSaveFileSystem.WriteSaveMetadata(2);
//HierarchyXmlDumper.DumpSceneToFile();
}


Expand Down
102 changes: 101 additions & 1 deletion PrimitierModdingFramework/CustomSaveFileData/CustomSaveFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using Il2CppNewtonsoft.Json;
using MelonLoader.TinyJSON;
using PrimitierModdingFramework.SubstanceModding;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
Expand All @@ -13,6 +17,7 @@ public class CustomSaveFileSystem : PMFSystem
public static bool IsEnabled { get; private set; } = false;

internal static List<LoadRequest> LoadRequests = new List<LoadRequest>();
internal static int LastParsedSaveSlot = -1;

internal class LoadRequest
{
Expand All @@ -24,6 +29,101 @@ internal class LoadRequest

}

public static void WriteSaveMetadata(int slot)
{
var fullPathToMetaFile = Path.Combine(SaveAndLoad.GetSaveDirectory(), SaveAndLoad.mapFileDirectoryName, SaveAndLoad.mapFileName+ slot + ".pmf.json");
PMFLog.Message("Writing meta data file");
var metaData = new MetaDataFile();
foreach (var mod in PMFSystem.RegisteredMods)
{
var modMetaData = new ModMetaData()
{
Id = Mod.ModId,
};

if (CustomSubstanceSystem.IsEnabled)
{

foreach (var settings in CustomSubstanceSystem.CustomSubstanceSettings.Values)
{
if (settings.ModId == mod.ModId)
{
modMetaData.SubstanceToIndexMaps.Add(new SubstanceToIndexMap() { Name = settings.NameKey, Index = settings.Index });
}
}
}


metaData.Mods.Add(modMetaData);
}

var json = JSON.Dump(metaData);
try
{
File.WriteAllText(fullPathToMetaFile, json);
}catch(Exception e)
{
PMFLog.Error($"Could not write save file meta data \n\n {e}");
}



}
public static SaveAndLoad.SaveData ReadMetaDataFile(int slot, SaveAndLoad.SaveData saveData)
{
var fullPathToMetaFile = Path.Combine(SaveAndLoad.GetSaveDirectory(), SaveAndLoad.mapFileDirectoryName, SaveAndLoad.mapFileName + slot + ".pmf.json");
PMFLog.Message("Reading meta data file");
string json = null;
try
{
json = File.ReadAllText(fullPathToMetaFile);
}
catch (Exception e)
{
PMFLog.Error($"Could not read save meta data \n\n {e}");
}
if (json == null)
return null;

var jsonVar = JSON.Load(json);
JSON.MakeInto(jsonVar, out MetaDataFile metaDataFile);

var subToIndexMaps = new List<SubstanceToIndexMap>();
foreach (var mod in metaDataFile.Mods)
{
subToIndexMaps.AddRange(mod.SubstanceToIndexMaps);
}
return SwapSubstances(saveData, subToIndexMaps);
}

public static SaveAndLoad.SaveData SwapSubstances(SaveAndLoad.SaveData data, List<SubstanceToIndexMap> maps)
{
var mapDict = new Dictionary<Substance, Substance>();
foreach (var map in maps)
{
mapDict.Add((Substance)map.Index, CustomSubstanceSystem.GetSubstanceByName(map.Name));

}


foreach (var chunk in data.chunks)
{
foreach (var group in chunk.groups)
{
foreach (var cube in group.cubes)
{
if (mapDict.TryGetValue(cube.substance, out var newSubstance))
{
cube.substance = newSubstance;
}

}

}

}
return data;
}

public override void OnSystemEnabled()
{
Expand Down
30 changes: 30 additions & 0 deletions PrimitierModdingFramework/CustomSaveFileData/MetaDataFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimitierModdingFramework
{
public class MetaDataFile
{
public List<ModMetaData> Mods = new List<ModMetaData>();

}


public class ModMetaData
{
public string Id;
public List<SubstanceToIndexMap> SubstanceToIndexMaps = new List<SubstanceToIndexMap>();


}

public class SubstanceToIndexMap
{
public string Name;
public int Index;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HarmonyLib;
using Il2CppSystem;
using Il2CppSystem.Threading.Tasks;
using System.Reflection;
using UnityEngine;

namespace PrimitierModdingFramework.CustomSaveFileData.Patches
{
[HarmonyPatch(typeof(SaveAndLoad), nameof(SaveAndLoad.Load))]
public class SaveAndLoad_Load
{
public static void Prefix(ref SaveAndLoad.SaveData saveData)
{
if (!CustomSaveFileSystem.IsEnabled)
return;

saveData = CustomSaveFileSystem.ReadMetaDataFile(CustomSaveFileSystem.LastParsedSaveSlot, saveData);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;

namespace PrimitierModdingFramework.CustomSaveFileData.Patches
{
[HarmonyPatch(typeof(SaveAndLoad), nameof(SaveAndLoad.Parse))]
public class SaveAndLoad_Parse
{

public static void Postfix(int slot)
{
CustomSaveFileSystem.LastParsedSaveSlot = slot;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using HarmonyLib;
using Il2CppSystem;
using System.Reflection;
using UnityEngine;

namespace PrimitierModdingFramework.CustomSaveFileData.Patches
{
[HarmonyPatch(typeof(SaveAndLoad), nameof(SaveAndLoad.Save))]
public class SaveAndLoad_Save
{
public static void Postfix(int slot)
{
if (!CustomSaveFileSystem.IsEnabled)
return;

CustomSaveFileSystem.WriteSaveMetadata(slot);
}

}
}
5 changes: 5 additions & 0 deletions PrimitierModdingFramework/PrimitierMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public abstract class PrimitierMod : MelonMod
public bool OnSceneWasLoadedCalled { get; private set; } = false;
public bool RealyLateStartCalled { get; private set; } = false;

/// <summary>
/// The Id of the mod (Author.Name_version)
/// </summary>
public string ModId { get { return $"{Info.Author}.{Info.Name}_{Info.Version}"; } }

/// <summary>
/// Runs when a Scene has Loaded and is passed the Scene's Build Index and Name.
/// When overriding call base.OnSceneWasLoaded() before you do anything.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace PrimitierModdingFramework.SubstanceModding
/// </summary>
public class CustomSubstanceSettings
{
internal string NameKey;
internal string ModId;
internal int Index;


/// <summary>
/// Called when the substance is initialized so when the world loads or a new instance of your substance it created.
/// This is used to add components or do other things with the substance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CustomSubstanceSystem : PMFSystem
{
public static bool IsEnabled { get; private set; } = false;

private static Dictionary<string, Material> s_customMats = new Dictionary<string, Material>();
private static Dictionary<string, Dictionary<string, Material>> s_customMats = new Dictionary<string, Dictionary<string, Material>>();

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

Expand All @@ -27,6 +27,17 @@ public override void OnSystemDisabled()
IsEnabled = false;
}

private static Dictionary<string, Material> GetCustomMatDict()
{
if (!s_customMats.TryGetValue(Mod.ModId, out var customMats))
{
var newDict = new Dictionary<string, Material>();
s_customMats.Add(Mod.ModId, newDict);
return newDict;
}
return customMats;
}


/// <summary>
/// Gets the loaded custom material by name
Expand All @@ -38,7 +49,9 @@ public static Material GetCustomMaterial(string name)
if (!IsEnabled)
throw new PMFSystemNotEnabledException(typeof(CustomSubstanceSystem));

if(s_customMats.TryGetValue(name, out var outMat))

var customMats = GetCustomMatDict();
if (customMats.TryGetValue(name, out var outMat))
{
return outMat;
}
Expand All @@ -64,6 +77,9 @@ public static void LoadCustomSubstance(SubstanceParameters.Param substance, Cust

if (settings != null)
{
settings.NameKey = substance.displayNameKey;
settings.Index = SubstanceManager.instance.param.Count;
settings.ModId = Mod.ModId;
CustomSubstanceSettings.Add(substance.displayNameKey, settings);

PMFLocalizer.AddJpEntry(substance.displayNameKey, settings.JpName);
Expand Down Expand Up @@ -145,7 +161,9 @@ public static void LoadCustomMaterial(Material material)
if (!IsEnabled)
throw new PMFSystemNotEnabledException(typeof(CustomSubstanceSystem));

s_customMats.Add(material.name, material);
var customMats = GetCustomMatDict();

customMats.Add(material.name, material);
}


Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ It uses [MelonLoader](https://github.com/LavaGang/MelonLoader).
It adds helper classes to make mods for Primitier.



## Downloading a mod
[Downloading mods](Documentation/DownloadingMods.md)

Expand Down

0 comments on commit a2ce5f6

Please sign in to comment.