Skip to content

Commit

Permalink
v1.7.0 - instant update, thirdperson preview
Browse files Browse the repository at this point in the history
  • Loading branch information
samyycX committed Jul 24, 2024
1 parent 5fd6172 commit 803abc6
Show file tree
Hide file tree
Showing 18 changed files with 312 additions and 62 deletions.
5 changes: 4 additions & 1 deletion Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public ModelCacheManager(IStorage storage) {
this.storage = storage;
}
public void ResyncCache() {
cache = storage.GetAllPlayerModel();
var data = storage.GetAllPlayerModel();
if (data != null) {
cache = data;
}
}

public void SetAllTModels(string tmodel, bool permissionBypass) {
Expand Down
4 changes: 4 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ public class ModelConfig : BasePluginConfig
[JsonPropertyName("MySQL_Table")] public string MySQLTable { get; set; } = "playermodelchanger";

[JsonPropertyName("ModelForBots")] public BotsConfig ModelForBots { get; set; } = new BotsConfig();
[JsonPropertyName("ModelChangeCooldownSecond")] public float ModelChangeCooldownSecond { get; set; } = 0f;

[JsonPropertyName("DisableInstantUpdate")] public bool DisableInstantUpdate { get; set; } = false;
[JsonPropertyName("DisableThirdPersonPreview")] public bool DisableThirdPersonPreview { get; set; } = false;
[JsonPropertyName("DisablePrecache")] public bool DisablePrecache { get; set; } = false;
[JsonPropertyName("DisableRandomModel")] public bool DisableRandomModel { get; set; } = false;
[JsonPropertyName("DisableAutoCheck")] public bool DisableAutoCheck { get; set; } = false;
[JsonPropertyName("DisablePlayerSelection")] public bool DisablePlayerSelection { get; set; } = false;
[JsonPropertyName("AutoResyncCache")] public bool AutoResyncCache { get; set; } = false;
[JsonPropertyName("ConfigVersion")] public override int Version { get; set; } = 1;
}
34 changes: 22 additions & 12 deletions PlayerModelChanger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@
using Service;
using System.Drawing;
using CounterStrikeSharp.API.Modules.Config;
using System.Reflection;
using System.Text.Json;
namespace PlayerModelChanger;

public partial class PlayerModelChanger : BasePlugin, IPluginConfig<ModelConfig>
{
public override string ModuleName => "Player Model Changer";
public override string ModuleVersion => "1.6.1";
public override string ModuleVersion => "1.7.0";

public override string ModuleAuthor => "samyyc";
public required ModelConfig Config { get; set; }
public required ModelService Service { get; set; }

public required DefaultModelManager DefaultModelManager { get;set; }

public static PlayerModelChanger? INSTANCE;

public bool Enable = true;

public override void Load(bool hotReload)
{
INSTANCE = this;
IStorage? Storage = null;
switch (Config.StorageType) {
case "sqlite":
Expand All @@ -43,7 +48,7 @@ public override void Load(bool hotReload)
RegisterListener<Listeners.OnServerPrecacheResources>(PrecacheResource);
}
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawnEvent);
RegisterListener<Listeners.OnMapEnd>(() => Unload(true));
RegisterListener<Listeners.OnTick>(OnTick);

Console.WriteLine($"Player Model Changer loaded {Service.GetModelCount()} model(s) successfully.");
}
Expand All @@ -56,10 +61,12 @@ private void PrecacheResource(ResourceManifest manifest) {
}
}

