forked from NebulaModTeam/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request NebulaModTeam#666 from starfi5h/pr-feature
Construction drones range limit & command /playerdata
- Loading branch information
Showing
7 changed files
with
315 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
NebulaPatcher/Patches/Transpilers/ConstructionSystem_Transpiler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#region | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using NebulaModel.Logger; | ||
using NebulaModel.Packets.Factory; | ||
using NebulaWorld; | ||
using NebulaWorld.Player; | ||
using UnityEngine; | ||
|
||
#endregion | ||
|
||
namespace NebulaPatcher.Patches.Transpilers; | ||
|
||
[HarmonyPatch(typeof(ConstructionSystem))] | ||
internal class ConstructionSystem_Transpiler | ||
{ | ||
[HarmonyTranspiler] | ||
[HarmonyPatch(nameof(ConstructionSystem.AddBuildTargetToModules))] | ||
public static IEnumerable<CodeInstruction> AddBuildTargetToModules_Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
try | ||
{ | ||
/* Sync Prebuild.itemRequired changes by player, insert local method call after player.package.TakeTailItems | ||
Replace: if (num8 <= num) { this.player.mecha.constructionModule.InsertBuildTarget ... } | ||
With: if (num8 <= num && IsClosestPlayer(ref pos)) { this.player.mecha.constructionModule.InsertBuildTarget ... } | ||
*/ | ||
|
||
var codeMatcher = new CodeMatcher(instructions) | ||
.MatchForward(true, | ||
new CodeMatch(OpCodes.Ldloc_S), | ||
new CodeMatch(OpCodes.Ldloc_0), | ||
new CodeMatch(OpCodes.Bgt_Un) | ||
); | ||
Log.Info(codeMatcher.IsValid); | ||
var sqrDist = codeMatcher.InstructionAt(-2).operand; | ||
var skipLabel = codeMatcher.Operand; | ||
codeMatcher.Advance(1) | ||
.Insert( | ||
new CodeInstruction(OpCodes.Ldloc_S, sqrDist), | ||
new CodeInstruction(OpCodes.Ldarg_2), //ref Vector3 pos | ||
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ConstructionSystem_Transpiler), nameof(IsClosestPlayer))), | ||
new CodeInstruction(OpCodes.Brfalse_S, skipLabel) | ||
); | ||
|
||
return codeMatcher.InstructionEnumeration(); | ||
} | ||
catch (System.Exception e) | ||
{ | ||
Log.Error("Transpiler ConstructionSystem.AddBuildTargetToModules failed."); | ||
Log.Error(e); | ||
return instructions; | ||
} | ||
} | ||
|
||
static bool IsClosestPlayer(float sqrDist, ref Vector3 pos) | ||
{ | ||
if (!Multiplayer.IsActive || sqrDist < DroneManager.MinSqrDistance) return true; | ||
return sqrDist <= Multiplayer.Session.Drones.GetClosestRemotePlayerSqrDistance(pos); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
#region | ||
|
||
using NebulaWorld.MonoBehaviours.Local.Chat; | ||
using System.Collections.Generic; | ||
using NebulaAPI.GameState; | ||
using HarmonyLib; | ||
using NebulaModel.DataStructures.Chat; | ||
using System.IO; | ||
using NebulaModel; | ||
using NebulaModel.Logger; | ||
using NebulaAPI.DataStructures; | ||
using NebulaModel.Networking; | ||
using NebulaModel.Packets.Players; | ||
|
||
#endregion | ||
|
||
namespace NebulaWorld.Chat.Commands; | ||
|
||
public class PlayerDataCommandHandler : IChatCommandHandler | ||
{ | ||
public void Execute(ChatWindow window, string[] parameters) | ||
{ | ||
if (parameters.Length < 1) | ||
{ | ||
throw new ChatCommandUsageException("Not enough arguments!".Translate()); | ||
} | ||
|
||
// Due to dependency order, get SaveManager.playerSaves by reflection | ||
var saveManagerType = AccessTools.TypeByName("NebulaNetwork.SaveManager"); | ||
var playerSaves = (Dictionary<string, IPlayerData>)AccessTools.Field(saveManagerType, "playerSaves").GetValue(null); | ||
|
||
switch (parameters[0]) | ||
{ | ||
case "list": | ||
{ | ||
var resp = $"Player count in .server file: {playerSaves.Count}\n"; | ||
foreach (var pair in playerSaves) | ||
{ | ||
resp += $"[{pair.Key.Substring(0, 5)}] {pair.Value.Username}"; | ||
} | ||
window.SendLocalChatMessage(resp, ChatMessageType.CommandOutputMessage); | ||
break; | ||
} | ||
case "load" when parameters.Length < 2: | ||
throw new ChatCommandUsageException("Need to specifiy hash string or name of a player!"); | ||
case "load": | ||
{ | ||
var input = parameters[1]; | ||
foreach (var pair in playerSaves) | ||
{ | ||
if (input == pair.Key.Substring(0, input.Length) || input == pair.Value.Username) | ||
{ | ||
window.SendLocalChatMessage($"Load [{pair.Key.Substring(0, 5)}] {pair.Value.Username}", ChatMessageType.CommandOutputMessage); | ||
LoadPlayerData(pair.Value); | ||
return; | ||
} | ||
} | ||
break; | ||
} | ||
case "remove" when parameters.Length < 2: | ||
throw new ChatCommandUsageException("Need to specifiy hash string or name of a player!"); | ||
case "remove": | ||
{ | ||
var input = parameters[1]; | ||
var removeHash = ""; | ||
foreach (var pair in playerSaves) | ||
{ | ||
if (input == pair.Key.Substring(0, input.Length) || input == pair.Value.Username) | ||
{ | ||
window.SendLocalChatMessage($"Remove [{pair.Key.Substring(0, 5)}] {pair.Value.Username}", ChatMessageType.CommandOutputMessage); | ||
removeHash = pair.Key; | ||
break; | ||
} | ||
} | ||
playerSaves.Remove(removeHash); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public string GetDescription() | ||
{ | ||
return "Manage the stored multiplayer player data".Translate(); | ||
} | ||
|
||
public string[] GetUsage() | ||
{ | ||
return ["list", "load <hashString>", "remove <hashString>"]; | ||
} | ||
|
||
static void LoadPlayerData(IPlayerData playerData) | ||
{ | ||
Log.Info($"Teleporting to target planet {GameMain.localPlanet?.id ?? 0} => {playerData.LocalPlanetId}"); | ||
var actionSail = GameMain.mainPlayer.controller.actionSail; | ||
if (playerData.LocalPlanetId > 0) | ||
{ | ||
if (playerData.LocalPlanetId == GameMain.localPlanet?.id) | ||
{ | ||
UIRoot.instance.uiGame.globemap.LocalPlanetTeleport(playerData.LocalPlanetPosition.ToVector3()); | ||
} | ||
else | ||
{ | ||
var destPlanet = GameMain.galaxy.PlanetById(playerData.LocalPlanetId); | ||
actionSail.fastTravelTargetPlanet = destPlanet; | ||
actionSail.fastTravelTargetLPos = playerData.LocalPlanetPosition.ToVector3(); | ||
actionSail.fastTravelTargetUPos = destPlanet.uPosition + (VectorLF3)(destPlanet.runtimeRotation * actionSail.fastTravelTargetLPos); | ||
actionSail.StartFastTravelToUPosition(actionSail.fastTravelTargetUPos); | ||
} | ||
} | ||
else | ||
{ | ||
GameMain.mainPlayer.controller.actionSail.StartFastTravelToUPosition(playerData.UPosition.ToVectorLF3()); | ||
} | ||
|
||
var mechaData = playerData.Mecha; | ||
Log.Info("Loading player inventory..."); | ||
using (var ms = new MemoryStream()) | ||
{ | ||
var bw = new BinaryWriter(ms); | ||
mechaData.Inventory.Export(bw); | ||
mechaData.DeliveryPackage.Export(bw); | ||
mechaData.WarpStorage.Export(bw); | ||
|
||
ms.Seek(0, SeekOrigin.Begin); | ||
var br = new BinaryReader(ms); | ||
GameMain.mainPlayer.package.Import(br); | ||
GameMain.mainPlayer.deliveryPackage.Import(br); | ||
GameMain.mainPlayer.mecha.warpStorage.Import(br); | ||
} | ||
if (!Config.Options.SyncSoil) | ||
{ | ||
GameMain.mainPlayer.sandCount = mechaData.SandCount; | ||
} | ||
GameMain.mainPlayer.mecha.coreEnergy = mechaData.CoreEnergy; | ||
GameMain.mainPlayer.mecha.reactorEnergy = mechaData.ReactorEnergy; | ||
|
||
if (playerData.Appearance != null) | ||
{ | ||
Log.Info("Loading custom appearance..."); | ||
playerData.Appearance.CopyTo(GameMain.mainPlayer.mecha.appearance); | ||
GameMain.mainPlayer.mechaArmorModel.RefreshAllPartObjects(); | ||
GameMain.mainPlayer.mechaArmorModel.RefreshAllBoneObjects(); | ||
GameMain.mainPlayer.mecha.appearance.NotifyAllEvents(); | ||
|
||
var editor = UIRoot.instance.uiMechaEditor; | ||
editor.selection.ClearSelection(); | ||
editor.saveGroup._Close(); | ||
if (editor.mecha.diyAppearance == null) | ||
{ | ||
editor.mecha.diyAppearance = new MechaAppearance(); | ||
editor.mecha.diyAppearance.Init(); | ||
} | ||
GameMain.mainPlayer.mecha.appearance.CopyTo(editor.mecha.diyAppearance); | ||
editor.mechaArmorModel.RefreshAllPartObjects(); | ||
editor.mechaArmorModel.RefreshAllBoneObjects(); | ||
editor.mecha.diyAppearance.NotifyAllEvents(); | ||
editor._left_content_height_max = 0f; | ||
editor.SetLeftScrollTop(); | ||
editor.saveGroup._Open(); | ||
|
||
using var writer = new BinaryUtils.Writer(); | ||
GameMain.mainPlayer.mecha.appearance.Export(writer.BinaryWriter); | ||
Multiplayer.Session.Network.SendPacket(new PlayerMechaArmor(Multiplayer.Session.LocalPlayer.Id, | ||
writer.CloseAndGetBytes())); | ||
} | ||
} | ||
} |
Oops, something went wrong.