na most talán ez a geci mukodik???
This commit is contained in:
48
LoadSaveOverrides/LoadSaveLoadHooks.cs
Normal file
48
LoadSaveOverrides/LoadSaveLoadHooks.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Harmony;
|
||||
using KCM.LoadSaveOverrides;
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM
|
||||
{
|
||||
public static class LoadSaveLoadAtPathHook
|
||||
{
|
||||
public static byte[] saveData = new byte[1];
|
||||
public static string lastLoadedPath = string.Empty;
|
||||
}
|
||||
|
||||
public static class LoadSaveLoadHook
|
||||
{
|
||||
public static byte[] saveBytes = null;
|
||||
public static bool memoryStreamHook = false;
|
||||
public static MultiplayerSaveContainer saveContainer = null;
|
||||
}
|
||||
|
||||
[HarmonyPatch(typeof(LoadSave), "LoadAtPath")]
|
||||
public class LoadSaveLoadAtPathCaptureHook
|
||||
{
|
||||
public static void Prefix(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
return;
|
||||
|
||||
LoadSaveLoadAtPathHook.lastLoadedPath = path;
|
||||
|
||||
if (!File.Exists(path))
|
||||
return;
|
||||
|
||||
LoadSaveLoadAtPathHook.saveData = File.ReadAllBytes(path);
|
||||
Main.Log($"Captured save bytes from: {path} ({LoadSaveLoadAtPathHook.saveData.Length} bytes)");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.Log("Failed capturing save bytes from LoadSave.LoadAtPath");
|
||||
Main.Log(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
114
Main.cs
114
Main.cs
@@ -9,11 +9,11 @@ using KCM.Packets.State;
|
||||
using KCM.StateManagement.Observers;
|
||||
using KCM.StateManagement.Sync;
|
||||
using KCM.StateManagement.BuildingState;
|
||||
using KCM.Enums;
|
||||
using Riptide;
|
||||
using Steamworks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
@@ -74,7 +74,7 @@ namespace KCM
|
||||
if (helper != null)
|
||||
helper.Log(message ?? string.Empty);
|
||||
else
|
||||
Debug.Log(message ?? string.Empty);
|
||||
UnityEngine.Debug.Log(message ?? string.Empty);
|
||||
}
|
||||
|
||||
public static void Log(Exception ex)
|
||||
@@ -104,6 +104,116 @@ namespace KCM
|
||||
UnityEngine.SceneManagement.SceneManager.LoadScene(scene);
|
||||
}
|
||||
|
||||
public static void TransitionTo(MenuState menuState)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (GameState.inst != null && GameState.inst.mainMenuMode != null)
|
||||
{
|
||||
GameState.inst.SetNewMode(GameState.inst.mainMenuMode);
|
||||
SetMainMenuState(GameState.inst.mainMenuMode, (int)menuState);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log(ex);
|
||||
}
|
||||
|
||||
TransitionTo(menuState.ToString());
|
||||
}
|
||||
|
||||
private static void SetMainMenuState(object mainMenuMode, int menuStateValue)
|
||||
{
|
||||
if (mainMenuMode == null)
|
||||
return;
|
||||
|
||||
Type type = mainMenuMode.GetType();
|
||||
const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
|
||||
|
||||
string[] methodNames = new string[]
|
||||
{
|
||||
"SetMenuState",
|
||||
"SetState",
|
||||
"GoToState",
|
||||
"GotoState",
|
||||
"TransitionToState",
|
||||
"TransitionTo",
|
||||
};
|
||||
|
||||
MethodInfo[] methods = type.GetMethods(flags);
|
||||
for (int i = 0; i < methodNames.Length; i++)
|
||||
{
|
||||
string name = methodNames[i];
|
||||
for (int j = 0; j < methods.Length; j++)
|
||||
{
|
||||
MethodInfo method = methods[j];
|
||||
if (method == null || method.Name != name)
|
||||
continue;
|
||||
|
||||
ParameterInfo[] parameters = method.GetParameters();
|
||||
if (parameters == null || parameters.Length != 1)
|
||||
continue;
|
||||
|
||||
Type paramType = parameters[0].ParameterType;
|
||||
if (paramType == typeof(int))
|
||||
{
|
||||
method.Invoke(mainMenuMode, new object[] { menuStateValue });
|
||||
return;
|
||||
}
|
||||
|
||||
if (paramType != null && paramType.IsEnum)
|
||||
{
|
||||
object enumValue = Enum.ToObject(paramType, menuStateValue);
|
||||
method.Invoke(mainMenuMode, new object[] { enumValue });
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FieldInfo[] fields = type.GetFields(flags);
|
||||
for (int i = 0; i < fields.Length; i++)
|
||||
{
|
||||
FieldInfo field = fields[i];
|
||||
if (field == null)
|
||||
continue;
|
||||
|
||||
if (field.FieldType == typeof(int))
|
||||
{
|
||||
field.SetValue(mainMenuMode, menuStateValue);
|
||||
return;
|
||||
}
|
||||
|
||||
if (field.FieldType != null && field.FieldType.IsEnum)
|
||||
{
|
||||
field.SetValue(mainMenuMode, Enum.ToObject(field.FieldType, menuStateValue));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
PropertyInfo[] properties = type.GetProperties(flags);
|
||||
for (int i = 0; i < properties.Length; i++)
|
||||
{
|
||||
PropertyInfo prop = properties[i];
|
||||
if (prop == null || !prop.CanWrite)
|
||||
continue;
|
||||
|
||||
if (prop.PropertyType == typeof(int))
|
||||
{
|
||||
prop.SetValue(mainMenuMode, menuStateValue, null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (prop.PropertyType != null && prop.PropertyType.IsEnum)
|
||||
{
|
||||
prop.SetValue(mainMenuMode, Enum.ToObject(prop.PropertyType, menuStateValue), null);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Log($"TransitionTo(MenuState) could not set state value={menuStateValue} on {type.FullName}");
|
||||
}
|
||||
|
||||
public static void ResetMultiplayerState()
|
||||
{
|
||||
// Reset multiplayer state
|
||||
|
||||
@@ -103,15 +103,15 @@ namespace KCM.Packets.Lobby
|
||||
// Handle completed transfer here
|
||||
Main.helper.Log("Save Transfer complete!");
|
||||
|
||||
LoadSaveLoadHook.saveBytes = saveData;
|
||||
LoadSaveLoadHook.memoryStreamHook = true;
|
||||
KCM.LoadSaveLoadHook.saveBytes = saveData;
|
||||
KCM.LoadSaveLoadHook.memoryStreamHook = true;
|
||||
|
||||
LoadSave.Load();
|
||||
|
||||
try
|
||||
{
|
||||
Main.SetMultiplayerSaveLoadInProgress(true);
|
||||
LoadSaveLoadHook.saveContainer.Unpack(null);
|
||||
KCM.LoadSaveLoadHook.saveContainer.Unpack(null);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -400,7 +400,7 @@ namespace KCM
|
||||
SfxSystem.PlayUiSelect();
|
||||
|
||||
|
||||
Main.TransitionTo(MenuState.Menu);
|
||||
Main.TransitionTo(KCM.Enums.MenuState.Menu);
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user