Stickers: cleanup

This commit is contained in:
NotAKidoS 2024-09-21 01:07:04 -05:00
parent 50804b323d
commit dbc6341f9e
12 changed files with 113 additions and 126 deletions

View file

@ -1,15 +1,12 @@
using ABI_RC.Core.Util.AnimatorManager;
using NAK.Stickers.Properties;
namespace NAK.Stickers.Networking;
namespace NAK.Stickers.Networking;
public static partial class ModNetwork
{
#region Constants
internal const int MaxTextureSize = 1024 * 256; // 256KB
private const string NetworkVersion = "1.0.2"; // change each time network protocol changes
private const string NetworkVersion = "1.0.3"; // change each time network protocol changes
private const string ModId = $"MelonMod.NAK.Stickers_v{NetworkVersion}";
private const int ChunkSize = 1024; // roughly 1KB per ModNetworkMessage
private const int MaxChunkCount = MaxTextureSize / ChunkSize;

View file

@ -43,7 +43,8 @@ public static partial class ModNetwork
if (ModSettings.Entry_FriendsOnly.Value && !Friends.FriendsWith(sender))
return false; // ignore messages from non-friends if friends only is enabled
if (StickerSystem.RestrictedInstance == true) // ignore messages from users when the world is restricted. This also includes older or modified version of Stickers mod.
if (StickerSystem.Instance.IsRestrictedInstance) // ignore messages from users when the world is restricted. This also includes older or modified version of Stickers mod.
return false;
return true;

View file

@ -69,16 +69,16 @@ namespace NAK.Stickers
}
}
public Guid GetTextureHash(int spawnerIndex = 0)
{
if (spawnerIndex < 0 || spawnerIndex >= _decalSpawners.Length)
{
StickerMod.Logger.Warning("Invalid spawner index!");
return Guid.Empty;
}
return _textureHashes[spawnerIndex];
}
// public Guid GetTextureHash(int spawnerIndex = 0)
// {
// if (spawnerIndex < 0 || spawnerIndex >= _decalSpawners.Length)
// {
// StickerMod.Logger.Warning("Invalid spawner index!");
// return Guid.Empty;
// }
//
// return _textureHashes[spawnerIndex];
// }
public bool CheckHasTextureHash(Guid textureHash)
{
@ -132,7 +132,7 @@ namespace NAK.Stickers
Transform rootObject = null;
GameObject hitGO = hit.transform.gameObject;
if (hitGO.scene.buildIndex == 4 // additive (dynamic) content
|| hitGO.TryGetComponent(out Animator _) // potentially movable
|| hitGO.GetComponentInParent<Animator>() != null // potentially movable
|| hitGO.GetComponentInParent<Rigidbody>() != null) // movable
rootObject = hitGO.transform;
@ -244,21 +244,18 @@ namespace NAK.Stickers
if (_previewDecalSpawner == null)
return; // uh fuck
// clear previous
ClearPreview();
// place at hit pos
Transform rootObject = null;
GameObject hitGO = hit.transform.gameObject;
if (hitGO.scene.buildIndex == 4 || hitGO.TryGetComponent(out Animator _) || hitGO.GetComponentInParent<Rigidbody>() != null)
if (hitGO.scene.buildIndex == 4 // additive (dynamic) content
|| hitGO.GetComponentInParent<Animator>() != null // potentially movable
|| hitGO.GetComponentInParent<Rigidbody>() != null) // movable
rootObject = hitGO.transform;
Vector3 position = hit.point;
_previewDecalSpawner.AddDecal(position,
Quaternion.LookRotation(forwardDirection, upDirection),
hitGO,
DECAL_SIZE, DECAL_SIZE, 1f, 1f, 0f,
rootObject);
_previewDecalSpawner.AddDecal(
hit.point, Quaternion.LookRotation(forwardDirection, upDirection),
hitGO,
DECAL_SIZE, DECAL_SIZE, 1f, 1f, 0f, rootObject);
}
public void UpdatePreview(int spawnerIndex)

View file

