程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB.NET >> 游戲大廳從基礎開始(4)-通過L2X用配置文件反射組裝程序(VB only)

游戲大廳從基礎開始(4)-通過L2X用配置文件反射組裝程序(VB only)

編輯:VB.NET

很久沒更新了。前一陣工作比較充實(就是比較勞累拉~),加上向老趙學習努力瘦身,精神愈發痛 苦,難免就懶惰下來。通用倒了,項目突然死亡,On bench了一個月,越發的無力。

好消息也是 有的,在m$ msdn論壇努力回答問題,總算當上了個版主。雖然7月申請mvp失敗 但是鄙人生性好顯擺,好聽一點就是愛分享,所以一個咕噜爬起來,又是條好漢。

-----------------------閒話到此 為止-----------------------

通過配置來制定程序

說起通過配置來制定程序,不得不 提及asp.net

還記得asp.net membership 在web.config中的聲明麼?

<membership defaultProvider="Main_SqlMemberShipProvider" userIsOnlineTimeWindow="20">

<providers>

<add connectionStringName="GTSCOM_DATABASE_DATAConnectionString1" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" passwordFormat="Clear" applicationName="GTSCOM_Customer" name="Main_SqlMemberShipProvider" type="System.Web.Security.SqlMembershipProvider"/>

</providers>

</membership>

實現不同工廠產生了相同接口的實體類型 MembershipUser

Web.config 就是這麼神奇,動不動什麼東西就加入進來了~ 編譯器往往是根據web.config生成 臨時cs/vb代碼,將其編譯結果作為網站的基礎 。

編譯config提升的性能我可是非常羨慕阿,但 是改配置就重新編譯這樣的事情也經常被诟病。改一個連接字符串整個網站編譯5分鐘的抱怨,偶爾也會 聽到。

咱們的游戲大廳,無論是做web game 還是做聯眾,都不適合

另一種辦法就是反射 了。

l 大廳組裝要求

n 動態加載需要的類庫(多種游戲邏輯、房間邏輯、副本類型的 工廠載入)

n 策略配置 (策略模式的策略類的載入)

n 通過reload config 修改工廠 類和策略類 實現無重啟的“下一場游戲生效”配置更新。

n 性能不能受到過多影 響

Okay,反射都能勝任。

如果反射得到的都是實現主程序引用中IFactory接口的工廠,返 回的都是符合IProductClass 接口的對象,訪問成員都通過訪問接口進行,初始化以外性能上也不會受 影響。看起來十分可行!

另一方面:

l 配置文件要求

n 讓我這個懶蛋比較容易 讀(架構清晰)

n 讓我這個懶蛋比較方便寫 (編寫智能提示)

n 寫程序的時候比較舒 坦 (編程智能提示)

恩,眼睛裡冒賊光了。

咱們看看下面的xsd文件

