少年修仙传客户端基础资源
leonard Wu
2018-08-08 a1fd98c6ae0854327472b369f2bc63fc3ad1d897
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
 
public class GenerateClass
{
    private string _classPath; //生成脚本路径
    private CodeNamespace _codeNamespace;//命名空间
    public CodeTypeDeclaration _customerClass;
    private CodeCompileUnit _unit;
 
    public GenerateClass(string classPath)
    {
        _classPath = classPath;
    }
 
    /// <summary>
    /// 创建命名空间
    /// </summary>
    /// <param name="spaceName"></param>
    public void GenerateNamespace(string spaceName)
    {
        _unit = new CodeCompileUnit();
        _codeNamespace = new CodeNamespace(spaceName);
        _unit.Namespaces.Add(_codeNamespace);
    }
 
    /// <summary>
    ///  添加引入的命名空间
    /// </summary>
    /// <param name="spaceName"></param>
    public void AddImportNamespace(string spaceName)
    {
        if (_codeNamespace == null) {
            DesignDebug.Log("_codeNamespace is null");
            return;
        }
        _codeNamespace.Imports.Add(new CodeNamespaceImport(spaceName));
    }
 
    /// <summary>
    /// 创建类名
    /// </summary>
    /// <param name="className"></param>
    public void GenerateClassName(string className, string extendClass = "")
    {
        if (_codeNamespace == null) {
            DesignDebug.Log("_codeNamespace is null");
            return;
        }
        _customerClass = new CodeTypeDeclaration(className);
        _customerClass.IsClass = true;
        _customerClass.IsPartial = true;
        _customerClass.TypeAttributes = TypeAttributes.Public;
        if (!string.IsNullOrEmpty(extendClass)) {
            _customerClass.BaseTypes.Add(extendClass);
        }
        _codeNamespace.Types.Add(_customerClass);
    }
 
    /// <summary>
    /// 添加熟悉
    /// </summary>
    /// <param name="type"></param>
    /// <param name="fieldName"></param>
    public void AddMemberField(Type type, string srcFieldName, string fieldName, string CommentStatement)
    {
        if (type == null) {
            UnityEngine.Debug.LogError(_customerClass.Name + "  " + srcFieldName + "`s type is Null");
            return;
        }
        CodeMemberField field = new CodeMemberField(type, fieldName);
        field.Attributes = MemberAttributes.Private;
        _customerClass.Members.Add(field);
 
        CodeMemberProperty property = new CodeMemberProperty();
 
        property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
 
        property.Name = srcFieldName;
 
        property.HasGet = true;
 
        property.HasSet = false;
 
        property.Type = new CodeTypeReference(type);
 
        property.Comments.Add(new CodeCommentStatement(CommentStatement));
 
        property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName)));
 
        //property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName), new CodePropertySetValueReferenceExpression()));
 
        _customerClass.Members.Add(property);
    }
 
    /// <summary>
    /// 添加方法
    /// </summary>
    public void AddFunction(string funcName, MemberAttributes memberAtt, List<KeyValuePair<Type, string>> paramLst, List<string> statementLst, Type returanType)
    {
        //为这个类添加一个方法   public override int 方法名(string str);
        System.CodeDom.CodeMemberMethod method = new CodeMemberMethod();
        method.Name = funcName;
        method.Attributes = memberAtt;//声明方法是公开 并且override的
 
        if (paramLst != null) {
            for (int i = 0; i < paramLst.Count; i++) {
                method.Parameters.Add(new CodeParameterDeclarationExpression(paramLst[i].Key, paramLst[i].Value));
            }
        }
 
        if (statementLst != null) {
            for (int i = 0; i < statementLst.Count; i++) {
                method.Statements.Add(new CodeSnippetStatement(statementLst[i]));
            }
        }
 
        method.ReturnType = new CodeTypeReference(returanType);//声明返回值的类型
        _customerClass.Members.Add(method);
    }
 
    /// <summary>
    /// 写入文件
    /// </summary>
    public void WriteToFile()
    {
        _customerClass.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(SerializableAttribute))));
 
        //生成代码
 
        CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
 
        CodeGeneratorOptions options = new CodeGeneratorOptions();
 
        options.BracingStyle = "C";
 
        options.BlankLinesBetweenMembers = true;
 
        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(_classPath)) {
            provider.GenerateCodeFromCompileUnit(_unit, sw, options);
        }
    }
}