Skip to content

Commit f78f311

Browse files
committed
added ability for mods to save custom byte data to layouts
1 parent 97fce67 commit f78f311

2 files changed

Lines changed: 70 additions & 6 deletions

File tree

PolyTechMain.cs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class PolyTechMain : PolyTechMod
2424
public new const string
2525
PluginGuid = "polytech.polytechframework",
2626
PluginName = "PolyTech Framework",
27-
PluginVersion = "0.9.3";
27+
PluginVersion = "0.9.4";
2828
private static BindingList<PolyTechMod>
2929
noncheatMods = new BindingList<PolyTechMod> { },
3030
cheatMods = new BindingList<PolyTechMod> { };
@@ -707,7 +707,7 @@ static void patchSerializerOne(SandboxLayoutData __instance, List<byte> bytes)
707707
{
708708
ptfInstance.ptfLogger.LogMessage($"Layout pre version: {__instance.m_Version}");
709709
if (GameStateManager.GetState() != GameState.BUILD && GameStateManager.GetState() != GameState.SANDBOX) return;
710-
if (cheatMods.Where(x => x.isEnabled).Count() == 0) return;
710+
if (noncheatMods.Where(x => x.shouldSaveData).Count() + cheatMods.Where(x => x.shouldSaveData).Count() == 0) return;
711711
__instance.m_Version *= -1;
712712
//PopUpMessage.Display("You have cheat mods enabled, do you want to store them?\n(This will make the layout incompatible with vanilla PB2)", yes, no);
713713
ptfInstance.ptfLogger.LogMessage($"Version after cheat question: {__instance.m_Version.ToString()}");
@@ -718,10 +718,41 @@ static void patchSerializerOne(SandboxLayoutData __instance, List<byte> bytes)
718718
private static void patchSerializerTwo(SandboxLayoutData __instance, List<byte> bytes)
719719
{
720720
ptfInstance.ptfLogger.LogMessage($"Layout post version: {__instance.m_Version}");
721+
722+
// add number of mods stored
723+
//bytes.AddRange(ByteSerializer.SerializeInt(noncheatMods.Where(x => x.shouldSaveData).Count() + cheatMods.Where(x => x.shouldSaveData).Count()));
721724

722-
string[] mods = cheatMods.Where(x => x.isEnabled).Select(x => $"{x.Info.Metadata.Name}\u058D{x.Info.Metadata.Version}\u058D{x.getSettings()}").ToArray();
725+
// add mod data for each mod
726+
727+
// make sure to be backwards compatible!
728+
List<string> modData = cheatMods.Where(x => x.isEnabled).Select(x => $"{x.Info.Metadata.Name}\u058D{x.Info.Metadata.Version}\u058D{x.getSettings()}").ToList();
729+
modData.AddRange(noncheatMods.Where(x => x.shouldSaveData).Select(x => $"{x.Info.Metadata.Name}\u058D{x.Info.Metadata.Version}\u058D{x.getSettings()}").ToList());
730+
string[] mods = modData.ToArray();
731+
723732
if (__instance.m_Version >= 0) return;
724733
bytes.AddRange(ByteSerializer.SerializeStrings(mods));
734+
735+
bytes.AddRange(ByteSerializer.SerializeInt(modData.Count));
736+
foreach (var mod in noncheatMods){
737+
if (mod.isEnabled && mod.shouldSaveData){
738+
bytes.AddRange(ByteSerializer.SerializeString(
739+
$"{mod.Info.Metadata.Name}\u058D{mod.Info.Metadata.Version}"
740+
));
741+
bytes.AddRange(ByteSerializer.SerializeByteArray(
742+
mod.saveData()
743+
));
744+
}
745+
}
746+
foreach (var mod in cheatMods){
747+
if (mod.isEnabled && mod.shouldSaveData){
748+
bytes.AddRange(ByteSerializer.SerializeString(
749+
$"{mod.Info.Metadata.Name}\u058D{mod.Info.Metadata.Version}"
750+
));
751+
bytes.AddRange(ByteSerializer.SerializeByteArray(
752+
mod.saveData()
753+
));
754+
}
755+
}
725756
ptfInstance.ptfLogger.LogMessage($"Serialized {mods.Length.ToString()} Mod Names");
726757
}
727758

@@ -762,16 +793,41 @@ public static void patchDeserializerPostfix(SandboxLayoutData __instance, byte[]
762793

763794
ptfInstance.ptfLogger.LogInfo($" -- {str.Replace("\u058D", " - v")}");
764795

765-
var currMod = cheatMods.Where(p => p.Info.Metadata.Name == name).First();
796+
var currMod = cheatMods.Where(p => p.Info.Metadata.Name == name).FirstOrDefault();
797+
if (currMod == null) currMod = noncheatMods.Where(p => p.Info.Metadata.Name == name).FirstOrDefault();
766798

767799
ptfInstance.checkMods(0, name, version, settings, currMod);
768800
}
801+
if (offset == bytes.Length) return;
802+
int extraSaveDataCount = ByteSerializer.DeserializeInt(bytes, ref offset);
803+
if (extraSaveDataCount == 0) return;
804+
805+
ptfInstance.Logger.LogInfo($"Layout created with custom data from mods: ");
806+
807+
for (int i = 0; i < extraSaveDataCount; i++){
808+
string modIdentifier = ByteSerializer.DeserializeString(bytes, ref offset);
809+
byte[] customModSaveData = ByteSerializer.DeserializeByteArray(bytes, ref offset);
810+
811+
string[] partsOfMod = modIdentifier.Split('\u058D');
812+
string name = partsOfMod.Length >= 1 ? partsOfMod[0] : null;
813+
string version = partsOfMod.Length >= 2 ? partsOfMod[1] : null;
814+
815+
ptfInstance.Logger.LogInfo($" -- {name} - v{version}");
816+
817+
var currMod = cheatMods.Where(p => p.Info.Metadata.Name == name).FirstOrDefault();
818+
if (currMod == null) currMod = noncheatMods.Where(p => p.Info.Metadata.Name == name).FirstOrDefault();
819+
820+
if (currMod == null) return;
821+
if (currMod.Info.Metadata.Version.ToString() != version) return;
822+
823+
currMod.loadData(customModSaveData);
824+
}
769825
}
770826