WGSConfig.xsd
<?xml  version="1.0" encoding="utf-8"?>
<xs:schema  attributeFormDefault="unqualified" elementFormDefault="qualified"  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element  name="config">
         <xs:complexType>
             <xs:all>
                 <xs:element name="Assemblies" minOccurs="0"  maxOccurs="1">
                     <xs:complexType>
                         <xs:sequence>
                             <xs:element maxOccurs="unbounded"  name="Assembly" minOccurs="1">
                                 <xs:complexType>
                                     <xs:attribute  name="relativepath" type="xs:string" use="required"  />
                                 </xs:complexType>
                             </xs:element>
                         </xs:sequence>
                     </xs:complexType>
                 </xs:element>
                 <xs:element name="LinkListeners" maxOccurs="1"  minOccurs="1">
                     <xs:complexType>
                         <xs:sequence>
                             <xs:element maxOccurs="unbounded"  name="LinkListener">
                                 <xs:complexType>
                                     <xs:sequence>
                                          <xs:element maxOccurs="unbounded" name="param"  type="KeyValuePair">
                                         </xs:element>
                                      </xs:sequence>
                                     <xs:attribute  name="namespace" type="xs:string" use="required" />
                                 </xs:complexType>
                             </xs:element>
                         </xs:sequence>
                     </xs:complexType>
                 </xs:element>
                 <xs:element name="Construction" maxOccurs="1"  minOccurs="1">
                     <xs:complexType>
                         <xs:sequence>
                             <xs:element name="Services">
                                 <xs:complexType>
                                     <xs:sequence>
                                         <xs:element  name="Service" maxOccurs="unbounded">
                                              <xs:complexType>
                                                  <xs:sequence>
                                                      <xs:element name="Createparam" maxOccurs="unbounded"  type="KeyValuePair">
                                                      </xs:element>
                                                      <xs:element name="ServiceConfig" type="xs:anyType" />
                                                  </xs:sequence>
                                                 <xs:attribute  name="builderNamespace" type="xs:string" />
                                              </xs:complexType>
                                         </xs:element>
                                      </xs:sequence>
                                 </xs:complexType>
                             </xs:element>
                         </xs:sequence>
                     </xs:complexType>
                 </xs:element>
                 <xs:element name="DataAccess" maxOccurs="1"  minOccurs="1">
                     <xs:complexType>
                         <xs:sequence>
                             <xs:element name="compoment">
                                 <xs:complexType>
                                     <xs:sequence>
                                         <xs:element  name="ConnectionString" type="xs:string" />
                                     </xs:sequence>
                                     <xs:attribute  name="namespace" type="xs:string" use="required" />
                                 </xs:complexType>
                             </xs:element>
                         </xs:sequence>
                     </xs:complexType>
                 </xs:element>
                 <xs:element name="Membership" maxOccurs="1"  minOccurs="1">
                     <xs:complexType>
                         <xs:sequence>
                             <xs:element name="UserFactory">
                                 <xs:complexType>
                                     <xs:sequence>
                                         <xs:element  maxOccurs="unbounded" name="param"  type="KeyValuePair">
                                         </xs:element>
                                      </xs:sequence>
                                     <xs:attribute  name="namespace" type="xs:string" use="required" />
                                 </xs:complexType>
                             </xs:element>
                         </xs:sequence>
                     </xs:complexType>
                 </xs:element>
                 <xs:element name="ChatChannels" maxOccurs="1"  minOccurs="0">
                     <xs:complexType>
                         <xs:sequence>
                             <xs:element name="ChatChannel">
                                 <xs:complexType>
                                     <xs:sequence>
                                         <xs:element  name="param" type="KeyValuePair" />
                                     </xs:sequence>
                                 </xs:complexType>
                             </xs:element>
                         </xs:sequence>
                     </xs:complexType>
                 </xs:element>
             </xs:all>
         </xs:complexType>
     </xs:element>
     <xs:complexType name="KeyValuePair">
         <xs:attribute name="key" type="xs:string"  use="required" />
         <xs:attribute name="value" type="xs:string"  use="required" />
     </xs:complexType>
</xs:schema>

是不是沒什麼感覺?

換用vs2005打開,感覺又不一樣 了.

怎麼樣 是不是很清晰呢?

*vs2008的xsd編輯不太方便,我們可以用sql2005 帶的 vs2005 設置

看看我們的要求表

配置文件要求

n 讓我這個懶蛋比較容易讀(架構清晰) checked!

n 讓我這個懶蛋比較方便寫 (編寫智能提示)

n 寫程序的時候比較舒坦 ( 編程智能提示)

有了xsd文件,我們就可以更方便得建立自己的XML 配置文件了

在工程中 添加一個xml文件 在文件的屬性窗口中選擇視圖(schemas)

把剛才的xsd 選中

Oh yeah~~

我們在編輯xml的時候   可以得到及時的智能提示了@_@

看看我們的要求表

配 置文件要求

n 讓我這個懶蛋比較容易讀(架構清晰) checked!

n 讓我這個懶蛋比較方 便寫 (編寫智能提示)checked!

n 寫程序的時候比較舒坦 (編程智能提示)

恩... 距離完美的解放生產力還差一點點,我們看看編成集成提示支持得怎樣

寫一條linq 查詢所有的 factory吧

嗯,,,效果不錯.看來vb 編譯器體貼的預編譯了我們的xsd文件. 

。。。。。。c# 啥時候能集成這個阿,哭啊。。。公司項目不讓用vb阿。。。。。

看看我們的要求表

配置文件要求

n 讓我這個懶蛋比較容易讀(架構清晰) checked!

n 讓我這個懶蛋比較 方便寫 (編寫智能提示)checked!

n 寫程序的時候比較舒坦 (編程智能提示)checked!

於是幾乎在不停的智能提示中~~  我完成了配置類...

Happy end
Imports System.Collections.Generic
Namespace StrategiesNamespace Strategies

    Public Class GlobalConfigClass GlobalConfig
        Implements IGlobalConfig

        Public ReadOnly Property Config()Property Config() As Xml.Linq.XDocument Implements IGlobalConfig.Config
            Get
                Return _ConfigFile
            End Get
        End Property

        Protected _LinkListeners As New List(Of ILinkListener)
        Public ReadOnly Property LinkListeners()Property LinkListeners() As System.Collections.Generic.IEnumerable(Of Communicate.ILinkListener) Implements IGlobalConfig.LinkListeners
             Get
                Return _LinkListeners
             End Get
        End Property

        Private Shared _Instance As GlobalConfig
        Public Shared Property Current()Property Current() As GlobalConfig

            Get

                 If _Instance Is Nothing Then
                    _Instance = New GlobalConfig
                End If

                 Return _Instance
            End Get

             Set(ByVal value As GlobalConfig)
                _Instance = value
            End Set
        End Property

        Private Shared _ConfigFile As XDocument

        Private Sub New()Sub New()
            _Instance = Me

            Dim path As String = System.Diagnostics.Process.GetCurrentProcess.MainModule.FileName

             path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), "WGSConfig.xml")
            If System.IO.File.Exists(path) Then
                _ConfigFile = XDocument.Load(path)
             Else

                Throw New Exception (WayneGameSolution.Resources.CurrentCurlture.FileLoad_ConigLoad_ErrorMessage)
             End If

            Dim tmps = From n In Config.<config>.<Assemblies>.<Assembly> _
                 Select n.@relativepath

            For Each s As String In tmps
                AttachTypes(s)
            Next

            Dim listeners = From xn As XElement In Config.<config>.<LinkListeners>.<LinkListener> _
                                     Select lk = CType(CreateObject (xn.@Namespace ,namespace, xn), ILinkListener) Where Not lk Is Nothing

_LinkListeners.AddRange(listeners)

'For Each xn As XElement In Config.<config>.<Construction>.<Services>.<Service>
             '    _Serivces.Add(CreateObject(xn.@builderNamespace, xn))

            'Next

Dim services = From xn As XElement In Config.<config>.<Construction>.<Services>.<Service> _
                                   Select builder = CType (CreateObject(xn.@builderNamespace, _
                                                                 xn), IGameServiceBuilder) Where Not builder Is Nothing _
                                                                  Select service = builder.CreateService()

            _Serivces.AddRange(services)

            _UserFactory = CreateObject(Config.<config>.<Membership>.<UserFactory>.@Namespace ,namespace, _
                                         Config.<config>.<Membership>.<UserFactory>.First)

            _PackFlagEnumType = GetTypeFrom (Config.<config>.<PackEnum>.@Namespace )namespace)

