Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
Features:
- Added respawn mechanic; no more being disconnected on death
- Added more in-game messages about connection status
- Synced the occurrence of World Events

Bug fixes:
- The game now restarts after disconnecting from server to prevent infinite loading error (skips splash screen on reload)
- Fixed the UI breaking after 1.2.0
- Fixed the game being able to pause after 1.2.0 (time should always move for all players)
  • Loading branch information
xen-42 authored Jul 7, 2023
2 parents 5216f24 + 5d1dbb6 commit 467199b
Show file tree
Hide file tree
Showing 53 changed files with 819 additions and 463 deletions.
Empty file.
8 changes: 1 addition & 7 deletions CosmicHorrorFishingBuddies/AudioSync/AudioEnum.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CosmicHorrorFishingBuddies.AudioSync
namespace CosmicHorrorFishingBuddies.AudioSync
{
public enum AudioEnum
{
Expand Down
2 changes: 1 addition & 1 deletion CosmicHorrorFishingBuddies/BaseSync/TransformSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CosmicHorrorFishingBuddies.BaseSync
{
internal abstract class TransformSync : NetworkBehaviour
internal abstract class TransformSync : NetworkBehaviour
{
protected abstract Transform InitLocalTransform();
protected abstract Transform InitRemoteTransform();
Expand Down
229 changes: 140 additions & 89 deletions CosmicHorrorFishingBuddies/Core/CFBCore.cs
Original file line number Diff line number Diff line change
@@ -1,126 +1,177 @@
using CosmicHorrorFishingBuddies.AudioSync;
using CosmicHorrorFishingBuddies.Debugging;
using CosmicHorrorFishingBuddies.Save;
using CosmicHorrorFishingBuddies.UI;
using CosmicHorrorFishingBuddies.Util;
using HarmonyLib;
using Sirenix.Utilities;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using Winch.Core;

namespace CosmicHorrorFishingBuddies.Core
{
public class CFBCore : MonoBehaviour
{
public static CFBCore Instance { get; private set; }
{
public static CFBCore Instance { get; private set; }

public static GameObject terminal;
public static GameObject terminal;

public UnityEvent PlayerLoaded = new();
public const string INTRO_SKIP_ARG = "skipintro";

public void Awake()
{
try
{
Instance = this;
public UnityEvent PlayerLoaded = new();
public UnityEvent<string> SwitchSceneRequested = new();

LogInfo($"Cosmic Fishing Buddies is loaded!");
private bool _restarting;

InitAssemblies();
public void Awake()
{
try
{
Instance = this;

new Harmony(nameof(CFBCore)).PatchAll();
LogInfo($"Cosmic Fishing Buddies is loaded!");

Application.runInBackground = true;
InitAssemblies();

gameObject.AddComponent<CFBNetworkManager>();
gameObject.AddComponent<AudioClipManager>();
gameObject.AddComponent<UIHelper>();
gameObject.AddComponent<MainMenuManager>();
new Harmony(nameof(CFBCore)).PatchAll();

Application.logMessageReceived += Application_logMessageReceived;
}
catch (Exception e)
{
LogError($"Failed to load Cosmic Fishing Buddies: {e}");
}
}
Application.runInBackground = true;

private void Application_logMessageReceived(string condition, string stackTrace, LogType type)
{
LogInfo($"{condition} : {stackTrace}");
}
gameObject.AddComponent<CFBNetworkManager>();
gameObject.AddComponent<AudioClipManager>();
gameObject.AddComponent<UIHelper>();
gameObject.AddComponent<MainMenuManager>();

public static string GetModFolder() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#if DEBUG
gameObject.AddComponent<DebugKeyPadCommands>();
gameObject.AddComponent<TerminalCommands>();
#endif

private static void InitAssemblies()
{
Application.logMessageReceived += Application_logMessageReceived;
}
catch (Exception e)
{
LogError($"Failed to load Cosmic Fishing Buddies: {e}");
}
}

private void Application_logMessageReceived(string condition, string stackTrace, LogType type)
{
LogInfo($"{condition} : {stackTrace}");
}

public static string GetModFolder() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

private static void InitAssemblies()
{
// Have to manually load the EOSDK shipping library since this is a mod
[DllImport("Kernel32.dll", SetLastError = true)]
static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("Kernel32.dll", SetLastError = true)]
static extern IntPtr LoadLibrary(string dllToLoad);

var libPath = Path.Combine(GetModFolder(), "lib\\EOSSDK-Win32-Shipping.dll");
var libraryPointer = LoadLibrary(libPath);
var libPath = Path.Combine(GetModFolder(), "lib\\EOSSDK-Win32-Shipping.dll");
var libraryPointer = LoadLibrary(libPath);

if (libraryPointer == IntPtr.Zero)
{
LogError($"Failed to load EOS SDK library {libPath} - error code: {Marshal.GetLastWin32Error()}");
}
else
{
LogInfo($"Loaded EOS SDK library");
}
if (libraryPointer == IntPtr.Zero)
{
LogError($"Failed to load EOS SDK library {libPath} - error code: {Marshal.GetLastWin32Error()}");
}
else
{
LogInfo($"Loaded EOS SDK library");
}

// JohnCorby is a hero (stolen from QSB)
// [RunTimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] Attribute methods just never get called since this is a mod not Unity
// We have to manually initialize them
static void Init(Assembly assembly)
{
LogInfo(assembly.ToString());
assembly
.GetTypes()
.SelectMany(x => x.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly))
.Where(x => x.IsDefined(typeof(RuntimeInitializeOnLoadMethodAttribute)))
.ForEach(x => x.Invoke(null, null));
}

LogInfo("Running RuntimeInitializeOnLoad methods for our assemblies");
foreach (var path in Directory.EnumerateFiles(GetModFolder(), "*.dll"))
{
var assembly = Assembly.LoadFile(path);
Init(assembly);
}

LogInfo("Assemblies initialized");
}

public static void LogInfo(object msg)
{
try
{
WinchCore.Log.Info(msg);
}
catch { }
}

public static void LogError(object msg)
{
try
{
WinchCore.Log.Error(msg);
}
catch { }
}

public static void LogWarning(object msg)
{
try
{
WinchCore.Log.Warn(msg);
}
catch { }
}
}
{
LogInfo(assembly.ToString());
assembly
.GetTypes()
.SelectMany(x => x.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly))
.Where(x => x.IsDefined(typeof(RuntimeInitializeOnLoadMethodAttribute)))
.ForEach(x => x.Invoke(null, null));
}

LogInfo("Running RuntimeInitializeOnLoad methods for our assemblies");
foreach (var path in Directory.EnumerateFiles(GetModFolder(), "*.dll"))
{
var assembly = Assembly.LoadFile(path);
Init(assembly);
}

LogInfo("Assemblies initialized");
}

public static void LogInfo(object msg)
{
try
{
WinchCore.Log.Info(msg);
}
catch { }
}

public static void LogError(object msg)
{
try
{
WinchCore.Log.Error(msg);
}
catch { }
}

public static void LogWarning(object msg)
{
try
{
WinchCore.Log.Warn(msg);
}
catch { }
}

public static bool CanRestart()
{
return !SaveManagerPatches.IsSaving && !SaveManagerPatches.IsLoading && !SaveManagerPatches.IsSavingSettings;
}

public static void RestartGame()
{
if (Instance._restarting)
{
LogError("Game is already restarting");
return;
}
else
{
Instance._restarting = true;

// Save their game even though they aren't at a dock its fine
if (SceneManager.GetActiveScene().name == Scenes.Game)
{
GameManager.Instance.Save();
}

// Make sure we aren't saving/loading any data before restarting to prevent corruption
Delay.RunWhen(CanRestart, RestartInternal);
}
}

private static void RestartInternal()
{
LogInfo("Restarting the game");
var args = Environment.GetCommandLineArgs().ToList();
if (!args.Contains(INTRO_SKIP_ARG)) args.Add(INTRO_SKIP_ARG);
Process.Start(Application.dataPath.Replace("_Data", ".exe"), string.Join(" ", args));
Application.Quit();
}
}
}
Loading

0 comments on commit 467199b

Please sign in to comment.