cfclient/F6/MetaTable.cs
2024-05-18 18:38:27 +08:00

169 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
namespace F6
{
public delegate bool TraverseCb<T>(T ele);
public interface IMetaTable
{
bool IsNoLoad()
{
return false;
}
void Load()
{
}
void PreInit1()
{
}
void ElementsInit(int round)
{
}
void PostInit1()
{
}
}
public sealed class MetaTableFieldAttribute : Attribute
{
public string fieldName;
}
public class BaseMeta
{
public Dictionary<string, int> nameIdxHash = new Dictionary<string, int>();
}
public class BaseTable
{
private BaseMeta _meta;
private BitArray _flags;
public bool HasValue(string fieldName)
{
return this.HasValue(this.GetFieldIdx(fieldName));
}
public bool HasValue(int fieldIdx)
{
return this._flags.Get(fieldIdx);
}
public int GetFieldIdx(string fieldName)
{
int idx = 0;
this._meta.nameIdxHash.TryGetValue(fieldName, out idx);
return idx;
}
}
public class RawMetaTable<T> : IMetaTable
{
protected string fileName;
protected string primKey;
protected bool noLoad;
protected List<T> rawList = new List<T>();
public RawMetaTable(string fileName, string primKey)
{
this.fileName = fileName;
this.primKey = primKey;
}
public void Traverse(TraverseCb<T> cb)
{
foreach(var item in this.rawList)
{
if (!cb(item))
{
break;
}
}
}
public T RandElement()
{
if (this.rawList.Count > 0)
{
return this.rawList[new Random().Next(0, this.rawList.Count)];
}
return default(T);
}
public virtual void Load()
{
}
protected virtual void LoadPost()
{
}
}
public class IdMetaTable<T>: RawMetaTable<T>
{
protected Dictionary<Int64, T> idHash = new Dictionary<Int64, T> ();
public T GetById(Int64 id)
{
T v = default(T);
this.idHash.TryGetValue(id, out v);
return v;
}
protected override void LoadPost()
{
base.LoadPost();
}
public IdMetaTable(string fileName, string primKey) :base(fileName, primKey)
{
// base(fileName, primKey);
}
}
public class NameMetaTable<T>: RawMetaTable<T>
{
protected Dictionary<string, T> nameHash = new Dictionary<string, T>();
public T GetByName(string key)
{
T v = default(T);
this.nameHash.TryGetValue(key, out v);
return v;
}
protected override void LoadPost()
{
base.LoadPost();
}
public NameMetaTable(string fileName, string primKey):base(fileName, primKey)
{
}
}
}