771827

772828
void checkMods(int step, string name, string version, string settings, PolyTechMod currMod)
773829
{
774-
if (step <= 0 && currMod.Info.Metadata.Name != name) missingMod(name, version, settings, currMod);
830+
if (currMod == null || (step <= 0 && currMod.Info.Metadata.Name != name)) missingMod(name, version, settings, currMod);
775831
else if (step <= 1 && currMod.Info.Metadata.Version.ToString() != version) wrongVersion(name, version, settings, currMod);
776832
else if (step <= 2 && !currMod.isEnabled) notEnabled(name, version, settings, currMod);
777833
else if (step <= 3 && currMod.getSettings() != settings) wrongSettings(name, version, settings, currMod);
@@ -782,7 +838,7 @@ void missingMod(string name, string version, string settings, PolyTechMod currMo
782838
ptfInstance.ptfLogger.LogWarning("Mod in layout not present.");
783839
PopUpMessage.Display(
784840
$"Mod ({name}) in layout not present.",
785-
() => checkMods(1, name, version, settings, currMod)
841+
() => {}
786842
);
787843
}
788844

PolyTechMod.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@ public virtual void setSettings(string settings)
3838
PopUpWarning.Display("Something tried to automatically set the settings for a mod, but the mod doesn't support this feature. Try setting them manually.", PopUpWarningCategory.OLDER_PHYSICS_ENGINE);
3939
}
4040

41+
public virtual byte[] saveData(){
42+
return new byte[] {};
43+
}
44+
45+
public virtual void loadData(byte[] bytes){
46+
return;
47+
}
4148
public bool isEnabled;
4249
public bool isCheat;
4350
public string repositoryUrl;
4451
public string[] authors;
52+
public bool shouldSaveData;
4553
}
4654
}

0 commit comments

Comments
 (0)