(client) fix:修复定义链接不正确赋值的问题

This commit is contained in:
m0_75251201
2025-07-16 15:57:13 +08:00
parent ffeb65ba6b
commit 41c20a498d
35 changed files with 85 additions and 63 deletions

View File

@ -8,13 +8,15 @@ using System.Xml.Linq;
namespace Data namespace Data
{ {
public abstract class Define public class Define
{ {
public string defName; public string defName;
public string description; public string description;
public string label; public string label;
public string packID; public string packID;
public bool isReferene=false;
/// <summary> /// <summary>
/// 初始化方法,根据传入的 XML 元素 (<paramref name="xmlDef" />) 进行处理。 /// 初始化方法,根据传入的 XML 元素 (<paramref name="xmlDef" />) 进行处理。
/// </summary> /// </summary>
@ -112,18 +114,5 @@ namespace Data
return string.Join(Environment.NewLine, text.Split('\n').Select(line => prefix + line)); return string.Join(Environment.NewLine, text.Split('\n').Select(line => prefix + line));
} }
} }
public class DefineReference : Define
{
public Define def;
public string className;
public string fieldName;
public DefineReference(string className, string defName, string fieldName)
{
this.defName = defName;
this.className = className;
this.fieldName = fieldName;
}
}
} }

View File

@ -262,8 +262,12 @@ namespace Data
} }
else else
{ {
value = new DefineReference(field.FieldType.Name, element.Value, field.Name); var reference = (Define)Activator.CreateInstance(field.FieldType);
reference.isReferene = true;
reference.description=field.FieldType.Name;
reference.label = field.Name;
reference.defName = element.Value;
value = reference;
} }
} }
else else

View File

@ -8,7 +8,7 @@ namespace Data
{ {
public class TileDef : Define public class TileDef : Define
{ {
public string texturePath = ""; public ImageDef texture;
public string name = ""; public string name = "";
public override bool Init(XElement xmlDef) public override bool Init(XElement xmlDef)
@ -45,6 +45,12 @@ namespace Data
return true; return true;
} }
} }
public class ImageDef : Define
{
public string path;
public int wCount;
public int hCount;
}
} }

View File

@ -2,7 +2,11 @@ namespace Entity
{ {
public class Monster public class Monster
{ {
public Protocol.MonsterPack ToPack()
{
var pack= new Protocol.MonsterPack();
return pack;
}
} }
public class MonsterAttributes public class MonsterAttributes

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection;
using Data; using Data;
using UnityEngine; using UnityEngine;
using Utils; using Utils;
@ -30,52 +32,53 @@ namespace Managers
var pack = new DefinePack(); var pack = new DefinePack();
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack); if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
} }
List<Tuple<Define,DefineReference>> defineRefs = new();
Dictionary<Type, FieldInfo[]> fieldCache = new();
//不优化到循环里面是因为要先建立索引再链接
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
foreach (var pack in packs) foreach (var pack in packs)
{ {
foreach (var define in pack.Value.defines) foreach (var define in pack.Value.defines)
{ {
var typeName=define.Key; var typeName = define.Key;
var defList=define.Value; var defList = define.Value;
if (!defines.ContainsKey(typeName)) if (!defines.ContainsKey(typeName))
defines[typeName] = new Dictionary<string, Define>(); defines[typeName] = new Dictionary<string, Define>();
foreach (var def in defList) foreach (var def in defList)
{ {
defines[typeName][def.defName] = def; defines[typeName][def.defName] = def;
if (def is DefineReference reference)
// 如果字段信息已经缓存,则直接使用缓存
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
{ {
defineRefs.Add(new(def,reference)); // 获取所有字段类型为 Define 或其派生类型的字段
defineFields = def.GetType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
.ToArray();
// 缓存字段信息
fieldCache[def.GetType()] = defineFields;
}
foreach (var defineField in defineFields)
{
var defRef=(Define)defineField.GetValue(def);
if (defRef==null || !defRef.isReferene)
continue;
defineCache.Add(new(def, defineField, defRef));
} }
} }
} }
} }
foreach (var defineRef in defineRefs) foreach (var defRef in defineCache)
{ {
var define = defineRef.Item1; defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
var reference = defineRef.Item2;
var referenceDef=FindDefine(reference.className,define.defName);
var property = define.GetType().GetProperty(reference.fieldName);
if (property != null && property.CanWrite)
{
property.SetValue(define, referenceDef);
}
else
{
// 如果是字段而不是属性
var field = define.GetType().GetField(reference.fieldName);
if (field != null)
{
field.SetValue(define, referenceDef);
}
else
{
// 处理找不到成员的情况
Debug.LogError($"Could not find field or property '{reference.fieldName}' in type {define.GetType().Name}");
}
}
} }
} }
/// <summary> /// <summary>
/// 查找指定定义类型的定义名对应的 Define 对象。 /// 查找指定定义类型的定义名对应的 Define 对象。

View File

@ -7,26 +7,29 @@ namespace Map
{ {
public class DoubleMap : MonoBehaviour public class DoubleMap : MonoBehaviour
{ {
public Tilemap dataLevel; public List<List<int>> mapData = new();
// public Tilemap dataLevel;
public Tilemap textureLevel; public Tilemap textureLevel;
public Dictionary<string, TileBase> tileDict = new(); public Dictionary<string, TileBase> tileDict = new();
void Start() void Start()
{ {
tileDict = Configs.ConfigProcessor.LoadResources<TileBase>("TileMap"); TileManager.Instance.Init();
var tile= tileDict.Values;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
textureLevel.SetTile(new(i,j),tile.First());
}
}
} }
}
public class TileMappingTable:Utils.Singleton<TileMappingTable> public void UpdateTexture()
{ {
}
} }
public class TileManager:Utils.Singleton<TileManager>
{
Dictionary<int, TileBase> tileDict = new();
public void Init()
{
}
}
} }

View File

@ -1,5 +1,18 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Define> <Define>
<ImageDef>
<defName>GrassDirtTexture</defName>
<path>Map\GrassSoild.png</path>
<wCount>4</wCount>
<hCount>4</hCount>
</ImageDef>
<TileDef>
<defName>GrassDirtTiles</defName>
<texture>GrassDirtTexture</texture>
<name>GrassDirt</name>
</TileDef>
<TileMappingTableDef> <TileMappingTableDef>
<defName>GrassDirtTable</defName> <defName>GrassDirtTable</defName>
<Grass_Grass_Grass_Grass value="GrassDirt_6"/> <Grass_Grass_Grass_Grass value="GrassDirt_6"/>

View File

@ -3,7 +3,7 @@
<CharacterAttributesDef> <CharacterAttributesDef>
<defName>CatGirl</defName> <defName>CatGirl</defName>
<health>100</health> <health>100</health>
<moveSpeed>1.2</moveSpeed> <moveSpeed>2</moveSpeed>
</CharacterAttributesDef> </CharacterAttributesDef>
<CharacterDef> <CharacterDef>

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 33 KiB

View File

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

View File

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 36 KiB

View File

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

Before

Width:  |  Height:  |  Size: 8.4 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 656 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 876 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1001 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 915 B