Skip to content

Commit

Permalink
命令提示框,服饰系统基本架构
Browse files Browse the repository at this point in the history
  • Loading branch information
TianMengLucky committed Sep 9, 2023
1 parent e89f17b commit 86f59a8
Show file tree
Hide file tree
Showing 21 changed files with 810 additions and 105 deletions.
39 changes: 34 additions & 5 deletions NextShip/Chat/Patches/ChatPatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using HarmonyLib;
using TMPro;
using UnityEngine;
Expand All @@ -11,6 +12,7 @@ namespace NextShip.Chat.Patches;
public static class ChatPatch
{
private static GameObject CommandPromptBox;
private static List<GameObject> AllButton = new ();

[HarmonyPatch(typeof(ChatController), nameof(ChatController.Awake)), HarmonyPostfix]
public static void ChatController_Awake_Postfix(ChatController __instance)
Expand All @@ -25,7 +27,7 @@ private static void OnChatChanged(ChatController __instance)
InitCommandPromptBox(__instance);
var text = __instance.freeChatField.Text;
CommandPromptBox.SetActive(text.StartsWith("/"));
if (text.StartsWith("/")) UpdateCommandPromptBox(text.Remove(1).Split(" "));
if (text.StartsWith("/")) UpdateCommandPromptBox(text.Replace("/", "").Split(""));
}

private static void InitCommandPromptBox(ChatController __instance)
Expand All @@ -46,20 +48,47 @@ private static void InitCommandPromptBox(ChatController __instance)

