90 lines
3.1 KiB
C#
Raw Normal View History

2020-02-10 23:03:28 +01:00
using OWML.Common;
using OWML.ModHelper;
2020-02-12 21:39:59 +01:00
using System.Collections.Generic;
2020-02-10 23:03:28 +01:00
using UnityEngine;
using UnityEngine.Networking;
namespace QSB {
public class QSB: ModBehaviour {
static QSB _instance;
public static Dictionary<uint, Transform> playerSectors;
2020-02-10 23:03:28 +01:00
void Awake () {
Application.runInBackground = true;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
void Update () {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
void Start () {
_instance = this;
playerSectors = new Dictionary<uint, Transform>();
2020-02-12 21:39:59 +01:00
2020-02-10 23:03:28 +01:00
var assetBundle = ModHelper.Assets.LoadBundle("assets/network");
var networkManager = Instantiate(assetBundle.LoadAsset<GameObject>("assets/networkmanager.prefab"));
var networkPlayerPrefab = assetBundle.LoadAsset<GameObject>("assets/networkplayer.prefab");
networkPlayerPrefab.AddComponent<NetworkPlayer>();
networkManager.GetComponent<NetworkManager>().playerPrefab = networkPlayerPrefab;
ModHelper.HarmonyHelper.AddPrefix<PlayerSectorDetector>("OnAddSector", typeof(Patches), "OnAddSector");
2020-02-10 23:03:28 +01:00
}
2020-02-12 21:39:59 +01:00
static string JoinAll (params object[] logObjects) {
var result = "";
foreach (var obj in logObjects) {
result += obj + " ";
}
return result;
2020-02-10 23:03:28 +01:00
}
2020-02-12 21:39:59 +01:00
//public static void Log (params object[] logObjects) {
// _instance.ModHelper.Console.WriteLine(JoinAll(logObjects));
//}
public static void LogToScreen (params object[] logObjects) {
NotificationData data = new NotificationData(NotificationTarget.Player, JoinAll(logObjects), 5f, true);
NotificationManager.SharedInstance.PostNotification(data, false);
}
public static Transform GetSectorByName (Sector.Name sectorName) {
var sectors = GameObject.FindObjectsOfType<Sector>();
foreach (var sector in sectors) {
if (sectorName == sector.GetName()) {
return sector.transform;
}
}
return null;
}
2020-02-12 21:39:59 +01:00
public static void OnReceiveMessage (NetworkMessage netMsg) {
QSB.LogToScreen("Global message receive");
2020-02-12 21:39:59 +01:00
SectorMessage msg = netMsg.ReadMessage<SectorMessage>();
var sectorName = (Sector.Name) msg.sectorId;
var sectorTransform = GetSectorByName(sectorName);
if (sectorTransform == null) {
QSB.LogToScreen("Sector", sectorName, "not found");
return;
}
QSB.LogToScreen("Found sector", sectorName, ", setting for", msg.senderId);
playerSectors[msg.senderId] = sectorTransform;
2020-02-12 21:39:59 +01:00
}
static class Patches {
static void OnAddSector (Sector sector, PlayerSectorDetector __instance) {
if (NetworkPlayer.localInstance != null) {
NetworkPlayer.localInstance.EnterSector(sector);
}
}
}
2020-02-10 23:03:28 +01:00
}
}