maker_csharp

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2022 License: MulanPSL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTmplGoConstFileData = &TmplGoConstFileData{
	Namespace: "protocol",

	ConstDef: []*generator.ConstValue{},
	ErrorDef: []*generator.ConstValue{},

	Plugins: make(map[string]interface{}),
}
View Source
var DefaultTmplMessageFileData = &TmplMessageFileData{
	Namespace: "protocol",
}
View Source
var TmplConstFile = &generator.TemplateHeader{
	Name:     "csharp_const",
	Filepath: "csharp_const.tmpl",

	BuiltinTemplateText: `using System;

namespace {{.Namespace}}
{
    public partial class MessageDef
	{
{{if (len .ConstDef | ne 0)}}{{range $index, $elem := .ConstDef}}            public readonly static UInt32 {{$elem.Name}} = {{$elem.Value}};{{with (ne $elem.Comment "")}} {{$elem.Comment}}{{end}}
{{end}}{{end}}
{{if (len .ErrorDef | ne 0)}}{{range $index, $elem := .ErrorDef}}            public readonly static UInt32 {{$elem.Name}} = {{$elem.Value}};{{with (ne $elem.Comment "")}} {{$elem.Comment}}{{end}}
{{end}}{{end}}
	}
}
`,
}
View Source
var TmplMessageFile = &generator.TemplateHeader{
	Name:     "csharp_message",
	Filepath: "csharp_message.tmpl",

	BuiltinTemplateText: `using System;
using System.Collections.Generic;

namespace {{.Namespace}}
{
    public abstract class DefineST
	{
        public abstract string Name();
        public abstract DefineST NewST();

        public abstract Byte[] Encode();
        public abstract void Decode(Byte[] bs);

        public abstract override string ToString();
    }

    public abstract class Message : DefineST
	{
        public abstract UInt32 ProtoID();
        public abstract Message New();
    }

    public partial class MessageDef
	{
        public static void RegMessageDef(Message def)
		{
            if (mMessageID.ContainsKey(def.ProtoID()) || mMessageName.ContainsKey(def.Name()))
			{
                throw new Exception($"duplicated message define [{def.Name()}]({def.ProtoID()})");
            }

            mMessageID.Add(def.ProtoID(), def);
            mMessageName.Add(def.Name(), def);
        }

        public static T? New<T>(UInt32 id) where T : Message
		{
            var def = mMessageID.GetValueOrDefault(id);
            if (def == null)
			{
                return null;
            }
            return (T)def.New();
        }

        public static Byte[] Encode(Message msg)
		{
            return msg.Encode();
        }

        public static T Decode<T>(UInt32 id, Byte[] bs) where T : Message
		{
            return (T)Decode(id, bs);
        }

        public static Message Decode(UInt32 id, Byte[] bs)
		{
            var def = mMessageID.GetValueOrDefault(id);
            if (def == null)
			{
                throw new Exception($"unknown message [{id}]");
            }

            def.Decode(bs);
            return def;
        }
    }
}
`,
}
View Source
var TmplMessageRegFile = &generator.TemplateHeader{
	Name:     "csharp_message_reg",
	Filepath: "csharp_message_reg.tmpl",

	BuiltinTemplateText: `using System;
using System.Collections.Generic;

namespace {{.Namespace}}
{
    public partial class MessageDef
    {
        private static Dictionary<UInt32, Message> mMessageID = new Dictionary<UInt32, Message>()
        { {{range .Message}}{{"\n"}}            { {{.ProtoIDName}}, new {{.Name}}()},{{end}}
        };

        private static Dictionary<string, Message> mMessageName = new Dictionary<string, Message>()
        { {{range .Message}}{{"\n"}}            { "{{.Name}}", new {{.Name}}()},{{end}}
        };
    }
}
`,
}

Functions

func GenCodeForConstValue

func GenCodeForConstValue(pSet *generator.PrototypeSet) (string, error)

func GenCodeForIMessage

func GenCodeForIMessage() (string, error)

func GenCodeForMessageReg

func GenCodeForMessageReg(pSet *generator.PrototypeSet) (string, error)

func GenFileForConstValue

func GenFileForConstValue(pSet *generator.PrototypeSet) error

func GenFileForIMessage

func GenFileForIMessage() error

func GenFileForMessageReg

func GenFileForMessageReg(pSet *generator.PrototypeSet) error

Types

type GenConfig

type GenConfig struct {
	Dir           string
	NamespaceName string

	MessageFilename string
	ConstFilename   string

	Plugins []string
}
var GenCFG *GenConfig

type TmplGoConstFileData added in v0.2.0

type TmplGoConstFileData struct {
	Namespace string

	ConstDef []*generator.ConstValue
	ErrorDef []*generator.ConstValue

	Plugins map[string]interface{}
}

type TmplMessageFileData added in v0.2.0

type TmplMessageFileData struct {
	Namespace string
}

type TmplMessageRegFileData added in v0.2.0

type TmplMessageRegFileData struct {
	Namespace string

	Message []*TmplMessageRegFileDataMessage
}

type TmplMessageRegFileDataMessage added in v0.2.0

type TmplMessageRegFileDataMessage struct {
	Name        string
	ProtoIDName string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL