GameFrameWork frame custom table type

The commonly used table matching types of the GameFrameWork framework are basically included, but when actually developing projects, we will inevitably encounter some special types that need to be customized by ourselves. The following records the process of customizing the table matching of the GameFrameWork framework. Take the custom List<int> type as an example.

First of all, we need to customize our own DataTableProcessor.List_intProcessor.cs class, refer to the existing custom types provided, and draw symbols according to the same. Below is the detailed code.

using System.Collections.Generic;
using System.IO;

namespace FishOfHuaNong.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class List_intProcessor : GenericDataProcessor<List<int>>
        {
            //是否是系统自带的类型
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            /// <summary>
            /// 定义的类型名称
            /// </summary>
            public override string LanguageKeyword
            {
                get
                {
                    return "List<int>";
                }
            }

            /// <summary>
            /// 定义的类型名称
            /// </summary>
            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "List<int>",
                };
            }

           /// <summary>
           /// 解析输入的值,返回定义的类型值
           /// </summary>
           /// <param name="value"></param>
           /// <returns></returns>
            public override List<int> Parse(string value)
            {
                string[] values = value.Split(',');
                List<int> temp = new List<int>();
                foreach(var VarIAble in values) 
                {
                    temp.Add(int.Parse(VarIAble));
                }
                return temp;
            }

            /// <summary>
            /// 写入二进制流
            /// </summary>
            /// <param name="dataTableProcessor"></param>
            /// <param name="binaryWriter"></param>
            /// <param name="value"></param>
            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                List<int> intList = Parse(value);
                foreach(var intItem in intList) 
                {
                    binaryWriter.Write(intItem);
                }
                
            }
        }
    }
}

Then extend the method of reading custom types in BinaryReaderExtension and DataTableExtension respectively

 Under normal circumstances, this step can already generate data normally, but because the custom type is special, with special symbols such as < >, these special symbols cannot be directly used as method name characters, so we still need to generate in the code generation class Make some special judgments in DataTableGenerator.cs

 

 After these operations are completed, click Generate DataTable on the toolbar to automatically generate the corresponding table matching class. The final generated table matching class is as follows.

//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:[email protected]
//------------------------------------------------------------
// 此文件由工具自动生成,请勿直接修改。
// 生成时间:2022-01-18 00:53:04.927
//------------------------------------------------------------

using GameFramework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace FishOfHuaNong
{
    /// <summary>
    /// 实体表。
    /// </summary>
    public class DRTest : DataRowBase
    {
        private int m_Id = 0;

        /// <summary>
        /// 获取实体编号。
        /// </summary>
        public override int Id
        {
            get
            {
                return m_Id;
            }
        }

        /// <summary>
        /// 获取资源名称。
        /// </summary>
        public string AssetName
        {
            get;
            private set;
        }

        /// <summary>
        /// 获取资源数组。
        /// </summary>
        public List<int> AssetList
        {
            get;
            private set;
        }

        public override bool ParseDataRow(string dataRowString, object userData)
        {
            string[] columnStrings = dataRowString.Split(DataTableExtension.DataSplitSeparators);
            for (int i = 0; i < columnStrings.Length; i++)
            {
                columnStrings[i] = columnStrings[i].Trim(DataTableExtension.DataTrimSeparators);
            }

            int index = 0;
            index++;
            m_Id = int.Parse(columnStrings[index++]);
            index++;
            AssetName = columnStrings[index++];
            //自定义的扩展方法
            AssetList = DataTableExtension.ParseList_int(columnStrings[index++]);

            GeneratePropertyArray();
            return true;
        }

        public override bool ParseDataRow(byte[] dataRowBytes, int startIndex, int length, object userData)
        {
            using (MemoryStream memoryStream = new MemoryStream(dataRowBytes, startIndex, length, false))
            {
                using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8))
                {
                    m_Id = binaryReader.Read7BitEncodedInt32();
                    AssetName = binaryReader.ReadString();
                    //自定义的扩展方法
                    AssetList = binaryReader.ReadList_int();
                }
            }

            GeneratePropertyArray();
            return true;
        }

        private void GeneratePropertyArray()
        {

        }
    }
}

Guess you like

Origin blog.csdn.net/qq_20991149/article/details/122551900