public override void Unload(bool hotReload)
{
RemoveListener<Listeners.OnServerPrecacheResources>(PrecacheResource);
DeregisterEventHandler<EventPlayerSpawn>(OnPlayerSpawnEvent);
public override void Unload(bool hotReload) {
INSTANCE = null;
RemoveListener<Listeners.OnServerPrecacheResources>(PrecacheResource);
RemoveListener<Listeners.OnTick>(OnTick);
DeregisterEventHandler<EventPlayerSpawn>(OnPlayerSpawnEvent);
Console.WriteLine("Player Model Changer unloaded successfully.");
}

public void ReloadConfig() {
Expand Down Expand Up @@ -113,10 +120,13 @@ public void OnConfigParsed(ModelConfig config)
Config = config;
}

public void OnTick() {
ThirdPerson.UpdateCamera();
}

// from https://github.com/Challengermode/cm-cs2-defaultskins/
[GameEventHandler]
public HookResult OnPlayerSpawnEvent(EventPlayerSpawn @event, GameEventInfo info) {

if (!Enable) {
return HookResult.Continue;
}
Expand Down Expand Up @@ -148,7 +158,7 @@ public HookResult OnPlayerSpawnEvent(EventPlayerSpawn @event, GameEventInfo info
}
var botmodel = Service.GetModel(modelindex);
if (botmodel != null) {
SetModelNextServerFrame(player.Pawn.Value, botmodel.path, botmodel.disableleg);
SetModelNextServerFrame(player, botmodel.path, botmodel.disableleg);
} else {
Server.NextFrame(() => {
var originalRender = player.Pawn.Value.Render;
Expand Down Expand Up @@ -190,7 +200,7 @@ public HookResult OnPlayerSpawnEvent(EventPlayerSpawn @event, GameEventInfo info
var model = Service.GetPlayerNowTeamModel(player);

if (model != null) {
SetModelNextServerFrame(player.PlayerPawn.Value, model.path, model.disableleg);
SetModelNextServerFrame(player, model.path, model.disableleg);
} else {
Server.NextFrame(() => {
var originalRender = player.PlayerPawn.Value.Render;
Expand All @@ -206,10 +216,10 @@ public HookResult OnPlayerSpawnEvent(EventPlayerSpawn @event, GameEventInfo info
return HookResult.Continue;
}

public static void SetModelNextServerFrame(CBasePlayerPawn pawn, string model, bool disableleg)
public void SetModelNextServerFrame(CCSPlayerController player, string model, bool disableleg)
{
Server.NextFrame(() =>
{
Server.NextFrame(() => {
var pawn = player.Pawn.Value!;
pawn.SetModel(model);
var originalRender = pawn.Render;
pawn.Render = Color.FromArgb(disableleg ? 254 : 255, originalRender.R, originalRender.G, originalRender.B);
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ If you like this plugin, please give a star :)
### This plugin can cause a GSLT ban, please use at your own risk.
[中文教程请点这里](https://github.com/samyycX/CS2-PlayerModelChanger/blob/master/README_CN.md)
- **[Before you use](#before-you-use)**
- [Feature](#feature)
- [Installation Guide](#installation-guide)
- [Optional Dependencies](#optional-dependencies)
- [Commands](#commands)
Expand All @@ -21,6 +22,16 @@ Custom model parts:
## Before you use
1. **this plugin can cause a GSLT ban, use at your own risk**

## Feature
- a model select menu (support wasd menu)
- can set different model for T and CT
- random model
- update model after change instantly
- thirdperson preview
- can set default model
- can provide special models for specified permission or player
- can disable the display of leg model

## Installation Guide
Download the plugin from latest [Release](https://github.com/samyycX/CS2-PlayerModelChanger/releases), then put it into your counterstrikesharp plugin folder.

Expand Down Expand Up @@ -69,6 +80,7 @@ See the [Configuration](#configuration)

## Credits
- Method to change model: [DefaultSkins](https://github.com/Challengermode/cm-cs2-defaultskins) by ChallengerMode
- Thirdperson preview code: [ThirdPerson-WIP](https://github.com/UgurhanK/ThirdPerson-WIP) by UgurhanK

## TODOs
1. Translation
Expand Down
15 changes: 13 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
如果你喜欢这个插件请给个Star :)
### 此插件可能导致GSLT封禁,请自行承担风险
- **[用前须知](#用前须知)**
- [功能](#功能)
- [自定义模型依赖插件](#自定义模型依赖插件)
- [安装指南](#安装指南)
- [命令](#命令)
Expand All @@ -17,10 +18,19 @@
- [如何添加原版或创意工坊模型](#如何添加原版或创意工坊模型)
- [如何把你的模型打包并上传到创意工坊](#如何把你的模型打包并上传到创意工坊)


## 用前须知
1. **此插件可能导致GSLT封禁,请自行承担风险**

## 功能
- 菜单式选择模型 (支持wasd菜单)
- 可为T/CT阵营设置不同模型
- 随机模型
- 选择后可立刻更新
- 第三人称预览
- 可设置默认模型
- 可按照权限和玩家设置特殊模型
- 可禁用腿部模型

## 自定义模型依赖插件
**如果你不使用自定义模型,不用安装这些插件**
1. [MultiAddonManager](https://github.com/Source2ZE/MultiAddonManager)
Expand Down Expand Up @@ -70,7 +80,8 @@
请看 [配置](#配置) 部分

## 感谢
- 替换模型的方法: [DefaultSkins](https://github.com/Challengermode/cm-cs2-defaultskins), 作者 ChallengerMode
- 替换模型的方法: [DefaultSkins](https://github.com/Challengermode/cm-cs2-defaultskins) by ChallengerMode
- 第三人称预览代码: [ThirdPerson-WIP](https://github.com/UgurhanK/ThirdPerson-WIP) by UgurhanK

## TODOs
1. 翻译
Expand Down
33 changes: 30 additions & 3 deletions Service.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Drawing;
using System.Net.Http.Headers;
using Config;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
Expand All @@ -21,6 +23,9 @@ public class ModelService {

private ModelCacheManager cacheManager;


private Dictionary<ulong, long> ModelChangeCooldown = new Dictionary<ulong, long>();

public ModelService(ModelConfig Config, IStorage storage, IStringLocalizer localizer, DefaultModelManager defaultModelManager) {
this.config = Config;
this.storage = storage;
Expand Down Expand Up @@ -85,6 +90,12 @@ public void SetPlayerModel(ulong steamid, string? modelIndex, string side, bool
storage.SetPlayerCTModel(steamid, modelIndex, permissionBypass);
}
);
if (!config.DisableInstantUpdate) {
var player = Utilities.GetPlayerFromSteamId(steamid);
if (Utils.isUpdatingSameTeam(player, side)) {
Utils.RespawnPlayer(player, config.DisableThirdPersonPreview);
}
}
}

public void SetPlayerAllModel(ulong steamid, string? tModel, string? ctModel, bool permissionBypass) {
Expand All @@ -95,6 +106,10 @@ public void SetPlayerAllModel(ulong steamid, string? tModel, string? ctModel, bo
storage.SetPlayerTModel(steamid, tModel, permissionBypass).ContinueWith((_) => {
storage.SetPlayerCTModel(steamid, ctModel, permissionBypass);
});
if (!config.DisableInstantUpdate) {
var player = Utilities.GetPlayerFromSteamId(steamid);
Utils.RespawnPlayer(player, config.DisableThirdPersonPreview);
}
}
public void SetPlayerModel(ulong steamid, string? modelIndex, CsTeam team, bool permissionBypass) {
var side = team == CsTeam.Terrorist ? "t" : "ct";
Expand Down Expand Up @@ -176,7 +191,6 @@ public bool CheckModel(CCSPlayerController player, string side, ModelCache? mode
if (modelIndex == "" || modelIndex == "@random") {
return true;
}
CsTeam team = side.ToLower() == "t" ? CsTeam.Terrorist : CsTeam.CounterTerrorist;

var model = GetModel(modelIndex!);
if (model == null) {
Expand All @@ -186,6 +200,14 @@ public bool CheckModel(CCSPlayerController player, string side, ModelCache? mode
}

public void SetPlayerModelWithCheck(CCSPlayerController player, string modelIndex, string side) {
if (ModelChangeCooldown.ContainsKey(player.SteamID)) {
var lastTime = ModelChangeCooldown[player.SteamID];
if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - lastTime < (config.ModelChangeCooldownSecond*1000)) {
player.PrintToChat(localizer["command.model.cooldown"]);
return;
}
}

var isSpecial = modelIndex == "" || modelIndex == "@random";

if (modelIndex == "@default") {
Expand All @@ -196,7 +218,9 @@ public void SetPlayerModelWithCheck(CCSPlayerController player, string modelInde
() => SetPlayerModel(player!.AuthorizedSteamID!.SteamId64, tDefault == null ? "" : tDefault.index, side, false),
() => SetPlayerModel(player!.AuthorizedSteamID!.SteamId64, ctDefault == null ? "" : ctDefault.index, side, false)
);
player.PrintToChat(localizer["command.model.success", localizer["side."+side]]);
if (config.DisableInstantUpdate || !Utils.isUpdatingSameTeam(player, side)) {
player.PrintToChat(localizer["command.model.success", localizer["side."+side]]);
}
return;
}

Expand All @@ -221,7 +245,10 @@ public void SetPlayerModelWithCheck(CCSPlayerController player, string modelInde

var steamid = player!.AuthorizedSteamID!.SteamId64;
SetPlayerModel(steamid, modelIndex, side, false);
player.PrintToChat(localizer["command.model.success", localizer["side."+side]]);
ModelChangeCooldown[steamid] = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (config.DisableInstantUpdate || !Utils.isUpdatingSameTeam(player, side)) {
player.PrintToChat(localizer["command.model.success", localizer["side."+side]]);
}

}
public Model? GetPlayerModel(CCSPlayerController player, string side) {
Expand Down
2 changes: 1 addition & 1 deletion Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Storage;
public interface IStorage {
public string? GetPlayerTModel(ulong SteamID);
public string? GetPlayerCTModel(ulong SteamID);
public List<ModelCache> GetAllPlayerModel();
public List<ModelCache>? GetAllPlayerModel();
public Task<int> SetPlayerTModel(ulong SteamID, string modelName, bool permissionBypass);

public Task<int> SetPlayerCTModel(ulong SteamID, string modelName, bool permissionBypass);
Expand Down
Loading

0 comments on commit 803abc6

Please sign in to comment.