0

我有一个非常奇怪的问题。我尝试了一切来修复它,但到目前为止没有任何效果。

正如您在第一张图片中看到的,当我尝试从“UnnamedGameServer”外部的命名空间中引用静态类“SharedConstants”时,编译器返回以下错误:

命名空间“SharedConstants”中不存在类型或命名空间名称“ServerInfo”

我发现使用 UnnamedGameServer.SharedConstants 而不是 SharedConstants 并使用 UnnamedGameServer 引用此类来解决此问题;在 .cs 文件的顶部。但我更喜欢避免在我使用该类的每一行上引用它。

这是我的代码的一些屏幕截图:

第一张截图:

Visual Studio 中的错误

第二张截图:

Visual Studio 中的错误

编辑:使用语句的屏幕截图:

Visual Studio 中的错误

编辑2:没有截图的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using ProtoBuf;
using UnnamedGameServer;
using System.Threading;

namespace Unnamed_game
{
    class Connection
    {
        public struct UserInformation
        {
            public string Username;
            public int UserID;
        }

        static private Connection instance;

        private Connection()
        {
            client = new TcpClient();
            header = new PacketHeader();
            _isLoggedCharacter = false;
            _isLoggedUser = false;
            magicByte = UnnamedGameServer.SharedConstants.ServerInfo.MagicByte;
        }

        // properties
        TcpClient client;
        PacketHeader header;
        Thread serverCommThread;
        byte[] magicByte;
        bool _isLoggedCharacter;
        CharacterInformation chInformation
        {
            get
            {
                return Player.Instance().Information;
            }
        }
        bool _isLoggedUser;
        public UserInformation Information;

        // methods
        static public Connection Instance()
        {
            if(instance == null)
            {
                instance = new Connection();
                return instance;
            }
            else
            {
                return instance;
            }
        }
        /// <summary>
        /// Should never be used. Use the parameterless one to get the server address and information. It doesn't use try-catch, the exception handling should be done on Game1.Initialize()
        /// </summary>
        public void ConnectServer(IPEndPoint endPoint)
        {
            if (client.Connected)
            {
                return;
            }
            else
            {
                client.Connect(endPoint);
                serverCommThread = new Thread(HandleServerCommunication);
                serverCommThread.Start();
            }
        }
        public void ConnectServer()
        {
            this.ConnectServer(new IPEndPoint(UnnamedGameServer.SharedConstants.ServerInfo.ServerAddress, UnnamedGameServer.SharedConstants.ServerInfo.Port));
        }
        private void HandleServerCommunication()
        {
            if (client == null)
            {
                throw new Exception("The TcpClient is null");
            }
            else
            {
                // this doesn't work
                byte[] magicByte = SharedConstants.ServerInfo.MagicByte;
                // this works
                magicByte = UnnamedGameServer.SharedConstants.ServerInfo.MagicByte;
            }
        }
        private void SendPacket(ActionType actionType, object packetStruct)
        {
            try
            {
                header.ActionTypeNumber = (short)actionType;
                using (NetworkStream stream = client.GetStream())
                {
                    stream.Write(magicByte, 0, magicByte.Length);
                    Serializer.SerializeWithLengthPrefix<PacketHeader>(stream, header, PrefixStyle.Base128);
                    switch (actionType)
                    {
                        case ActionType.Connect:
                            Serializer.SerializeWithLengthPrefix<PacketConnect>(stream, (PacketConnect)packetStruct, PrefixStyle.Base128);
                            break;
                    }
                    stream.Write(magicByte, 0, magicByte.Length);
                }
            }
            catch (Exception)
            {
                // error
                return;
            }
        }
        public void CreateNewCharacter(string characterName, CharacterClass chClass, CharacterRace chRace)
        {
            var info = new NewCharacterInfo();
            info.Name = characterName;
            info.OwnerUsername = Information.Username;
            info.Class = chClass;
            info.Race = chRace;
            CreateNewCharacter(info);
        }
        public void CreateNewCharacter(NewCharacterInfo info)
        {
            var packet = new PacketCreateNewCharacter();
            packet.chInfo = info;
            SendPacket(ActionType.CreateNewCharacter, packet);
        }
        public void ConnectUser(string username, string unhashedPassword)
        {
            var packet = new PacketConnect();
            packet.Username = username;
            packet.UnhashedPassword = unhashedPassword;
            ConnectUser(packet);
        }
        public void ConnectUser(PacketConnect packet)
        {
            if (_isLoggedCharacter || _isLoggedUser)
            {
                return;
            }
            else
            {
                SendPacket(ActionType.Connect, packet);
            }
        }
        public void ConnectCharacter(string characterName, short serverID)
        {
            var packet = new PacketLoginCharacter();
            packet.CharacterName = characterName;
            packet.ServerID = serverID;
            ConnectCharacter(packet);
        }
        public void ConnectCharacter(PacketLoginCharacter packet)
        {
            if (_isLoggedCharacter || !_isLoggedUser)
            {
                return;
            }
            else
            {
                SendPacket(ActionType.LoginCharacter, packet);
            }
        }
        public void Disconnect(PacketDisconnect packet)
        {
            if (!_isLoggedUser)
            {
                return;
            }
            else
            {
                SendPacket(ActionType.Disconnect, packet);
            }
        }
    }
}