'Sub New (ByVal id As String, ByVal name As String, ByVal timeoutSecond As Int32, ByVal type As IChatChannel.ChannelType)
            Dim channelsQuery = From elm As XElement In Config.<config>.<ChatChannels>.<ChatChannel> Select New ChatChannel(elm.@channelID, elm.@channelName, CInt(elm.@timeout), CInt(elm.@type))

            For Each chn As Chat.IChatChannel In channelsQuery
                 Me.ChatChannels.Add(chn.ID, chn)

            Next

        End Sub
        Public Function params2Dic()Function params2Dic(ByVal nodes As IEnumerable(Of XElement)) As Dictionary(Of String, String)
            Dim dic As New Sync.SyncDictionary(Of String, String)
            For Each xn As XElement In nodes
                 dic.Add(xn.@key, xn.@value)

            Next
            Return dic
        End Function

         Public Shared Function GetTypeFrom()Function GetTypeFrom(ByVal name_space As String) As Type
            Dim T As Type = Nothing
            T = Type.GetType(name_space, False, True)
            If T Is Nothing Then _ReflectTypes.TryGetValue(name_space, T)
            Return T
         End Function

        Public Shared Function CreateObject_Instance()Function CreateObject_Instance(ByVal name_space As String, ByVal ParamArray parms() As Object)

            Dim T As Type = GetTypeFrom(name_space)

            If T Is Nothing Then Return Nothing

            Return Activator.CreateInstance(T, parms)

        End Function
        Private Shared _ReflectTypes As New Sync.SyncDictionary(Of String, Type)
        Public ReadOnly Property ReflectTypes()Property ReflectTypes() As System.Collections.Generic.IDictionary(Of String, System.Type) Implements IGlobalConfig.ReflectTypes
            Get
                 Return _ReflectTypes
            End Get
        End Property

        Public Function AttachTypes()Function AttachTypes(ByVal relativePath As String) As Boolean Implements IGlobalConfig.AttachTypes
            Dim path As String = System.Diagnostics.Process.GetCurrentProcess.MainModule.FileName

             path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), relativePath)
            Dim asm As System.Reflection.Assembly = Nothing

            asm = System.Reflection.Assembly.LoadFrom(path)
            If asm Is Nothing Then
                Return False
            Else
                Dim tps As Type() = asm.GetTypes()

                For Each T As Type In tps
                    Dim FullName As String = T.Namespace & "." & T.Name
                    If Not _ReflectTypes.ContainsKey(FullName) Then _ReflectTypes.Add(FullName, T)
                 Next

            End If
             Return True

        End Function

        Public Function CreateObject()Function CreateObject(ByVal name_space As String, ByVal ParamArray parms() As Object) As Object Implements IGlobalConfig.CreateObject
             Return CreateObject_Instance(name_space, parms)
        End Function
        Private _ChatChannels As New Generic.Sync.SyncDictionary(Of String, Chat.IChatChannel)
        Public ReadOnly Property ChatChannels() Property ChatChannels() As System.Collections.Generic.IDictionary(Of String, Chat.IChatChannel) Implements IGlobalConfig.ChatChannels
            Get
                Return _ChatChannels
            End Get
        End Property
        Private _LogedUsers As New Generic.Sync.SyncDictionary(Of String, IUser)
        Public ReadOnly Property LogedUsers()Property LogedUsers() As System.Collections.Generic.IDictionary(Of String, Membership.IUser) Implements IGlobalConfig.LogedUsers
            Get
                Return _LogedUsers
            End Get
        End Property
        Private _MountedAreas As New Generic.Sync.SyncDictionary(Of String, IArea)
        Public ReadOnly Property MountedAreas()Property MountedAreas() As System.Collections.Generic.IDictionary(Of String, Construct.IArea) Implements IGlobalConfig.MountedAreas
            Get
                Return _MountedAreas
            End Get
        End Property

        Private _PackFlagEnumType As Type
        Public ReadOnly Property PackFlagEnumType()Property PackFlagEnumType() As System.Type Implements IGlobalConfig.PackFlagEnumType
             Get
                Return _PackFlagEnumType
             End Get
        End Property
        Friend _Serivces As New List(Of Services.IGameService)
        Public ReadOnly Property Serivces()Property Serivces() As System.Collections.Generic.IEnumerable(Of Services.IGameService) Implements IGlobalConfig.Serivces
            Get
                Return _Serivces
            End Get
        End Property
        Protected _AreaFactory As Construct.IAreaFactory
        Public ReadOnly Property AreaFactory()Property AreaFactory() As Construct.IAreaFactory Implements IGlobalConfig.AreaFactory
             Get
                Return _AreaFactory
             End Get
        End Property

        Protected _UserFactory As Membership.IUserFactory
        Public ReadOnly Property UserFactory()Property UserFactory() As Membership.IUserFactory Implements IGlobalConfig.UserFactory
            Get
                 Return _UserFactory
            End Get
        End Property
    End Class

End Namespace

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved