(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
{
public abstract class Define
public class Define
{
public string defName;
public string description;
public string label;
public string packID;
public bool isReferene=false;
/// <summary>
/// 初始化方法,根据传入的 XML 元素 (<paramref name="xmlDef" />) 进行处理。
/// </summary>
@ -112,18 +114,5 @@ namespace Data
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
{
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

View File

@ -8,7 +8,7 @@ namespace Data
{
public class TileDef : Define
{
public string texturePath = "";
public ImageDef texture;
public string name = "";
public override bool Init(XElement xmlDef)
@ -45,6 +45,12 @@ namespace Data
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 Protocol.MonsterPack ToPack()
{
var pack= new Protocol.MonsterPack();
return pack;
}
}
public class MonsterAttributes

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Data;
using UnityEngine;
using Utils;
@ -30,52 +32,53 @@ namespace Managers
var pack = new DefinePack();
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 define in pack.Value.defines)
{
var typeName=define.Key;
var defList=define.Value;
var typeName = define.Key;
var defList = define.Value;
if (!defines.ContainsKey(typeName))
defines[typeName] = new Dictionary<string, Define>();
foreach (var def in defList)
{
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;
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}");
}
}
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
}
}
/// <summary>
/// 查找指定定义类型的定义名对应的 Define 对象。

View File

@ -7,26 +7,29 @@ namespace Map
{
public class DoubleMap : MonoBehaviour
{
public Tilemap dataLevel;
public List<List<int>> mapData = new();
// public Tilemap dataLevel;
public Tilemap textureLevel;
public Dictionary<string, TileBase> tileDict = new();
void Start()
{
tileDict = Configs.ConfigProcessor.LoadResources<TileBase>("TileMap");
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());
}
}
TileManager.Instance.Init();
}
}
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"?>
<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>
<defName>GrassDirtTable</defName>
<Grass_Grass_Grass_Grass value="GrassDirt_6"/>

View File

@ -3,7 +3,7 @@
<CharacterAttributesDef>
<defName>CatGirl</defName>
<health>100</health>
<moveSpeed>1.2</moveSpeed>
<moveSpeed>2</moveSpeed>
</CharacterAttributesDef>
<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