private static void UpdateCommandPromptBox(string[] text)
{
var BoxSR = CommandPromptBox.GetComponent<SpriteRenderer>();
var list = Command.GetCommands(text);
if (list == null || list.Count == 0)
{
BoxSR.size = new Vector2(6.4f, 0.5f);
AllButton.Do(n => n.Destroy());
return;
}

var buttonSprite = ObjetUtils.Find<Sprite>("smallButtonBorder");
var count = 1;
Vector3 vector3 = new(-0.0017f, -0.12f, -1);
foreach (var command in Command.GetCommands(text))
{
var button = new GameObject(command.key.ToString());
if (count == 5) break;
var button = new GameObject($"Button {text[0]}");
button.transform.SetParent(CommandPromptBox.transform);
var SR = button.AddComponent<SpriteRenderer>();
SR.sprite = ObjetUtils.Find<Sprite>("smallButtonBorder");
SR.sprite = buttonSprite;
SR.drawMode = SpriteDrawMode.Sliced;
button.CreatePassiveButton(OnMouseOut: () => SR.color = new Color(0.962f, 1, 1, 0.298f), OnMouseOver: () => SR.color = Palette.White);
SR.size = new Vector2(6.4f, 0.25f);
button.transform.localPosition = vector3;;
vector3.y += 0.2f;

var ButtonText = new GameObject("text");
ButtonText.transform.SetParent(button.transform);
ButtonText.transform.localPosition = new Vector3(6.85f, -2.34f, -1);
var TMP = ButtonText.AddComponent<TextMeshPro>();
TMP.text = command.GetText();
TMP.fontSize = 3;
TMP.color = new Color(0.962f, 1, 1, 0.298f);

button.CreatePassiveButton(onClick: command.CommandEvent,OnMouseOut: () =>
{
TMP.color = new Color(0.962f, 1, 1, 0.298f);
SR.color = new Color(0, 0, 0, 0);
}, OnMouseOver: () => SR.color = TMP.color = Palette.White);

BoxSR.size = new Vector2(6.4f, count * 0.08f);
count++;
AllButton.Add(button);
}

CommandPromptBox.AllGameObjectDo(n => n.layer = LayerUtils.GetUILayer());
Expand Down Expand Up @@ -125,7 +154,7 @@ public static List<Command> GetCommands(string[] keys)
{
var Commands = AllCommand.Where(n => n.Compare(keys)).ToList();
Commands.Sort((x, y) => x.KeyCount.CompareTo(y.KeyCount));
if (Commands.First().KeyCount > Commands.Last().KeyCount) Commands.Reverse();
/*if (Commands.First().KeyCount > Commands.Last().KeyCount) Commands.Reverse();*/

return Commands;
}
Expand Down
116 changes: 116 additions & 0 deletions NextShip/Cosmetics/CosmeticsCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System.Collections.Generic;
using System.Linq;
using NextShip.Config;
using NextShip.Cosmetics.Loaders;
using NextShip.Utilities;
using UnityEngine;
using UnityEngine.AddressableAssets;

namespace NextShip.Cosmetics;

public class CosmeticsCreator
{
private static Material hatShader;

public List<Sprite> AllSprites;

public CosmeticsCreator(List<Sprite> allSprites)
{
AllSprites = allSprites;
}

private Sprite Get(string name) => AllSprites.FirstOrDefault(n => n.name == name);

public (HatViewData, HatData) CreateHat(CosmeticsInfo info)
{
if (hatShader == null) hatShader = FastDestroyableSingleton<HatManager>.Instance.PlayerMaterial;

var hatData = ScriptableObject.CreateInstance<HatData>();
var hatView = ScriptableObject.CreateInstance<HatViewData>();

hatView.MainImage = hatView.FloorImage = Get(info.Resource);
if (info.ClimbResource != null) hatView.LeftClimbImage = hatView.ClimbImage = Get(info.ClimbResource);
if (info.Adaptive && hatShader != null) hatView.AltShader = hatShader;
if (info.BackResource != null)
{
hatView.BackImage = Get(info.BackResource);
info.Behind = true;
}

hatData.name = info.Name;
hatData.displayOrder = 99;
hatData.ProductId = info.Id;
hatData.InFront = !info.Behind;
hatData.NoBounce = !info.Bounce;
hatData.ChipOffset = new Vector2(0f, 0.2f);
hatData.Free = true;

var assetRef = new AssetReference(hatView.Pointer);

hatData.ViewDataRef = assetRef;
hatData.CreateAddressableAsset();

return (hatView, hatData);
}

public (NamePlateViewData, NamePlateData) CreateNamePlate(CosmeticsInfo info)
{
var namePlateData = ScriptableObject.CreateInstance<NamePlateData>();
var namePlateView = ScriptableObject.CreateInstance<NamePlateViewData>();

namePlateView.Image = Get(info.Resource);
namePlateData.name = info.Name;
namePlateData.displayOrder = 99;
namePlateData.ProductId = info.Id;
namePlateData.Free = true;

var assetRef = new AssetReference(namePlateView.Pointer);

namePlateData.ViewDataRef = assetRef;
namePlateData.CreateAddressableAsset();

return (namePlateView, namePlateData);
}

public (SkinViewData, SkinData) CreateSkin(CosmeticsInfo info)
{
var skinData = ScriptableObject.CreateInstance<SkinData>();
var skinView = ScriptableObject.CreateInstance<SkinViewData>();

skinView.IdleFrame = Get(info.Resource);
skinView.EjectFrame = Get(info.BackResource);

skinData.name = info.Name;
skinData.displayOrder = 99;
skinData.ProductId = info.Id;
skinData.Free = true;

var assetRef = new AssetReference(skinView.Pointer);

skinData.ViewDataRef = assetRef;
skinData.CreateAddressableAsset();
return (skinView, skinData);
}

public (VisorViewData, VisorData) CreateVisor(CosmeticsInfo info)
{
var visorData = ScriptableObject.CreateInstance<VisorData>();
var visorView = ScriptableObject.CreateInstance<VisorViewData>();

visorView.FloorFrame = Get(info.Resource);
visorView.ClimbFrame = Get(info.ClimbResource);
visorView.IdleFrame = Get(info.FlipResource);
visorView.LeftIdleFrame = Get(info.BackFlipResource);

visorData.name = info.Name;
visorData.displayOrder = 99;
visorData.ProductId = info.Id;
visorData.Free = true;

var assetRef = new AssetReference(visorData.Pointer);

visorData.ViewDataRef = assetRef;
visorData.CreateAddressableAsset();
return (visorView, visorData);
}
}
69 changes: 63 additions & 6 deletions NextShip/Cosmetics/CustomCosmeticsManager.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HarmonyLib;
using Il2CppInterop.Runtime.Attributes;
using NextShip.Config;
using NextShip.Cosmetics.Loaders;
using NextShip.Manager;
using UnityEngine;

namespace NextShip.Cosmetics;

public class CustomCosmeticsManager
public static class CustomCosmeticsManager
{
public static List<string> AllCustomCosmeticId = new();
public static Dictionary<string, CosmeticsInfo> AllCustomCosmeticNameAndInfo = new();
public static Dictionary<string, CosmeticRepoType> AllCosmeticRepoRepo = new ();

public static CosmeticsLoader _Loaders;
public static HashSet<Sprite> AllCustomCosmeticSprites = new ();
public static HashSet<string> AllCosmeticId = new();

public static Dictionary<string, HatViewData> AllCustomHatViewData = new();
public static List<SkinViewData> AllCustomSkinViewData = new();
public static List<VisorViewData> AllCustomVisorViewData = new();
public static List<NamePlateViewData> AllCustomNamePlateViewData = new();


private static CosmeticsLoader _Loaders;
public static List<CosmeticsLoader> AllLoaders = new();

public const string RepoFile = "CosmeticRepo";
public static readonly string RepoFilePath = Path.Combine(FilesManager.CreativityPath, RepoFile.Is(FilesManager.FileType.Json));
internal static readonly string RepoFilePath = Path.Combine(FilesManager.CreativityPath, RepoFile.Is(FilesManager.FileType.Json));

private static readonly CosmeticsConfig[] ModConfig = {

};

public static Sprite GetSprite(string name) => AllCustomCosmeticSprites.FirstOrDefault(n => n.name == name);

public static void LoadHat()
{
var configs = new List<CosmeticsConfig>();
configs.AddRange(ModConfig);
configs.AddRange(ReadRepoFile());
if (configs.Count == 0) return;

try
{
foreach (var config in from config in configs let regex = new Regex(@"/https?\/\/:[-a-z0-9]+(\.[-a-z0-9])*\.(com|cn|edu|uk)\/[-a-z0-9_:@&?=+,.!/~*'%$]*/ig") where regex.IsMatch(config.RepoURL) select config)
{
Load(config.CosmeticRepoType);
_ = _Loaders?.LoadFormOnRepo(config);
}
}
catch (Exception e)
{
Exception(e);
}
}

public static List<CosmeticsConfig> ReadRepoFile()
{
Expand All @@ -33,16 +75,31 @@ public static void WriteRepoFile(string text)
textWriter.Write(text);
}

public static void Load(CosmeticRepoType cosmeticRepoType)
private static void Load(CosmeticRepoType cosmeticRepoType)
{
_Loaders = cosmeticRepoType switch
{
CosmeticRepoType.TOR => null,
CosmeticRepoType.TOR => new TORCosmeticsLoader(),
CosmeticRepoType.EXR => null,
CosmeticRepoType.NOS => null,
CosmeticRepoType.TIS => null,
_ => null
};

if (_Loaders != null)
AllLoaders.Add(_Loaders);
}

public static void AddToList(HatManager __instance)
{
foreach (var vaLoader in AllLoaders)
{
if (vaLoader.AddToList) continue;
var list = new List<HatData>();
list.AddRange(__instance.allHats);
vaLoader.Hats.Values.Do(n => n.Keys.Do(data => list.Add(data)));
__instance.allHats = list.ToArray();
}
}
}

Expand Down
Loading

0 comments on commit 86f59a8

Please sign in to comment.