@ -16,9 +16,7 @@ namespace NAK.Stickers;
public partial class StickerSystem
{
#region Singleton
public static bool RestrictedInstance = false;
public static StickerSystem Instance { get; private set; }
public static void Initialize()
@ -45,8 +43,9 @@ public partial class StickerSystem
private void OnPlayerSetupStart()
{
// TODO: this can be spammed by world author toggling CVRWorld.enabled state
CVRGameEventSystem.World.OnLoad.AddListener(_ => OnWorldLoad());
CVRGameEventSystem.World.OnUnload.AddListener(_ => OnWorldUnload());
CVRGameEventSystem.World.OnLoad.AddListener(_ => OnWorldLoad());
CVRGameEventSystem.Instance.OnConnected.AddListener((_) => { if (!Instances.IsReconnecting) OnInitialConnection(); });
CVRGameEventSystem.Player.OnJoinEntity.AddListener(Instance.OnPlayerJoined);
@ -61,30 +60,21 @@ public partial class StickerSystem
private void OnInitialConnection()
{
OnWorldLoad(); //Checks the world again in case the bundle updated.
ClearStickersSelf(); // clear stickers on remotes just in case we rejoined
ModNetwork.Reset(); // reset network buffers and metadata
}
private void OnWorldLoad()
{
GameObject StickerWorldRestriction = GameObject.Find("[DisableStickers]");
if (StickerWorldRestriction != null)
{
RestrictedInstance = true;
MelonLogger.Msg("This is a Restricted Instance");
}
else
{
MelonLogger.Msg("This is NOT a Restricted Instance");
}
BTKUIAddon.UpdateStickerMenu();
IsRestrictedInstance = GameObject.Find("[DisableStickers]") != null;
if (IsRestrictedInstance) StickerMod.Logger.Msg("Stickers are restricted by the world author.");
BTKUIAddon.OnStickerRestrictionUpdated(IsRestrictedInstance);
}
private void OnWorldUnload()
{
RestrictedInstance = false;
CleanupAllButSelf(); // release all stickers except for self
IsRestrictedInstance = false;
CleanupAllButSelf();
}
#endregion Game Events
@ -107,6 +97,8 @@ public partial class StickerSystem
// }
// }
public bool IsRestrictedInstance { get; internal set; }
private string SelectedStickerName => ModSettings.Hidden_SelectedStickerNames.Value[_selectedStickerSlot];
private const float StickerKillTime = 30f;
@ -131,7 +123,7 @@ public partial class StickerSystem
get => _isInStickerMode;
set
{
_isInStickerMode = value;
_isInStickerMode = value && !IsRestrictedInstance; // ensure cannot enter when restricted
if (_isInStickerMode)
{
CohtmlHud.Instance.SelectPropToSpawn(

View file

@ -51,7 +51,7 @@ public partial class StickerSystem
private bool PlaceStickerSelf(Vector3 position, Vector3 forward, Vector3 up, bool alignWithNormal = true)
{
if (!AttemptPlaceSticker(PlayerLocalId, position, forward, up, alignWithNormal, SelectedStickerSlot, RestrictedInstance))
if (!AttemptPlaceSticker(PlayerLocalId, position, forward, up, alignWithNormal, SelectedStickerSlot))
return false; // failed
// placed, now network
@ -59,8 +59,12 @@ public partial class StickerSystem
return true;
}
private bool AttemptPlaceSticker(string playerId, Vector3 position, Vector3 forward, Vector3 up, bool alignWithNormal = true, int stickerSlot = 0, bool RestrictedInstance = false, bool isPreview = false)
private bool AttemptPlaceSticker(string playerId, Vector3 position, Vector3 forward, Vector3 up, bool alignWithNormal = true, int stickerSlot = 0, bool isPreview = false)
{
// if the world contained a gameobject with the [DisableStickers] name and restricted the instance disable stickers!
if (IsRestrictedInstance)
return false;
StickerData stickerData = GetOrCreateStickerData(playerId);
if (Time.time - stickerData.LastPlacedTime < StickerCooldown)
return false;
@ -75,10 +79,6 @@ public partial class StickerSystem
if (hit.transform.gameObject.name.StartsWith("[NoSticker]"))
return false;
// if the world contained a gameobject with the [DisableStickers] name and restricted the instance disable stickers!
if (RestrictedInstance == true)
return false;
if (isPreview)
{
stickerData.PlacePreview(hit, alignWithNormal ? -hit.normal : forward, up, stickerSlot);
@ -181,6 +181,7 @@ public partial class StickerSystem
if (!IsInStickerMode) return;
StickerData localStickerData = GetOrCreateStickerData(PlayerLocalId);
localStickerData.ClearPreview(); // clear prior frames sticker preview
localStickerData.UpdatePreview(SelectedStickerSlot);
}