编辑 3: : 存储 SharedConstants 的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace UnnamedGameServer
{
    /// <summary>
    /// ALL CONSTANTS THAT ARE SHARED BY THE CLIENT AND THE SERVER ARE STORED HERE. DONT ADD MORE CLASSES THAT STORE CONSTANTS. Use GameConstants for game-only constants.
    /// </summary>
    public static class SharedConstants
    {
        /// <summary>
        /// Server information with port, IP and MagicByte
        /// </summary>
        public static class ServerInfo
        {
            public const short Port = 6483;
            public static readonly IPAddress ServerAddress = IPAddress.Loopback;
            public static byte[] MagicByte
            {
                get
                {
                    return new byte[4] { 0xF1, 0x64, 0x83, 0xC4 };
                }
            }
        }
        /// <summary>
        /// Character constants shared by client/server
        /// </summary>
        public static class CharacterConstants
        {
            public static class Movement
            {
                /// <summary>
                /// Coordinates per update
                /// </summary>
                public const float DefaultCoordinatePerUpdate = 8;
                public const float ModifierNormal = 1f;
                public const float ModifierFrozen = 0.8f;
                public const float ModifierSpeedBuff = 1.2f;
                public const float ModifierStop = 0f;
            }
        }
        /// <summary>
        /// Networking constants
        /// </summary>
        public static class Networking
        {
            public const int MilisecondsPerMovementUpdate = 100;
            public const ushort ActionTypeNonMetaActionStart = 0x0FFF + 1;
            /// <summary>
            /// What is the number of actions a non-logged user can perform
            /// </summary>
            public const ushort ActionTypeNonLoggedUser = 0x000F;
        }
    }

    enum CharacterDirection
    {
        NoMovement = 0x00,
        Top = 0x01,
        TopRight = 0x02,
        TopLeft = 0x03,
        Right = 0x04,
        BottomRight = 0x05,
        Bottom = 0x06,
        BottomLeft = 0x07,
        Left = 0x08
    }
    enum CharacterStatus
    {
        Alive = 0x01,
        Dead = 0x02
    }
    enum CharacterClass
    {
        Mage = 0x01,
        Knight = 0x02
    }
    enum CharacterRace
    {
        Human = 0x01
    }
    enum CharacterType
    {
        Player = 0x01,
        GameMaster = 0x02
    }
    enum CharacterFaction
    {
        Newbie = 0x01,
        Army = 0x02,
        Chaos = 0x03
    }
    enum CharacterMovementStatus
    {
        Normal = 0x01,
        Frozen = 0x02,
        SpeedBuff = 0x03,
        Stop = 0x04
    }
    struct CharacterExperience
    {
        public CharacterExperience(short level, int experience)
        {
            Level = level;
            Experience = experience;
        }

        public short Level;
        public int Experience;
    }
}
4

2 回答 2

2

有一个 using 语句应该可以工作。如果在 using 语句之前有任何代码,那么这可能会导致问题。

您可以尝试将 using 语句放在命名空间之后,例如:

namespace Unnamed_game
{
    using UnamedGameServer;
    class Connection
于 2012-07-12T03:17:33.790 回答
0

这应该有效:

using UnnamedGameServer;

class MyClass {
byte[] b = SharedConstants.ServerInfo.MagicByte;
}

如果这不能解决您的问题,请发布显示问题的简短但完整示例的代码(以文本形式,而不是屏幕截图)。

于 2012-07-12T02:56:50.017 回答